9
$\begingroup$

I would like to convert a list of $n$ complex equations to a list of $2n$ real ones. At the moment I am doing it like this:

eqs = {a + I b == 0, c + I d == 0}
Flatten[{ComplexExpand[Re[First[#]]] == 0 & /@ eqs,  
         ComplexExpand[Im[First[#]]] == 0 & /@ eqs}] 

I would like to know how I can write this more compactly, since I'm basically using the same command twice, with the only difference being changing the function Re -> Im. Perhaps I can use a pure function to map over a list of these 2 functions?

Thanks!

$\endgroup$
  • 1
    $\begingroup$ Equal[#, 0] & /@ {Re@#, Im@#} & /@ First /@ eqs $\endgroup$ – Dr. belisarius Jan 8 '13 at 18:52
  • 1
    $\begingroup$ @belisarius you forgot ComplexExpand. $\endgroup$ – Mr.Wizard Jan 8 '13 at 22:33
  • 2
    $\begingroup$ @Mr.Wizard One more item for the list I'm sowing thru my life $\endgroup$ – Dr. belisarius Jan 8 '13 at 22:35
2
$\begingroup$

I would probably do something like this:

eqs = {a + I b == 0, c + I d == 0};

Join @@ Thread /@ ComplexExpand @ Map[{Re@#, Im@#} &, eqs, {2}]
{a == 0, b == 0, c == 0, d == 0}

Or shorter:

Join @@ ComplexExpand[Map[#, eqs, {2}] & /@ {Re, Im}]
{a == 0, c == 0, b == 0, d == 0}

The order is slightly different, if it matters.

$\endgroup$
4
$\begingroup$

This seems like a good place for Through, since it can take multiple functions (like Im and Re) and apply them to the same arguments, so for example,

Through[{Re, Im}[a + b I]]

{-Im[b] + Re[a], Im[a] + Re[b]}

For the specific problem at hand, this can be done

eqs = {a + I b == 0, c + I d == 0};
Join@@Thread/@(ComplexExpand@Through[{Re, Im}[First[#]]] == {0, 0} & /@ eqs)

{a == 0, b == 0, c == 0, d == 0}
$\endgroup$
1
$\begingroup$

Using Outer:

eqs = {a + I b == 0, c + I d == 0};
Flatten@Outer[
  ComplexExpand[#1[#2]] == 0 &,
  {Re, Im}, {First /@ eqs}]

Might have to specify the level in the general case.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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