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 ofk
. 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. $\endgroup$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. $\endgroup$