Mercurial > octave
annotate scripts/optimization/lsqnonneg.m @ 31551:fd29c7a50a78 stable
maint: use commas, semicolons consistently with Octave conventions.
* makeValidName.m: Remove %!test and move BIST %!asserts to column 1.
* base64decode.m, base64encode.m, which.m, logm.m, uniquetol.m, perms.m:
Delete semicolon (';') at end of %!assert BIST.
* lin2mu.m, interp2.m, interpn.m, lsqnonneg.m, pqpnonneg.m, uniquetol.m,
betainc.m, normalize.m: Add semicolon (';') to end of assert statement within
%!test BIST.
* __memoize__.m, tar_is_bsd.m, publish.m: Add semicolon (';') to line with
keyword "persistent".
* stft.m: Use comma (',') after "case" keyword when code immediately follows.
* gallery.m: Align commas used in case statements in massive switch block.
Remove unnecessary parentheses around a numeric case argument.
* ranks.m: Remove semicolon (';') from case statemnt argument.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 26 Nov 2022 06:32:08 -0800 |
parents | 10a813492497 |
children | 597f3ee61a48 |
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) 2008-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/>. |
7682 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
7682 | 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:
23220
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. |
7682 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
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. |
7682 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
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:
23220
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 ######################################################################## |
7682 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
27 ## @deftypefn {} {@var{x} =} lsqnonneg (@var{c}, @var{d}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
28 ## @deftypefnx {} {@var{x} =} lsqnonneg (@var{c}, @var{d}, @var{x0}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
29 ## @deftypefnx {} {@var{x} =} lsqnonneg (@var{c}, @var{d}, @var{x0}, @var{options}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
30 ## @deftypefnx {} {[@var{x}, @var{resnorm}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
31 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
32 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
33 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}, @var{output}] =} lsqnonneg (@dots{}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20735
diff
changeset
|
34 ## @deftypefnx {} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}, @var{output}, @var{lambda}] =} lsqnonneg (@dots{}) |
22769 | 35 ## |
36 ## Minimize @code{norm (@var{c}*@var{x} - @var{d})} subject to | |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
37 ## @code{@var{x} >= 0}. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
38 ## |
22769 | 39 ## @var{c} and @var{d} must be real matrices. |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
40 ## |
22769 | 41 ## @var{x0} is an optional initial guess for the solution @var{x}. |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
42 ## |
22769 | 43 ## @var{options} is an options structure to change the behavior of the |
28959
5394d688d456
doc: Use @code{} within alternate text for @xref,@pxref macros for better Info display.
Rik <rik@octave.org>
parents:
28942
diff
changeset
|
44 ## algorithm (@pxref{XREFoptimset,,@code{optimset}}). @code{lsqnonneg} |
5394d688d456
doc: Use @code{} within alternate text for @xref,@pxref macros for better Info display.
Rik <rik@octave.org>
parents:
28942
diff
changeset
|
45 ## recognizes these options: @qcode{"MaxIter"}, @qcode{"TolX"}. |
7682 | 46 ## |
47 ## Outputs: | |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
48 ## |
22770
31b86d30874d
lsqnonneg.m: Change @itemize to @table so docs compile.
Rik <rik@octave.org>
parents:
22769
diff
changeset
|
49 ## @table @var |
7682 | 50 ## @item resnorm |
22769 | 51 ## The squared 2-norm of the residual: @code{norm (@var{c}*@var{x}-@var{d})^2} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
52 ## |
7682 | 53 ## @item residual |
22769 | 54 ## The residual: @code{@var{d}-@var{c}*@var{x}} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
55 ## |
7682 | 56 ## @item exitflag |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
57 ## An indicator of convergence. 0 indicates that the iteration count was |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
58 ## exceeded, and therefore convergence was not reached; >0 indicates that the |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
59 ## algorithm converged. (The algorithm is stable and will converge given |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
60 ## enough iterations.) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
61 ## |
7682 | 62 ## @item output |
63 ## A structure with two fields: | |
14366
b76f0740940e
doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
64 ## |
7682 | 65 ## @itemize @bullet |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
66 ## @item @qcode{"algorithm"}: The algorithm used (@qcode{"nnls"}) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
67 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17097
diff
changeset
|
68 ## @item @qcode{"iterations"}: The number of iterations taken. |
7682 | 69 ## @end itemize |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
70 ## |
7682 | 71 ## @item lambda |
31286
10a813492497
maint: Use "nonzero" in docstrings and error messages.
Rik <rik@octave.org>
parents:
31268
diff
changeset
|
72 ## Lagrange multipliers. If these are nonzero, the corresponding @var{x} |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
73 ## values should be zero, indicating the solution is pressed up against a |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
74 ## coordinate plane. The magnitude indicates how much the residual would |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
75 ## improve if the @code{@var{x} >= 0} constraints were relaxed in that |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
76 ## direction. |
31266
e5e35410da90
doc: Documented undocumented outputs for lsqnonneg and pqpnonneg
Arun Giridhar <arungiridhar@gmail.com>
parents:
30564
diff
changeset
|
77 ## |
22769 | 78 ## @end table |
79 ## @seealso{pqpnonneg, lscov, optimset} | |
7682 | 80 ## @end deftypefn |
81 | |
13027
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
82 ## PKG_ADD: ## Discard result to avoid polluting workspace with ans at startup. |
b9a89ca0fb75
prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
83 ## PKG_ADD: [~] = __all_opts__ ("lsqnonneg"); |
8648
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8600
diff
changeset
|
84 |
22769 | 85 ## This is implemented from Lawson and Hanson's 1973 algorithm on page 161 of |
86 ## Solving Least Squares Problems. | |
7682 | 87 |
30330
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
88 function [x, resnorm, residual, exitflag, output, lambda] = lsqnonneg (c, d, x0 = [], options = struct ()) |
8600 | 89 |
22769 | 90 ## Special case: called to find default optimization options |
91 if (nargin == 1 && ischar (c) && strcmp (c, "defaults")) | |
26138
804e18e3e320
Reenable query of optimization options (bugs #54952 and #55089).
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
25579
diff
changeset
|
92 x = struct ("MaxIter", 1e5); |
17312
088d014a7fe2
Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
93 return; |
8600 | 94 endif |
95 | |
28789
28de41192f3c
Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
96 if (nargin < 2) |
8600 | 97 print_usage (); |
98 endif | |
7682 | 99 |
22769 | 100 if (! (isnumeric (c) && ismatrix (c)) || ! (isnumeric (d) && ismatrix (d))) |
101 error ("lsqnonneg: C and D must be numeric matrices"); | |
102 endif | |
103 | |
104 if (! isstruct (options)) | |
105 error ("lsqnonneg: OPTIONS must be a struct"); | |
106 endif | |
107 | |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
108 ## Lawson-Hanson Step 1 (LH1): initialize the variables. |
8600 | 109 m = rows (c); |
110 n = columns (c); | |
22769 | 111 if (isempty (x0)) |
112 ## Initial guess is all zeros. | |
8600 | 113 x = zeros (n, 1); |
114 else | |
115 ## ensure nonnegative guess. | |
22769 | 116 x = max (x0, 0); |
7682 | 117 endif |
118 | |
22769 | 119 useqr = (m >= n); |
8507 | 120 max_iter = optimget (options, "MaxIter", 1e5); |
7682 | 121 |
8600 | 122 ## Initialize P, according to zero pattern of x. |
123 p = find (x > 0).'; | |
124 if (useqr) | |
125 ## Initialize the QR factorization, economized form. | |
126 [q, r] = qr (c(:,p), 0); | |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
127 endif |
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
128 |
7682 | 129 iter = 0; |
8600 | 130 |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
131 ## LH3: test for completion. |
8600 | 132 while (iter < max_iter) |
133 while (iter < max_iter) | |
20735
418ae0cb752f
Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents:
20231
diff
changeset
|
134 iter += 1; |
8600 | 135 |
136 ## LH6: compute the positive matrix and find the min norm solution | |
137 ## of the positive problem. | |
138 if (useqr) | |
139 xtmp = r \ q'*d; | |
140 else | |
141 xtmp = c(:,p) \ d; | |
142 endif | |
143 idx = find (xtmp < 0); | |
144 | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
145 if (isempty (idx)) |
8600 | 146 ## LH7: tmp solution found, iterate. |
147 x(:) = 0; | |
148 x(p) = xtmp; | |
149 break; | |
150 else | |
151 ## LH8, LH9: find the scaling factor. | |
152 pidx = p(idx); | |
22769 | 153 sf = x(pidx) ./ (x(pidx) - xtmp(idx)); |
8600 | 154 alpha = min (sf); |
155 ## LH10: adjust X. | |
156 xx = zeros (n, 1); | |
157 xx(p) = xtmp; | |
158 x += alpha*(xx - x); | |
159 ## LH11: move from P to Z all X == 0. | |
160 ## This corresponds to those indices where minimum of sf is attained. | |
20231
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
161 idx = idx(sf == alpha); |
8600 | 162 p(idx) = []; |
163 if (useqr) | |
164 ## update the QR factorization. | |
165 [q, r] = qrdelete (q, r, idx); | |
166 endif | |
167 endif | |
168 endwhile | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
169 |
8600 | 170 ## compute the gradient. |
171 w = c'*(d - c*x); | |
172 w(p) = []; | |
14787
acb09716fc94
lsqnonneg have tolerance option for convergence (bug #33347)
Axel Mathéi <axel.mathei@gmail.com>
parents:
14366
diff
changeset
|
173 tolx = optimget (options, "TolX", 10*eps*norm (c, 1)*length (c)); |
acb09716fc94
lsqnonneg have tolerance option for convergence (bug #33347)
Axel Mathéi <axel.mathei@gmail.com>
parents:
14366
diff
changeset
|
174 if (! any (w > tolx)) |
8600 | 175 if (useqr) |
176 ## verify the solution achieved using qr updating. | |
177 ## in the best case, this should only take a single step. | |
178 useqr = false; | |
179 continue; | |
180 else | |
181 ## we're finished. | |
182 break; | |
183 endif | |
184 endif | |
185 | |
186 ## find the maximum gradient. | |
7682 | 187 idx = find (w == max (w)); |
188 if (numel (idx) > 1) | |
189 warning ("lsqnonneg:nonunique", | |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
190 "a non-unique solution may be returned due to equal gradients"); |
7682 | 191 idx = idx(1); |
192 endif | |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21178
diff
changeset
|
193 ## move the index from Z to P. Keep P sorted. |
8600 | 194 z = [1:n]; z(p) = []; |
195 zidx = z(idx); | |
196 jdx = 1 + lookup (p, zidx); | |
197 p = [p(1:jdx-1), zidx, p(jdx:end)]; | |
198 if (useqr) | |
199 ## insert the column into the QR factorization. | |
200 [q, r] = qrinsert (q, r, jdx, c(:,zidx)); | |
201 endif | |
7682 | 202 |
203 endwhile | |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
204 ## LH12: complete. |
7682 | 205 |
7697
0bdfff62cc49
lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents:
7682
diff
changeset
|
206 ## Generate the additional output arguments. |
22769 | 207 if (isargout (2)) |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
208 resnorm = norm (c*x - d) ^ 2; |
7682 | 209 endif |
22769 | 210 if (isargout (3)) |
8406
0b7566aea627
fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
8304
diff
changeset
|
211 residual = d - c*x; |
7682 | 212 endif |
22769 | 213 if (isargout (4)) |
214 if (iter >= max_iter) | |
215 exitflag = 0; | |
216 else | |
217 exitflag = iter; | |
218 endif | |
7682 | 219 endif |
22769 | 220 if (isargout (5)) |
7682 | 221 output = struct ("algorithm", "nnls", "iterations", iter); |
222 endif | |
22769 | 223 if (isargout (6)) |
8600 | 224 lambda = zeros (size (x)); |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
225 lambda (setdiff (1:numel(x), p)) = w; |
7682 | 226 endif |
227 | |
228 endfunction | |
229 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
230 |
7682 | 231 %!test |
232 %! C = [1 0;0 1;2 1]; | |
233 %! d = [1;3;-2]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
234 %! assert (lsqnonneg (C, d), [0;0.5], 100*eps); |
7682 | 235 |
236 %!test | |
237 %! C = [0.0372 0.2869;0.6861 0.7071;0.6233 0.6245;0.6344 0.6170]; | |
238 %! d = [0.8587;0.1781;0.0747;0.8405]; | |
239 %! xnew = [0;0.6929]; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
240 %! assert (lsqnonneg (C, d), xnew, 0.0001); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
241 |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
242 ## Test Lagrange multiplier duality: x .* lambda == 0 |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
243 |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
244 %!test |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
245 %! [x, resn, resid, ~, ~, lambda] = lsqnonneg ([1 0; 0 1; 2 1], [1 1 3]'); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
246 %! assert (x, [1 1]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
247 %! assert (resn, 0, 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
248 %! assert (resid, [0 0 0]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
249 %! assert (lambda, [0 0]', 10*eps); |
31551
fd29c7a50a78
maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents:
31286
diff
changeset
|
250 %! assert (x .* lambda, [0 0]'); |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
251 |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
252 %!test |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
253 %! [x, resn, resid, ~, ~, lambda] = lsqnonneg ([1 0; 0 1; 2 1], [1 -1 1]'); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
254 %! assert (x, [0.6 0]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
255 %! assert (resn, 1.2, 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
256 %! assert (resid, [0.4 -1 -0.2]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
257 %! assert (lambda, [0 -1.2]', 10*eps); |
31551
fd29c7a50a78
maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents:
31286
diff
changeset
|
258 %! assert (x .* lambda, [0 0]'); |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
259 |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
260 %!test |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
261 %! [x, resn, resid, ~, ~, lambda] = lsqnonneg ([1 0; 0 1; 2 1], [-1 1 -1]'); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
262 %! assert (x, [0 0]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
263 %! assert (resn, 3, 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
264 %! assert (resid, [-1 1 -1]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
265 %! assert (lambda, [-3 0]', 10*eps); |
31551
fd29c7a50a78
maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents:
31286
diff
changeset
|
266 %! assert (x .* lambda, [0 0]'); |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
267 |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
268 %!test |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
269 %! [x, resn, resid, ~, ~, lambda] = lsqnonneg ([1 0; 0 1; 2 1], [-1 -1 -3]'); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
270 %! assert (x, [0 0]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
271 %! assert (resn, 11, 20*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
272 %! assert (resid, [-1 -1 -3]', 10*eps); |
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
273 %! assert (lambda, [-7 -4]', 10*eps); |
31551
fd29c7a50a78
maint: use commas, semicolons consistently with Octave conventions.
Rik <rik@octave.org>
parents:
31286
diff
changeset
|
274 %! assert (x .* lambda, [0 0]'); |
31268
44a68ac1a22f
Fix incorrect lambda outputs for lsqnonneg and pqpnonneg (bug #63178)
Arun Giridhar <arungiridhar@gmail.com>
parents:
31266
diff
changeset
|
275 |
28942
fc4bb4bd1d5e
maint: Use '##' as lead-in for full-line comments.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
276 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
277 %!error <Invalid call> lsqnonneg () |
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28789
diff
changeset
|
278 %!error <Invalid call> lsqnonneg (1) |
22769 | 279 %!error <C .* must be numeric matrices> lsqnonneg ({1},2) |
280 %!error <C .* must be numeric matrices> lsqnonneg (ones (2,2,2),2) | |
281 %!error <D must be numeric matrices> lsqnonneg (1,{2}) | |
282 %!error <D must be numeric matrices> lsqnonneg (1, ones (2,2,2)) | |
283 %!error <OPTIONS must be a struct> lsqnonneg (1, 2, [], 3) |