annotate scripts/optimization/lsqnonneg.m @ 20266:83792dd9bcc1

Use in-place operators in m-files where possible. * scripts/audio/@audioplayer/set.m, scripts/audio/@audiorecorder/set.m, scripts/audio/mu2lin.m, scripts/elfun/cosd.m, scripts/general/del2.m, scripts/general/profexplore.m, scripts/general/quadl.m, scripts/general/rat.m, scripts/general/rotdim.m, scripts/help/get_first_help_sentence.m, scripts/help/private/__strip_html_tags__.m, scripts/image/cubehelix.m, scripts/io/textread.m, scripts/linear-algebra/duplication_matrix.m, scripts/linear-algebra/housh.m, scripts/linear-algebra/krylov.m, scripts/linear-algebra/logm.m, scripts/linear-algebra/normest.m, scripts/linear-algebra/onenormest.m, scripts/optimization/fminsearch.m, scripts/optimization/lsqnonneg.m, scripts/optimization/qp.m, scripts/plot/appearance/annotation.m, scripts/plot/appearance/axis.m, scripts/plot/appearance/legend.m, scripts/plot/appearance/specular.m, scripts/plot/draw/colorbar.m, scripts/plot/draw/hist.m, scripts/plot/draw/plotmatrix.m, scripts/plot/draw/private/__stem__.m, scripts/plot/util/__actual_axis_position__.m, scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/findobj.m, scripts/plot/util/print.m, scripts/plot/util/private/__go_draw_axes__.m, scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/rotate.m, scripts/polynomial/pchip.m, scripts/polynomial/polyaffine.m, scripts/polynomial/polyder.m, scripts/polynomial/private/__splinefit__.m, scripts/polynomial/residue.m, scripts/signal/arch_fit.m, scripts/signal/arch_rnd.m, scripts/signal/bartlett.m, scripts/signal/blackman.m, scripts/signal/freqz.m, scripts/signal/hamming.m, scripts/signal/hanning.m, scripts/signal/spectral_adf.m, scripts/signal/spectral_xdf.m, scripts/signal/stft.m, scripts/sparse/bicgstab.m, scripts/sparse/cgs.m, scripts/sparse/private/__sprand_impl__.m, scripts/sparse/qmr.m, scripts/sparse/sprandsym.m, scripts/sparse/svds.m, scripts/specfun/legendre.m, scripts/special-matrix/gallery.m, scripts/statistics/base/gls.m, scripts/statistics/models/logistic_regression.m, scripts/statistics/tests/kruskal_wallis_test.m, scripts/statistics/tests/manova.m, scripts/statistics/tests/wilcoxon_test.m, scripts/time/datevec.m: Use in-place operators in m-files where possible.
author Rik <rik@octave.org>
date Tue, 26 May 2015 21:07:42 -0700
parents f1d0f506ee78
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19105
diff changeset
1 ## Copyright (C) 2008-2015 Bill Denney
8406
0b7566aea627 fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
2 ## Copyright (C) 2008 Jaroslav Hajek
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
3 ## Copyright (C) 2009 VZLU Prague
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
4 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
5 ## This file is part of Octave.
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
6 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
7 ## Octave is free software; you can redistribute it and/or modify it
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
8 ## under the terms of the GNU General Public License as published by
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or (at
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
10 ## your option) any later version.
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
11 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
15 ## General Public License for more details.
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
16 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public License
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
18 ## along with Octave; see the file COPYING. If not, see
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
19 ## <http://www.gnu.org/licenses/>.
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
20
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
21 ## -*- texinfo -*-
10793
be55736a0783 Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents: 9635
diff changeset
22 ## @deftypefn {Function File} {@var{x} =} lsqnonneg (@var{c}, @var{d})
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
23 ## @deftypefnx {Function File} {@var{x} =} lsqnonneg (@var{c}, @var{d}, @var{x0})
14787
acb09716fc94 lsqnonneg have tolerance option for convergence (bug #33347)
Axel Mathéi <axel.mathei@gmail.com>
parents: 14366
diff changeset
24 ## @deftypefnx {Function File} {@var{x} =} lsqnonneg (@var{c}, @var{d}, @var{x0}, @var{options})
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
25 ## @deftypefnx {Function File} {[@var{x}, @var{resnorm}] =} lsqnonneg (@dots{})
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
26 ## @deftypefnx {Function File} {[@var{x}, @var{resnorm}, @var{residual}] =} lsqnonneg (@dots{})
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
27 ## @deftypefnx {Function File} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}] =} lsqnonneg (@dots{})
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
28 ## @deftypefnx {Function File} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}, @var{output}] =} lsqnonneg (@dots{})
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
29 ## @deftypefnx {Function File} {[@var{x}, @var{resnorm}, @var{residual}, @var{exitflag}, @var{output}, @var{lambda}] =} lsqnonneg (@dots{})
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
30 ## Minimize @code{norm (@var{c}*@var{x} - d)} subject to
20200
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
31 ## @code{@var{x} >= 0}.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
32 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
33 ## @var{c} and @var{d} must be real.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
34 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
35 ## @var{x0} is an optional initial guess for @var{x}.
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
36 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
37 ## Currently, @code{lsqnonneg} recognizes these options: @qcode{"MaxIter"},
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
38 ## @qcode{"TolX"}. For a description of these options, see
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
39 ## @ref{XREFoptimset,,optimset}.
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
40 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
41 ## Outputs:
14366
b76f0740940e doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
42 ##
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
43 ## @itemize @bullet
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
44 ## @item resnorm
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
45 ##
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14787
diff changeset
46 ## The squared 2-norm of the residual: norm (@var{c}*@var{x}-@var{d})^2
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
47 ##
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
48 ## @item residual
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
49 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
50 ## The residual: @var{d}-@var{c}*@var{x}
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
51 ##
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
52 ## @item exitflag
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
53 ##
20200
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
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: 19867
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: 19867
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: 19867
diff changeset
57 ## enough iterations.)
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
58 ##
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
59 ## @item output
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
60 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
61 ## A structure with two fields:
14366
b76f0740940e doc: Periodic grammar check of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
62 ##
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
63 ## @itemize @bullet
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17097
diff changeset
64 ## @item @qcode{"algorithm"}: The algorithm used (@qcode{"nnls"})
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
65 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17097
diff changeset
66 ## @item @qcode{"iterations"}: The number of iterations taken.
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
67 ## @end itemize
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
68 ##
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
69 ## @item lambda
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
70 ##
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
71 ## Not implemented.
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
72 ## @end itemize
19105
c9113e28fae8 New lscov function (bug #43118)
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents: 17744
diff changeset
73 ## @seealso{optimset, pqpnonneg, lscov}
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
74 ## @end deftypefn
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
75
13027
b9a89ca0fb75 prevent optimization functions from setting ans in workspace at startup
John W. Eaton <jwe@octave.org>
parents: 11588
diff changeset
76 ## 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
77 ## 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
78
7697
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
79 ## This is implemented from Lawson and Hanson's 1973 algorithm on page
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
80 ## 161 of Solving Least Squares Problems.
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
81
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
82 function [x, resnorm, residual, exitflag, output, lambda] = lsqnonneg (c, d, x = [], options = struct ())
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
83
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
84 if (nargin == 1 && ischar (c) && strcmp (c, 'defaults'))
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
85 x = optimset ("MaxIter", 1e5);
17312
088d014a7fe2 Use semicolon after "return" statement in core m-files.
Rik <rik@octave.org>
parents: 17281
diff changeset
86 return;
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
87 endif
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
88
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
89 if (nargin < 2 || nargin > 4
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
90 || ! (ismatrix (c) && ismatrix (d) && isstruct (options)))
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
91 print_usage ();
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
92 endif
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
93
7697
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
94 ## Lawson-Hanson Step 1 (LH1): initialize the variables.
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
95 m = rows (c);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
96 n = columns (c);
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
97 if (isempty (x))
7697
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
98 ## Initial guess is 0s.
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
99 x = zeros (n, 1);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
100 else
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
101 ## ensure nonnegative guess.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
102 x = max (x, 0);
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
103 endif
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
104
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
105 useqr = m >= n;
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8407
diff changeset
106 max_iter = optimget (options, "MaxIter", 1e5);
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
107
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
108 ## Initialize P, according to zero pattern of x.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
109 p = find (x > 0).';
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
110 if (useqr)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
111 ## Initialize the QR factorization, economized form.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
112 [q, r] = qr (c(:,p), 0);
8406
0b7566aea627 fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
113 endif
0b7566aea627 fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
114
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
115 iter = 0;
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
116
7697
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
117 ## LH3: test for completion.
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
118 while (iter < max_iter)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
119 while (iter < max_iter)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
120 iter++;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
121
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
122 ## LH6: compute the positive matrix and find the min norm solution
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
123 ## of the positive problem.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
124 if (useqr)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
125 xtmp = r \ q'*d;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
126 else
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
127 xtmp = c(:,p) \ d;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
128 endif
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
129 idx = find (xtmp < 0);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
130
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
131 if (isempty (idx))
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
132 ## LH7: tmp solution found, iterate.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
133 x(:) = 0;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
134 x(p) = xtmp;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
135 break;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
136 else
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
137 ## LH8, LH9: find the scaling factor.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
138 pidx = p(idx);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
139 sf = x(pidx)./(x(pidx) - xtmp(idx));
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
140 alpha = min (sf);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
141 ## LH10: adjust X.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
142 xx = zeros (n, 1);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
143 xx(p) = xtmp;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
144 x += alpha*(xx - x);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
145 ## LH11: move from P to Z all X == 0.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
146 ## This corresponds to those indices where minimum of sf is attained.
20266
83792dd9bcc1 Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents: 20200
diff changeset
147 idx = idx(sf == alpha);
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
148 p(idx) = [];
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
149 if (useqr)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
150 ## update the QR factorization.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
151 [q, r] = qrdelete (q, r, idx);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
152 endif
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
153 endif
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
154 endwhile
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
155
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
156 ## compute the gradient.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
157 w = c'*(d - c*x);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
158 w(p) = [];
14787
acb09716fc94 lsqnonneg have tolerance option for convergence (bug #33347)
Axel Mathéi <axel.mathei@gmail.com>
parents: 14366
diff changeset
159 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
160 if (! any (w > tolx))
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
161 if (useqr)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
162 ## verify the solution achieved using qr updating.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
163 ## in the best case, this should only take a single step.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
164 useqr = false;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
165 continue;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
166 else
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
167 ## we're finished.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
168 break;
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
169 endif
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
170 endif
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
171
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
172 ## find the maximum gradient.
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
173 idx = find (w == max (w));
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
174 if (numel (idx) > 1)
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
175 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
176 "a non-unique solution may be returned due to equal gradients");
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
177 idx = idx(1);
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
178 endif
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
179 ## move the index from Z to P. Keep P sorted.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
180 z = [1:n]; z(p) = [];
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
181 zidx = z(idx);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
182 jdx = 1 + lookup (p, zidx);
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
183 p = [p(1:jdx-1), zidx, p(jdx:end)];
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
184 if (useqr)
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
185 ## insert the column into the QR factorization.
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
186 [q, r] = qrinsert (q, r, jdx, c(:,zidx));
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
187 endif
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
188
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
189 endwhile
7697
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
190 ## LH12: complete.
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
191
7697
0bdfff62cc49 lsqnonneg: use optimset, correctly index Z and P in main loop
bill@denney.ws
parents: 7682
diff changeset
192 ## Generate the additional output arguments.
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
193 if (nargout > 1)
8406
0b7566aea627 fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
194 resnorm = norm (c*x - d) ^ 2;
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
195 endif
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
196 if (nargout > 2)
8406
0b7566aea627 fix & optimize lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8304
diff changeset
197 residual = d - c*x;
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
198 endif
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
199 exitflag = iter;
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 8407
diff changeset
200 if (nargout > 3 && iter >= max_iter)
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
201 exitflag = 0;
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
202 endif
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
203 if (nargout > 4)
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
204 output = struct ("algorithm", "nnls", "iterations", iter);
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
205 endif
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
206 if (nargout > 5)
8600
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
207 lambda = zeros (size (x));
a6c1aa6f5915 improve lsqnonneg
Jaroslav Hajek <highegg@gmail.com>
parents: 8507
diff changeset
208 lambda(p) = w;
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
209 endif
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
210
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
211 endfunction
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
212
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
213
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
214 %!test
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
215 %! C = [1 0;0 1;2 1];
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
216 %! 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
217 %! assert (lsqnonneg (C, d), [0;0.5], 100*eps);
7682
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
218
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
219 %!test
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
220 %! C = [0.0372 0.2869;0.6861 0.7071;0.6233 0.6245;0.6344 0.6170];
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
221 %! d = [0.8587;0.1781;0.0747;0.8405];
f7474c83c782 lsqnonneg: new function
bill@denney.ws
parents:
diff changeset
222 %! 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
223 %! 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
224