Mercurial > hg > octave-lyh
annotate scripts/sparse/spaugment.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | be150a172010 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 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 -*- | |
20 ## @deftypefn {Function File} {@var{s} =} spaugment (@var{a}, @var{c}) | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
21 ## Creates 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 |
7681 | 25 ## [@var{c} * eye(@var{m}, @var{m}),@var{a}; @var{a}', zeros(@var{n}, |
26 ## @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 | |
31 ## This is related to the leasted squared solution of | |
32 ## @code{@var{a} \\ @var{b}}, by | |
33 ## | |
34 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## @group |
7681 | 36 ## @var{s} * [ @var{r} / @var{c}; x] = [@var{b}, zeros(@var{n}, |
37 ## columns(@var{b})] | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## @end group |
7681 | 39 ## @end example |
40 ## | |
41 ## @noindent | |
42 ## where @var{r} is the residual error | |
43 ## | |
44 ## @example | |
45 ## @var{r} = @var{b} - @var{a} * @var{x} | |
46 ## @end example | |
47 ## | |
48 ## As the matrix @var{s} is symmetric indefinite it can be factorized | |
49 ## 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
|
50 ## without the need for a @code{qr} factorization. As the residual |
7681 | 51 ## error will be @code{zeros (@var{m}, @var{m})} for under determined |
52 ## problems, and example can be | |
53 ## | |
54 ## @example | |
55 ## @group | |
56 ## m = 11; n = 10; mn = max(m ,n); | |
8516 | 57 ## a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)], |
58 ## [-1, 0, 1], m, n); | |
7681 | 59 ## x0 = a \ ones (m,1); |
60 ## s = spaugment (a); | |
61 ## [L, U, P, Q] = lu (s); | |
62 ## x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
63 ## x1 = x1(end - n + 1 : end); | |
64 ## @end group | |
65 ## @end example | |
66 ## | |
67 ## To find the solution of an overdetermined problem needs an estimate | |
68 ## of the residual error @var{r} and so it is more complex to formulate | |
69 ## a minimum norm solution using the @code{spaugment} function. | |
70 ## | |
71 ## In general the left division operator is more stable and faster than | |
72 ## using the @code{spaugment} function. | |
73 ## @end deftypefn | |
74 | |
75 function s = spaugment (a, c) | |
76 if (nargin < 2) | |
77 if (issparse (a)) | |
78 c = max (max (abs (a))) / 1000; | |
79 else | |
80 if (ndims (a) != 2) | |
81 error ("spaugment: expecting 2-dimenisional matrix") | |
82 else | |
83 c = max (abs (a(:))) / 1000; | |
84 endif | |
85 endif | |
86 elseif (!isscalar (c)) | |
87 error ("spaugment: c must be a scalar"); | |
88 endif | |
89 | |
90 [m, n] = size (a); | |
91 s = [ c * speye(m, m), a; a', sparse(n, n)]; | |
92 endfunction | |
93 | |
8871 | 94 %!testif HAVE_UMFPACK |
7681 | 95 %! m = 11; n = 10; mn = max(m ,n); |
96 %! a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n); | |
97 %! x0 = a \ ones (m,1); | |
98 %! s = spaugment (a); | |
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); | |
7687
795be0215bf7
spaugment: reduce test script tolerance
Ben Abbott <bpabbott@mac.com>
parents:
7681
diff
changeset
|
102 %! assert (x1, x0, 1e-6) |