Mercurial > octave-nkf
annotate scripts/general/cplxpair.m @ 20610:a61f0d6beb71
inputParser: do not perform validation of default values (bug #45837)
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Thu, 08 Oct 2015 20:15:19 +0100 |
parents | 4c2e76cbdc7d |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18713
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}) |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
23 ## Sort the numbers @var{z} into complex conjugate pairs ordered by increasing |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
24 ## real part. |
20193
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
25 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
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:
19867
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:
19867
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 ## |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
31 ## @var{tol} is a weighting factor which determines the tolerance of matching. |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
32 ## The default value is 100 and the resulting tolerance for a given complex |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
33 ## pair is @code{100 * eps (abs (@var{z}(i))}. |
5820 | 34 ## |
35 ## By default the complex pairs are sorted along the first non-singleton | |
20193
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
36 ## 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:
19867
diff
changeset
|
37 ## sorted along this dimension. |
5820 | 38 ## |
9154
761fc0d3d980
Update section 17.2 (Complex Arithmetic) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9153
diff
changeset
|
39 ## Signal an error if some complex numbers could not be paired. Signal an |
20193
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19867
diff
changeset
|
40 ## 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:
19867
diff
changeset
|
41 ## 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:
19867
diff
changeset
|
42 ## differing imaginary parts. |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
43 ## @c Set example in small font to prevent overfull line |
5820 | 44 ## |
7031 | 45 ## @smallexample |
5831 | 46 ## cplxpair (exp(2i*pi*[0:4]'/5)) == exp(2i*pi*[3; 2; 4; 1; 0]/5) |
7031 | 47 ## @end smallexample |
5820 | 48 ## @end deftypefn |
49 | |
8202
cf59d542f33e
replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents:
7795
diff
changeset
|
50 ## FIXME: subsort returned pairs by imaginary magnitude |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
51 ## FIXME: Why doesn't exp (2i*pi*[0:4]'/5) produce exact conjugates? Does |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
52 ## 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
|
53 ## FIXME: to be exact conjugates, and not rely on a tolerance test. |
5820 | 54 |
55 ## 2006-05-12 David Bateman - Modified for NDArrays | |
56 | |
57 function y = cplxpair (z, tol, dim) | |
58 | |
14552
86854d032a37
maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
59 if (nargin < 1 || nargin > 3) |
6046 | 60 print_usage (); |
5820 | 61 endif |
62 | |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
63 if (isempty (z)) |
5820 | 64 y = zeros (size (z)); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
65 return; |
5820 | 66 endif |
67 | |
68 if (nargin < 2 || isempty (tol)) | |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
69 tol = 100; |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
70 elseif (! isscalar (tol) || tol < 0) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
71 error ("cplxpair: TOL must be a positive scalar number") |
5820 | 72 endif |
73 | |
74 nd = ndims (z); | |
75 if (nargin < 3) | |
76 ## Find the first singleton dimension. | |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
77 sz = size (z); |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
78 (dim = find (sz > 1, 1)) || (dim = 1); |
5820 | 79 else |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14552
diff
changeset
|
80 dim = floor (dim); |
5820 | 81 if (dim < 1 || dim > nd) |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
82 error ("cplxpair: invalid dimension DIM"); |
5820 | 83 endif |
84 endif | |
85 | |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
86 ## Move dimension to treat to first position, and convert to a 2-D matrix. |
5820 | 87 perm = [dim:nd, 1:dim-1]; |
88 z = permute (z, perm); | |
89 sz = size (z); | |
19867
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19731
diff
changeset
|
90 n = sz(1); |
5820 | 91 m = prod (sz) / n; |
92 z = reshape (z, n, m); | |
93 | |
8507 | 94 ## Sort the sequence in terms of increasing real values. |
5820 | 95 [q, idx] = sort (real (z), 1); |
96 z = z(idx + n * ones (n, 1) * [0:m-1]); | |
97 | |
8507 | 98 ## Put the purely real values at the end of the returned list. |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
99 cls = ifelse (isa (z, "single"), "single", "double"); |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
100 [idxi, idxj] = find (abs (imag (z)) ./ (abs (z) + realmin (cls)) ... |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
101 < tol*eps (abs (z))); |
5820 | 102 q = sparse (idxi, idxj, 1, n, m); |
103 nr = sum (q, 1); | |
104 [q, idx] = sort (q, 1); | |
105 z = z(idx); | |
106 y = z; | |
107 | |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
108 ## For each remaining z, place the value and its conjugate at the start of |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
109 ## the returned list, and remove them from further consideration. |
5820 | 110 for j = 1:m |
111 p = n - nr(j); | |
8507 | 112 for i = 1:2:p |
5820 | 113 if (i+1 > p) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
114 error ("cplxpair: could not pair all complex numbers"); |
5820 | 115 endif |
116 [v, idx] = min (abs (z(i+1:p) - conj (z(i)))); | |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
117 if (v > tol*eps (abs (z(i)))) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
118 error ("cplxpair: could not pair all complex numbers"); |
5820 | 119 endif |
120 if (imag (z(i)) < 0) | |
10549 | 121 y([i, i+1]) = z([i, idx+i]); |
5820 | 122 else |
10549 | 123 y([i, i+1]) = z([idx+i, i]); |
5820 | 124 endif |
125 z(idx+i) = z(i+1); | |
126 endfor | |
127 endfor | |
128 | |
8507 | 129 ## Reshape the output matrix. |
5820 | 130 y = ipermute (reshape (y, sz), perm); |
131 | |
132 endfunction | |
133 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
134 |
5820 | 135 %!demo |
136 %! [ cplxpair(exp(2i*pi*[0:4]'/5)), exp(2i*pi*[3; 2; 4; 1; 0]/5) ] | |
137 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
138 %!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
|
139 %!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
|
140 %!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
|
141 %!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
|
142 %! [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
|
143 %!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
|
144 %! [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
|
145 %!assert (cplxpair ([0, 1, 2]), [0, 1, 2]) |
5820 | 146 |
147 %!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
|
148 %! 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
|
149 %!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
|
150 %!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
|
151 %!assert (cplxpair (z(randperm (7))), z) |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
152 %!assert (cplxpair ([z(randperm (7)), z(randperm (7))]), [z,z]) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
153 %!assert (cplxpair ([z(randperm (7)), z(randperm (7))],[],1), [z,z]) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
154 %!assert (cplxpair ([z(randperm (7)).'; z(randperm (7)).'],[],2), [z.';z.']) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
155 |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
156 ## Test tolerance |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
157 %!assert (cplxpair ([2000 * (1+eps) + 4j; 2000 * (1-eps) - 4j]), ... |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
158 %! [(2000 - 4j); (2000 + 4j)], 100*eps(200)) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
159 %!error <could not pair> cplxpair ([2000 * (1+eps) + 4j; 2000 * (1-eps) - 4j], 0); |
5820 | 160 |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
161 %!error <could not pair> cplxpair ([2e6 + j; 2e6 - j; 1e-9 * (1 + j); 1e-9 * (1 - 2j)]); |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17336
diff
changeset
|
162 |
20508
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
163 ## Test input validation |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
164 %!error cplxpair () |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
165 %!error cplxpair (1,2,3,4) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
166 %!error <TOL must be .* positive> cplxpair (1, -1) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
167 %!error <TOL must be .* scalar number> cplxpair (1, ones (2,2)) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
168 %!error <invalid dimension DIM> cplxpair (1, [], 3) |
4c2e76cbdc7d
cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents:
20193
diff
changeset
|
169 |