Python Numerical Integration in Python

AI Thread Summary
The discussion revolves around finding a numerical solution for a complex nested integral in Python, which represents the cumulative distribution function (CDF) of a random variable. The initial implementation yielded results outside the expected range of 0 to 1, prompting inquiries about potential errors in the code. Key issues identified include the need to correctly account for the area of integration and the proper application of factorials in the recursive function. Adjustments were suggested, such as modifying the recursion start point and ensuring the correct multiplication of factorials occurs only once. Ultimately, comparisons with Monte Carlo simulations and analytic results revealed discrepancies, highlighting the importance of verifying the correctness of the integral's formulation.
  • #51
EngWiPy said:
I need to evaluate say G(1,1,0), G(2,1,0) ... G(10,1,0). These function are independent of each others. But in for loop these will look something like

Code:
for(int i=1; i <= 10; i++){
cout  <<  G(i, 1,0);
}

which are evaluated sequentially. These can be evaluated in parallel. But how? I am not familiar with multithreading, but I think we can get Kx speed up, where K is the number of cores.
My computer, which I've had for five years, has an Intel Quad Core Duo processor, with each of the four cores having effectively two processors. This means that eight threads can be running. You're not going to have K cores, with K being an arbitrary integer. Your program also can't be using all the cores, as some of them will be used by the OS and other processes.

If I were going to write the code, I wouldn't have it iterate 10 times. Instead, I would iterate 4, or 8, or some power of 2, times, and I wouldn't use a loop. If I had four threads to play with, I would create four thread objects to evaluate G(1, 1.0), G(2, 1.0), G(3, 1.0), and G(4, 1.0). When the last one of these is finished, I would use the threads to evaluate G with a first argument of 5, 6, 7, and 8.

Also, I wouldn't output them in the loop. Instead, I would store the return values from G() in an array, and print them out when the array is full.

I can't say too much more about threads and such, since there's quite a bit more than I have explained. Threads are pretty low level. At a higher level of abstraction are async objects, which are easier to work with.
 
  • Like
Likes EngWiPy

Similar threads

Back
Top