1 ## Copyright (C) 2008-2012 David Bateman
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {@var{s} =} spaugment (@var{A}, @var{c})
21 ## Create the augmented matrix of @var{A}. This is given by
25 ## [@var{c} * eye(@var{m}, @var{m}), @var{A};
26 ## @var{A}', zeros(@var{n}, @var{n})]
31 ## This is related to the least squares solution of
32 ## @code{@var{A} \ @var{b}}, by
36 ## @var{s} * [ @var{r} / @var{c}; x] = [ @var{b}, zeros(@var{n}, columns(@var{b})) ]
41 ## where @var{r} is the residual error
44 ## @var{r} = @var{b} - @var{A} * @var{x}
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
55 ## m = 11; n = 10; mn = max (m, n);
56 ## A = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],
58 ## x0 = A \ ones (m,1);
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);
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.
70 ## In general the left division operator is more stable and faster than
71 ## using the @code{spaugment} function.
74 function s = spaugment (A, c)
77 c = max (max (abs (A))) / 1000;
80 error ("spaugment: expecting 2-dimenisional matrix");
82 c = max (abs (A(:))) / 1000;
85 elseif (!isscalar (c))
86 error ("spaugment: C must be a scalar");
90 s = [ c * speye(m, m), A; A', sparse(n, n)];
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);
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)