For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > December 2006 > Complex Numbers to list of points









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Complex Numbers to list of points
planetmarshalluk@hotmail.com

2006-12-13, 4:31 pm

Hi there,

I have a list of mixed complex and real numbers returned from a function, e=
g

{1+3i, 2, 1-3i}

I wish to convert the list to a list of points.

{1+3i,2,1-3i} /. z_Complex->{Re[z],Im[z]}

gives

{{1,3},2,{1,-3}}

Which is almost what I want, but has not dealt with the possibility of non-=
complex numbers in the list. Is there a compact way of doing this, such wit=
h a list of rules? Nothing obvious seems to work, the only success I have h=
ad is splitting the list into real and complex sublists, applying rules to =
each list individually and then recombining them, which seems like an awful=
lot of hard work.

What I want is
toPoints[{1+3i, 2, 1-3i}] == {{1,3},{2,0},{1,-3}}

Any help appreciated,
Thanks.

Andrew.

Mark Westwood

2006-12-14, 9:09 am

Andrew

Try the following:

{1+3i, 2, 1-3i} /. (z_ /; NumericQ[z]) -> {Re[z], Im[z]}

which gives me

{{1, 3}, {2, 0}, {1, -3}}

Note the () around the pattern with condition.

HTH

Mark Westwood

On 13 Dec, 12:40, planetmarshal...@hotmail.com wrote:
> Hi there,
>
> I have a list of mixed complex and real numbers returned from a function, e=
> g
>
> {1+3i, 2, 1-3i}
>
> I wish to convert the list to a list of points.
>
> {1+3i,2,1-3i} /. z_Complex->{Re[z],Im[z]}
>
> gives
>
> {{1,3},2,{1,-3}}
>
> Which is almost what I want, but has not dealt with the possibility of non-=
> complex numbers in the list. Is there a compact way of doing this, such wit=
> h a list of rules? Nothing obvious seems to work, the only success I have h=
> ad is splitting the list into real and complex sublists, applying rules to =
> each list individually and then recombining them, which seems like an awful=
> lot of hard work.
>
> What I want is
> toPoints[{1+3i, 2, 1-3i}] == {{1,3},{2,0},{1,-3}}
>
> Any help appreciated,
> Thanks.
>
> Andrew.


dh

2006-12-14, 9:09 am



Hi Andrew,

you must specify the level where your replacement should act:

Replace[{1 + 3I, 2, 1 - 3I}, x_ -> {Re[x], Im[x]}, 1]

without thi, x can matches the whole expression.

Daniel



planetmarshalluk@hotmail.com wrote:

> Hi there,


>


> I have a list of mixed complex and real numbers returned from a function, e=


> g


>


> {1+3i, 2, 1-3i}


>


> I wish to convert the list to a list of points.


>


> {1+3i,2,1-3i} /. z_Complex->{Re[z],Im[z]}


>


> gives


>


> {{1,3},2,{1,-3}}


>


> Which is almost what I want, but has not dealt with the possibility of non-=


> complex numbers in the list. Is there a compact way of doing this, such wit=


> h a list of rules? Nothing obvious seems to work, the only success I have h=


> ad is splitting the list into real and complex sublists, applying rules to =


> each list individually and then recombining them, which seems like an awful=


> lot of hard work.


>


> What I want is


> toPoints[{1+3i, 2, 1-3i}] == {{1,3},{2,0},{1,-3}}


>


> Any help appreciated,


> Thanks.


>


> Andrew.


>



Jean-Marc Gulliet

2006-12-14, 9:09 am

planetmarshalluk@hotmail.com wrote:
> Hi there,
>
> I have a list of mixed complex and real numbers returned from a function, e=
> g
>
> {1+3i, 2, 1-3i}
>
> I wish to convert the list to a list of points.
>
> {1+3i,2,1-3i} /. z_Complex->{Re[z],Im[z]}
>
> gives
>
> {{1,3},2,{1,-3}}
>
> Which is almost what I want, but has not dealt with the possibility of non-=
> complex numbers in the list. Is there a compact way of doing this, such wit=
> h a list of rules? Nothing obvious seems to work, the only success I have h=
> ad is splitting the list into real and complex sublists, applying rules to =
> each list individually and then recombining them, which seems like an awful=
> lot of hard work.
>
> What I want is
> toPoints[{1+3i, 2, 1-3i}] == {{1,3},{2,0},{1,-3}}
>
> Any help appreciated,
> Thanks.
>
> Andrew.
>


The following expression should do the trick:

Rationalize[{1 + 3*I, 2, 1 - 3*I} /. x_Integer -> Complex[x, 0.0] /.
z_Complex -> {Re[z], Im[z]}]

--> {{1, 3}, {2, 0}, {1, -3}}

Regards,
Jean-Marc

dimitris

2006-12-15, 8:14 am

({Re[#1], Im[#1]} & ) /@ {1 + 3*I, 2, 1 - 3*I}
{{1, 3}, {2, 0}, {1, -3}}

Dimitris



Ï/Ç planetmarshalluk@hotmail.com Ýãñáøå:
> Hi there,
>
> I have a list of mixed complex and real numbers returned from a function, e=
> g
>
> {1+3i, 2, 1-3i}
>
> I wish to convert the list to a list of points.
>
> {1+3i,2,1-3i} /. z_Complex->{Re[z],Im[z]}
>
> gives
>
> {{1,3},2,{1,-3}}
>
> Which is almost what I want, but has not dealt with the possibility of non-=
> complex numbers in the list. Is there a compact way of doing this, such wit=
> h a list of rules? Nothing obvious seems to work, the only success I have h=
> ad is splitting the list into real and complex sublists, applying rules to =
> each list individually and then recombining them, which seems like an awful=
> lot of hard work.
>
> What I want is
> toPoints[{1+3i, 2, 1-3i}] == {{1,3},{2,0},{1,-3}}
>
> Any help appreciated,
> Thanks.
>
> Andrew.


Dana DeLouis

2006-12-17, 8:12 am

Hi. Same idea as others, but with the use of '?.

data = {1 + 3I, 2, 1 - 3I};

data /. z_?NumericQ -> {Re[z], Im[z]}

{{1, 3}, {2, 0}, {1, -3}}

--
HTH :> )
Dana DeLouis
Mathematica 5.2 Windows Xp.


<planetmarshalluk@hotmail.com> wrote in message
news:elosbs$ojk$1@smc.vnet.net...
> Hi there,
>
> I have a list of mixed complex and real numbers returned from a function,

e=
> g
>
> {1+3i, 2, 1-3i}
>
> I wish to convert the list to a list of points.
>
> {1+3i,2,1-3i} /. z_Complex->{Re[z],Im[z]}
>
> gives
>
> {{1,3},2,{1,-3}}
>
> Which is almost what I want, but has not dealt with the possibility of

non-=
> complex numbers in the list. Is there a compact way of doing this, such

wit=
> h a list of rules? Nothing obvious seems to work, the only success I have

h=
> ad is splitting the list into real and complex sublists, applying rules to

=
> each list individually and then recombining them, which seems like an

awful=
> lot of hard work.
>
> What I want is
> toPoints[{1+3i, 2, 1-3i}] == {{1,3},{2,0},{1,-3}}
>
> Any help appreciated,
> Thanks.
>
> Andrew.
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com