Mercurial > hg > octave-nkf
annotate scripts/general/cplxpair.m @ 20509:c5a8eff5a05d
gallery: add very basic tests (check output size) for all matrix types.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Fri, 03 Jul 2015 16:53:08 +0100 |
parents | 7503499a252b |
children | 4c2e76cbdc7d |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18834
diff
changeset
|
1 ## Copyright (C) 2000-2015 Paul Kienzle |
5820 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5820 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5820 | 18 |
19 ## -*- texinfo -*- | |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
20 ## @deftypefn {Function File} {} cplxpair (@var{z}) |
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
21 ## @deftypefnx {Function File} {} cplxpair (@var{z}, @var{tol}) |
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
22 ## @deftypefnx {Function File} {} cplxpair (@var{z}, @var{tol}, @var{dim}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
23 ## Sort the numbers @var{z} into complex conjugate pairs ordered by |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
24 ## increasing real part. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## The negative imaginary complex numbers are placed first within each pair. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
27 ## All real numbers (those with |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
28 ## @code{abs (imag (@var{z}) / @var{z}) < @var{tol}}) are placed after the |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
29 ## complex pairs. |
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
30 ## |
9154
761fc0d3d980
Update section 17.2 (Complex Arithmetic) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
31 ## If @var{tol} is unspecified the default value is 100*@code{eps}. |
5820 | 32 ## |
33 ## By default the complex pairs are sorted along the first non-singleton | |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
34 ## dimension of @var{z}. If @var{dim} is specified, then the complex pairs are |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
35 ## sorted along this dimension. |
5820 | 36 ## |
9154
761fc0d3d980
Update section 17.2 (Complex Arithmetic) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
37 ## Signal an error if some complex numbers could not be paired. Signal an |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
38 ## error if all complex numbers are not exact conjugates (to within @var{tol}). |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
39 ## Note that there is no defined order for pairs with identical real parts but |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
40 ## differing imaginary parts. |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
41 ## @c Set example in small font to prevent overfull line |
5820 | 42 ## |
7031 | 43 ## @smallexample |
5831 | 44 ## cplxpair (exp(2i*pi*[0:4]'/5)) == exp(2i*pi*[3; 2; 4; 1; 0]/5) |
7031 | 45 ## @end smallexample |
5820 | 46 ## @end deftypefn |
47 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7795
diff
changeset
|
48 ## FIXME: subsort returned pairs by imaginary magnitude |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
49 ## FIXME: Why doesn't exp (2i*pi*[0:4]'/5) produce exact conjugates. Does |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
50 ## FIXME: it in Matlab? The reason is that complex pairs are supposed |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
51 ## FIXME: to be exact conjugates, and not rely on a tolerance test. |
5820 | 52 |
53 ## 2006-05-12 David Bateman - Modified for NDArrays | |
54 | |
55 function y = cplxpair (z, tol, dim) | |
56 | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
57 if (nargin < 1 || nargin > 3) |
6046 | 58 print_usage (); |
5820 | 59 endif |
60 | |
61 if (length (z) == 0) | |
62 y = zeros (size (z)); | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
63 return; |
5820 | 64 endif |
65 | |
66 if (nargin < 2 || isempty (tol)) | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
67 if (isa (z, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
68 tol = 100 * eps("single"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
69 else |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
70 tol = 100*eps; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
71 endif |
5820 | 72 endif |
73 | |
74 nd = ndims (z); | |
75 orig_dims = size (z); | |
76 if (nargin < 3) | |
77 ## Find the first singleton dimension. | |
78 dim = 0; | |
79 while (dim < nd && orig_dims(dim+1) == 1) | |
80 dim++; | |
81 endwhile | |
82 dim++; | |
83 if (dim > nd) | |
84 dim = 1; | |
85 endif | |
86 else | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
87 dim = floor (dim); |
5820 | 88 if (dim < 1 || dim > nd) |
89 error ("cplxpair: invalid dimension along which to sort"); | |
90 endif | |
91 endif | |
92 | |
8507 | 93 ## Move dimension to treat first, and convert to a 2-D matrix. |
5820 | 94 perm = [dim:nd, 1:dim-1]; |
95 z = permute (z, perm); | |
96 sz = size (z); | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
97 n = sz(1); |
5820 | 98 m = prod (sz) / n; |
99 z = reshape (z, n, m); | |
100 | |
8507 | 101 ## Sort the sequence in terms of increasing real values. |
5820 | 102 [q, idx] = sort (real (z), 1); |
103 z = z(idx + n * ones (n, 1) * [0:m-1]); | |
104 | |
8507 | 105 ## Put the purely real values at the end of the returned list. |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
106 cls = "double"; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
107 if (isa (z, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
108 cls = "single"; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7031
diff
changeset
|
109 endif |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
110 [idxi, idxj] = find (abs (imag (z)) ./ (abs (z) + realmin (cls)) < tol); |
5820 | 111 q = sparse (idxi, idxj, 1, n, m); |
112 nr = sum (q, 1); | |
113 [q, idx] = sort (q, 1); | |
114 z = z(idx); | |
115 y = z; | |
116 | |
117 ## For each remaining z, place the value and its conjugate at the | |
118 ## start of the returned list, and remove them from further | |
119 ## consideration. | |
120 for j = 1:m | |
121 p = n - nr(j); | |
8507 | 122 for i = 1:2:p |
5820 | 123 if (i+1 > p) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
124 error ("cplxpair: could not pair all complex numbers"); |
5820 | 125 endif |
126 [v, idx] = min (abs (z(i+1:p) - conj (z(i)))); | |
127 if (v > tol) | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
128 error ("cplxpair: could not pair all complex numbers"); |
5820 | 129 endif |
130 if (imag (z(i)) < 0) | |
10549 | 131 y([i, i+1]) = z([i, idx+i]); |
5820 | 132 else |
10549 | 133 y([i, i+1]) = z([idx+i, i]); |
5820 | 134 endif |
135 z(idx+i) = z(i+1); | |
136 endfor | |
137 endfor | |
138 | |
8507 | 139 ## Reshape the output matrix. |
5820 | 140 y = ipermute (reshape (y, sz), perm); |
141 | |
142 endfunction | |
143 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
144 |
5820 | 145 %!demo |
146 %! [ cplxpair(exp(2i*pi*[0:4]'/5)), exp(2i*pi*[3; 2; 4; 1; 0]/5) ] | |
147 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 %!assert (isempty (cplxpair ([]))) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 %!assert (cplxpair (1), 1) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
150 %!assert (cplxpair ([1+1i, 1-1i]), [1-1i, 1+1i]) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
151 %!assert (cplxpair ([1+1i, 1+1i, 1, 1-1i, 1-1i, 2]), ... |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
152 %! [1-1i, 1+1i, 1-1i, 1+1i, 1, 2]) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
153 %!assert (cplxpair ([1+1i; 1+1i; 1; 1-1i; 1-1i; 2]), ... |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
154 %! [1-1i; 1+1i; 1-1i; 1+1i; 1; 2]) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
155 %!assert (cplxpair ([0, 1, 2]), [0, 1, 2]) |
5820 | 156 |
157 %!shared z | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
158 %! z = exp (2i*pi*[4; 3; 5; 2; 6; 1; 0]/7); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 %!assert (cplxpair (z(randperm (7))), z) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 %!assert (cplxpair (z(randperm (7))), z) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
161 %!assert (cplxpair (z(randperm (7))), z) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
162 %!assert (cplxpair ([z(randperm(7)),z(randperm(7))]), [z,z]) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
163 %!assert (cplxpair ([z(randperm(7)),z(randperm(7))],[],1), [z,z]) |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
164 %!assert (cplxpair ([z(randperm(7)).';z(randperm(7)).'],[],2), [z.';z.']) |
5820 | 165 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
14868
diff
changeset
|
166 ## tolerance test |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
167 %!assert (cplxpair ([1i, -1i, 1+(1i*eps)],2*eps), [-1i, 1i, 1+(1i*eps)]) |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17336
diff
changeset
|
168 |