MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: sorting complex number?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57505] Re: sorting complex number?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at 9online.fr>
  • Date: Sun, 29 May 2005 21:00:26 -0400 (EDT)
  • Organization: New York University
  • References: <d79f0f$ldr$1@smc.vnet.net>
  • Reply-to: jmg336 at nyu.edu
  • Sender: owner-wri-mathgroup at wolfram.com

Sean2 wrote:
> Hi, I need some help
>   I am trying to sort a list of complext number, say, List={a+b*I, c+d*I};
>  and I want a>c and b>d. I know that Sort can do this with a small list of complex numbers, but what I deal with is a list of about 100000 and maybe more, and it doesn't seem to work with the imaginary part when you just Sort[List],
>  Does anyone have a small program to do this?
>  thank you
> 
Is this what you are looking for?

In[1]:=
data=Table[Random[Complex,{-1-I,1+I}],{10}]

Out[1]//InputForm=
{0.0821271856782817 + 0.2029639541984125*I,
  0.06837510881823627 + 0.847443835410322*I,
  -0.10011961132922509 - 0.7090021038645318*I,
  -0.29863518479415907 - 0.676509694560786*I,
  -0.6876398064114924 + 0.9893164268004881*I,
  0.35689272889096113 - 0.6162016388396594*I,
  -0.47362970566225704 + 0.21374267988832485*I,
  -0.1928811783911134 - 0.22833001190101454*I,
  -0.4514416407833921 - 0.5575670386850278*I,
  -0.8240116837488103 + 0.9290434802764991*I}

In[2]:=
Sort[data,((Re[#1]>Re[#2])&&(Im[#1]>Im[#2]))&]

Out[2]//InputForm=
{-0.8240116837488103 + 0.9290434802764991*I,
  0.06837510881823627 + 0.847443835410322*I,
  0.0821271856782817 + 0.2029639541984125*I,
  -0.1928811783911134 - 0.22833001190101454*I,
  -0.4514416407833921 - 0.5575670386850278*I,
  -0.47362970566225704 + 0.21374267988832485*I,
  0.35689272889096113 - 0.6162016388396594*I,
  -0.6876398064114924 + 0.9893164268004881*I,
  -0.29863518479415907 - 0.676509694560786*I,
  -0.10011961132922509 - 0.7090021038645318*I}

In[3]:=
Sort[data,(Re[#1]>Re[#2])&]

Out[3]//InputForm=
{0.35689272889096113 - 0.6162016388396594*I,
  0.0821271856782817 + 0.2029639541984125*I,
  0.06837510881823627 + 0.847443835410322*I,
  -0.10011961132922509 - 0.7090021038645318*I,
  -0.1928811783911134 - 0.22833001190101454*I,
  -0.29863518479415907 - 0.676509694560786*I,
  -0.4514416407833921 - 0.5575670386850278*I,
  -0.47362970566225704 + 0.21374267988832485*I,
  -0.6876398064114924 + 0.9893164268004881*I,
  -0.8240116837488103 + 0.9290434802764991*I}

In[4]:=
Sort[data,(Im[#1]>Im[#2])&]

Out[4]//InputForm=
{-0.6876398064114924 + 0.9893164268004881*I,
  -0.8240116837488103 + 0.9290434802764991*I,
  0.06837510881823627 + 0.847443835410322*I,
  -0.47362970566225704 + 0.21374267988832485*I,
  0.0821271856782817 + 0.2029639541984125*I,
  -0.1928811783911134 - 0.22833001190101454*I,
  -0.4514416407833921 - 0.5575670386850278*I,
  0.35689272889096113 - 0.6162016388396594*I,
  -0.29863518479415907 - 0.676509694560786*I,
  -0.10011961132922509 - 0.7090021038645318*I}

In[5]:=
data=Chop[Table[(n+\[ImaginaryI] n),{n,-1,1,0.2}]]

Out[5]//InputForm=
{-1 - I, -0.8 - 0.8*I, -0.6 - 0.6*I,
  -0.39999999999999997 - 0.39999999999999997*I,
  -0.19999999999999996 - 0.19999999999999996*I, 0,
  0.20000000000000007 + 0.20000000000000007*I,
  0.4000000000000001 + 0.4000000000000001*I,
  0.6000000000000001 + 0.6000000000000001*I,
  0.8 + 0.8*I, 1. + 1.*I}

In[6]:=
Sort[data,((Re[#1]>Re[#2])&&(Im[#1]>Im[#2]))&]

Out[6]//InputForm=
{1. + 1.*I, 0.8 + 0.8*I, 0.6000000000000001 +
   0.6000000000000001*I, 0.4000000000000001 +
   0.4000000000000001*I, 0.20000000000000007 +
   0.20000000000000007*I, 0, -0.19999999999999996 -
   0.19999999999999996*I, -0.39999999999999997 -
   0.39999999999999997*I, -0.6 - 0.6*I, -0.8 - 0.8*I,
  -1 - I}

Best regards,
/J.M.


  • Prev by Date: Re: How to get decent quality plots.
  • Next by Date: Re: sorting complex number?
  • Previous by thread: Re: sorting complex number?
  • Next by thread: Re: sorting complex number?