annotate scripts/general/cplxpair.m @ 31198:863730dd0f83 stable

nextpow2: Fix for input between 0.5 and 1 (bug #62947). * scripts/general/nextpow2.m: Switch to a naïve implementation using log2 with a single output argument and ceil.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 24 Aug 2022 17:15:34 +0200
parents 796f54d4ddbf
children 5d3faba0342e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30330
diff changeset
3 ## Copyright (C) 2000-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
7 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
8 ## This file is part of Octave.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23973
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23973
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
14 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
19 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23973
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
25
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20473
diff changeset
27 ## @deftypefn {} {} cplxpair (@var{z})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20473
diff changeset
28 ## @deftypefnx {} {} cplxpair (@var{z}, @var{tol})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20473
diff changeset
29 ## @deftypefnx {} {} cplxpair (@var{z}, @var{tol}, @var{dim})
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
30 ## 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: 20158
diff changeset
31 ## real part.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## 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: 19833
diff changeset
34 ## All real numbers (those with
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
35 ## @code{abs (imag (@var{z})) / abs (@var{z}) < @var{tol}}) are placed after
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
36 ## the complex pairs.
9141
c1fff751b5a8 Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9051
diff changeset
37 ##
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
38 ## @var{tol} is a weighting factor in the range [0, 1) which determines the
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
39 ## tolerance of the matching. The default value is @code{100 * eps} and the
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
40 ## resulting tolerance for a given complex pair is
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
41 ## @code{@var{tol} * abs (@var{z}(i)))}.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
42 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
43 ## By default the complex pairs are sorted along the first non-singleton
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## 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: 19833
diff changeset
45 ## sorted along this dimension.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
46 ##
9154
761fc0d3d980 Update section 17.2 (Complex Arithmetic) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents: 9153
diff changeset
47 ## Signal an error if some complex numbers could not be paired. Signal an
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
48 ## 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: 19833
diff changeset
49 ## 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: 19833
diff changeset
50 ## differing imaginary parts.
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
51 ## @c Set example in small font to prevent overfull line
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
52 ##
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7017
diff changeset
53 ## @smallexample
21436
b0920076dd98 doc: Add missing ')' in some docstrings.
Rik <rik@octave.org>
parents: 21385
diff changeset
54 ## cplxpair (exp (2i*pi*[0:4]'/5)) == exp (2i*pi*[3; 2; 4; 1; 0]/5)
7031
120f3135952f [project @ 2007-10-15 15:30:03 by jwe]
jwe
parents: 7017
diff changeset
55 ## @end smallexample
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
56 ## @end deftypefn
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
57
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
58 ## 2006-05-12 David Bateman - Modified for NDArrays
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
59
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
60 function y = cplxpair (z, tol, dim)
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
61
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 27923
diff changeset
62 if (nargin < 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5831
diff changeset
63 print_usage ();
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
64 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
65
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
66 if (isempty (z))
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
67 y = zeros (size (z));
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
68 return;
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
69 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
70
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
71 cls = ifelse (isa (z, "single"), "single", "double");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
72 if (nargin < 2 || isempty (tol))
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
73 tol = 100*eps (cls);
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
74 elseif (! isscalar (tol) || tol < 0 || tol >= 1)
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
75 error ("cplxpair: TOL must be a scalar number in the range 0 <= TOL < 1");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
76 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
77
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
78 nd = ndims (z);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
79 if (nargin < 3)
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
80 ## Find the first singleton dimension.
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
81 sz = size (z);
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
82 (dim = find (sz > 1, 1)) || (dim = 1);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
83 else
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14552
diff changeset
84 dim = floor (dim);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
85 if (dim < 1 || dim > nd)
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
86 error ("cplxpair: invalid dimension DIM");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
87 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
88 endif
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
89
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
90 ## Move dimension to analyze to first position, and convert to a 2-D matrix.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
91 perm = [dim:nd, 1:dim-1];
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
92 z = permute (z, perm);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
93 sz = size (z);
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
94 n = sz(1);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
95 m = prod (sz) / n;
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
96 z = reshape (z, n, m);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
97
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
98 ## Sort the sequence in terms of increasing real values.
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
99 [~, idx] = sort (real (z), 1);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
100 z = z(idx + n * ones (n, 1) * [0:m-1]);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
101
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
102 ## Put the purely real values at the end of the returned list.
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
103 [idxi, idxj] = find (abs (imag (z)) ./ (abs (z) + realmin (cls)) <= tol);
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
104 ## Force values detected to be real within tolerance to actually be real.
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
105 z(idxi + n*(idxj-1)) = real (z(idxi + n*(idxj-1)));
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
106 q = sparse (idxi, idxj, 1, n, m);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
107 nr = sum (q, 1);
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
108 [~, idx] = sort (q, 1);
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
109 midx = idx + rows (idx) * ones (rows (idx), 1) * [0:columns(idx)-1];
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
110 z = z(midx);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
111 y = z;
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
112
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
113 ## 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: 20158
diff changeset
114 ## the returned list, and remove them from further consideration.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
115 for j = 1:m
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
116 p = n - nr(j);
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
117 for i = 1:2:p
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
118 if (i+1 > p)
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
119 error ("cplxpair: could not pair all complex numbers");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
120 endif
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
121 [v, idx] = min (abs (z(i+1:p,j) - conj (z(i,j))));
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
122 if (v >= tol * abs (z(i,j)))
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
123 error ("cplxpair: could not pair all complex numbers");
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
124 endif
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
125 ## For pairs, select the one with positive imaginary part and use it and
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
126 ## it's conjugate, but list the negative imaginary pair first.
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
127 if (imag (z(i,j)) > 0)
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
128 y([i, i+1],j) = [conj(z(i,j)), z(i,j)];
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
129 else
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
130 y([i, i+1],j) = [conj(z(idx+i,j)), z(idx+i,j)];
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
131 endif
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
132 z(idx+i,j) = z(i+1,j);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
133 endfor
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
134 endfor
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
135
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8202
diff changeset
136 ## Reshape the output matrix.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
137 y = ipermute (reshape (y, sz), perm);
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
138
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
139 endfunction
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
140
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
141
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
142 %!demo
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
143 %! [ cplxpair(exp(2i*pi*[0:4]'/5)), exp(2i*pi*[3; 2; 4; 1; 0]/5) ]
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
144
14237
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 (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
146 %!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
147 %!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
148 %!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
149 %! [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
150 %!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
151 %! [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
152 %!assert (cplxpair ([0, 1, 2]), [0, 1, 2])
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
153
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
154 %!shared z,y
14237
11949c9795a0 Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
155 %! z = exp (2i*pi*[4; 3; 5; 2; 6; 1; 0]/7);
28912
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28886
diff changeset
156 %! z(2) = conj (z(1));
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28886
diff changeset
157 %! z(4) = conj (z(3));
0de38a6ef693 maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents: 28886
diff changeset
158 %! z(6) = conj (z(5));
14237
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)
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
162 %!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: 20158
diff changeset
163 %!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: 20158
diff changeset
164 %!assert (cplxpair ([z(randperm (7)).'; z(randperm (7)).'],[],2), [z.';z.'])
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
165 %! y = [ -1-1i; -1+1i;-3; -2; 1; 2; 3];
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
166 %!assert (cplxpair ([z(randperm (7)), y(randperm (7))]), [z,y])
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
167 %!assert (cplxpair ([z(randperm (7)), y(randperm (7)),z(randperm (7))]),
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
168 %! [z,y,z])
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
169
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
170 ## Test tolerance
30330
01de0045b2e3 maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents: 29359
diff changeset
171 %!assert (cplxpair ([2000 * (1+eps) + 4j; 2000 * (1-eps) - 4j]),
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
172 %! [(2000 - 4j); (2000 + 4j)], 100*eps(200))
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
173 %!error <could not pair>
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
174 %! cplxpair ([2000 * (1+eps) + 4j; 2000 * (1-eps) - 4j], 0);
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
175 %!error <could not pair>
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
176 %! 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
177
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
178 ## Test input validation
28886
d8318c12d903 test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents: 28789
diff changeset
179 %!error <Invalid call> cplxpair ()
23972
b4cf41d173dd * cplxpair.m: Improve accuracy and compatibility (bug #50124)
Bill Lash
parents: 23220
diff changeset
180 %!error <cplxpair: TOL must be .* scalar number> cplxpair (1, ones (2,2))
23973
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
181 %!error <cplxpair: TOL must be .* in the range 0 <= TOL < 1> cplxpair (1, -1)
a315ac23dc6c cplxpair.m: Use more Octave coding standards.
Rik <rik@octave.org>
parents: 23972
diff changeset
182 %!error <cplxpair: TOL must be .* in the range 0 <= TOL < 1> cplxpair (1, -1)
20473
4c2e76cbdc7d cplxpair.m: Use tolerance that depends on Z to evaluate pairing (bug #45810).
Rik <rik@octave.org>
parents: 20158
diff changeset
183 %!error <invalid dimension DIM> cplxpair (1, [], 3)