13

Evaluating the following integral should be non-zero, and mathematica correctly gives a non-zero result

Integrate[ Cos[ (Pi * x)/2 ]^2 * Cos[ (3*Pi*x)/2 ]^2, {x, -1, 1}]

However, attempting a more general integral:

FullSimplify[
    Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2], 
              {x, -1, 1}], 
    Element[{m, n}, Integers]]

yields zero, which is definitely not true for m = n = 1

I'd expect a conditional expression. Is it possible to "tell" mathematica about my constraints on m and n before the integral is evaluated so that it handles the special cases properly?

6
  • For m=n=1 you get division by zero operation. So MMA 8 tells that the integral is undefined as far as Real field is concerned. Here is the integral (Sin[(-1 + m - n)*Pi]/(-1 + m - n) + (2*Sin[(m - n)*Pi])/(m - n) + Sin[(1 + m - n)*Pi]/(1 + m - n) + Sin[(m + n)*Pi]/(m + n) + (2*Sin[(1 + m + n)*Pi])/(1 + m + n) + Sin[(2 + m + n)*Pi]/(2 + m + n))/(4*Pi) Commented Oct 12, 2011 at 17:28
  • 2
    Integrate is not going to do anything useful with an assumption of integrality of one or more parameters. Best one can hope is that it will internally convert that to one of real-valuedness. That way at least the result is not likely to be zero. Then Limit can be used to get nonzero results for the finitely many cases of interest. Commented Oct 12, 2011 at 18:17
  • @Daniel Is the same limitation (ie. useful with only one parameter) valid for GenerateConditions? Commented Oct 12, 2011 at 19:31
  • 2
    @belisarius Not sure what you are asking. Assumptions is not limitated to working with only one parameter. It does have a limitation that it will not do much of interest with assuming things to be integers. GenerateConditions->False attempts, among other things, to ignore parameter restrictions that might be deduced while processing an integral. It allows for the processing flow to continue when it might otherwise get stuck. Were that processing instead electricity, the setting would be akin to sticking a penny in your fuse box. Commented Oct 12, 2011 at 19:38
  • @Daniel Trying to clarify. In this particular case, GenerateConditions is no detecting the given result is indeterminate for a few conditions (those that nullify the denominator) However, for example Integrate[1/(x - b), {x, 0, 1}, GenerateConditions -> True] does find conditions on b. I guess the two cases are somewhat different ... Commented Oct 12, 2011 at 19:56

4 Answers 4

11

While I'm late to the party, no one has given a complete solution, thus far.

Sometimes, it pays to understand the integrand better before you integrate. Consider,

ef = TrigReduce[
    Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2]]/.
  Cos[a_] :> Cos[ Simplify[a, Element[{m,n}, Integers] ] ]

which returns

(2 Cos[(m - n) Pi x] + Cos[(1 + m - n) Pi x] + Cos[(1 - m + n) Pi x] + 
 Cos[(m + n) Pi x] + 2 Cos[(1 + m + n) Pi x] + Cos[(2 + m + n) Pi x] )/8

where each term has the form Cos[q Pi x] with integral q. Now, there are two cases to consider when integrating Cos[q Pi x] over -1 to 1 (where q is integral): q == 0 and q != 0.

Case q = 0: This is a special case that Mathematica misses in the general result, as it implies a constant integrand. (I'll often miss it, also, when doing this by hand, so Mathematica isn't entirely to blame.) So, the integral is 2, in this case.

Strictly speaking, this isn't true. When told to integrate Cos[ q Pi x ] over -1 < x < 1, Mathematica returns

2 Sin[ Pi q ]/( Pi q )

which is 0 except when q == 0. At that point, the function is undefined in the strict sense, but Limit[Sin[x]/x, q -> 0] == 1. As the singularity at q == 0 is removable, the integral is 2 when q -> 0. So, Mathematica does not miss it, it is just in a form not immediately recognized.

Case q != 0: Since Cos[Pi x] is periodic with period 2, an integral of Cos[q Pi x] from x == -1 to x == 1 will always be over q periods. In other words,

Integrate[ Cos[q Pi x], {x, -1, 1}, 
  Assumptions -> (Element[ q, Integers ] && q != 0) ] == 0

Taken together, this means

Integrate[ Cos[q Pi x], {x, -1, 1}, Assumptions -> Element[ q, Integers ] ] == 
Piecewise[{{ q == 0, 2 }, { 0, q!=0 }}]

Using this, we can integrate the expanded form of the integrand via

intef = ef /. Cos[q_ Pi x] :> Piecewise[{{2, q == 0}, {0, q != 0}}] // 
 PiecewiseExpand 

which admits non-integral solutions. To clean that up, we need to reduce the conditions to only those that have integral solutions, and we might as well simplify as we go:

(Piecewise[{#1, 
    LogicalExpand[Reduce[#2 , {m, n}, Integers]] // 
     Simplify[#] &} & @@@ #1, #2] & @@ intef) /. C[1] -> m

\begin{Edit}

To limit confusion, internally Piecewise has the structure

{ { { value, condition } .. }, default }

In using Apply (@@), the condition list is the first parameter and the default is the second. To process this, I need to simplify the condition for each value, so then I use the second short form of Apply (@@@) on the condition list so that for each value-condition pair I get

{ value, simplified condition }

The simplification process uses Reduce to restrict the conditions to integers, LogicalExpand to help eliminate redundancy, and Simplify to limit the number of terms. Reduce internally uses the arbitrary constant, C[1], which it sets as C[1] == m, so we set C[1] back to m to complete the simplification

\end{Edit}

which gives

Piecewise[{
 {3/4, (1 + n == 0 || n == 0) && (1 + m == 0 || m == 0)},
 {1/2, Element[m, Integers] && 
       (n == m || (1 + m + n == 0 && (m <= -2 || m >= 1)))},
 {1/4, (n == 1 + m || (1 + n == m && (m <= -1 || m >= 1)) || 
       (m + n == 0 && (m >= 1 || m <= 0)) || 
       (2 + m + n == 0 && (m <= -1 || m >= 0))) && 
       Element[m, Integers]},
 {0, True}
}

as the complete solution.

Another Edit: I should point out that both the 1/2 and 1/4 cases include the values for m and n in the 3/4 case. It appears that the 3/4 case may be the intersection of the other two, and, hence, their sum. (I have not done the calc out, but I strongly suspect it is true.) Piecewise evaluates the conditions in order (I think), so there is no chance of getting this incorrect.

Edit, again: The simplification of the Piecewise object is not as efficient as it could be. At issue is the placement of the replacement rule C[1] -> m. It happens to late in the process for Simplify to make use of it. But, if it is brought inside the LogicalExpand and assumptions are added to Simplify

(Piecewise[{#1, 
    LogicalExpand[Reduce[#2 , {m, n}, Integers] /. C[1] -> m] // 
     Simplify[#, {m, n} \[Element] Integers] &} & @@@ #1, #2] & @@ intef)

then a much cleaner result is produce

Piecewise[{
 {3/4, -2 < m < 1 && -2 < n < 1}, 
 {1/2, (1 + m + n == 0 && (m >= 1 || m <= -2)) || m == n}, 
 {1/4, 2 + m + n == 0 || (m == 1 + n && m != 0) || m + n == 0 || 1 + m == n},
 {0, True}
}]
Sign up to request clarification or add additional context in comments.

8 Comments

There's a lot of special mathematica notation and functions in your answer that I will have to look up, but the idea of your approach is at least clear. Nicely crafted answer!
@PeeterJoot, which ones? The usual suspects are @@, @@@, and :>. Any others?
more than you think... I'm a newbie: # #1 & all look to be used in special ways. Where did C[1] come from? /. C[1] -> m appears to be a substitution operation by your comments, a shortcut for the With[] function?
@PeeterJoot, # is called a Slot. It is a parameter in a anonymous (pure) function, and the number, e.g. #2, refers to which parameter (# == #1). For instance, #1^2 & [2] == 4, in prefix form #^2& @ 2, or postfix form 2 // #^2&. The & tells Mathematica that it is a pure function. Prefix and postfix form can only accept a single parameter, infix (1 ~ #1 + #2& ~ 2) two params, but Apply (@@ and @@@) will accept arbitrary numbers of them.
+1. I think, your answer shows the essense of how one should work in Mathematica, and emphasizes that it is a (very sophisticated) tool that still has to be driven, directed and helped by a human, to be used optimally. All too often complete solutions are expected out of the box, but, while this is surely the ultimate goal, a motivated human + Mathematica, working in tandem, will always be able to achieve immeasurably more than just Mathematica alone. Would vote for this twice if I could.
|
5

Not always zero ...

k = Integrate[
         Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2], 
         {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];

(*Let's find the zeroes of the denominator *)

d = Denominator[k];
s = Solve[d == 0, {m, n}]

(*The above integral is indeterminate at those zeroes, so let's compute 
  the integral again there (a Limit[] could also do the work) *)

denZ = Integrate[
          Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] /.s, 
          {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]];

(* All possible results are generated with m=1 *)

denZ /. m -> 1

(*
{1/4, 1/2, 1/4, 1/4, 1/2, 1/4}
*)

Visualizing those cases:

Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2] 
     /. s /. m -> 1, {x, -1, 1}]

enter image description here

Compare with a zero result integral one:

Plot[Cos[(Pi x)/2]^2 Cos[((2 (n) + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/ 2] 
     /. {m -> 1, n -> 4}, {x, -1, 1}]

enter image description here

6 Comments

You could use Limit instead of redoing the integral. See here. However, the interesting thing to note is that Reduce does not return the right result, despite the docs saying that "The result of Reduce[expr,vars] always describes exactly the same mathematical set as expr."
@Simon I am trying to get Mma to realize the domain exclusions by itself. Still no luck ...
@Simon And the Reduce thing is weird!
@Simon Try Reduce[Sin[x Pi]/x==0,x,Integers] and Reduce[Sin[x Pi]/x==0,x]
Thanks, I should know to check simple cases by now! So Reduce[Sin[x Pi]/x == 0, x, Integers] misses the X != 0 property that Simplify[Reduce[Sin[x Pi]/x == 0, x], Element[x, Integers]] manages to find. So maybe there's a bug/problem in the assumption/simplification system built into Reduce...
|
4

If you just drop the whole FullSimplify part, mathematica does the integration neatly for you.

Integrate[
 Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
    2], {x, -1, 1}]

enter image description here

To include the condition that m and n are integers, it's better to use the Assumptions option in Integrate.

Integrate[
 Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
    2], {x, -1, 1}, Assumptions -> Element[{m, n}, Integers]]

enter image description here

12 Comments

I get just zero if I evaluate your second integral with the assumption.
That's weird. Straight off my mma. I don't know if it's got something to do with the version. I use version 7.0
You are right I can reproduce your result in MMA 7 but MMA 8 gives just zero.
@yoda Sin[ p Pi] == 0 ... so the only non zero result is when the denominator is zero
@I get the same as Yoda. However, if you put the same assumptions in Simplify you get 0, and this is generally correct for an infinite set of integers except for those where n=m holds. Limit[integral, n->m] yields the correct answer too.
|
2

Lets use some conclusive conditions about the two integers m=n||m!=n.

Assuming[{(n \[Element] Integers && m \[Element] Integers && m == n)},
Integrate[Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/2],
{x, -1, 1}]]

The answer for this case is 1/2. For the other case it is

Assuming[{(n \[Element] Integers && m \[Element] Integers && m != n)},
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
 2], {x, -1, 1}]]

and the answer is 0.

However I am amazed to see that if we add this two conditions as an "either or stuff", Mathematica returns one zero after integration. I mean in case of the following I am getting only zero but not ``1/2||0`.

Assuming[{(n \[Element] Integers && m \[Element] Integers && 
 m == n) || (n \[Element] Integers && m \[Element] Integers && 
 m != n)}, 
Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
 2], {x, -1, 1}]]

By the way we can see the conditions exclusively where this integral becomes Indeterminate.

res = Integrate[
Cos[(Pi x)/2]^2 Cos[((2 n + 1) Pi x)/2] Cos[((2 m + 1) Pi x)/
  2], {x, -1, 1}] // Simplify

The output is here.

enter image description here

Now lets see all the relations m and n can have to make the Integral bad!

BadPart = (res*4 Pi);
Flatten@(Solve[(Denominator[#] == 0), m] & /@ 
 Table[BadPart[[i]], {i, 1, Length@BadPart}] /. 
Rule -> Equal) // TableForm

enter image description here

So these are the special cases which as Sjoerd mentioned are having infinite instances.

BR

2 Comments

What result do you get from the integrate with {m -> 2, n -> 1}?
See my answer for those "special cases" results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.