annotate scripts/set/uniquetol.m @ 30564:796f54d4ddbf stable

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2021. In all .txi and .texi files except gpl.txi and gpl.texi in the doc/liboctave and doc/interpreter directories, change the copyright to "Octave Project Developers", the same as used for other source files. Update copyright notices for 2022 (not done since 2019). For gpl.txi and gpl.texi, change the copyright notice to be "Free Software Foundation, Inc." and leave the date at 2007 only because this file only contains the text of the GPL, not anything created by the Octave Project Developers. Add Paul Thomas to contributors.in.
author John W. Eaton <jwe@octave.org>
date Tue, 28 Dec 2021 18:22:40 -0500
parents 8afdeac24ba4
children df030ac26390
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
1 ########################################################################
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30383
diff changeset
3 ## Copyright (C) 2020-2022 The Octave Project Developers
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
4 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
7 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
8 ## This file is part of Octave.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
9 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
13 ## (at your option) any later version.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
14 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
18 ## GNU General Public License for more details.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
19 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
22 ## <https://www.gnu.org/licenses/>.
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
23 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
24 ########################################################################
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
25
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
26 ## -*- texinfo -*-
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
27 ## @deftypefn {} {@var{c} =} uniquetol (@var{A})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
28 ## @deftypefnx {} {@var{c} =} uniquetol (@var{A}, @var{tol})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
29 ## @deftypefnx {} {@var{c} =} uniquetol (@dots{}, @var{property}, @var{value})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
30 ## @deftypefnx {} {[@var{c}, @var{ia}, @var{ic}] =} uniquetol (@dots{})
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
31 ## Return the unique elements of @var{A} within tolerance @var{tol}.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
32 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
33 ## Two values, @var{x} and @var{y}, are within relative tolerance if
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
34 ## @code{abs (@var{x} - @var{y}) <= @var{tol} * max (abs (@var{A}(:)))}.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
35 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
36 ## The input @var{A} must be a floating point type (double or single).
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
37 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
38 ## If @var{tol} is unspecified, the default tolerance is 1e-12 for double
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
39 ## precision input or 1e-6 for single precision input.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
40 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
41 ## The function may also be called with the following optional property/value
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
42 ## pairs. Property/value pairs must be passed after other input arguments:
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
43 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
44 ## @table @asis
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
45 ## @item @qcode{"ByRows"} (default: @code{false})
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
46 ## When true, return the unique rows of @var{A}. @var{A} must be a 2-D array
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
47 ## to use this option. For rows, the criteria for uniqueness is changed to
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
48 ## @code{all (abs (@var{x} - @var{y}) <= @var{tol}*max (abs (@var{A}),[],1))}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
49 ## which compares each column component of a row against a column-specific
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
50 ## tolerance.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
51 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
52 ## @item @qcode{"DataScale"}
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
53 ## The tolerance test is changed to
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
54 ## @code{abs (@var{x} - @var{y}) <= @var{tol}*@var{DS}} where @var{DS} is a
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
55 ## scalar unless the property @qcode{"ByRows"} is true. In that case, @var{DS}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
56 ## can either be a scalar or a vector with a length equal to the number of
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
57 ## columns in @var{A}. Using a value of @code{1.0} for @var{DS} will change
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
58 ## the tolerance from a relative one to an absolute tolerance. Using a value
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
59 ## of @code{Inf} will disable testing.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
60 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
61 ## @item @qcode{"OutputAllIndices"} (default: @code{false})
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
62 ## When true, @var{ia} is a cell array (not a vector) that contains the indices
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
63 ## for @emph{all} elements in @var{A} that are within tolerance of a value in
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
64 ## @var{C}. That is, each cell in @var{ia} corresponds to a single unique
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
65 ## value in @var{C}, and the values in each cell correspond to locations in
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
66 ## @var{A}.
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
67 ## @end table
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
68 ##
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
69 ## The output @var{c} is a row vector if the input @var{A} is a row vector.
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
70 ## For all other cases, a column vector is returned.
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
71 ##
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
72 ## The optional output @var{ia} is a column index vector such that
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
73 ## @code{@var{c} = @var{A}(@var{ia})}. If the @qcode{"ByRows"} property is
29312
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
74 ## true, the condition is @code{@var{c} = @var{A}(@var{ia}, :)}. If the
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
75 ## @qcode{"OutputAllIndices"} property is true, then the values
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
76 ## @code{@var{A}(@var{ia}@{@var{i}@})} are all within tolerance of the unique
875d799ab0b3 uniquetol.m: Fix texinfo syntax errors (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29311
diff changeset
77 ## value @code{@var{c}(@var{i})}.
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
78 ##
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
79 ## The optional output @var{ic} is a column index vector such that
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
80 ## @code{@var{A} = @var{c}(@var{ic})} when @var{A} is a vector. When @var{A}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
81 ## is a matrix, @code{@var{A}(:) = @var{c}(@var{ic})}. If the @qcode{"ByRows"}
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
82 ## property is true then @code{@var{A} = @var{c}(@var{ic},:)}.
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
83 ##
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
84 ## Example: small round-off errors require @code{uniquetol}, not @code{unique}
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
85 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
86 ## @example
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
87 ## @group
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
88 ## x = [1:5];
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
89 ## ## Inverse_Function (Function (x)) should return exactly x
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
90 ## y = exp (log (x));
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
91 ## D = unique ([x, y])
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
92 ## @result{} [1.0000 2.0000 3.0000 3.0000 4.0000 5.0000 5.0000]
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
93 ## C = uniquetol ([x, y])
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
94 ## @result{} [1 2 3 4 5]
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
95 ## @end group
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
96 ## @end example
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
97 ##
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
98 ## @seealso{unique, union, intersect, setdiff, setxor, ismember}
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
99 ## @end deftypefn
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
100
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
101
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
102 function [c, ia, ic] = uniquetol (A, varargin)
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
103
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
104 if (nargin < 1)
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
105 print_usage ();
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
106 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
107
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
108 if (isempty (A))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
109 c = A;
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
110 ia = [];
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
111 ic = [];
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
112 return;
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
113 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
114
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
115 if (! isfloat (A) || iscomplex (A))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
116 error ("Octave:uniquetol:unsupported-type",
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
117 "uniquetol: A must be a double or single precision non-complex array");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
118 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
119
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
120 if (nargin == 1 || ischar (varargin{1}))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
121 tol = ifelse (isa (A, "double"), 1e-12, 1e-6);
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
122 elseif (! (isfloat (varargin{1}) && isscalar (varargin{1}))
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
123 || iscomplex (varargin{1}))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
124 error ("Octave:uniquetol:unsupported-type",
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
125 "uniquetol: TOL must be a double or single precision non-complex scalar");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
126 else
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
127 tol = varargin{1};
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
128 varargin(1) = [];
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
129 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
130
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
131 if (mod (numel (varargin), 2))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
132 error ("uniquetol: PROPERTY/VALUE arguments must be passed in pairs");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
133 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
134
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
135 by_rows = false;
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
136 output_all_indices = false;
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
137 data_scale = [];
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
138
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
139 for k = 1:2:numel (varargin)
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
140 if (! ischar (varargin{k}))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
141 error ("uniquetol: PROPERTY must be a string");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
142 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
143
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
144 if (strcmpi (varargin{k}, "ByRows"))
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
145 by_rows = logical (varargin{k+1});
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
146 if (by_rows && ndims (A) > 2)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
147 error ('uniquetol: A must be a 2-D array when "ByRows" is true');
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
148 endif
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
149 elseif (strcmpi (varargin{k}, "OutputAllIndices"))
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
150 output_all_indices = logical (varargin{k+1});
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
151 elseif (strcmpi (varargin{k}, "DataScale"))
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
152 data_scale = varargin{k+1}(:).';
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
153 if (! isfloat (data_scale) || iscomplex (data_scale)
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
154 || any (data_scale(:) < 0) || any (isnan (data_scale(:))))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
155 error ("uniquetol: DataScale must be a non-NaN, positive floating point scalar or vector");
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
156 endif
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
157 cols_data_scale = columns (data_scale);
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
158 if (cols_data_scale != 1 && cols_data_scale != columns (A))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
159 error ("uniquetol: invalid DataScale size");
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
160 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
161 else
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
162 error ("uniquetol: unknown property '%s'", varargin{k});
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
163 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
164 endfor
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
165
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
166 if (isempty (data_scale))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
167 data_scale = max (abs (A(! isinf (A))(:)));
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
168 endif
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
169
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
170 tol *= data_scale;
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
171
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
172 if (by_rows)
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
173
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
174 nr = rows (A);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
175 nc = columns (A);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
176 Iall = zeros (nr, 1);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
177 I = NaN (nc, 1);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
178 ia = {};
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
179 J = NaN (nc, 1);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
180 j = 1;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
181 ii = 0;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
182
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
183 for i = 1:nr
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
184 if (any (Iall == i))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
185 continue;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
186 else
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
187 equ = all (abs (A - A(i,:)) <= tol, 2);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
188 equ(i,1) = equ(i,1) || any (! isfinite (A(i,:)), 2);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
189 sumeq = sum (equ);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
190 ia_tmp = find (equ);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
191 if (output_all_indices)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
192 ia{end+1} = ia_tmp;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
193 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
194 Iall(ii+(1:sumeq)) = ia_tmp;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
195 I(j) = ia_tmp(1);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
196 J(equ) = j;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
197 ii += sumeq;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
198 j += 1;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
199 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
200 endfor
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
201
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
202 I(isnan (I)) = [];
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
203 J(isnan (J)) = [];
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
204 c = A(I,:);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
205
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
206 if (! output_all_indices)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
207 ia = I(1:j-1);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
208 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
209 ic = J;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
210
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
211 else
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
212 isrowvec = isrow (A);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
213 A = A(:);
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
214 nr = rows (A);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
215 isnanA = isnan (A);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
216 anyisnanA = any (isnanA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
217 [sortA, sAi] = sort (A);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
218 diffsortA = diff (sortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
219 isinfsortA = isinf (sortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
220 isnansortA = isnan (sortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
221 numnan = sum (isnansortA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
222 if (any (isinfsortA))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
223 sAnin = sortA(! (isinfsortA | isnansortA));
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
224 diffsortA(isinf (diffsortA)) = abs (sAnin(end) - sAnin(1)) + 10;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
225 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
226 csdx = cumsum (diffsortA);
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
227 ue = [true; diff([0; csdx-mod(csdx,tol)]) > eps(max(csdx))];
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
228 ueold = NaN;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
229 while (any (ueold != ue))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
230 ueold = ue;
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
231 belowtol = [false; diff(sortA(ue)) < tol];
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
232 if (any (belowtol))
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
233 needstomove = find (ue)(belowtol);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
234 ue(needstomove) = false;
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
235 needstomove(needstomove >= nr-numnan) = [];
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
236 ue(needstomove+1) = true;
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
237 endif
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
238 endwhile
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
239 c = sortA(ue);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
240 [~, sortsAi] = sort (sAi);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
241 cumsumue = cumsum (ue);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
242 ic = cumsumue(sortsAi);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
243 if (anyisnanA)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
244 findisnanA = find (isnanA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
245 else
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
246 findisnanA = [];
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
247 endif
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
248 if (output_all_indices)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
249 nu = cumsumue(end);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
250 ia = cell (1, nu);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
251 for k = 1:nu
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
252 ia{k} = setdiff (sAi(cumsumue==k), findisnanA);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
253 endfor
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
254 else
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
255 ia = sAi(ue);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
256 endif
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
257
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
258 if (anyisnanA)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
259 rowsc1 = rows (c) + (1:sum (isnanA));
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
260 c(rowsc1) = NaN;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
261 ia(rowsc1) = findisnanA;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
262 ic(isnanA) = rowsc1;
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
263 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
264
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
265 ## FIXME: Matlab-compatible orientation of output
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
266 ## Actually, Matlab prefers row vectors (2021/03/24), but this is different
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
267 ## from all the other set functions which prefer column vectors. Assume
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
268 ## that this is a bug in Matlab's implementation and prefer column vectors.
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
269 if (isrowvec)
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
270 c = c.';
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
271 endif
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
272
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
273 endif
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
274
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
275 endfunction
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
276
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
277
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
278 %!assert (uniquetol ([1 1 2; 1 2 1; 1 1 2+10*eps]), [1;2])
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
279 %!assert (uniquetol ([1 1 2; 1 0 1; 1 1 2+10*eps], "byrows", true),
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
280 %! [1 1 2; 1 0 1])
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
281 %!assert (uniquetol ([]), [])
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
282 %!assert (uniquetol ([1]), [1])
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
283 %!assert (uniquetol ([2, 1]), [1, 2]);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
284 %!assert (uniquetol ([1; 2]), [1; 2])
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
285 %!assert (uniquetol ([-Inf, 1, NaN, Inf, NaN, Inf]), [-Inf, 1, Inf, NaN, NaN]);
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
286 %!assert (uniquetol (zeros (1, 0)), zeros (1, 0));
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
287 %!assert (uniquetol (zeros (1, 0), "byrows", true), zeros (1, 0))
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
288 %!assert (uniquetol ([1,2,2,3,2,4], "byrows", true), [1,2,2,3,2,4])
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
289 %!assert (uniquetol ([1,2,2,3,2,4]), [1,2,3,4])
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
290 %!assert (uniquetol ([1,2,2,3,2,4].', "byrows", true), [1;2;3;4])
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
291 %!assert (uniquetol (sparse ([2,0;2,0])), sparse ([0;2]))
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
292 %!assert (uniquetol (sparse ([1,2;2,3])), sparse ([1;2;3]))
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
293 %!assert (uniquetol (single ([1,2,2,3,2,4]), "byrows", true),
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
294 %! single ([1,2,2,3,2,4]))
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
295 %!assert (uniquetol (single ([1,2,2,3,2,4])), single ([1,2,3,4]))
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
296 %!assert (uniquetol (single ([1,2,2,3,2,4].'), "byrows", true),
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
297 %! single ([1;2;3;4]))
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
298
29463
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
299 ## Matlab compatibility of output
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
300 %!test
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
301 %! x = 1:0.045:3;
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
302 %! y = uniquetol (x, 0.1, "datascale", 1);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
303 %! assert (y(1:4), [1, 1.135, 1.27, 1.405]);
09767c20dec9 uniquetol.m: variable naming cleanup and addition of BIST test.
Rik <rik@octave.org>
parents: 29461
diff changeset
304
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
305 ## Test index vector return arguments
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
306 %!test
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
307 %! [c, ia, ic] = uniquetol ([1,1,2,3,3,3,4]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
308 %! assert (c, [1,2,3,4]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
309 %! assert (ia, [1;3;4;7]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
310 %! assert (ic, [1;1;2;3;3;3;4]);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
311
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
312 ## Test index vector return arguments with "ByRows"
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
313 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
314 %! A = [2, 3, 4; 2, 3, 4];
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
315 %! [c, ia, ic] = uniquetol (A, "byrows", true);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
316 %! assert (c, [2, 3, 4]);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
317 %! assert (A(ia,:), c);
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
318 %! assert (c(ic,:), A);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
319
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
320 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
321 %! x = (2:7)'*pi;
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
322 %! y = exp (log (x));
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
323 %! C = uniquetol ([x; y]);
29461
df641f946202 uniquetol.m: Improve performance when "byrows" is false (bug #59850).
Steven <steven.waldrip@gmail.com>
parents: 29359
diff changeset
324 %! assert (C, x, 1e-12);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
325
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
326 ## Test "ByRows" Property
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
327 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
328 %! A = [0.06, 0.21, 0.38; 0.38, 0.21, 0.39; 0.54, 0.56, 0.41; 0.46, 0.52, 0.95];
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
329 %! B = log (exp (A));
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
330 %! C = uniquetol ([A; B], "ByRows", true);
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
331 %! assert (C, A);
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
332
29311
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
333 ## Test "DataScale" Property
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
334 %!test
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
335 %! x = 10^11;
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
336 %! C = uniquetol ([x, exp(log(x))], 1e-6, "DataScale", 1);
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
337 %! assert (C, [x, exp(log(x))]);
2d26113ddf57 uniquetol.m: Update documentation and BIST tests (bug #59850).
Rik <rik@octave.org>
parents: 29309
diff changeset
338
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
339 ## Test "OutputAllIndices" Property
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
340 %!test
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
341 %! A = [.1 .2 .3 10];
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
342 %! [C, ia, ic] = uniquetol (A, .1, "OutputAllIndices", true);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
343 %! assert (C, [.1, 10]);
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
344 %! assert (ia, {(1:3)', 4});
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
345 %! assert (ic, [1; 1; 1; 2]);
29308
f3272029d42c uniquetol.m: Add new function (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents:
diff changeset
346
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
347 ## Test input validation
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
348 %!error <Invalid call> uniquetol ()
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
349 %!error <A must be a double or single precision> uniquetol (int8 (1))
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
350 %!error <A must be .* non-complex> uniquetol (1i)
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
351 %!error <TOL must be a double .* precision> uniquetol (1, int8 (1))
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
352 %!error <TOL must be a .* scalar> uniquetol (1, [1, 2])
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
353 %!error <TOL must be .* non-complex> uniquetol (1, 1i)
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
354 %!error <arguments must be passed in pairs> uniquetol (1, 2, "byrows")
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
355 %!error <PROPERTY must be a string> uniquetol (1, 2, 3, "bar")
30383
8afdeac24ba4 maint: Use space after function name and before opening parenthesis.
Rik <rik@octave.org>
parents: 30296
diff changeset
356 %!error <A must be a 2-D array> uniquetol (ones (2,2,2), "byrows", true)
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
357 %!error <DataScale must be a .* floating point> uniquetol (1, "DataScale", '1')
30296
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
358 %!error <DataScale must be .* positive> uniquetol (1, "DataScale", -1)
d6415c931759 uniquetol.m: Check for complex input (bug #59850).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29463
diff changeset
359 %!error <DataScale must be .* positive> uniquetol (1, "DataScale", 1i)
29309
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
360 %!error <DataScale must be a non-NaN> uniquetol (1, "DataScale", NaN)
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
361 %!error <invalid DataScale size> uniquetol (1, "DataScale", [1 2])
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
362 %!error <unknown property 'foo'> uniquetol (1, "foo", "bar")
83fe13ca9ce3 uniquetol.m: Update to use more Octave conventions (bug #59850).
Rik <rik@octave.org>
parents: 29308
diff changeset
363 %!error <unknown property 'foo'> uniquetol (1, 2, "foo", "bar")