Mercurial > hg > octave-lyh
annotate scripts/sparse/spaugment.m @ 17535:c12c688a35ed default tip lyh
Fix warnings
author | LYH <lyh.kernel@gmail.com> |
---|---|
date | Fri, 27 Sep 2013 17:43:27 +0800 |
parents | f3d52523cde1 |
children |
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 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
93 |
8871 | 94 %!testif HAVE_UMFPACK |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
95 %! 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
|
96 %! 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
|
97 %! 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
|
98 %! s = spaugment (A); |
7681 | 99 %! [L, U, P, Q] = lu (s); |
100 %! x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
101 %! x1 = x1(end - n + 1 : end); | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
102 %! assert (x1, x0, 1e-6); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
103 |