# Integrate wrong for absolute value of trig function

I was trying to get $\int_0^1 \lvert \cos(2 \pi k x) \rvert \,\mathrm{d}x$ for $k \in \mathbb{Z}$, and was surprised by the result (using Mathematica 10.0.1.0):

Integrate[Abs[Cos[2 π k x]], {x, 0, 1}, Assumptions -> Element[k, Integers]]

(Abs[Cos[2 k π]] Tan[2 k π])/(2 k π)

Simplify[%, Assumptions->Element[k, Integers]]

 0

Table[Integrate[Abs[Cos[2 π k x]], {x, 0, 1}], {k, 0, 10}]

 {1, 2/π, 2/π, 2/π, 2/π, 2/π, 2/π, 2/π, 2/π, 2/π, 2/π}

Table[NIntegrate[Abs[Cos[2 π k x]], {x, 0, 1}], {k, 0, 10}]

{1., 0.63662, 0.63662, 0.63662, 0.63662, 0.63662,
0.63662, 0.63662, 0.63662, 0.63662, 0.63662}


The answer of 0 is obviously ridiculous, since the integrand is nonnegative and not identically zero. Integrate with specific integers seems to get it right; the relevant identity at the bottom of this section of Wikipedia's list of integrals agrees that the value should be $\frac{2}{\pi}$ for $k \ne 0$, and of course it should be 1 for $k = 0$.

A similar issue happens with $\int_0^1 \lvert \sin(2 \pi k x) \rvert \,\mathrm{d}x$: Integrate gives $$\frac{\text{sgn}(k) (k-\cot (2 \pi k) \left| k \sin (2 k \pi )\right| )}{2 \pi k^2}$$ which is indeterminant for integral $k$ but has limit 0 at each integer, but whose value should also be $\frac{2}{\pi}$ for $k \ne 0$ and 0 for $k = 0$. (Again, Integrate with any particular value of $k$ gets it right.)

If I use the variable k without any Assumptions, Integrate eventually gives up.

Am I doing something wrong here in specifying my assumptions or whatever, or is this a bug for a surprisingly simple integral?

• Integrate gives a result that is correct only for a smallish interval of values of k. After that it will mess up. It cannot unravel correctly the absolute value behavior in the presence of a parameter, and does not seem to figure out restrictions on that parameter that would make the result work. As for an assumption of integrality, Integrate will only regard that as meaning the parameter is real valued. Oct 16, 2014 at 14:09
• Interesting. I suppose I knew that Integrate sometimes gives results that are only valid for a subset of parameter values, but (a) I kind of wish it would say when it's doing that and (b) I didn't know it couldn't interpret the integrality assumption. Oct 16, 2014 at 14:32

The problem arises because of the branch-cuts of the Tan. Since Mathematica 8.0 doesn't integrate the indefinite Integral with Abs[...], do it with Sqrt[Sqr[Cos[...]]]

In:= int1[x_, k_] = Integrate[Sqrt[Cos[2 \[Pi] k x]^2], x]

Out= (Sqrt[Cos[2 k \[Pi] x]^2] Tan[2 k \[Pi] x])/(2 k \[Pi])


Plotting this, shows it has 2 k Branch-Cuts where the Tan is +-Infinity.

Plot[(Sqrt[Cos[2 k \[Pi] x]^2] Tan[2 k \[Pi] x])/(2 k \[Pi]) /.
k -> 3, {x, 0, 1}]


At the first branch-cut at x= 1/(4 k) the functions upper limit is

In:= Limit[(Sqrt[Cos[2 k \[Pi] x]^2] Tan[2 k \[Pi] x])/(
2 k \[Pi]), x -> 1/4/k, Direction -> -1]

Out= -(Sqrt[k^2]/(2 k^2 \[Pi]))


The lower limit there is (Sqrt[k^2]/(2 k^2 [Pi])) The total jump at x=1/(4 k) is therefore 1/(k Pi). This is the same for the other branch-cuts. Multiplied with the 2 k branch-cuts you get the right result: 2/Pi

Mathematica didn't take into accout this in the definite integral.