Mercurial > octave-antonio
annotate scripts/sparse/spaugment.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | fb1c929dbbb7 |
children | 1bf0ce0930be |
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}) | |
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); | |
8516 | 53 ## a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)], |
54 ## [-1, 0, 1], m, n); | |
7681 | 55 ## x0 = a \ ones (m,1); |
56 ## s = spaugment (a); | |
57 ## [L, U, P, Q] = lu (s); | |
58 ## x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
59 ## x1 = x1(end - n + 1 : end); | |
60 ## @end group | |
61 ## @end example | |
62 ## | |
63 ## To find the solution of an overdetermined problem needs an estimate | |
64 ## of the residual error @var{r} and so it is more complex to formulate | |
65 ## a minimum norm solution using the @code{spaugment} function. | |
66 ## | |
67 ## In general the left division operator is more stable and faster than | |
68 ## using the @code{spaugment} function. | |
69 ## @end deftypefn | |
70 | |
71 function s = spaugment (a, c) | |
72 if (nargin < 2) | |
73 if (issparse (a)) | |
74 c = max (max (abs (a))) / 1000; | |
75 else | |
76 if (ndims (a) != 2) | |
77 error ("spaugment: expecting 2-dimenisional matrix") | |
78 else | |
79 c = max (abs (a(:))) / 1000; | |
80 endif | |
81 endif | |
82 elseif (!isscalar (c)) | |
83 error ("spaugment: c must be a scalar"); | |
84 endif | |
85 | |
86 [m, n] = size (a); | |
87 s = [ c * speye(m, m), a; a', sparse(n, n)]; | |
88 endfunction | |
89 | |
8871 | 90 %!testif HAVE_UMFPACK |
7681 | 91 %! m = 11; n = 10; mn = max(m ,n); |
92 %! a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n); | |
93 %! x0 = a \ ones (m,1); | |
94 %! s = spaugment (a); | |
95 %! [L, U, P, Q] = lu (s); | |
96 %! x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)]))); | |
97 %! x1 = x1(end - n + 1 : end); | |
7687
795be0215bf7
spaugment: reduce test script tolerance
Ben Abbott <bpabbott@mac.com>
parents:
7681
diff
changeset
|
98 %! assert (x1, x0, 1e-6) |