I have the following problem: I want to compute an integral of the form $$\int\limits_0^{t_g}\mathrm{d}t_2\int\limits_0^{t_2}\mathrm{d}t_1\left[A(t_1),A(t_2)\right],$$where $\left[B,C\right]=BC-CB$ is the commutator of two matrices (in my case both 3x3). All entries of the resulting commutator have a similiar form, so let us just focus on the first diagonal entry, which reads $$\left(\Omega_1(t_2)+e^{i\gamma t_2}\Omega_2(t_2)\right)\left(\Omega_1^*(t_1)+e^{-i\gamma t_1}\Omega_2^*(t_1)\right)-\text{h.c.}\quad (1)$$where h.c. means the hermitian conjugate. The functions $\Omega_i(t_j)$ are complex functions that are basically composed by sums and products of Sine/Cosine with different frequencies (please find the explicit definitions in the code block below).
Obviously Eq. (1) is just $2i \;{\rm Im[\ldots]}$ where $\ldots$ denotes the term in front of "h.c.". So I wanted to use this observation as a starting point for following symbolic computation. Nevertheless, extracting the imaginary part by using ComplexExpand[Im[...]]
yields a term consisting of approximately 5100 summands that could "easily" be simplified by factoring out some stuff. Using Simplify
for that job seems to take forever on my machine.
Even worse is the nested integration which I would like to compute symbolically in order to see if some conditions can be obtained from the result (results should be stored as a function to be used for further calculation).
So my questions are:
- Is there a way to speed up simplification of the
ComplexExpand
part? - Is there a more elegant/faster way of performing the nested integration, apart from
Integrate[Integrate[...],{t1,0,t2}],{t2,0,tg}]
And here is the code defining my functions
ex1Temp[t_, tg_] := (1 - Cos[(2*π*t)/tg])*(1 -A1*Cos[ωx1*(t - tg/2)]);
ex1[t_, tg_] :=ex1Temp[t, tg] + b21*D[ex1Temp[x, tg], {x, 2}] /. {x -> t};
ey1[t_, tg_] := (b11*D[ex1Temp[x, tg], {x, 1}] + b31*D[ex1Temp[x, tg], {x, 3}]) /. {x -> t};
O1[t_, tg_] := ex1[t, tg] + I*ey1[t, tg];
O1c[t_, tg_] := ex1[t, tg] - I*ey1[t, tg];
ex2Temp[t_,tg_] := (1 - Cos[(2*π*t)/tg])*(1 - A2*Cos[ωx2*(t - tg/2)]);
ex2[t_, tg_] := ex2Temp[t, tg] + b22*D[ex2Temp[x, tg], {x, 2}] /. {x -> t};
ey2[t_, tg_] := (b12*D[ex2Temp[x, tg], {x, 1}] + b32*D[ex2Temp[x, tg], {x, 3}]) /. {x -> t};
O2[t_, tg_] := ex2[t, tg] + I*ey2[t, tg];
O2c[t_, tg_] := ex2[t, tg] - I*ey2[t, tg];
comm00[t1_, t2_, tg_] = ComplexExpand[Im[(O1[t2, tg] + Exp[I*γ*t2]*O2[t2, tg])*(O1c[t1, tg] + Exp[-I*γ*t1]*O2c[t1, tg])]];
fun[tg_]=Integrate[comm00[t1,t2,tg],{t1,0,t2}],{t2,0,tg}]
Notice that $\Omega$ from my latex equations at the top are represented by O
in the Mathematica code. Any help and/or tipps are well appreciated.
Edit 1 (based on Daniel Lichtblau's recommendations)
A good point was to apply rules to the different summands of the appearing integrals. I am working on this but need some help with generalization of the results. So for this Edit, please let us just focus on the following code snippet:
intRule = Integrate[a_ + b_, c_] :> Integrate[a, c] + Integrate[b, c];
trafo = Integrate[Cos[w_*t_]*ex1[t_, tg_], {t_, 0,t2_}] :> (1 - w^2*b21)*Integrate[Cos[w*t]*ex1Temp[t, tg], {t,0, t2}];
intRule
is used to transform the integral over a sum to a sum of integrals. One of these summands is $\cos(\omega t)\epsilon_{x1}(t,t_g)$ which appears in my rule trafo
. I am now facing a problem/uncertainty regarding efficiently applying the transformation rule(s). Inside trafo
there is another integral which again be easily computed analytically, say we name the result F1[t2]
. What is now a better way to "replace" that integral in rule trafo
?
- Define the function
F1[t2]
withSet
(notSetDelayed
) and changetrafo
totrafo=Integrate[Cos[w_*t_]*ex1[t_, tg_], {t_, 0,t2_}] :> (1 - w^2*b21)*F1[t2]
, or - Apply another rule in a second step that acts in a similar way as $#1$, but maybe performs better
Edit 2 (based on Michael E2's answer)
As there is a small issue in MichaelE2's answer (as of 8:49AM (MEZ), 10/12/14) with terms that go like t2*Sin[t2]
the rules need to be changed to something like
trigInt[v_, a_, b_] := {
integrand_ /;FreeQ[integrand, v] :> (b - a) integrand,(*constant rule*)
integrand_Plus :> Replace[integrand, trigInt[v, a, b], 1],(*addition rule*)
integrand_ /; trigCheck[integrand] :>(*sin/cos rules*)
Expand[Expand[integrand] /. {
Sin[arg_] :> 1/Coefficient[arg, v]*First@Differences[-Cos[arg] /. {{v -> a}, {v -> b}}],
Cos[arg_] :> 1/Coefficient[arg, v]*First@Differences[Sin[arg] /. {{v -> a}, {v -> b}}],
v*Sin[arg_] :> 1/Coefficient[arg, v]*(First@Differences[-v*Cos[arg] +1/Coefficient[arg, v]*Sin[arg] /. {{v -> a}, {v -> b}}]),
v*Cos[arg_] :> 1/Coefficient[arg, v]*(First@Differences[v*Sin[arg] +1/Coefficient[arg, v]*Cos[arg] /. {{v -> a}, {v -> b}}])}],
integrand_ :> Inactive[Integrate][integrand, {v, a, b}]};(*failure rule*)
To generalize this one can think of using Gamma
function to build up rules for $\int\mathrm{d}x\,x^n\sin(x)$.