Skip to main content

calculus and analysis - Solution to a specific problem caused by generic simplification


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.



Answer



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 *)

Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

list manipulation - Selecting multiple columns from a matrix?

Sample data: data = { {{2013, 1, 1}, 24.13, 167.67, 231.82}, {{2013, 1, 2}, 32.15, 170.92, 225.99}, {{2013, 1, 3}, 35.43, 172.68, 221.67}, {{2013, 1, 4}, 36.73, 173.05, 218.32}, {{2013, 1, 5}, 58.19, 165.96, 197.05}, {{2013, 1, 6}, 69.99, 163.50, 187.52}, {{2013, 1, 7}, 71.37, 154.21, 175.58}, {{2013, 1, 8}, 72.51, 149.66, 163.25}}; I want a DateListPlot with three graphs, so for a matrix formed by columns 1 and 2, one for columns 1 and 3, and 1 for columns 1 and 4. At the moment I'm using this code: data2 = Transpose[{data[[All, 1]], data[[All, 2]]}]; data3 = Transpose[{data[[All, 1]], data[[All, 3]]}]; data4 = Transpose[{data[[All, 1]], data[[All, 4]]}]; DateListPlot[{data2, data3, data4}, Joined -> True, Filling -> {3 -> {1}}] but I have a hunch that this can be done more efficiently. I don't like the Transpose s in particular. Any ideas? edit (for extra credit) What if I need to multiply the second column by 2, which in my solution is simp...

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...