Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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. – Leo Fang Oct 9 '13 at 0:08
@LeoFang Thanks for the tip. I find that this commutation of the operations actually takes slightly more time than the original. – Nick P Oct 9 '13 at 20:28
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. – Leo Fang Oct 10 '13 at 1:00
@LeoFang Thanks again for the tip. I've edited my question a bit to add more of a description of my system. – Nick P Oct 10 '13 at 4:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.