6
Assuming[Element[n, Integers], Integrate[Sin[x]*Sin[n*x],{x,0,Pi}]]

returns 0, which is obviously wrong for n=1.

Assuming[n==1, Integrate[Sin[x]*Sin[n*x],{x,0,Pi}]]

does return Pi/2 (so in particular not 0).

Just evaluating the integral yields

-Sin[n*Pi]/(-1+n^2)

which is indetermined for n=1.

Can someone explain to me what Mathematica is doing and how to obtain a correct result?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
chris
  • 63
  • 3
  • The last expression is not undetermined if you take the limit Limit[-(Sin[n \[Pi]]/(-1 + n^2)), n -> 1]. – b.gates.you.know.what Feb 11 '14 at 11:23
  • Yes, l'Hospital works, however Mathematica does not seem to use that for simplification. Simplify[-Sin[n Pi]/(-1+n^2), Assumptions -> Element[n,Integers]] also gives 0 . – chris Feb 11 '14 at 11:29
  • I answered a similar question on stackoverflow, and I don't have time right now to transcribe it here. – rcollyer Feb 11 '14 at 12:46
  • In document (F1) of Simplify and FullSimplify, the Possible Issues section is related to this problem. – Yi Wang Feb 11 '14 at 12:55
  • Thanks for pointing me to the Possible Issues section. – chris Feb 11 '14 at 13:33
  • Here's a weird thing: Integrate[Sin[x]*Sin[n*x], {x, 0, Pi}, Assumptions -> Element[n, Integers]] does not return 0. I thought that Assuming[something, Integrate[...]] and Integrate[..., Assumptions -> something] were always equivalent. Is Assuming affecting something used internally by Integrate? – Szabolcs Feb 11 '14 at 13:41
  • @Szabolcs I was under the impression that Assuming[a, b] was effectively Block[{$Assumptions = a}, b]. I may be wrong, though. – rcollyer Feb 11 '14 at 14:00

1 Answers1

8

If you look in the help file for Integrate under the section on "Possible Issues", there is an explanation. The docs comment: "Parameters like n are assumed to be generic inside indefinite integrals:" and the example is given of Integrate[x^n, x] which returns x^(1 + n)/(1 + n). As with the OPs integral, the answer is true for generic n, but not for a specific value n=-1 (or n=1 for the OPs integrand).

I had not seen it before, but rcollyer's explanation of this issue is very detailed and has some good pointers on how to handle this kind of situation. His post begins: "Sometimes, it pays to understand the integrand better before you integrate..." In the particular integral here, the integral of Sin[x]*Sin[n x], it is clear that n=1 is very special -- it is also clear from the indefinite integral answer -Sin[n*Pi]/(-1+n^2) that n=1 is special. But I know of no way to automatically tell what the generic/specific conditions are in all cases.

bill s
  • 68,936
  • 4
  • 101
  • 191
  • This is easy to see with Integrate[Cos[n x], {x, 0, Pi}] which equals $\pi$ when $n=0$, but $0$ otherwise. – rcollyer Feb 11 '14 at 13:40
  • @rcollyer And again, it only returns 0 if you use Assuming but not if you use Assumptions. – Szabolcs Feb 11 '14 at 13:44
  • 2
    I could agree with this explanation, nonetheless genericity makes sense in the reals (meaning e.g. non-zero measure) but it doesn't (or at least it does mean something very different) in the integers. Apparently the documentation should be improved. Similar problems which I find even more harmful one finds here Why the inequality does not take into account the domain?. I think you should mention explicitly this problem (+1). – Artes Feb 11 '14 at 13:46
  • 1
    @rcollyer This is very weird. Try quitting the kernel, then evaluate your example first with Assuming then with Assumptions. Both give 0. Now quit again, then evaluate first using Assumptions then with Assuming. The first one does not give 0, the second one does. The first one doesn't give zero on re-evaluation either. (Cached result?) I smell a bug. – Szabolcs Feb 11 '14 at 13:54
  • Thanks, that seems to be the issue. Is there any way to tell Mathematica to also give out the non-generic cases or at least list them? I would rather have at least a small notice that there are exceptional cases instead of receiving a generically correct answer. – chris Feb 11 '14 at 13:56
  • 1
    @Szabolcs I don't see that behavior on more recent sources, so if it is a bug, it is fixed. – rcollyer Feb 11 '14 at 13:56
  • @chris the essence of my answer on stackoverflow is you need to understand your integrand enough that you can pick out these potential problems. No matter how good an automated system is, it still needs to be guided by a person. – rcollyer Feb 11 '14 at 13:58
  • 2
    @rcollyer Are you sure it's not due to the order you evaluated them in? Please see here. – Szabolcs Feb 11 '14 at 14:00
  • @Szabolcs I see it now, too. Let's see if Artes can find Daniel's answer. – rcollyer Feb 11 '14 at 14:03