Skip to content

cplxpair

Sorting complex numbers into conjugate pairs

cplxpair(A)

  • A is an array of real or complex numbers.
  • It sorts complex numbers in acesending order of their real parts. Conjugate pairs are grouped such that the one with a negative imaginary part comes before its conjugate. Real numbers are sorted in ascend order and appear after all complex numbers in the sorted array.
  • If A is a vector, it sorts in the direction of the longer dimension. If A is a matrix or an array, it sorts in the direction of its first non-singleton dimension.
  • A number is considered as real if the absolute value of its imaginary part does not exceed eps(0).
  • Complex numbers c1 = a1 + b1*i and c2 = a2 + b2*i are conjugate pair if abs(a1-a2) and abs(b1-b2) are both smaller than the default tolerance max(abs(c1),abs(c2)) * eps(1), and b1 and b2 have opposite signs.
  • It returns error if the conjugate of a complex number does not exist in A. This happens when, for example, there are odd number of complex numbers.

Example 1: Sorting a complex vector.

a=[-27.62 - 4.075i
 -0.014 + 0.032i
 -27.62 + 7.557i
 -0.014 - 19.00i
  28.05 - 0.161i
 -27.62 - 7.557i
  28.05 - 4.384i
 -0.014 + 19.00i
  28.05 + 4.384i
  26.37 + 0.000i
 -0.014 - 0.032i
  0.020 + 0.000i
  0.005 + 0.000i
 -27.62 + 4.075i
  28.05 + 0.161i];
cplxpair(a)
ans =
 -27.62 - 4.075i
 -27.62 + 4.075i
 -27.62 - 7.557i
 -27.62 + 7.557i
 -0.014 - 0.032i
 -0.014 + 0.032i
 -0.014 - 19.00i
 -0.014 + 19.00i
  28.05 - 0.161i
  28.05 + 0.161i
  28.05 - 4.384i
  28.05 + 4.384i
  0.005 + 0.000i
  0.020 + 0.000i
  26.37 + 0.000i

cplxpair(A, [ ], dim)

  • A is as specified in the description above for cplxpair(A).
  • [] means the empty array.
  • It sorts in the direction of dimension dim.
  • dim should be an integer satisfying 1 <= dim <= ndims(A).

Example 2: Sorting a complex matrix along the second dimension.

a=[
-0.495 - 0.344i  -49.62 - 0.014i
-4.611 + 0.000i  -3.663 + 0.000i
 0.085 + 0.042i  -47.09 - 0.024i
 0.029 + 20.22i   0.682 - 10.84i
 0.085 - 0.042i  -47.09 + 0.024i
 0.029 + 0.216i   0.682 - 1.132i
-0.495 + 38.07i  -49.62 + 2.114i
 0.085 - 4.951i  -47.09 + 0.022i
-0.495 - 38.07i  -49.62 - 2.114i
 0.085 + 4.951i  -47.09 - 0.022i
 0.522 + 0.000i   1.122 + 0.000i
 0.029 - 0.216i   0.682 + 1.132i
-0.495 + 0.344i  -49.62 + 0.014i
 0.029 - 20.22i   0.682 + 10.84i
-2.575 + 0.000i  -37.70 + 0.000i
]'
cplxpair(a,[],2)
a =
Columns 1 through 6:
-0.495 + 0.344i  -4.611 + 0.000i   0.085 - 0.042i   0.029 - 20.22i   0.085 + 0.042i   0.029 - 0.216i
-49.62 - 0.014i  -3.663 + 0.000i  -47.09 - 0.024i   0.682 + 10.84i  -47.09 + 0.024i   0.682 + 1.132i
Columns 7 through 12:
-0.495 - 38.07i   0.085 + 4.951i  -0.495 + 38.07i   0.085 - 4.951i   0.522 + 0.000i   0.029 + 0.216i
-49.62 + 2.114i  -47.09 + 0.022i  -49.62 - 2.114i  -47.09 - 0.022i   1.122 + 0.000i   0.682 - 1.132i
Columns 13 through 15:
-0.495 - 0.344i   0.029 + 20.22i  -2.575 + 0.000i
-49.62 + 0.014i   0.682 - 10.84i  -37.70 + 0.000i

ans =
Columns 1 through 6:
-0.495 - 0.344i  -0.495 + 0.344i  -0.495 - 38.07i  -0.495 + 38.07i   0.029 - 0.216i   0.029 + 0.216i
-49.62 - 0.014i  -49.62 + 0.014i  -49.62 - 2.114i  -49.62 + 2.114i  -47.09 - 0.022i  -47.09 + 0.022i
Columns 7 through 12:
 0.029 - 20.22i   0.029 + 20.22i   0.085 - 0.042i   0.085 + 0.042i   0.085 - 4.951i   0.085 + 4.951i
-47.09 - 0.024i  -47.09 + 0.024i   0.682 - 1.132i   0.682 + 1.132i   0.682 - 10.84i   0.682 + 10.84i
Columns 13 through 15:
-4.611 + 0.000i  -2.575 + 0.000i   0.522 + 0.000i
-37.70 + 0.000i  -3.663 + 0.000i   1.122 + 0.000i

cplxpair(A, tol)

  • A is as specified in the description above for cplxpair(A).
  • tol is a tolerance used in determining real numbers and conjugate pairs.
  • tol should be a non-negative scalar smaller than 1.
  • A number is considered as real if the absolute value of its imaginary part does not exceed tol.
  • Complex numbers c1 = a1 + b1*i and c2 = a2 + b2*i form a conjugate pair if abs(a1-a2) and abs(b1-b2) are both smaller than tol, and b1 and b2 have opposite signs.

Example 3: The matrix a is sorted along the first dimension with tolerance tol = 0.00101. Hence, in the first and second rows of the sorted array, -0.3794973 - 0.4599557i and -0.3784973 + 0.4609557i form a conjugate pair, even though the real parts (and also the imaginary parts) differ by 0.001.

a =[
-0.1092265 - 0.0175371i  -34.228177 + 4.7641329i
-0.0308166 + 0.0000000i  -0.1631809 + 0.0000000i
-0.1102265 + 0.0185371i  -34.229177 - 4.7631329i
 2.4420461 - 0.0238848i   0.1105287 - 0.1661568i
 2.4430461 + 0.0248848i   0.1115287 + 0.1671568i
-0.1102265 - 0.0621998i  -34.229177 - 41.254490i
 47.129957 + 0.0000000i   0.4034812 + 0.0000000i
-3.6544816 + 0.0000000i   0.0116494 + 0.0000000i
 2.4420461 - 1.7635095i   0.1105287 - 0.5415976i
-0.1102265 + 0.0621998i  -34.229177 + 41.254490i
 2.4420461 + 1.7635095i   0.1105287 + 0.5415976i
-0.3794973 - 0.4599557i   4.7495474 + 5.9845691i
-0.3794973 - 4.1173533i   4.7495474 + 0.1136352i
-0.3784973 + 0.4609557i   4.7505474 - 5.9835691i
-0.3794973 + 4.1173533i   4.7495474 - 0.1136352i
]
cplxpair(a,0.00101)
ans =
-0.3794973 - 0.4599557i  -34.229177 - 4.7631329i
-0.3784973 + 0.4609557i  -34.228177 + 4.7641329i
-0.3794973 - 4.1173533i  -34.229177 - 41.254490i
-0.3794973 + 4.1173533i  -34.229177 + 41.254490i
-0.1092265 - 0.0175371i   0.1105287 - 0.1661568i
-0.1102265 + 0.0185371i   0.1115287 + 0.1671568i
-0.1102265 - 0.0621998i   0.1105287 - 0.5415976i
-0.1102265 + 0.0621998i   0.1105287 + 0.5415976i
 2.4420461 - 0.0238848i   4.7495474 - 0.1136352i
 2.4430461 + 0.0248848i   4.7495474 + 0.1136352i
 2.4420461 - 1.7635095i   4.7505474 - 5.9835691i
 2.4420461 + 1.7635095i   4.7495474 + 5.9845691i
-3.6544816 + 0.0000000i  -0.1631809 + 0.0000000i
-0.0308166 + 0.0000000i   0.0116494 + 0.0000000i
 47.129957 + 0.0000000i   0.4034812 + 0.0000000i

cplxpair(A, tol, dim)

  • A, tol and dim are as specified in the descriptions above for cplxpair(A), cplxpair(A,[],dim) and cplxpair(A,tol).
  • It sorts A along the dim-th dimension using the tolerance tol.