Use Assumptions and Integrate?

Mathematica 11.1

Can someone explain why in the following “Assumptions” does not work as expected:

Integrate[ Sin[n x]^2, {x, 0, 2 Pi},  Assumptions -> n \[Element] Integers]

This gives:

Pi - Sin[4 n Pi ]/(4 n)

The second part is clearly redundant, but not even “Simplify” or “FullSimplify” will eliminate it.

Dear Daniel,

FullSimplify will eliminate it. But you have to tell it the assumptions again:

FullSimplify[Integrate[Sin[n x]^2, {x, 0, 2 Pi}, Assumptions -> n[Element] Integers], Assumptions -> n \[Element] Integers]

gives just Pi. Of course, one assumption in the outer most function does the trick, too.

FullSimplify[Integrate[Sin[n x]^2, {x, 0, 2 Pi}],  Assumptions -> n \[Element] Integers]

Cheers,

Marco

The result is correct, because one needs to account for n = 0, for this case the integral is 0. (The limit of the Sin[4n\[Pi]]/(4*n) for n -> 0 is Pi, such that it is cancelled.

I would say that it is perhaps a bug in FullSimplify. Though sometimes solutions are given that are true for most values. Like e.g.

Integrate[x^n, x]
Integrate[x^-1, x]

The first result will not work for n = -1. When to take the solution that is ‘mostly’ correct, and when not, is tricky business. Perhaps @Daniel Lichtblau can comment on this…

Dear Sander,

I don’t think that this is a bug. In the documentation for FullSimplify under Possible Issues it says:

Some of the transformations used by FullSimplify are only generically
correct:

I think that this is one of these cases.

Cheers,

Marco

Hi Sander,

unfortunately the same “singularity” shows even when you exclude the case n = 0:

Integrate[ Sin[n x]^2, {x, 0, 2 Pi}, Assumptions → n>0]

That was exactly my point; sometimes they simplify to things that are correct most of the time, like my example…

If you want to make a point you should include the other assumption as well:

Integrate[Sin[n x]^2, {x, 0, 2 Pi}, Assumptions -> n > 0 \[And] n \[Element] Integers]

And yes, I agree, for this case it could be more simplified. However, Integrate does not promise you to give the easiest possible outcome, it gives you the right result (which it does!), maybe not the simplest result. FullSimplify/Refine on the other hand (kinda) does promise you to give the simplest possible expression (which it does!), as per @Marco Thiel’s answer.

So this is all correct and expected outcome. The result is ok but could be simplified. As also described in the documentation of Integrate (under possible issues)

Specifying integer assumptions may not give a simpler result:

with nearly the same example as the OP. They propose to use refine, rather than FullSimplify, which might, indeed, be better suited for this…

Actually I have commented on various aspects in various forums. Here are some links.

https://mathematica.stackexchange.com/questions/67080/how-to-code-around-known-mma-special-case-failures

https://mathematica.stackexchange.com/questions/19833/usage-of-assuming-for-integration

https://mathematica.stackexchange.com/questions/51967/generate-conditions-seems-to-not-work