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?
I am not sure of your level of Mathematica experience, or the context of your need. Some basic facts about Mathematica may be helpful.
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).
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.
i
meant to be an imaginary unit? $\endgroup$I
and to use==
not=
first of all. $\endgroup$I
andi
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 bracketsHoldForm[+##] & @@ (ComplexExpand[{1, I} Through[{Re, Im}[expr]]])
$\endgroup$