# Integrals with conditional result

I am trying to get Mathematica to evaluate integrals such as the well-known Fourier orthogonality relations

$$\int_0^L \sin \frac{2 m \pi x}{L} \sin\frac{2 n \pi x}{L}\,\mathrm{d}x=\begin{cases}0& m\ne n\\\frac{L}{2} & m=n\end{cases}$$

I expected the following code would return a ConditionalExpression representing the two cases:

Assuming[Element[m, Integers] && Element[n, Integers],
Integrate[Sin[(2 m \[Pi] x)/L] Sin[(2 n \[Pi] x)/L], {x, 0, L}]]


However, the result is zero (i.e. it assumes $$m\ne n$$). I have to add an additional assumption that m==n to get Mathematica to evaluate the 'interesting' case. Am I missing something? Is there a way to get both results at once, without needing to already know the conditions for each case? (My ultimate application is a more complicated integral where I'm not confident I have identified all the relevant conditions.)

I'm sure finding the answer to this would be straightforward if only I knew what to search for!

If it's relevant, I'm using Mathematica 12.1.

• Try:Piecewise[{{FullSimplify[ Integrate[Sin[(2 m \[Pi] x)/L] Sin[(2 n \[Pi] x)/L], {x, 0, L}, Assumptions -> {L > 0, m \[Element] Integers, n \[Element] Integers, m == n}], Assumptions -> {{n, m} \[Element] Integers}], m == n}, {FullSimplify[ Integrate[Sin[(2 m \[Pi] x)/L] Sin[(2 n \[Pi] x)/L], {x, 0, L}, Assumptions -> {L > 0, m \[Element] Integers, n \[Element] Integers, m != n}], Assumptions -> {{n, m} \[Element] Integers}], m != n}}]? – Mariusz Iwaniuk Mar 30 at 15:47
• @MariuszIwaniuk - Assuming avoids some redundancy: Assuming[{L > 0, Element[m | n, Integers]}, Piecewise[{{Assuming[m == n, Integrate[Sin[(2 m \[Pi] x)/L] Sin[(2 n \[Pi] x)/L], {x, 0, L}] // FullSimplify], m == n}, {Assuming[m != n, Integrate[Sin[(2 m \[Pi] x)/L] Sin[(2 n \[Pi] x)/L], {x, 0, L}] // FullSimplify], m != n}}]] – Bob Hanlon Mar 30 at 16:50
• @BobHanlon. Oh Yes Thanks :), 1(+) for comment. – Mariusz Iwaniuk Mar 30 at 16:53
• @MariuszIwaniuk Thanks -- but doesn't this rely on already knowing the relevant conditions? (m = n and m ≠ n)? In a more complicated setting where you didn't know these, what would you do? – avid Mar 30 at 22:13
• Along the same line of thinking, @avid, how can you assume whether m or n are equal or unequal? They are different symbols, therefore they are assumed to be unequal unless otherwise specified. The other suggestions here show you how to find and display for both cases. There won’t be a third case, and you know they are either equal or not. – CA Trevillian Mar 31 at 8:04