Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose I have a very long expression (>500,000 characters) which contains a mixture of real and imaginary terms. e.g x = g*m*o*p*q*s*v + a*c*f*g*m^2*o*p*q*s*v + c*g*h*m^2*o*p*q*s*v - a*c*g*h*m^2*o*p*q*s*v - B*g*m*o*p*q*s*v*w - g*m*o*p*s*v*x - (1-I*Sqrt[3])*(2*g*o*p*s*v + 2*a*c*f*g*m*o*p*s*v) + 2*c*g*h*m*o*p*s*v + (1+I*Sqrt[3])*(2*g*p*q*v + m*g*f*q*m*o*s*v)... etc. I would like to seperate the real and imaginery terms and work with the real terms only.

I am using ComplexExpand[Re[x],TargetFunctions->(Re)}] but I keep getting the message 'No more memory available. Mathematica has shut down.Try quitting other applications and then retry'.

Is there a better way to discard the imaginary terms and group the real terms together without this problem?

share|improve this question

2 Answers 2

Assuming your expression is indeed a sum of terms as shown in your example you might want to try

expression = 
   g*m*o*p*q*s*v + a*c*f*g*m^2*o*p*q*s*v + c*g*h*m^2*o*p*q*s*v - 
   a*c*g*h*m^2*o*p*q*s*v - B*g*m*o*p*q*s*v*w - 
   g*m*o*p*s*v*
   x - (1 - I*Sqrt[3])*(2*g*o*p*s*v + 2*a*c*f*g*m*o*p*s*v) + 
   2*c*g*h*m*o*p*s*v + (1 + I*Sqrt[3])*(2*g*p*q*v + m*g*f*q*m*o*s*v)

ComplexExpand[Re[#], TargetFunctions -> (Re)] & /@ expression

2 g p q v - 2 g o p s v - 2 a c f g m o p s v + 2 c g h m o p s v +
f g m^2 o q s v + g m o p q s v + a c f g m^2 o p q s v + c g h m^2 o p q s v - a c g h m^2 o p q s v - B g m o p q s v w - g m o p s v x

This works because you can use Map (/@) on any function, not only on List. With a sum of terms you have an expression Plus[...]on the outside. With Map you now use ComplexExpand on the parts instead of on the whole. This may alleviate memory problems.

Whether it works for your long expression without memory problems I didn't test.

share|improve this answer
    
Thanks for response. Unfortunately it still runs out of memory. –  user3401348 Mar 27 at 14:03

If, as it seems in your question, the expression is a polynomial (or even a rational function) and the variables are all real, then this should work:

expr = 
 g*m*o*p*q*s*v + a*c*f*g*m^2*o*p*q*s*v + c*g*h*m^2*o*p*q*s*v - 
  a*c*g*h*m^2*o*p*q*s*v - B*g*m*o*p*q*s*v*w - g*m*o*p*s*v*x -
   (1 - I*Sqrt[3])*(2*g*o*p*s*v + 2*a*c*f*g*m*o*p*s*v) + 
  2*c*g*h*m*o*p*s*v + (1 + I*Sqrt[3])*(2*g*p*q*v + m*g*f*q*m*o*s*v)

expr /. zz_Complex :> Re[zz]

(*
  2 g p q v - 2 g o p s v - 2 a c f g m o p s v + 2 c g h m o p s v + 
    f g m^2 o q s v + g m o p q s v + a c f g m^2 o p q s v + 
    c g h m^2 o p q s v - a c g h m^2 o p q s v - B g m o p q s v w - 
    g m o p s v x
*)

If you have algebraic or transcendental functions in your expression, then it might not work.

share|improve this answer
    
Thanks for your response. I have evaluated the full expression and the one using zz_complex:>Re[zz] by substituting some numerical values for all the parameters, however, the magnitude of the real component is different in both. I think my expression contains some algebraic functions. –  user3401348 Mar 27 at 15:48

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.