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


The timing for these computations, however, is radically different.

That is

{0.203348, Null}
{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.

  • $\begingroup$ This isn't an answer to your question, but try to reverse the order of Re and ComplexExpand, i.e., do ComplexExpand[Re[something]] to get rid of Re and Im in your expressions. $\endgroup$
    – Leo Fang
    Oct 9, 2013 at 0:08
  • $\begingroup$ @LeoFang Thanks for the tip. I find that this commutation of the operations actually takes slightly more time than the original. $\endgroup$
    – Nick P
    Oct 9, 2013 at 20:28
  • $\begingroup$ To Nick, as I said this isn't an answer to your question. Without knowing what your ar[n] and ai[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]] and ComplexExpand[Re[x^2 - 2 x + 1]] which might be crucial in your work. $\endgroup$
    – Leo Fang
    Oct 10, 2013 at 1:00
  • $\begingroup$ @LeoFang Thanks again for the tip. I've edited my question a bit to add more of a description of my system. $\endgroup$
    – Nick P
    Oct 10, 2013 at 4:49


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.