0
$\begingroup$

I have an expression:

a + i*b + d + i*e + r = 0

Now I want Mathematica to sort this like:

a + d + r + i(b + e)

How can I do that?

$\endgroup$
6
  • $\begingroup$ Is i meant to be an imaginary unit? $\endgroup$
    – Kuba
    Commented Jul 2, 2014 at 8:53
  • 1
    $\begingroup$ So you need I and to use == not = first of all. $\endgroup$
    – Kuba
    Commented Jul 2, 2014 at 8:55
  • $\begingroup$ Thanks, but it should not be a mathematica input, only to demonstrate the problem :D $\endgroup$
    – MichaelS
    Commented Jul 2, 2014 at 8:56
  • 1
    $\begingroup$ Well, it does matter since you want some kind of MMa script, I and i behaves differently. Is this waht you are after?: expr = a + I*b + d - I*e + r; Row[(ComplexExpand[{1, I} Through[{Re, Im}[expr]]]), "+"] or more safe but with additional brackets HoldForm[+##] & @@ (ComplexExpand[{1, I} Through[{Re, Im}[expr]]]) $\endgroup$
    – Kuba
    Commented Jul 2, 2014 at 9:00
  • $\begingroup$ Yep, that's right! I thought already about the command 'ComplexExpand', sometimes it does the right thing. $\endgroup$
    – MichaelS
    Commented Jul 2, 2014 at 9:05

1 Answer 1

3
$\begingroup$

I am not sure of your level of Mathematica experience, or the context of your need. Some basic facts about Mathematica may be helpful.

  1. Mathematica sorts output lexicographically (roughly alphabetic order). So if you have a choice of variable names make all of the complex coefficients later in the alphabet than the real coefficients. e.g.

    ComplexExpand[a + r I + b + s I + d] 
    
     a + b + d + I (r + s).
    
  2. Mathematica assumes that unassigned variables are in general complex numbers. ComplexExpand[] overrides this assumption. You can also do it manually with something like: Simplify[Re[exp], Assumptions -> {Element[{a, b, c, d, e, f}, Reals]}], which will give the real part of the expression, assuming the listed coefficients are Real.

I hope that you find this helpful.

$\endgroup$

Your Answer

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

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