I am setting up a large system of ODEs and in order to use the IDA method (which is sig. faster for my system and thus attractive), I must split my equations into real and imaginary parts.
I am experiencing a severe bottleneck in taking real and imaginary parts of my functions, using ComplexExpand
, and I suspect that this slowdown has to do with my inability to specify that the range of certain functions are real. A simple example is given below:
Consider two tables, whose elements I would like to decompose into real and imaginary parts:
A1 = Table[ar[n]+I*ai[n], {n, 1, 500}];
A2 = Table[(n+I*n),{n,1,500}];
Now, to find the real part of, e.g., A1.A1
, I would take
Re[ComplexExpand[A1.A1]];
The timing for these computations, however, is radically different.
That is
Timing[Re[ComplexExpand[A1.A1]];]
{0.203348, Null}
Timing[Re[ComplexExpand[A2.A2]];]
{0.000585, Null}
So we see there is a difference of around 400
!
Intuitively, it would seem that if I can tell Mathematica to treat {ar[n],ai[n]}
as being functions with ranges over the reals, I can significantly improve the timing. Right now the {ar[n],ai[n]}
are effectively constants, but in the future they might be functions.
For the purpose of ComplexExpand
, is it possible to have Mathematica treat these objects simply as real constants?
Note: For completeness, I add that the system of equations I am going to be decomposing, in terms of real and imaginary parts, will be a sum of maximum degree 4 polynomials in the variables A[n][t]=Ar[n][t]+I*Ai[n][t]
where n
is some index number on the dependent variables, which will take values between 1
and N
(right now I have N
at 40, but in the future it'll be something around 100
or 200
). For each equation, there are around N+N^2
terms. For my governing equations, with N=40
, this real/imaginary decomposition is taking around 3500
CPU seconds.
Re
andComplexExpand
, i.e., doComplexExpand[Re[something]]
to get rid ofRe
andIm
in your expressions. $\endgroup$ar[n]
andai[n]
exactly are, it's not easy to point you to the way out, so I just gave a general suggestion since there is a difference between, for example,Re[ComplexExpand[x^2 - 2 x + 1]]
andComplexExpand[Re[x^2 - 2 x + 1]]
which might be crucial in your work. $\endgroup$