1

Edit: this question is essentially identical to this one and I encourage viewers to go there for more information. To add to that answer: because my function lacks narrow bumps, singularities, etc; has limited integration bounds; and is known to have a small integer solution (I only need 1-2 sigfigs), it is a near-ideal choice for a Monte Carlo algorithm. In the end 1e6 points were more than enough to solve it.

I have a pretty complicated trig function of five variables x1, x2, x3, y1, z1 I'm trying to integrate over (-pi,pi) (once for every variable). I know the solution is an integer, so I'm only asking for one digit of precision during my numerical integration with scipy.integrate.nquad(); but it still ran for 74.5 hours on my laptop with no result before I stopped it. I also had no idea what my progress was by that point.

My actual code (and the function):

low_lim = -np.pi
hi_lim = np.pi

fn = lambda x1,y1,z1,x2,x3: (
                        (
                        cos((x1+y1+z1)/2) * cos((-x1+y1+z1)/2) *
                        cos((x1-y1+z1)/2) * cos((x1+y1-z1)/2)
                        ) *
                        (
                        cos((x2+y1+z1)/2) * cos((-x2+y1+z1)/2) *
                        cos((x2-y1+z1)/2) * cos((x2+y1-z1)/2)
                        ) *
                        (
                        cos((x3+y1+z1)/2) * cos((-x3+y1+z1)/2) *
                        cos((x3-y1+z1)/2) * cos((x3+y1-z1)/2)
                        ) *
                        (sin((x1-x2)/2)**2 * sin((x1+x2)/2)**2 ) * 
                        (sin((x2-x3)/2)**2 * sin((x2+x3)/2)**2 ) * 
                        (sin((x1-x3)/2)**2 * sin((x1+x3)/2)**2 )
        )


result = nquad(fn,[[low_lim,hi_lim],[low_lim,hi_lim],[low_lim,hi_lim],
                    [low_lim,hi_lim],[low_lim,hi_lim]],opts={'epsabs':1e0})

(To be unnecessarily precise, this integral will be integer-valued once I normalize it with a known prefactor.)

I'm wondering, is there a better way to perform this integration than nquad()? Or if not, how can I add progress checks so I least have a sense of how far into the integration I am, perhaps by printing a timestamp each time it finishes integrating over one of the variables?

(edit: small fix to incorrectly-copied code)

Natavi
  • 25
  • 5

1 Answers1

0

Looks like a case for a monte carlo integration.

ev-br
  • 24,968
  • 9
  • 65
  • 78
  • 1
    Accepting your answer because it pointed me in the right direction, and I very quickly found [this question](https://stackoverflow.com/questions/14071704/integrating-a-multidimensional-integral-in-scipy#14075572) which my own probably counts as a repeat of. – Natavi Apr 21 '20 at 14:29