upio_4
upio_4

Reputation: 73

Using sympy to evaluate a double integral

I want to write some code to evaluate functions, in terms of x and y, using a double integral. The lower boundary of both the inner and outer integral being -1 and the upper boundary being 1 for a given function.

Here's the code I tried to use to get to evaluate an example polynomial:

import sympy

example_poly = sympy.Poly(x/2-y**2,x,y)
a = sympy.integrate(example_poly, (x, -1, 1))
b = sympy.integrate(a, (y,-1,1))
print(b)

However, I just get an error saying 'GeneratorsNeeded: can't initialize from 'dict' without generators' pointing at the following line:

----> 5 b = sympy.integrate(a, (y,-1,1))

What's going wrong here?

Upvotes: 1

Views: 1380

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14530

You do not need to use the Poly class to integrate polynomial expressions:

In [13]: example_poly = x/2-y**2

In [14]: example_poly
Out[14]: 
x    2
─ - y 
2     

In [15]: integrate(example_poly, (x, -1, 1), (y, -1, 1))
Out[15]: -4/3

Unless you know of some reason why you should use Poly rather than ordinary sympy expressions then my advice is not to use Poly. The Poly class is used internally by sympy in order to compute things like integrals but you do not need to use it just because your expression looks like a polynomial.

Upvotes: 2

Related Questions