]> Creatis software - CreaPhase.git/blob - octave_packages/m/sparse/spaugment.m
update packages
[CreaPhase.git] / octave_packages / m / sparse / spaugment.m
1 ## Copyright (C) 2008-2012 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 ## Create the augmented matrix of @var{A}.  This is given by
22 ##
23 ## @example
24 ## @group
25 ## [@var{c} * eye(@var{m}, @var{m}), @var{A};
26 ##             @var{A}', zeros(@var{n}, @var{n})]
27 ## @end group
28 ## @end example
29 ##
30 ## @noindent
31 ## This is related to the least squares solution of
32 ## @code{@var{A} \ @var{b}}, by
33 ##
34 ## @example
35 ## @group
36 ## @var{s} * [ @var{r} / @var{c}; x] = [ @var{b}, zeros(@var{n}, columns(@var{b})) ]
37 ## @end group
38 ## @end example
39 ##
40 ## @noindent
41 ## where @var{r} is the residual error
42 ##
43 ## @example
44 ## @var{r} = @var{b} - @var{A} * @var{x}
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
49 ## without the need for a @code{qr} factorization.  As the residual
50 ## error will be @code{zeros (@var{m}, @var{m})} for under determined
51 ## problems, and example can be
52 ##
53 ## @example
54 ## @group
55 ## m = 11; n = 10; mn = max (m, n);
56 ## A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],
57 ##              [-1, 0, 1], m, n);
58 ## x0 = A \ ones (m,1);
59 ## s = spaugment (A);
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
74 function s = spaugment (A, c)
75   if (nargin < 2)
76     if (issparse (A))
77       c = max (max (abs (A))) / 1000;
78     else
79       if (ndims (A) != 2)
80         error ("spaugment: expecting 2-dimenisional matrix");
81       else
82         c = max (abs (A(:))) / 1000;
83       endif
84     endif
85   elseif (!isscalar (c))
86     error ("spaugment: C must be a scalar");
87   endif
88
89   [m, n] = size (A);
90   s = [ c * speye(m, m), A; A', sparse(n, n)];
91 endfunction
92
93 %!testif HAVE_UMFPACK
94 %! m = 11; n = 10; mn = max(m ,n);
95 %! A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n);
96 %! x0 = A \ ones (m,1);
97 %! s = spaugment (A);
98 %! [L, U, P, Q] = lu (s);
99 %! x1 = Q * (U \ (L \ (P  * [ones(m,1); zeros(n,1)])));
100 %! x1 = x1(end - n + 1 : end);
101 %! assert (x1, x0, 1e-6)