How to compute this numerical integral in Python?

Viewed 130

I have a list of vectors:

x = [x1, x2, x3, x4]

where x1,.. are some variables. I have another matrix

b = array([[0, 2],
       [0, 3],
       [1, 2],
       [1, 3],
       [2, 3]])

At the end I would like to calculate the following:

Integral[ np.exp(-np.dot(np.ones(len(b)),np.cos(x[b[:,0]]-x[b[:,1]]))), {x1, 0, Pi}, {x2,0,Pi}...]

So it seems like when the size of x is large, it is better to define x symbolically

from sympy import *
x = symarray('x', 4)

and since x contains variables that will be integrated out, is there any good way to set up the integral through symbolic variables and do the evaluation at the end?

Update:

Here is one code snippet which does what I want, but the code is really really slow.

x =symarray('x', M0)
ij_args = x[b[:,0]]-x[b[:,1]]
ij_cosargs = [cos(ij_args[i]) for i in range(len(ij_args))]
integrand = exp(np.sum(ij_cosargs))
integrate(integrand, (x[0], 0, np.pi), (x[1], 0, np.pi), (x[2], 0, np.pi), (x[3], 0, np.pi))

One more problem with the above code is that I have to enter bounds for each symbol separately, like (x[0], 0, np.pi)

0 Answers
Related