Mercurial > hg > octave-nkf
annotate scripts/sparse/spaugment.m @ 11472:1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 09 Jan 2011 21:33:04 -0800 |
parents | 994e2a93a8e2 |
children | fd0a3ac60b0e |
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 -*- | |
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}) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
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 |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
25 ## [@var{c} * eye(@var{m}, @var{m}),@var{A}; @var{A}', zeros(@var{n}, |
7681 | 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 | |
9066
be150a172010
Cleanup documentation for diagperm.texi, sparse.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
31 ## This is related to the least squares solution of |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
32 ## @code{@var{A} \\ @var{b}}, by |
7681 | 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 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 ## @var{r} = @var{b} - @var{A} * @var{x} |
7681 | 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); | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
57 ## A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)], |
8516 | 58 ## [-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
|
59 ## 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
|
60 ## s = spaugment (A); |
7681 | 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 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
75 function s = spaugment (A, c) |
7681 | 76 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
|
77 if (issparse (A)) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
78 c = max (max (abs (A))) / 1000; |
7681 | 79 else |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
80 if (ndims (A) != 2) |
10549 | 81 error ("spaugment: expecting 2-dimenisional matrix") |
7681 | 82 else |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
83 c = max (abs (A(:))) / 1000; |
7681 | 84 endif |
85 endif | |
86 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
|
87 error ("spaugment: C must be a scalar"); |
7681 | 88 endif |
89 | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
90 [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
|
91 s = [ c * speye(m, m), A; A', sparse(n, n)]; |
7681 | 92 endfunction |
93 | |
8871 | 94 %!testif HAVE_UMFPACK |
7681 | 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); | |
7687
795be0215bf7
spaugment: reduce test script tolerance
Ben Abbott <bpabbott@mac.com>
parents:
7681
diff
changeset
|
102 %! assert (x1, x0, 1e-6) |