Re: Complex -> List does not work
- To: mathgroup@smc.vnet.net
- Subject: [mg10635] Re: [mg10495] Complex -> List does not work
- From: David Withoff <withoff@wolfram.com>
- Date: Tue, 27 Jan 1998 03:10:00 -0500
>I naively tried the following way of turning a complex number into a
>couple of real numbers:
>
> 2 + 3 I /. Complex -> List
>
>but it gives me back
>
> 2 + 3 I
>
>even though the full form of 2+3I is Complex[2,3]. The reverse however
>is possible:
>
> {2,3}/.List->Complex gives 2+3I
>
>The help browser says that "you have to use Re and Im to extract parts
>of Complex numbers".
>
>Why this exception to the basic principles of replacement rules?
>
> Gianluca Gorni
One of the basic principles of replacement rules is that replacements
are not made inside of atomic expressions. For example, 123 /. 2 -> 5
will not replace the digit 2 in 123. Since complex numbers are atoms,
replacement rules won't normally replace parts of complex numbers
either.
In[1]:= AtomQ[2 + 3 I]
Out[1]= True
The exception here is not in the replacement rules, but in the way that
complex numbers are entered and displayed. When you enter 2 + 3 I, or
Complex[2, 3], the input is a normal expression, not an atom. The
corresponding atomic complex number is constructed only when these
inputs are evaluated. Similarly, the display of complex numbers
suggests that they are not atoms, and have parts that can be replaced.
As you point out, the documented way to pick out the real and imaginary
parts of a complexc number is to use Re and Im.
In[2]:= 2 + 3 I /. p_Complex :> {Re[p], Im[p]}
Out[2]= {2, 3}
Dave Withoff
Wolfram Research