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?

rcollyer
  • 10,475
  • 4
  • 48
  • 75
Peeter Joot
  • 7,848
  • 7
  • 48
  • 82
  • 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)` – PlatoManiac Oct 12 '11 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. – Daniel Lichtblau Oct 12 '11 at 18:17
  • @Daniel Is the same limitation (ie. useful with only one parameter) valid for `GenerateConditions`? – Dr. belisarius Oct 12 '11 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. – Daniel Lichtblau Oct 12 '11 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 ... – Dr. belisarius Oct 12 '11 at 19:56
  • @belisarius Yes, GenerateConditions->True can give rise to parameter conditions. But it is not in general able to deduce that parameters must miss some infinite discrete set. – Daniel Lichtblau Oct 12 '11 at 21:17

4 Answers4

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}
}]
Community
  • 1
  • 1
rcollyer
  • 10,475
  • 4
  • 48
  • 75
  • 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! – Peeter Joot Oct 13 '11 at 13:07
  • @PeeterJoot, which ones? The usual suspects are [`@@`](http://reference.wolfram.com/mathematica/ref/Apply.html), [`@@@`](http://reference.wolfram.com/mathematica/ref/Apply.html), and [`:>`](http://reference.wolfram.com/mathematica/ref/RuleDelayed.html). Any others? – rcollyer Oct 13 '11 at 13:11
  • 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? – Peeter Joot Oct 13 '11 at 13:18
  • @PeeterJoot, [`#`](http://reference.wolfram.com/mathematica/ref/Slot.html) 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. – rcollyer Oct 13 '11 at 13:27
  • @PeeterJoot, `C[1]` is an arbitrary constant that Mathematica uses on occasion. In this case, `Reduce` used it and added the condition `C[1] == m`, implying it is unnecessary. So, the substitution `C[1] -> m` is mathematically valid. [`Replace`](http://reference.wolfram.com/mathematica/ref/Replace.html) (`/.`) can perform any arbitrary transform, and most are not mathematically valid. So, when using it, you have to be careful. – rcollyer Oct 13 '11 at 13:31
  • @PeeterJoot, here's the link to [pure functions](http://reference.wolfram.com/mathematica/ref/Function.html). – rcollyer Oct 13 '11 at 13:33
  • @PeeterJoot, [`->`](http://reference.wolfram.com/mathematica/ref/Rule.html) and `:>` operate slightly differently. The right hand side of `->` is evaluated immediately, while in `:>` it is evaluated when it is used. This allows you to create parameter dependent transforms, like `Cos[q_ Pi x] :> Piecewise[ ... ]`. – rcollyer Oct 13 '11 at 13:37
  • 3
    +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. – Leonid Shifrin Oct 13 '11 at 14:25
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

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • You could use `Limit` instead of redoing the integral. See [here](http://pastebin.com/NxPBgizU). 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 Oct 12 '11 at 22:00
  • @Simon I am trying to get Mma to realize the domain exclusions by itself. Still no luck ... – Dr. belisarius Oct 12 '11 at 22:13
  • @Simon And the Reduce thing is weird! – Dr. belisarius Oct 12 '11 at 22:28
  • @Simon Try `Reduce[Sin[x Pi]/x==0,x,Integers]` and `Reduce[Sin[x Pi]/x==0,x]` – Dr. belisarius Oct 12 '11 at 22:35
  • 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`... – Simon Oct 13 '11 at 00:46
  • Check out the solution I posted below. I use the properties of cosine itself to generate all of the solutions to the integral. – rcollyer Oct 13 '11 at 12:55
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

abcd
  • 41,765
  • 7
  • 81
  • 98
  • I get just zero if I evaluate your second integral with the assumption. – PlatoManiac Oct 12 '11 at 17:38
  • That's weird. [Straight off my mma](http://i.stack.imgur.com/zPvOA.png). I don't know if it's got something to do with the version. I use version 7.0 – abcd Oct 12 '11 at 17:42
  • You are right I can reproduce your result in MMA 7 but MMA 8 gives just zero. – PlatoManiac Oct 12 '11 at 17:46
  • @yoda Sin[ p Pi] == 0 ... so the only non zero result is when the denominator is zero – Dr. belisarius Oct 12 '11 at 17:47
  • I do reproduce @yoda's result, M8.0 – Leonid Shifrin Oct 12 '11 at 17:49
  • @yoda Those special cases can be integrated separatedly – Dr. belisarius Oct 12 '11 at 17:50
  • 2
    @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. – Sjoerd C. de Vries Oct 12 '11 at 17:52
  • I was thinking about this result today, and I think your answer is perfectly correct provided that some things are pointed out. Specifically, `Limit[Sin[q Pi]/q, q -> 0] == 1`, although the function does not exist at that point. The limit, however, can be taken for the value. I think the fact that you have to invoke a limit implies that something odd is going on anyway. – rcollyer Jan 10 '12 at 19:07
  • @rcollyer That limit should be π, and it's the standard sinc function anyway (differing by a factor of π), so I don't see anything odd with it. – abcd Jan 10 '12 at 19:18
  • You're right the limit should be Pi. But, I guess the point I was trying to make was that this result is the final result, but at the time it just didn't seem that way when I wrote my answer. – rcollyer Jan 10 '12 at 19:20
  • @rcollyer I liked reading how each one poked at the problem differently though. Although this might've been the answer, my response was lacking in explanation =) – abcd Jan 10 '12 at 19:25
  • True, and I agree, the different methods was intriguing. – rcollyer Jan 10 '12 at 19:41
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

PlatoManiac
  • 1,442
  • 11
  • 31