Mercurial > hg > octave-nkf
annotate scripts/sparse/spaugment.m @ 14327:4d917a6a858b stable
doc: Use Octave coding conventions in @example blocks of docstrings.
* accumarray.m, accumdim.m, bar.m, base2dec.m, bincoeff.m, bitcmp.m, bitset.m,
celldisp.m, chop.m, clabel.m, cloglog.m, colon.m, compass.m, computer.m,
contour3.m, contourc.m, corr.m, cstrcat.m, ctime.m, cylinder.m, date.m,
dec2base.m, demo.m, dir.m, dlmwrite.m, expm.m, ezcontourf.m, ezcontour.m,
ezmeshc.m, ezmesh.m, ezplot.m, ezsurfc.m, ezsurf.m, feather.m, findobj.m,
flipdim.m, fplot.m, genvarname.m, getfield.m, hankel.m, hilb.m, hist.m,
idivide.m, index.m, int2str.m, interp1.m, is_leap_year.m, ismember.m,
isocolors.m, isonormals.m, isosurface.m, kurtosis.m, legendre.m, linkprop.m,
logit.m, logm.m, __makeinfo__.m, __marching_cube__.m, median.m, mkoctfile.m,
moment.m, mpoles.m, orderfields.m, pcg.m, pcr.m, plot3.m, plotmatrix.m,
polyaffine.m, polygcd.m, poly.m, polyout.m, print.m, qp.m, quadgk.m, qzhess.m,
randi.m, rat.m, refreshdata.m, residue.m, rose.m, rot90.m, saveas.m, saveobj.m,
shiftdim.m, skewness.m, spaugment.m, spdiags.m, sqp.m, stem.m, str2num.m,
strcat.m, strjust.m, strread.m, strsplit.m, structfun.m, subplot.m,
subsindex.m, substruct.m, surfl.m, surfnorm.m, svds.m, uimenu.m, union.m,
voronoi.m, warning_ids.m, wblpdf.m: Use Octave coding conventions in
@example blocks of docstrings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 04 Feb 2012 22:12:50 -0800 |
parents | 72c96de7a403 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12575
diff
changeset
|
1 ## Copyright (C) 2008-2012 David Bateman |
7681 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {@var{s} =} spaugment (@var{A}, @var{c}) |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11589
diff
changeset
|
21 ## Create the augmented matrix of @var{A}. This is given by |
7681 | 22 ## |
23 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
25 ## [@var{c} * eye(@var{m}, @var{m}), @var{A}; |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
26 ## @var{A}', zeros(@var{n}, @var{n})] |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## @end group |
7681 | 28 ## @end example |
29 ## | |
30 ## @noindent | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
31 ## This is related to the least squares solution of |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
32 ## @code{@var{A} \ @var{b}}, by |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
33 ## |
7681 | 34 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
36 ## @var{s} * [ @var{r} / @var{c}; x] = [ @var{b}, zeros(@var{n}, columns(@var{b})) ] |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
37 ## @end group |
7681 | 38 ## @end example |
39 ## | |
40 ## @noindent | |
41 ## where @var{r} is the residual error | |
42 ## | |
43 ## @example | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
44 ## @var{r} = @var{b} - @var{A} * @var{x} |
7681 | 45 ## @end example |
46 ## | |
47 ## As the matrix @var{s} is symmetric indefinite it can be factorized | |
48 ## with @code{lu}, and the minimum norm solution can therefore be found | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
49 ## without the need for a @code{qr} factorization. As the residual |
7681 | 50 ## error will be @code{zeros (@var{m}, @var{m})} for under determined |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 ## problems, and example can be |
7681 | 52 ## |
53 ## @example | |
54 ## @group | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 ## m = 11; n = 10; mn = max (m, n); |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
56 ## A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)], |
8516 | 57 ## [-1, 0, 1], m, n); |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
58 ## x0 = A \ ones (m,1); |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
59 ## s = spaugment (A); |
7681 | 60 ## [L, U, P, Q] = lu (s); |
61 ## x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
62 ## x1 = x1(end - n + 1 : end); | |
63 ## @end group | |
64 ## @end example | |
65 ## | |
66 ## To find the solution of an overdetermined problem needs an estimate | |
67 ## of the residual error @var{r} and so it is more complex to formulate | |
68 ## a minimum norm solution using the @code{spaugment} function. | |
69 ## | |
70 ## In general the left division operator is more stable and faster than | |
71 ## using the @code{spaugment} function. | |
72 ## @end deftypefn | |
73 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
74 function s = spaugment (A, c) |
7681 | 75 if (nargin < 2) |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
76 if (issparse (A)) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
77 c = max (max (abs (A))) / 1000; |
7681 | 78 else |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
79 if (ndims (A) != 2) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
80 error ("spaugment: expecting 2-dimenisional matrix"); |
7681 | 81 else |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
82 c = max (abs (A(:))) / 1000; |
7681 | 83 endif |
84 endif | |
85 elseif (!isscalar (c)) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11471
diff
changeset
|
86 error ("spaugment: C must be a scalar"); |
7681 | 87 endif |
88 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
89 [m, n] = size (A); |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
90 s = [ c * speye(m, m), A; A', sparse(n, n)]; |
7681 | 91 endfunction |
92 | |
8871 | 93 %!testif HAVE_UMFPACK |
7681 | 94 %! m = 11; n = 10; mn = max(m ,n); |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
95 %! A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n); |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
96 %! x0 = A \ ones (m,1); |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
97 %! s = spaugment (A); |
7681 | 98 %! [L, U, P, Q] = lu (s); |
99 %! x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
100 %! x1 = x1(end - n + 1 : end); | |
7687
795be0215bf7
spaugment: reduce test script tolerance
Ben Abbott <bpabbott@mac.com>
parents:
7681
diff
changeset
|
101 %! assert (x1, x0, 1e-6) |