5
$\begingroup$

I'm trying to get MMA to help me evaluate certain integrals of trig functions. Here is an example: (The actual expressions I want to evaluate are more complicated than this one, but this illustrates the problem.)

Assuming[
    n ∈ Integers && m ∈ Integers,
    Integrate[Cos[n π x] Cos[m π x], {x, 0, 1}]
]
(* 0 *)

This answer is of course wrong, strictly speaking. The correct answer is $\frac{1}{2}\left(\delta_{n,m}+\delta_{n,-m}\right)$. As discussed here, for instance, MMA aims to produce generically correct results, and the special case $m=\pm n$ ends up being overlooked. I understand all that.

My question is whether anyone can suggest a straightforward workaround to get MMA to produce a more generally correct result for slightly more complex cases such as Integrate[Sin[k π x] Cos[n π x] Sin[m π x], {x,0,1}].

To clarify, I will add that there is no difficulty evaluating the special cases, if you know what they are. For instance:

Assuming[
    n ∈ Integers,
    Integrate[Cos[n π x] Cos[n π x], {x, 0, 1}]
]
(* 1/2 *)

So this is one of those vexing questions where it's easy to find the answer, once you know what it is. And of course you can use trig identities to get the answer, but getting the signs right is a tedious, fiddly business. I'd like to let MMA do it for me.

In case anyone cares, these integrals arise from PDEs when the solutions are represented as a cosine series.

Thanks.

$\endgroup$
  • $\begingroup$ Use the assumption that they are real valued. Then take limits as needed. Integrate[Sin[k \[Pi] x] Cos[n \[Pi] x] Sin[m \[Pi] x], {x, 0, 1}, Assumptions -> Element[{k, m, n}, Reals]] gives a result that should behave with respect to limits. $\endgroup$ – Daniel Lichtblau Sep 13 '16 at 21:33
  • $\begingroup$ One of the little annoyances of mma is the inability to tell it what theory of integration to use. FourierTransform will yield generalized functions as needed, but Integrate will only output generalized functions if its input contains generalized functions. It's heuristic, not rigorous, and not easy for the user to control. $\endgroup$ – John Doty Sep 14 '16 at 23:20
  • 1
    $\begingroup$ While we were still on Stack Overflow, a very similar question was asked. Here's my answer to it. $\endgroup$ – rcollyer Sep 19 '16 at 13:49
5
$\begingroup$

Summary

Interesting question.

I have just found (18.09.16) a simplification that leads automatically to the KroneckerDelta representation. See fccs below. I still need to describe the procedure.

Studying a series of examples with increasing number of trig factors we give different useful expressions for the integral: Matrix, SparseArray and KroneckerDelta.

Most of the results are obatined automatically from Mathematica commands.

The approach can be generalized to an arbitrary number of trig factors.

Ingredients are: Limit[], auxiliary indices in Table[] combined with Limit[], SparseArray[]

Example of the OP

Let me start with the example of the OP

f0 = Assuming[n ∈ Integers && m ∈ Integers, 
  Integrate[Cos[n π x] Cos[m π x], {x, 0, 1}]]

0

Putting the assumptions under the integral gives

fcc = Integrate[Cos[n π x] Cos[m π x], {x, 0, 1}, 
  Assumptions -> {{n, m} ∈ Integers}]

$$\text{fcc} = \frac{m \sin (\pi m) \cos (\pi n)-n \cos (\pi m) \sin (\pi n)}{\pi m^2-\pi n^2}$$

Imposing no assumptions at all with the integral gives the same result. Hence at least in version 8 it is not necessary to proceed as proposed by Daniel Lichtblau.

Name convention: The notation cc stands for the product of the two cosines and will be adopted in the more genral cases cs (for Cos * Sin) etc. in the following.

Now we impose the condition of integrity of n and m on the result.

The numerator vanishes at integer values of n and m, but we must be careful as the denominator vanishes also for m^2 == n^2

The case n != ± m is simple

Simplify[fcc, {{n, m} ∈ Integers, n != m && n != -m}]

(* Out[6]= 0 *)

The case n^2 == m^2 must be treated using the Limit[]

Limit[fcc, m -> n]

(* Out[15]= 1/4 (2 + Sin[2 n π]/(n π)) *)

Limit[fcc, m -> -n]

(* Out[17]= 1/4 (2 + Sin[2 n π]/(n π)) *)

If n == m == 0 we have

Limit[fcc /. m -> 0, n -> 0]

(* Out[21]= 1 *)

The elements of fcc can be calculated uniformly using Limit in combination with an auxiliary index k:

tcc = Table[Limit[fcc, n -> k], {m, -3, 3}, {k, -3, 3}];
% // MatrixForm

$$tcc = \left( \begin{array}{ccccccc} \frac{1}{2} & 0 & 0 & 0 & 0 & 0 & \frac{1}{2} \\ 0 & \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 \\ 0 & 0 & \frac{1}{2} & 0 & \frac{1}{2} & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & \frac{1}{2} & 0 & \frac{1}{2} & 0 & 0 \\ 0 & \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 \\ \frac{1}{2} & 0 & 0 & 0 & 0 & 0 & \frac{1}{2} \\ \end{array} \right)$$

It is also interesting to define this tensor as a sparse array. In rule form this reads

acc = ArrayRules[SparseArray[tcc]]

$$\left\{\{1,1\}\to \frac{1}{2},\{1,7\}\to \frac{1}{2},\{2,2\}\to \frac{1}{2},\{2,6\}\to \frac{1}{2},\{3,3\}\to \frac{1}{2},\{3,5\}\to \frac{1}{2},\{4,4\}\to 1,\{5,3\}\to \frac{1}{2},\{5,5\}\to \frac{1}{2},\{6,2\}\to \frac{1}{2},\{6,6\}\to \frac{1}{2},\{7,1\}\to \frac{1}{2},\{7,7\}\to \frac{1}{2},\{\_,\_\}\to 0\right\}$$

Finally, fcc can be written in terms of KroneckerDelta[]

kcc = 1/2 ( KroneckerDelta[n - m] + KroneckerDelta[n + m])

$$kcc = \frac{1}{2} (\delta _{m-n}+\delta _{m+n})$$

tkcc = Table[kcc, {n, -3, 3}, {m, -3, 3}];

% == tcc

(* Out[49]= True *)

The Kronecker representation in this case was easy to guess. Below we shall provide a systematic method to find that representation.

Systematic study

Let us now extend this example to a more systematic study. Let p = 1, 2, 3, ... be the number of trig factors of the integrand.

The case p = 2 will be completed first.

Sin x Sin

fss = Integrate[Sin[n π x] Sin[m π x], {x, 0, 1}]

$$fss = \frac{n \sin (\pi m) \cos (\pi n)-m \cos (\pi m) \sin (\pi n)}{\pi m^2-\pi n^2}$$

Simplify[fss, {{n, m} ∈ Integers, n != m && n != -m}]

(* Out[3]= 0 *)

Limit[fss, m -> #] & /@ (m /. Solve[n^2 == m^2, m])
Simplify[%, n ∈ Integers]

{1/4 (-2 + Sin[2 n π]/(n π)), 1/2 - Sin[2 n π]/(4 n π)}

{-(1/2), 1/2}

tss = Table[Limit[fss, n -> k], {m, -3, 3}, {k, -3, 3}];
% // MatrixForm

$$\text{tss} = \left( \begin{array}{ccccccc} \frac{1}{2} & 0 & 0 & 0 & 0 & 0 & -\frac{1}{2} \\ 0 & \frac{1}{2} & 0 & 0 & 0 & -\frac{1}{2} & 0 \\ 0 & 0 & \frac{1}{2} & 0 & -\frac{1}{2} & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & -\frac{1}{2} & 0 & \frac{1}{2} & 0 & 0 \\ 0 & -\frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 \\ -\frac{1}{2} & 0 & 0 & 0 & 0 & 0 & \frac{1}{2} \\ \end{array} \right)$$

ass = ArrayRules[SparseArray[tss]]

$$\text{ass} = \left\{\{1,1\}\to \frac{1}{2},\{1,7\}\to -\frac{1}{2},\{2,2\}\to \frac{1}{2},\{2,6\}\to -\frac{1}{2},\{3,3\}\to \frac{1}{2},\{3,5\}\to -\frac{1}{2},\{5,3\}\to -\frac{1}{2},\{5,5\}\to \frac{1}{2},\{6,2\}\to -\frac{1}{2},\{6,6\}\to \frac{1}{2},\{7,1\}\to -\frac{1}{2},\{7,7\}\to \frac{1}{2},\{\_,\_\}\to 0\right\}$$

kss = 1/2 ( KroneckerDelta[n - m] - KroneckerDelta[n + m]);

$$\text{kss}=\frac{1}{2} (\delta _{n-m}-\delta _{m+n})$$

tdss = Table[kss, {n, -3, 3}, {m, -3, 3}];

tdss == tss

True

Cos x Sin

This case is interesting as it has a less trivial KroneckerDelta representation

fcs = Integrate[Cos[n π x] Sin[m π x], {x, 0, 1}]

$$fcs = \frac{-n \sin (\pi m) \sin (\pi n)+m (-\cos (\pi m)) \cos (\pi n)+m}{\pi m^2-\pi n^2}$$

Simplify[fcs, {{n, m} ∈ Integers, n != m&&n!= - m}]

((1 + (-1)^(1 + m + n)) m)/((m^2 - n^2) π)

Limit[fcs, m -> #] & /@ (m /. Solve[n^2 == m^2, m])
Simplify[%, n ∈ Integers]

{-(Sin[n π]^2/(2 n π)), Sin[n π]^2/(2 n π)}

{0, 0}

tcs = Table[Limit[fcs, n -> k], {m, -3, 3}, {k, -3, 3}];
% // MatrixForm

$$tcs = \left( \begin{array}{ccccccc} 0 & -\frac{6}{5 \pi } & 0 & -\frac{2}{3 \pi } & 0 & -\frac{6}{5 \pi } & 0 \\ \frac{4}{5 \pi } & 0 & -\frac{4}{3 \pi } & 0 & -\frac{4}{3 \pi } & 0 & \frac{4}{5 \pi } \\ 0 & \frac{2}{3 \pi } & 0 & -\frac{2}{\pi } & 0 & \frac{2}{3 \pi } & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & -\frac{2}{3 \pi } & 0 & \frac{2}{\pi } & 0 & -\frac{2}{3 \pi } & 0 \\ -\frac{4}{5 \pi } & 0 & \frac{4}{3 \pi } & 0 & \frac{4}{3 \pi } & 0 & -\frac{4}{5 \pi } \\ 0 & \frac{6}{5 \pi } & 0 & \frac{2}{3 \pi } & 0 & \frac{6}{5 \pi } & 0 \\ \end{array} \right) $$

acs = ArrayRules[SparseArray[tcs]]

$$\left\{\{1,2\}\to -\frac{6}{5 \pi },\{1,4\}\to -\frac{2}{3 \pi },\{1,6\}\to -\frac{6}{5 \pi },\{2,1\}\to \frac{4}{5 \pi },\{2,3\}\to -\frac{4}{3 \pi },\{2,5\}\to -\frac{4}{3 \pi },\{2,7\}\to \frac{4}{5 \pi },\{3,2\}\to \frac{2}{3 \pi },\{3,4\}\to -\frac{2}{\pi },\{3,6\}\to \frac{2}{3 \pi },\{5,2\}\to -\frac{2}{3 \pi },\{5,4\}\to \frac{2}{\pi },\{5,6\}\to -\frac{2}{3 \pi },\{6,1\}\to -\frac{4}{5 \pi },\{6,3\}\to \frac{4}{3 \pi },\{6,5\}\to \frac{4}{3 \pi },\{6,7\}\to -\frac{4}{5 \pi },\{7,2\}\to \frac{6}{5 \pi },\{7,4\}\to \frac{2}{3 \pi },\{7,6\}\to \frac{6}{5 \pi },\{\_,\_\}\to 0\right\}$$

The Kronecker representation is slightly more complicated as the matrix elements are not constants.

Here's a systematic procedure to calculate the KronekcerDelta repesentation:

Let us assume that

(1) n - m == q

here q is an arbitrary integer. Letting q = -2m + q1 shows that we have covered both cases m==n and m==-n so that (1) can be assumed in general.

Then we write (for thfirst

s1 = Sum[KroneckerDelta[n - m - q] Simplify[Limit[π/2 fcs, m -> n - q], 
    n ∈ Integers], {q, -3, 3}]

$$= \frac{(1-n) \delta _{m-n+1}}{2 n-1}+\frac{(3-n) \delta _{m-n+3}}{6 n-9}+\frac{(n+1) \delta _{-m+n+1}}{2 n+1}+\frac{(n+3) \delta _{-m+n+3}}{6 n+9}$$

This can be simplified using some minor guesswork to

r1 = Sum[((q - n) KroneckerDelta[q + m - n])/(-q^2 + 2 q n), {q, -3, 3, 2}];

r1 == s1 // Simplify

True

Now observing that only odd numbers q appear we write

((q - n) KroneckerDelta[q + m - n])/(-q^2 + 2 q n) /. q -> 2 i + 1

((1 + 2 i - n) KroneckerDelta[1 + 2 i + m - n])/(-(1 + 2 i)^2 + 2 (1 + 2 i) n)

and arrive at the final expression for the Kronecker representation for fcs

kcs[n_, m_] := 
 2/π Sum[(1 + 2 i - n) /((1 + 2 i) (2  n - (1 + 2 i)))
     KroneckerDelta[1 + 2 i + m - n], {i, -∞, ∞}]

Latex

$$\text{kcs}(\text{n$\_$},\text{m$\_$})\text{:=}\frac{2 \sum _{i=-\infty }^{\infty } \frac{(2 i-n+1) \delta _{2 i+m-n+1}}{(2 i+1) (2 n-(2 i+1))}}{\pi }$$

Checking it

Table[kcs[n, m], {m, -3, 3}, {n, -3, 3}] == tcs

True

The case p = 3

Cos x Cos x Cos

fccc = Integrate[Cos[n π x] Cos[m π x] Cos[k π x], {x, 0, 1}]

$$\frac{\frac{\sin (\pi (k-m-n))}{k-m-n}+\frac{\sin (\pi (k+m-n))}{k+m-n}+\frac{\sin (\pi (k-m+n))}{k-m+n}+\frac{\sin (\pi (k+m+n))}{k+m+n}}{4 \pi }$$

We connfine ourselves to the KroneckerDelta representation:

kccc = Sum[
  Simplify[KroneckerDelta[k - m - n - q] Limit[fccc, k -> n + m + q] + 
    KroneckerDelta[k + m - n - q] Limit[fccc, k -> n - m + q] + 
    KroneckerDelta[k - m + n - q] Limit[fccc, k -> -n + m + q] + 
    KroneckerDelta[k + m + n - q] Limit[fccc, k -> -n - m + q], {k, n, 
     m} \[Element] Integers], {q, -3, 3}]

$$\text{kccc}=\frac{1}{4} (\delta _{k-m-n}+\delta _{k+m-n}+\delta _{k-m+n}+\delta _{k+m+n})$$

No guesswork was required here.

Cos x Cos x Sin

fccs = Integrate[Cos[n π x] Cos[m π x] Sin[k π x], {x, 0, 1}]

(* Out[6]= (1/(k - m - n) + 1/(k + m - n) + 1/(k - m + n) + 1/(k + m + n) - 
 Cos[(k - m - n) π]/(k - m - n) - Cos[(k + m - n) π]/(k + m - n) - 
 Cos[(k - m + n) π]/(k - m + n) - Cos[(k + m + n) π]/(
 k + m + n))/(4 π) *)

Kronecker: some first terms

kccs3 = Sum[
  Simplify[KroneckerDelta[k - m - n - q] Limit[fccs, k -> n + m + q] + 
    KroneckerDelta[k + m - n - q] Limit[fccs, k -> n - m + q] + 
    KroneckerDelta[k - m + n - q] Limit[fccs, k -> -n + m + q] + 
    KroneckerDelta[k + m + n - q] Limit[fccs, k -> -n - m + q], {k, n, 
     m} \[Element] Integers], {q, -3, 3}];

(* Output skipped here *)

General result without any guesswork

Notice (18.09.16) We need to take only the term with KroneckerDelta[k - m - n - q]. The others are covered by q.

kccs[n_, m_, k_] := 
 Sum[1/(2 π) ((1/(1 + 2 i) + 1/(1 + 2 i - 2 m) + 1/(
       1 + 2 i - 2 n) + 1/(1 + 2 i - 2 m - 2 n)) KroneckerDelta[
      1 + 2 i - k - m - n]), {i, -∞, ∞}]

In LaTeX

$$\text{kccs}(\text{n$\_$},\text{m$\_$},\text{k$\_$})\text{:=}\sum _{i=-\infty }^{\infty } \frac{\left(\frac{1}{2 i-2 m-2 n+1}+\frac{1}{2 i-2 m+1}+\frac{1}{2 i-2 n+1}+\frac{1}{2 i+1}\right) \delta _{2 i-k-m-n+1}}{2 \pi }$$

General case

Step 1: calculate the integral f (no assumptions)

Step 2: KroneckerDelta representation

From the exponential representation of the trig functions we find that in theier product all combinations of the sum of all integers corresponsing to the trig factors appear with all possible signs of the summands.

Form the first few (eg. -3..+3) terms of the KroneckerDelta sum using all these sums + an integer parameter q similar to the examples given above.

Step 3: From the result guess the general form of the summands for all q. Look for the parity of the q's and simplify the sum and extend the elementary idex from - infty to + infty.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks. I've been thinking along somewhat the same lines, as you can see. $\endgroup$ – Leon Avery Sep 17 '16 at 15:42
2
$\begingroup$

Here's some progress, if not a totally satisfactory answer. As suggested, one begins by doing the integral without assuming integer $k,m, n$

rint=Simplify[
    Integrate[Sin[k π x] Cos[n π x] Sin[m π x],{x,0,1}]
];
{
    rint,
    Denominator[Together@rint],
    Numerator[Together@rint]
}//Column
(*
(Sin[(k-m-n) π]/(k-m-n)-Sin[(k+m-n) π]/(k+m-n)+Sin[(k-m+n) π]/(k-m+n)-Sin[(k+m+n) π]/(k+m+n))/(4 π)

4 (k-m-n) (k+m-n) (k-m+n) (k+m+n) π

k^3 Sin[(k-m-n) π]+k^2 m Sin[(k-m-n) π]-k m^2 Sin[(k-m-n) π]-m^3
Sin[(k-m-n) π]+k^2 n Sin[(k-m-n) π]+2 k m n Sin[(k-m-n) π]+m^2 n
Sin[(k-m-n) π]-k n^2 Sin[(k-m-n) π]+m n^2 Sin[(k-m-n) π]-n^3
Sin[(k-m-n) π]-k^3 Sin[(k+m-n) π]+k^2 m Sin[(k+m-n) π]+k m^2
Sin[(k+m-n) π]-m^3 Sin[(k+m-n) π]-k^2 n Sin[(k+m-n) π]+2 k m n
Sin[(k+m-n) π]-m^2 n Sin[(k+m-n) π]+k n^2 Sin[(k+m-n) π]+m n^2
Sin[(k+m-n) π]+n^3 Sin[(k+m-n) π]+k^3 Sin[(k-m+n) π]+k^2 m
Sin[(k-m+n) π]-k m^2 Sin[(k-m+n) π]-m^3 Sin[(k-m+n) π]-k^2 n
Sin[(k-m+n) π]-2 k m n Sin[(k-m+n) π]-m^2 n Sin[(k-m+n) π]-k n^2
Sin[(k-m+n) π]+m n^2 Sin[(k-m+n) π]+n^3 Sin[(k-m+n) π]-k^3
Sin[(k+m+n) π]+k^2 m Sin[(k+m+n) π]+k m^2 Sin[(k+m+n) π]-m^3
Sin[(k+m+n) π]+k^2 n Sin[(k+m+n) π]-2 k m n Sin[(k+m+n) π]+m^2 n
Sin[(k+m+n) π]+k n^2 Sin[(k+m+n) π]+m n^2 Sin[(k+m+n) π]-n^3
Sin[(k+m+n) π]
*)

The result should be a ratio of two expressions (although Together may be necessary to put it in that form), a numerator that is generically 0 for integer $k, m, n$, and a denominator that becomes 0 for specific values. These values are obviously the ones where the integral may be nonzero. First, I verify that the numerator is 0 for integer values,

Assuming[
    k ∈ Integers&&m ∈ Integers&&n ∈ Integers,
    Simplify[Numerator[Together[rint]]]
]
(*  0 *)

then I find the conditions under which the denominator is zero.

Reduce[0==Denominator[Together[rint]]]
(* k==-m-n||k==m-n||k==-m+n||k==m+n *)

Now I want to evaluate the integral for every such case. There is a complication here, because the above expression is a disjunction, and it is possible for more than one of the expressions to be true simultaneously (e.g., if $n=0$). So I form a list of cases by ANDing every subset:

cases=And@@@Subsets[List@@BooleanConvert[Reduce[0==Denominator[Together@rint]]]]
(*
 {True,k==-m-n,k==m-n,k==-m+n,k==m+n,k==-m-n&&k==m-n,k==-m-n&&k==-m+n,k==-m-n&&k==m+n,k==m-n&&k==-m+n,k==m-n&&k==m+n,k==-m+n&&k==m+n,k==-m-n&&k==m-n&&k==-m+n,k==-m-n&&k==m-n&&k==m+n,k==-m-n&&k==-m+n&&k==m+n,k==m-n&&k==-m+n&&k==m+n,k==-m-n&&k==m-n&&k==-m+n&&k==m+n}
 *)

then get rid of redundant ones.

cases=Union[Reduce/@cases]
(*
{True,m==0&&k==-n,m==0&&k==n,m==-n&&k==0,m==n&&k==0,n==0&&k==-m,n==0&&k==m,n==0&&m==0&&k==0,k==-m-n,k==m-n,k==-m+n,k==m+n}
*)

Now I can evaluate the integral for each case (and throw out those for which the result is 0).

results=Block[{res,results},
    results=Table[
        res=Simplify[
          Integrate[Sin[k π x] Cos[n π x] Sin[m π x],{x,0,1},
              Assumptions->case],
          k ∈ Integers&&m ∈ Integers&&n ∈ Integers&&case
         ];
        case->res,
        {case,cases}
        ];
    Select[results,#[[2]]!=0&]
    ];
results//Column
(*
n==0&&k==-m -> -(1/2)
n==0&&k==m -> 1/2
k==-m-n -> -(1/4)
k==m-n -> 1/4
k==-m+n -> -(1/4)
k==m+n -> 1/4
*)

The result in this case can be written $\frac{1}{4}\left(-\delta_{k,-m-n}+\delta_{k,m-n}-\delta_{k,-m+n}+\delta_{k,m+n}\right)$.

This is still a long way from a fully automatic solution, as I relied on inspection at several points to confirm my understanding of the solution.

| improve this answer | |
$\endgroup$
0
$\begingroup$

Here is a fully automated solution, using ideas from my previous answer and Dr. Hintze's. It is in the form of two functions: trigIntegralToPiecewise and piecewiseToDelta. trigIntegralToPiecewise takes the thing to be integrated (as a pure function of the integration variable) and a list of the integer variables. It returns a Function that evaluates to a Piecewise function of the variables. If trigIntegralToPiecewise returns successfully, this piecewise function should always be equivalent to the integral.

The function can then be used as input to piecewiseToDelta. This is a bit of a kludge but seems to work fairly well. It is called using something like:

{df, check1, check2} = piecewiseToDelta[pwf, {k, m, n}];

df1 is now a pure function of k, m, n that evaluates to a sum of Kronecker deltas. (This is useful not only because the delta form is typically more compact and easier to understand than the Piecewise form, but also because MMA is good at simplifying sums over Kronecker deltas.) It is not guaranteed to be equivalent to pwf. The check1 and check2 returns allow you to reassure yourself. check1 is an Inactive expression using Reduce that, activated, should evaluate to True if the two functions are equivalent. I have not always had good results with Reduce, so check2 is something else. check2[10] evaluates pwf[k,m,n]==df[k,m,n] for $k$, $m$, and $n$ from -10 to 10 and returns True if all equalities hold.

trigIntegralToPiecewise::nonzero = 
  "works only for generically zero integrals";

trigIntegralToPiecewise[
  func_,
  vars_
  ] := Module[{ri, num, den, dvs, cases, res, pw, pwfunc},
  ri = Integrate[func[x], {x, 0, 1}];
  num = Numerator[Together[ri]];
  den = Denominator[Together[ri]];
  If[0 =!= Simplify[num, Assumptions -> vars \[Element] Integers],
   Message[trigIntegralToPiecewise::nonzero];
   Return[$Failed]
   ];
  cases = BooleanConvert[Reduce[0 == den, vars]];
  cases = If[Head[cases] === Or,
List @@ cases,
{cases}
];
  cases = Reverse[Subsets[cases]];
  cases = And @@@ cases;
  cases = DeleteDuplicates[Reduce[#, vars] & /@ cases];
  pw = Table[
res = Simplify[
  Integrate[func[x], {x, 0, 1},
   Assumptions -> case],
  vars \[Element] Integers && case
  ];
{res, case},
{case, cases}
];
  pw = Append[pw, {0, True}]; (* To be safe *)
  pwfunc = Function @@ {
 vars,
 Piecewise[
  pw
  ]
 };
  pwfunc
  ]

piecewiseToDelta[
  pwf_,
  vars_,
  max_: 3,
  extrabasis_: {}
  ] := Module[{nv, basis, kds, iterators, lb, ub, cs, eqs, cvals, 
   dfunc, check1, check2},
  nv = Length[vars];
  iterators = 
   Transpose[{vars, ConstantArray[lb, nv], ConstantArray[ub, nv]}];
  basis = Join[{1}, vars, extrabasis];
  kds = Plus @@@ Tuples[Outer[Times, Drop[vars, 1], {1, -1}]];
  kds = Sum[
(Sum[C[i, j] basis[[j]], {j, Length[basis]}]) KroneckerDelta[
  vars[[1]], kds[[i]]],
{i, Length[kds]}
];
  cs = Flatten@Table[
 C[i, j],
 {j, Length[basis]}, {i, Length[kds]}
 ];
  eqs = Table @@ 
Prepend[iterators /. {lb -> -max, ub -> max}, 
 kds == pwf @@ vars];
  eqs = DeleteCases[Flatten[eqs], True];
  cvals = Solve[eqs, cs][[1]];
  dfunc = Function @@ {
 vars,
 kds /. cvals /. C[_, _] -> 0
 };
  check1 = Inactivate[
Assuming[
 vars \[Element] Integers,
 Simplify[
  Reduce[pwf @@ vars == dfunc @@ vars]
  ]
 ]
];
  check2[cmax_, it_: iterators] := Module[{iters},
iters = it /. {lb -> -cmax, ub -> cmax};
And @@ Flatten[
  Table @@ Prepend[
    iters,
    pwf @@ vars == dfunc @@ vars
    ]
  ]
];
  {dfunc, check1, check2}
  ]

Example:

pwf1 = trigIntegralToPiecewise[x ↦ Sin[k 𝝿 x] Cos[n 𝝿 x] Sin[m 𝝿 x], {k, m, n}];

{df1, check11, check21} = piecewiseToDelta[pwf1, {k, m, n}];
df1 // TraditionalForm

$$\{k,m,n\}↦-\frac{1}{4} \delta _{k,n-m}-\frac{1}{4} \delta _{k,-m-n}+\frac{\delta _{k,m-n}}{4}+\frac{\delta _{k,m+n}}{4}$$

check11[20]
(* True *)

Activate@check21
(* True *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ What is the result of (Cos Sin) and (Cos Cos Sin) with your method? $\endgroup$ – Dr. Wolfgang Hintze Sep 19 '16 at 15:49
  • $\begingroup$ "trigIntegralToPiecewise::nonzero: works only for generically zero integrals." Note: there were a couple bugs in the code which prevented it from giving that (intended) result. I just fixed them. $\endgroup$ – Leon Avery Sep 19 '16 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.