Julien is basically correct. A basic assumption that most computer algebra
systems make is that a symbol is not equal to zero (identically).
Equivalently, it assumes that different symbols are not equal (again
identically). Many algorithms rely on zero equivalence checking and would not
work without this assumption; others might work by using the piecewise thing
you use below, but for many symbols, the number of combinations would become
unwieldy.
Consider this example:
In [47]: var('a b')
Out[47]: (a, b)
In [48]: integrate(exp(a*x), x)
Out[48]:
a⋅x
ℯ
────
a
This result is correct, except when a == 0. In that case, the correct answer
is just x (because exp(0*x) == 1). You may think that this is different, but
if you replace a with a - b, you get instead exp((a - b)*x)/(a - b), which is
of course correct unless a == b (our integrate can't do that integral
unfortunately, but, for example, risch_integrate() in my integration3 branch
can).
Now, you can consider your example to be exactly the same by looking at the
indefinite integral:
In [50]: integrate(cos(m*x)*sin(n*x), x)
Out[50]:
m⋅sin(m⋅x)⋅sin(n⋅x) n⋅cos(m⋅x)⋅cos(n⋅x)
─────────────────── + ───────────────────
2 2 2 2
m - n m - n
which is of course true unless m == +/-n. If you want to consider m==n
separately, you probably will have to manually do it, using something similar
to what Julien wrote.
Of course, if you have any suggestions on how SymPy might get around this
difficulty, we welcome to hear them.
Aaron Meurer
On Feb 22, 2011, at 2:09 PM, Julien Rioux wrote:
> Maple returns the same. That is not to say that sympy shouldn't try to
> do better than maple here, but I think this is a hard problem. You
> need to do the special case yourself.
>
> Inn=integrate(f1*f1,(x,0,2*pi))
> Imn=integrate(f1*f2,(x,0,2*pi))
> I=Piecewise((Inn,Eq(m-n,0)),(Imn,True))
> pprint(I)
>
> ⎧π for m - n = 0
> ⎨
> ⎩0 otherwise
>
> Cheers,
> Julien
>
> On Feb 22, 2:54 pm, Mamba <[email protected]> wrote:
>> Hi,
>>
>> I am new in sympy. I would like to calculate the integration of
>> cos(nx)*cos(mx) on the interval [0,2pi].
>> I expected the following result : I=0 if n!=m and I!=0 if m=n but the
>> result is only 0 without distinction between the values of m and n.
>>
>> I used that piece of code in ipython
>>
>> from sympy import *
>> x=Symbol('x')
>> n=Symbol('n',integer=True)
>> m=Symbol('m',integer=True)
>> f1=cos(n*x)
>> f2=cos(m*x)
>> f3=f1*f2
>> I=integrate(f3,(x,0,2*pi))
>> pprint(I)
>> 0
>>
>> Did I do something wrong ? What is the good way to obtain the correct
>> answer ?
>>
>> Best Regards,
>>
>> Régis
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.