annotate scripts/optimization/pqpnonneg.m @ 25579:07c2c42f457e stable

doc: Miscellaneous documentation fixes all over the manual (bug #54288). * embedded.cc, defaults.cc, error.cc, hex2num.cc, load-save.cc, integral2.m, integral3.m, quad2d.m, warning_ids.m, decic.m, lsqnonneg.m, pqpnonneg.m, bicg.m, betaincinv.m: Removal of duplicate words and redundant ')' and ';' characters. Added missing '"', ')' characters. Renamed variables to match other occurrences in docstring. Use '*' instead of 'x' for multiply. Surround variables with @var{}.
author Rik <rik@octave.org>
date Wed, 11 Jul 2018 08:40:07 -0700
parents 6652d3823428
children 804e18e3e320
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25054
6652d3823428 maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25003
diff changeset
1 ## Copyright (C) 2008-2018 Bill Denney
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ## Copyright (C) 2008 Jaroslav Hajek
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
3 ## Copyright (C) 2009 VZLU Prague
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 ##
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 ## This file is part of Octave.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
7 ## Octave is free software: you can redistribute it and/or modify it
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## 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
9 ## 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
10 ## (at your option) any later version.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ##
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
15 ## GNU General Public License for more details.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ##
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public License
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 ## 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
19 ## <https://www.gnu.org/licenses/>.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
22 ## @deftypefn {} {@var{x} =} pqpnonneg (@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
23 ## @deftypefnx {} {@var{x} =} pqpnonneg (@var{c}, @var{d}, @var{x0})
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
24 ## @deftypefnx {} {@var{x} =} pqpnonneg (@var{c}, @var{d}, @var{x0}, @var{options})
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
25 ## @deftypefnx {} {[@var{x}, @var{minval}] =} pqpnonneg (@dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
26 ## @deftypefnx {} {[@var{x}, @var{minval}, @var{exitflag}] =} pqpnonneg (@dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20735
diff changeset
27 ## @deftypefnx {} {[@var{x}, @var{minval}, @var{exitflag}, @var{output}] =} pqpnonneg (@dots{})
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}, @var{minval}, @var{exitflag}, @var{output}, @var{lambda}] =} pqpnonneg (@dots{})
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ##
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
30 ## Minimize @code{1/2*@var{x}'*@var{c}*@var{x} + @var{d}'*@var{x}} subject to
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
31 ## @code{@var{x} >= 0}.
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ##
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
33 ## @var{c} and @var{d} must be real matrices, and @var{c} must be symmetric and
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## positive definite.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ##
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
36 ## @var{x0} is an optional initial guess for the solution @var{x}.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37 ##
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
38 ## @var{options} is an options structure to change the behavior of the
25579
07c2c42f457e doc: Miscellaneous documentation fixes all over the manual (bug #54288).
Rik <rik@octave.org>
parents: 25054
diff changeset
39 ## algorithm (@pxref{XREFoptimset,,optimset}). @code{pqpnonneg} recognizes
22769
9bd2ea5703e2 lsqnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22764
diff changeset
40 ## one option: @qcode{"MaxIter"}.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
41 ##
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 ## Outputs:
14366
b76f0740940e doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
43 ##
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
44 ## @table @var
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
45 ##
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
46 ## @item x
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
47 ## The solution matrix
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
48 ##
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
49 ## @item minval
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
50 ## The minimum attained model value,
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
51 ## @code{1/2*@var{xmin}'*@var{c}*@var{xmin} + @var{d}'*@var{xmin}}
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
52 ##
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 ## @item exitflag
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
54 ## 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
55 ## 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
56 ## 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
57 ## enough iterations.)
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
58 ##
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 ## @item output
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60 ## A structure with two fields:
14366
b76f0740940e doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
61 ##
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 ## @itemize @bullet
25003
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24534
diff changeset
63 ## @item @qcode{"algorithm"}: The algorithm used (@nospell{@qcode{"nnls"}})
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
64 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14366
diff changeset
65 ## @item @qcode{"iterations"}: The number of iterations taken.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66 ## @end itemize
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
67 ##
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 ## @item lambda
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
69 ## @c FIXME: Something is output from the function, but what is it?
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
70 ## Undocumented output
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
71 ## @end table
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
72 ## @seealso{lsqnonneg, qp, optimset}
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 ## @end deftypefn
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74
13027
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11588
diff changeset
75 ## 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
76 ## PKG_ADD: [~] = __all_opts__ ("pqpnonneg");
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
77
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
78 ## This is analogical to the lsqnonneg implementation, which is implemented
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
79 ## from Lawson and Hanson's 1973 algorithm on page 161 of Solving Least Squares
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
80 ## Problems. It shares the convergence guarantees.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81
22769
9bd2ea5703e2 lsqnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22764
diff changeset
82 function [x, minval, exitflag, output, lambda] = pqpnonneg (c, d, x0 = [],
9bd2ea5703e2 lsqnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22764
diff changeset
83 options = struct ())
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
85 ## Special case: called to find default optimization options
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
86 if (nargin == 1 && ischar (c) && strcmp (c, "defaults"))
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
87 x = optimset ("MaxIter", 1e5);
17312
088d014a7fe2 Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents: 17281
diff changeset
88 return;
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
89 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
90
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
91 if (nargin < 2 || nargin > 4)
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92 print_usage ();
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
94
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
95 if (! (isnumeric (c) && ismatrix (c)) || ! (isnumeric (d) && ismatrix (d)))
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
96 error ("pqpnonneg: C and D must be numeric matrices");
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
97 endif
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
98 if (! issquare (c))
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
99 error ("pqpnonneg: C must be a square matrix");
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
100 endif
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
101
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
102 if (! isstruct (options))
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
103 error ("pqpnonneg: OPTIONS must be a struct");
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
104 endif
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
105
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
106 ## Lawson-Hanson Step 1 (LH1): initialize the variables.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
107 n = columns (c);
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
108 if (isempty (x0))
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
109 ## Initial guess is all zeros.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
110 x = zeros (n, 1);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
111 else
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
112 ## ensure nonnegative guess.
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
113 x = max (x0, 0);
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
116 max_iter = optimget (options, "MaxIter", 1e5);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
117
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118 ## Initialize P, according to zero pattern of x.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
119 p = find (x > 0).';
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
120 ## Initialize the Cholesky factorization.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 r = chol (c(p, p));
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 usechol = true;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 iter = 0;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126 ## LH3: test for completion.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127 while (iter < max_iter)
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 while (iter < max_iter)
20735
418ae0cb752f Replace ++,-- with in-place operators for performance.
Rik <rik@octave.org>
parents: 20165
diff changeset
129 iter += 1;
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
131 ## LH6: compute the positive matrix and find the min norm solution
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 ## of the positive problem.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 if (usechol)
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134 xtmp = -(r \ (r' \ d(p)));
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 else
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 xtmp = -(c(p,p) \ d(p));
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
137 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138 idx = find (xtmp < 0);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
139
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
140 if (isempty (idx))
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
141 ## LH7: tmp solution found, iterate.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
142 x(:) = 0;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
143 x(p) = xtmp;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
144 break;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145 else
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146 ## LH8, LH9: find the scaling factor.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
147 pidx = p(idx);
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
148 sf = x(pidx) ./ (x(pidx) - xtmp(idx));
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
149 alpha = min (sf);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
150 ## LH10: adjust X.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
151 xx = zeros (n, 1);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
152 xx(p) = xtmp;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
153 x += alpha*(xx - x);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
154 ## LH11: move from P to Z all X == 0.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
155 ## This corresponds to those indices where minimum of sf is attained.
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
156 idx = idx(sf == alpha);
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
157 p(idx) = [];
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
158 if (usechol)
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
159 ## update the Cholesky factorization.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
160 r = choldelete (r, idx);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
161 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
162 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
163 endwhile
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
164
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
165 ## compute the gradient.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
166 w = -(d + c*x);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
167 w(p) = [];
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
168 if (! any (w > 0))
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
169 if (usechol)
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
170 ## verify the solution achieved using qr updating.
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
171 ## In the best case, this should only take a single step.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
172 usechol = false;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
173 continue;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
174 else
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
175 ## we're finished.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
176 break;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
177 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
178 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
179
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
180 ## find the maximum gradient.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
181 idx = find (w == max (w));
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
182 if (numel (idx) > 1)
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
183 warning ("pqpnonneg:nonunique",
11588
d5bd2766c640 style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents: 11587
diff changeset
184 "a non-unique solution may be returned due to equal gradients");
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
185 idx = idx(1);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
186 endif
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 21178
diff changeset
187 ## move the index from Z to P. Keep P sorted.
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
188 z = [1:n]; z(p) = [];
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
189 zidx = z(idx);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
190 jdx = 1 + lookup (p, zidx);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
191 p = [p(1:jdx-1), zidx, p(jdx:end)];
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
192 if (usechol)
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
193 ## insert the column into the Cholesky factorization.
10200
7c1b1c084af1 handle cholesky update failure in pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 9635
diff changeset
194 [r, bad] = cholinsert (r, jdx, c(p,zidx));
7c1b1c084af1 handle cholesky update failure in pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 9635
diff changeset
195 if (bad)
7c1b1c084af1 handle cholesky update failure in pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 9635
diff changeset
196 ## If the insertion failed, we switch off updates and go on.
7c1b1c084af1 handle cholesky update failure in pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 9635
diff changeset
197 usechol = false;
7c1b1c084af1 handle cholesky update failure in pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 9635
diff changeset
198 endif
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
199 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
200
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
201 endwhile
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
202 ## LH12: complete.
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
203
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
204 ## Generate the additional output arguments.
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
205 if (isargout (2))
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
206 minval = 1/2*(x'*c*x) + d'*x;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
207 endif
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
208 if (isargout (3))
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
209 if (iter >= max_iter)
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
210 exitflag = 0;
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
211 else
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
212 exitflag = iter;
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
213 endif
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 endif
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
215 if (isargout (4))
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216 output = struct ("algorithm", "nnls-pqp", "iterations", iter);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
217 endif
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
218 if (isargout (5))
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
219 lambda = zeros (size (x));
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
220 lambda(p) = w;
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
221 endif
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
222
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
223 endfunction
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
224
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
225
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
226 %!test
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
227 %! C = [5 2;2 2];
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
228 %! d = [3; -1];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
229 %! assert (pqpnonneg (C, d), [0;0.5], 100*eps);
9635
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
230
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
231 ## Test equivalence of lsq and pqp
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
232 %!test
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
233 %! C = rand (20, 10);
36d885c4a1ac implement pqpnonneg
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
234 %! d = rand (20, 1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
235 %! assert (pqpnonneg (C'*C, -C'*d), lsqnonneg (C, d), 100*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
236
22764
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
237 # Test input validation
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
238 %!error pqpnonneg ()
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
239 %!error pqpnonneg (1)
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
240 %!error pqpnonneg (1,2,3,4,5)
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
241 %!error <C .* must be numeric matrices> pqpnonneg ({1},2)
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
242 %!error <C .* must be numeric matrices> pqpnonneg (ones (2,2,2),2)
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
243 %!error <D must be numeric matrices> pqpnonneg (1,{2})
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
244 %!error <D must be numeric matrices> pqpnonneg (1, ones (2,2,2))
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
245 %!error <C must be a square matrix> pqpnonneg ([1 2], 2)
d261dcd73dcf pqpnonneg.m: Overhaul function.
Rik <rik@octave.org>
parents: 22760
diff changeset
246 %!error <OPTIONS must be a struct> pqpnonneg (1, 2, [], 3)