# How to obtain an unified expression for the symbolic integral?

When I use the Mathematica to obtain the integral of the symbolic expression, the output result seems not to be applied for all the cases. For example I use the code

Integrate[Cos[m Pi x] Cos[n Pi x], {x, 0, 1},
Assumptions -> m \[Element] Integers && n \[Element] Integers]


to calculate the integral $$\int\cos (m\pi x)\cos (n \pi x)\,\mathrm{d}x$$

The output is $$\frac{m \sin (\pi m) \cos (\pi n)-n \cos (\pi m) \sin (\pi n)}{\pi m^2-\pi n^2}$$

I define a function with respect to the variables $m$ and $n$

f[m_, n_] := (
m Cos[n \[Pi]] Sin[m \[Pi]] - n Cos[m \[Pi]] Sin[n \[Pi]])/(
m^2 \[Pi] - n^2 \[Pi]);


Obviously, the function $f(m,n)$ is only applicable for the cases $m\neq n$. Some assumptions are made during the calculation by Mathematica. Now I want to obatin the unified expressions for the integral, for example, The result can be a conditional expression.

• In fact the function f(m,n) is applicable for the cases m=n, if one takes the limit n->m instead of assignment. Try enhance the definition: f[m_, m_]:=Limit[f[m, n], n -> m].
– Mher
Dec 12, 2017 at 9:10
• @Mher Thanks for your comments. It works for this example. The example shown in this post is a minimal work example. I want to apply the function(The function in my real work is much complex than this example) to a large list and I found that the Limit is very time-consuming. Are there any alternative methods? Dec 12, 2017 at 9:21

f[m_, n_] =
PiecewiseExpand[
Which[m == n,
Integrate[Cos[m Pi x] Cos[n Pi x], {x, 0, 1},
Assumptions -> {m \[Element] Integers && n \[Element] Integers &&
m == n}], m != n,
Integrate[Cos[m Pi x] Cos[n Pi x], {x, 0, 1},
Assumptions -> {m \[Element] Integers && n \[Element] Integers &&
m != n}]]]

{f[1, 2],f[1,1]}

(*    {0, 1/2}    *)