Mercurial > hg > octave-nkf
annotate scripts/sparse/spaugment.m @ 7784:d3a7882fa0b3
style fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 20 May 2008 16:24:50 -0400 |
parents | 795be0215bf7 |
children | e2a179415bac |
rev | line source |
---|---|
7681 | 1 ## Copyright (C) 2008 David Bateman |
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}) | |
21 ## Creates the augmented matrix of @var{a}. This is given by | |
22 ## | |
23 ## @example | |
24 ## [@var{c} * eye(@var{m}, @var{m}),@var{a}; @var{a}', zeros(@var{n}, | |
25 ## @var{n})] | |
26 ## @end example | |
27 ## | |
28 ## @noindent | |
29 ## This is related to the leasted squared solution of | |
30 ## @code{@var{a} \\ @var{b}}, by | |
31 ## | |
32 ## @example | |
33 ## @var{s} * [ @var{r} / @var{c}; x] = [@var{b}, zeros(@var{n}, | |
34 ## columns(@var{b})] | |
35 ## @end example | |
36 ## | |
37 ## @noindent | |
38 ## where @var{r} is the residual error | |
39 ## | |
40 ## @example | |
41 ## @var{r} = @var{b} - @var{a} * @var{x} | |
42 ## @end example | |
43 ## | |
44 ## As the matrix @var{s} is symmetric indefinite it can be factorized | |
45 ## with @code{lu}, and the minimum norm solution can therefore be found | |
46 ## without the need for a @code{qr} factorization. As the residual | |
47 ## error will be @code{zeros (@var{m}, @var{m})} for under determined | |
48 ## problems, and example can be | |
49 ## | |
50 ## @example | |
51 ## @group | |
52 ## m = 11; n = 10; mn = max(m ,n); | |
53 ## a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n); | |
54 ## x0 = a \ ones (m,1); | |
55 ## s = spaugment (a); | |
56 ## [L, U, P, Q] = lu (s); | |
57 ## x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
58 ## x1 = x1(end - n + 1 : end); | |
59 ## @end group | |
60 ## @end example | |
61 ## | |
62 ## To find the solution of an overdetermined problem needs an estimate | |
63 ## of the residual error @var{r} and so it is more complex to formulate | |
64 ## a minimum norm solution using the @code{spaugment} function. | |
65 ## | |
66 ## In general the left division operator is more stable and faster than | |
67 ## using the @code{spaugment} function. | |
68 ## @end deftypefn | |
69 | |
70 function s = spaugment (a, c) | |
71 if (nargin < 2) | |
72 if (issparse (a)) | |
73 c = max (max (abs (a))) / 1000; | |
74 else | |
75 if (ndims (a) != 2) | |
76 error ("spaugment: expecting 2-dimenisional matrix") | |
77 else | |
78 c = max (abs (a(:))) / 1000; | |
79 endif | |
80 endif | |
81 elseif (!isscalar (c)) | |
82 error ("spaugment: c must be a scalar"); | |
83 endif | |
84 | |
85 [m, n] = size (a); | |
86 s = [ c * speye(m, m), a; a', sparse(n, n)]; | |
87 endfunction | |
88 | |
89 %!test | |
90 %! m = 11; n = 10; mn = max(m ,n); | |
91 %! a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n); | |
92 %! x0 = a \ ones (m,1); | |
93 %! s = spaugment (a); | |
94 %! [L, U, P, Q] = lu (s); | |
95 %! x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
96 %! x1 = x1(end - n + 1 : end); | |
7687
795be0215bf7
spaugment: reduce test script tolerance
Ben Abbott <bpabbott@mac.com>
parents:
7681
diff
changeset
|
97 %! assert (x1, x0, 1e-6) |