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

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.