Reputation: 5877
I have the following set of commands to do a definite integral
n [2]: from sympy import *
init_printing()
x,L = symbols('x, L')
n = symbols('n', integer = True)
exp = sin((n+Rational(1,2))*pi*x/L)**2
integrate(exp, (x,0,L))
The result of these commands shown below:
The first result implies that n=-1/2 which implies that n is not an integer. What is the point of giving an integer attribute to the symbol if it doesn't account for it in operations as shown above? How can I force sympy to recognize that the first part of the piecewise result is impossible if n is integer?
Upvotes: 10
Views: 6496
Reputation: 19047
If the equality had evaluated, this condition would have been rejected and the Piecewise would have evaluated to your expected result. Because SymPy didn't know if your L was zero or not, it couldn't evaluate that equality.
So try
>>> n = var('n', integer=True)
>>> L = var('L', nonzero=True)
>>> exp = sin((n+Rational(1,2))*pi*x/L)**2
>>> integrate(exp, (x,0,L))
L/2
And there you go! :-) (Note, however, that it should have been sufficient to just say that L was finite to know that the equality could never be true, but SymPy fails to evaluate that condition, too.)
/c
Upvotes: 9
Reputation: 14141
Declaring the symbol n
as an integer has no consequences for the evaluating except for the simplification of expressions:
The assumptions system allows users to specify that symbols have certain common mathematical properties, such as being positive, imaginary, or integer. SymPy is careful to never perform simplifications on an expression unless the assumptions allow them.
and
Assumptions are only needed to restrict a domain so that certain simplifications can be performed. They are not required to make the domain match the input of a function.
So in your case there is no need to specify the symbol n
to be an integer.
Upvotes: 3