# Should integrate detect orthogonality of functions in the integrand?

What would be the correct way to tell Mathematica that

 Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}]


Should not be zero when n is an integer for all n in the above? Since when n=3 the answer should be Pi/2 and zero for all other n values.

But Mathematica does not seem to detect this, even when told that n is integer:

ClearAll[x,n]
Integrate[Sin[3 x] Sin[n x], {x, 0, Pi},Assumptions -> Element[n, Integers] && n > 0]


 % /. n -> 3


And if

Assuming[Element[n,Integers]&&n>0,Integrate[Sin[3 x] Sin[n x],{x,0,Pi}]]
(*0*)


While

n=3;
Integrate[Sin[3 x] Sin[n x],{x,0,Pi}]
(* Pi/2*)


The question is, is it the programmer responsibility to tell Mathematica that sin(3 x)*sin(n x) is special case when n=3? May be make a function around it, something like

foo[n_Integer]:=
If[n==3,
Integrate[Sin[3 x]^2,{x,0,Pi}],
Integrate[Sin[3 x] Sin[n x],{x,0,Pi}]] (*0*)
];


But I do not think the above is a good way to do in in general.

I think a special rule is needed to tell Mathematica about orthogonality of trig functions, and then ask Integrate to use this rule? But how to do this?

The question is: Should Mathematica have been able to automatically handle the special case of n=3 above? If not, what would be the best way to add a rule to tell Integrate to do this?

• Based on one answer already posted, what I have often found is that MMA does not pay attention to Assumtions inside Intégrate, but does to Assuming. I mean, I get the correct result for assumptions by using Assuming instead. Commented Feb 16, 2018 at 9:40
• Some of this is explained here Commented Feb 16, 2018 at 15:53

One way to get the answer:

Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}, Assumptions -> Element[n, Integers] && n > 0]
Normal@Series[%, {n, 3, 0}]  (* evaluate at n -> 3 *)
(* Pi/2 *)


I suppose Mathematica should get the right answer, but there's way in which this shows the answer returned by Integrate contains the correct answer at n == 3. Algebraic notation is sometimes deficient, and for an analytic object like this result, the power series is probably the best mathematical representation of the object. The undefined points n == ±3 of the expression returned by Integrate are not even singularities of the series. (It is to address such a deficiency that the need for the Sinc function was felt.) Thus I think Series (also Limit) is a mathematically correct way to evaluate the result.

For this particular example, you could try using FourierSinCoefficient:

FourierSinCoefficient[Sin[3 x], x, n, FourierParameters->{-1,1}]


1/2 π DiscreteDelta[-3 + n]

I believe it's a bug!

I0=Simplify[Integrate[Sin[3 x] Sin[n x], {x, 0, Pi} ] ](*-((3 Sin[n \[Pi]])/(-9 + n^2))*)
I1=Simplify[Integrate[Sin[3 x] Sin[n x], {x, 0, Pi} ],Assumptions ->Element[n, Integers]](*0*)
I2=Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}, Assumptions -> n != 3](*-((3 Sin[n \[Pi]])/(-9 + n^2))*)
I3=Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}, Assumptions -> n ==  3](*\[Pi]/2*)


The integrals I0,I2,I3 are evaluated as expected whereras I1 gives a wrong result! It looks like the Limit/Singularity n->3 isn't recognized by the Simplify-Command!

• I understand that a singularity cannot be simplified. However, doing Limit does solve the problem in I1. Commented Feb 16, 2018 at 11:02
• Yes Limit[I0,n->3] shows that the solution I0(!) is ok. Nevertheless I1 is wrong. Commented Feb 16, 2018 at 11:16
f[n_] = Piecewise[{{Integrate[Sin[3 x]^2, {x, 0, Pi}], n == 3}},
Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}]]


EDIT: Didn't understand the question at first

First, note that to get 0, which is the answer when n≠3, you need to use Assuming instead of Assumptions:

ClearAll[x, n]
Assuming[
Element[n, Integers] && n > 0,
Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}]
]


Output:

0


Now the question is why Mathematica fails to notice the special case of n=3. I speculate that Mathematica's ability to recognize the need of a delta function is lacking here. For instance, if I type an integral that is equivalent to the delta function, I just get an error:

Integrate[Exp[-I k x], {k, -\[Infinity], \[Infinity]}]


Output:

During evaluation of In[1845]:= Integrate::idiv: Integral of E^(-I k x) does not converge on {-\[Infinity],\[Infinity]}.


Your case may be another specific case of Integrate not identifying opportunities for the delta function. I would perhaps report it as a bug.

• thank you. But I think that is what I wrote. I wanted Mathematica not to give zero for all n. I am not sure how your solution handles this special case? Commented Feb 16, 2018 at 6:39
• Ah I misunderstood your question. That is strange that it fails to notice the n=3 case. This seems to qualify as a bug, perhaps submit it to support if you don't get a better answer on SE? You can report it here: wolfram.com/support/contact/email/?topic=Feedback
– Max
Commented Feb 16, 2018 at 7:07
• Thanks. The thing is, I am not sure if it is a bug or not. Since this could be by design. i.e. it could be that Mathematica is not designed to detect special cases like this. But I am not sure, that is why I asked. Commented Feb 16, 2018 at 7:09
• Well I think it's fair to call anything that evaluates to an incorrect answer a 'bug', since presumably Mathematica would never be wrong by design. The correct answer would involve a delta function ∂(n-3). I'll update my answer.
– Max
Commented Feb 16, 2018 at 17:22