Discussion:
sorting complex number?
(too old to reply)
Sean2
2005-05-28 09:52:15 UTC
Permalink
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
David Park
2005-05-29 05:06:55 UTC
Permalink
complexlist =
Table[Random[Integer, {-100, 100}] +
I Random[Integer, {-100, 100}], {50}];

Sort[complexlist, Re[#1] < Re[#2] ||
(Re[#1] == Re[#2] && Im[#1] < Im[#2]) & ] // TableForm

David Park
***@earthlink.net
http://home.earthlink.net/~djmp/



From: Sean2 [mailto:***@wisc.edu]


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
Sean2
2005-06-08 08:17:32 UTC
Permalink
thanks, you help me a lot
Jens-Peer Kuska
2005-06-09 09:25:49 UTC
Permalink
Hi,

there is no ordering relation in the complex plane.
You can order the real parts or what ever.

Regards
Jens
Post by Sean2
thanks, you help me a lot
Pratik Desai
2005-05-29 05:10:44 UTC
Permalink
Post by Sean2
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],
Try this if you want it to sort it with respect to the imaginary value

values={-0.04484492919995264 - 21.97958759549533*I,
-0.04484492919995265 + 21.979587595495328*I,
-0.16025833149175872 + 18.801523562303874*I,
-0.16025833149175872 - 18.801523562303874*I,
-0.5008914185011427 + 15.633451946906193*I,
-0.5008914185011426 - 15.633451946906193*I,
-0.1751588280975272 + 12.564746566861638*I,
-0.1751588280975272 - 12.564746566861638*I,
-0.04711742781559087 - 9.418500468421371*I,
-0.04711742781559086 + 9.418500468421371*I,
-0.4590555619739296 + 6.232750821142238*I,
-0.4590555619739296 - 6.232750821142238*I,
-0.3331650057325463 + 3.1662410948948874*I,
-0.3331650057325463 - 3.1662410948948874*I}

Sort[values, Abs[Im[#1]] < Abs[Im[#2]] &]
Post by Sean2
{-0.3331650057325463 - 3.1662410948948874*I,
-0.3331650057325463 + 3.1662410948948874*I,
-0.4590555619739296 - 6.232750821142238*I,
-0.4590555619739296 + 6.232750821142238*I,
-0.04711742781559086 + 9.418500468421371*I,
-0.04711742781559087 - 9.418500468421371*I,
-0.1751588280975272 - 12.564746566861638*I,
-0.1751588280975272 + 12.564746566861638*I,
-0.5008914185011426 - 15.633451946906193*I,
-0.5008914185011427 + 15.633451946906193*I,
-0.16025833149175872 - 18.801523562303874*I,
-0.16025833149175872 + 18.801523562303874*I,
-0.04484492919995265 + 21.979587595495328*I,
-0.04484492919995264 - 21.97958759549533*I}

Or you can try something like this
complexsort[list_] := Block[{list1, list2}, list1 = Sort[list, Im[#1] < \
Im[#2] &]; list2 = Sort[Conjugate[list], Im[#1] > Im[#2] &];
Post by Sean2
Does anyone have a small program to do this?
thank you
I would be looking forward to it as you are

Best regards

Pratik Desai



Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134
Chris Chiasson
2005-05-29 05:18:25 UTC
Permalink
Like you said, it seems to work with a small list. Is your large list
symbolic or numeric?
Post by Sean2
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
--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161
Carl K. Woll
2005-05-30 01:04:07 UTC
Permalink
Post by Sean2
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
It seems that Mathematica sorts complex numbers first by the real part, and
then by the magnitude of the complex number, and then by the sign, e.g.:

Sort[{5 + 5 I, 5 - 10 I}]
{5 + 5 I, 5 - 10 I}

One possibility is to do as David Park suggests, and use Sort with an
ordering function p. Unfortunately, in your case you are interested in lists
of about 100000 elements, and sorting with an ordering function is much
slower than sorting without an ordering function. On my machine, sorting a
100000 element list with David's ordering function took several minutes.

As I wrote in an earlier post today, a much quicker alternative to sorting
with an ordering function is to use Ordering. For your case, we would do the
following:

data[[ Ordering[ Transpose[{Re[#],Im[#]}] ] ]] &[***@data]

On my machine, when data was a 100000 element list, the above computation
took less than 2 seconds.

Carl Woll
Jean-Marc Gulliet
2005-05-30 01:14:06 UTC
Permalink
Post by Sean2
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.
Continue reading on narkive:
Loading...