Skip to main content

calculus and analysis - How can I Integrate large trigonometric expressions more efficiently?


I have a requirement to integrate some large symbolic expressions containing products of trigonometric functions. A single integration is averaging 7 minutes, and I have 2628 expressions to integrate, so it will take 13 days to complete unless I can speed it up.


Here is an example:


expr = c14 (a0+a1 Cos[n x]+a3 Cos[2 n x]+a5 Cos[3 n x]+a7 Cos[4 n x]+a2 Sin[n x]+a4 Sin[2 n x]+a6 Sin[3 n x]+a8 Sin[4 n x]) (e1 Cos[n x]+e3 Cos[2 n x]+e5 Cos[3 n x]+e7 Cos[4 n x]+e2 Sin[n x]+e4 Sin[2 n x]+e6 Sin[3 n x]+e8 Sin[4 n x])+c44 (e1 Cos[n x]+e3 Cos[2 n x]+e5 Cos[3 n x]+e7 Cos[4 n x]+e2 Sin[n x]+e4 Sin[2 n x]+e6 Sin[3 n x]+e8 Sin[4 n x])^2+c66 (j0+j1 Cos[n x]+j3 Cos[2 n x]+j5 Cos[3 n x]+j7 Cos[4 n x]+j2 Sin[n x]+j4 Sin[2 n x]+j6 Sin[3 n x]+j8 Sin[4 n x])^2+c11 (1/2 (h0+h1 Cos[n x]+h3 Cos[2 n x]+h2 Sin[n x]+h4 Sin[2 n x])^2+3/2 (a0+a1 Cos[n x]+a3 Cos[2 n x]+a5 Cos[3 n x]+a7 Cos[4 n x]+a2 Sin[n x]+a4 Sin[2 n x]+a6 Sin[3 n x]+a8 Sin[4 n x])^2+1/2 (d0+d1 Cos[n x]+d3 Cos[2 n x]+d5 Cos[3 n x]+d7 Cos[4 n x]+d2 Sin[n x]+d4 Sin[2 n x]+d6 Sin[3 n x]+d8 Sin[4 n x])^2)+c14 ((b0+b1 Cos[n x]+b3 Cos[2 n x]+b5 Cos[3 n x]+b7 Cos[4 n x]+b2 Sin[n x]+b4 Sin[2 n x]+b6 Sin[3 n x]+b8 Sin[4 n x]) (d0+d1 Cos[n x]+d3 Cos[2 n x]+d5 Cos[3 n x]+d7 Cos[4 n x]+d2 Sin[n x]+d4 Sin[2 n x]+d6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]

n is an integer, if that helps.


How can I compute these integrals more efficiently?


Take case with this example too:


a12 = Cos[n x] (c14 (a0+a1 Cos[n x]+a3 Cos[2 n x]+a5 Cos[3 n x]+a7 Cos[4 n x]+a2 Sin[n x]+a4 Sin[2 n x]+a6 Sin[3 n x]+a8 Sin[4 n x]) (e1 Cos[n x]+e3 Cos[2 n x]+e5 Cos[3 n x]+e7 Cos[4 n x]+e2 Sin[n x]+e4 Sin[2 n x]+e6 Sin[3 n x]+e8 Sin[4 n x])+c44 (e1 Cos[n x]+e3 Cos[2 n x]+e5 Cos[3 n x]+e7 Cos[4 n x]+e2 Sin[n x]+e4 Sin[2 n x]+e6 Sin[3 n x]+e8 Sin[4 n x])^2+c66 (j0+j1 Cos[n x]+j3 Cos[2 n x]+j5 Cos[3 n x]+j7 Cos[4 n x]+j2 Sin[n x]+j4 Sin[2 n x]+j6 Sin[3 n x]+j8 Sin[4 n x])^2+c11 (1/2 (h0+h1 Cos[n x]+h3 Cos[2 n x]+h2 Sin[n x]+h4 Sin[2 n x])^2+3/2 (a0+a1 Cos[n x]+a3 Cos[2 n x]+a5 Cos[3 n x]+a7 Cos[4 n x]+a2 Sin[n x]+a4 Sin[2 n x]+a6 Sin[3 n x]+a8 Sin[4 n x])^2+1/2 (d0+d1 Cos[n x]+d3 Cos[2 n x]+d5 Cos[3 n x]+d7 Cos[4 n x]+d2 Sin[n x]+d4 Sin[2 n x]+d6 Sin[3 n x]+d8 Sin[4 n x])^2)+c14 ((b0+b1 Cos[n x]+b3 Cos[2 n x]+b5 Cos[3 n x]+b7 Cos[4 n x]+b2 Sin[n x]+b4 Sin[2 n x]+b6 Sin[3 n x]+b8 Sin[4 n x]) (d0+d1 Cos[n x]+d3 Cos[2 n x]+d5 Cos[3 n x]+d7 Cos[4 n x]+d2 Sin[n x]+d4 Sin[2 n x]+d6 Sin[3 n x]+d8 Sin[4 n x])+2 (a0+a1 Cos[n x]+a3 Cos[2 n x]+a5 Cos[3 n x]+a7 Cos[4 n x]+a2 Sin[n x]+a4 Sin[2 n x]+a6 Sin[3 n x]+a8 Sin[4 n x]) (e1 Cos[n x]+e3 Cos[2 n x]+e5 Cos[3 n x]+e7 Cos[4 n x]+e2 Sin[n x]+e4 Sin[2 n x]+e6 Sin[3 n x]+e8 Sin[4 n x])+(h0+h1 Cos[n x]+h3 Cos[2 n x]+h2 Sin[n x]+h4 Sin[2 n x]) (g0+g1 Cos[n x]+g3 Cos[2 n x]+g5 Cos[3 n x]+g7 Cos[4 n x]+g2 Sin[n x]+g4 Sin[2 n x]+g6 Sin[3 n x]+g8 Sin[4 n x]))+c12 (1/2 (b0+b1 Cos[n x]+b3 Cos[2 n x]+b5 Cos[3 n x]+b7 Cos[4 n x]+b2 Sin[n x]+b4 Sin[2 n x]+b6 Sin[3 n x]+b8 Sin[4 n x])^2+1/2 (e1 Cos[n x]+e3 Cos[2 n x]+e5 Cos[3 n x]+e7 Cos[4 n x]+e2 Sin[n x]+e4 Sin[2 n x]+e6 Sin[3 n x]+e8 Sin[4 n x])^2+1/2 (g0+g1 Cos[n x]+g3 Cos[2 n x]+g5 Cos[3 n x]+g7 Cos[4 n x]+g2 Sin[n x]+g4 Sin[2 n x]+g6 Sin[3 n x]+g8 Sin[4 n x])^2)+c13 (1/2 (c0+c1 Cos[n x]+c3 Cos[2 n x]+c2 Sin[n x]+c4 Sin[2 n x])^2+1/2 (f0+f1 Cos[n x]+f3 Cos[2 n x]+f5 Cos[3 n x]+f7 Cos[4 n x]+f2 Sin[n x]+f4 Sin[2 n x]+f6 Sin[3 n x]+f8 Sin[4 n x])^2+1/2 (j0+j1 Cos[n x]+j3 Cos[2 n x]+j5 Cos[3 n x]+j7 Cos[4 n x]+j2 Sin[n x]+j4 Sin[2 n x]+j6 Sin[3 n x]+j8 Sin[4 n x])^2))

Answer



Your integrand(s), when expanded, consist of terms that are constants times zero, one or two factors of sine or cosine. The terms can be integrated symbolically and stored as rules. It will be convenient to find the average value and multiply by 2 Pi at the end. (This will automatically handle constant terms without a special rule.) Thanks to Simon Woods for a much simplified set of rules.


Clear[a, b];

iRules = {(Cos | Sin)[_]^2 -> 1/2, (Cos | Sin)[_] -> 0};

ia11 = 2 Pi Expand[a11] /. iRules; // AbsoluteTiming
(* {0.010274, Null} *)

It's a lot faster:


evalRules = {   (* assumes  n  is an  Integer *)
Cos[(a_Integer: 1) n Pi] :> (-1)^a,
Sin[(b_Integer: 1) n Pi] :> 0
};

ja11 = Integrate[a11, {x, 0, 2 Pi}] /. evalRules; // AbsoluteTiming
(* {177.270803, Null} *)

And it gives the same result, provided the assumptions inherent in evalRules are correct:


ia11 == Expand[ja11] // Simplify
(* True *)



Update: Here's another way, about 20 times slower than iRules, but 1000 times faster than Integrate. It's a similar idea, but it should work on general products of sines and cosines. It assumes that sines and cosines are the only functions in the integrands.


ka11 = 2 Pi Expand[TrigToExp@a11] /. E^_ -> 0; // AbsoluteTiming

(* {0.205734, Null} *)

ka11 == Expand[ja11 /. evalRules] // Simplify
(* True *)

Or, sparked by a comment by the OP, more complicated combinations of sines and cosines can be fairly quickly simplified with TrigReduce.


2 Pi TrigReduce[a11] /. iRules; // AbsoluteTiming
(* {0.086483, Null} *)




Original rules


iRules =(*Dispatch@*)
{Cos[(a_Integer: 1) n x] Sin[(a_Integer: 1) n x] ->
Integrate[Cos[a n x] Sin[a n x], {x, 0, 2 Pi}]/(2 Pi),
Cos[(a_Integer: 1) n x] Sin[(b_Integer: 1) n x] /; a == -b ->
Integrate[Cos[a n x] Sin[-a n x], {x, 0, 2 Pi}]/(2 Pi),
Cos[(a_Integer: 1) n x] Sin[(b_Integer: 1) n x] ->
Integrate[Cos[a n x] Sin[b n x], {x, 0, 2 Pi}]/(2 Pi),
Sin[(a_Integer: 1) n x] Sin[(b_Integer: 1) n x] ->
Integrate[Cos[a n x] Sin[b n x], {x, 0, 2 Pi}]/(2 Pi),

Cos[(a_Integer: 1) n x] Cos[(b_Integer: 1) n x] ->
Integrate[Cos[a n x] Sin[b n x], {x, 0, 2 Pi}]/(2 Pi),
Cos[(a_Integer: 1) n x]^2 ->
Integrate[Cos[a n x]^2, {x, 0, 2 Pi}]/(2 Pi),
Sin[(b_Integer: 1) n x]^2 ->
Integrate[Sin[b n x]^2, {x, 0, 2 Pi}]/(2 Pi),
Cos[(a_Integer: 1) n x] ->
Integrate[Cos[a n x], {x, 0, 2 Pi}]/(2 Pi),
Sin[(b_Integer: 1) n x] ->
Integrate[Sin[b n x], {x, 0, 2 Pi}]/(2 Pi)};


Some remarks on the code: Note that the rule in iRules are Rule (->) not RuleDelayed (:>). This means that the integrals will be performed when iRules are defined instead of delayed until the application of iRules. Therefore the pattern symbols a and b must be cleared of any values. If that is not possible, then use other symbols or put in a Module (Module[{a, b}, iRules = {...}]).


Dispatch did not speed things up for me. That may change if the list of integration rules were to grow much larger.


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...