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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8871
diff changeset
1 ## Copyright (C) 2008, 2009 David Bateman
7681
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
2 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
3 ## This file is part of Octave.
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
4 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 ## your option) any later version.
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ## General Public License for more details.
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
18
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 ## -*- texinfo -*-
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{s} =} spaugment (@var{a}, @var{c})
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
21 ## Creates the augmented matrix of @var{a}. This is given by
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
22 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
23 ## @example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
24 ## [@var{c} * eye(@var{m}, @var{m}),@var{a}; @var{a}', zeros(@var{n},
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
25 ## @var{n})]
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
26 ## @end example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
27 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
28 ## @noindent
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
29 ## This is related to the leasted squared solution of
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 ## @code{@var{a} \\ @var{b}}, by
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 ## @example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
33 ## @var{s} * [ @var{r} / @var{c}; x] = [@var{b}, zeros(@var{n},
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
34 ## columns(@var{b})]
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
35 ## @end example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
37 ## @noindent
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 ## where @var{r} is the residual error
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
39 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 ## @example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 ## @var{r} = @var{b} - @var{a} * @var{x}
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 ## @end example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 ## As the matrix @var{s} is symmetric indefinite it can be factorized
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 ## with @code{lu}, and the minimum norm solution can therefore be found
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
46 ## without the need for a @code{qr} factorization. As the residual
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 ## error will be @code{zeros (@var{m}, @var{m})} for under determined
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
48 ## problems, and example can be
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
49 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 ## @example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
51 ## @group
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 ## m = 11; n = 10; mn = max(m ,n);
8516
e2a179415bac doc fixes
John W. Eaton <jwe@octave.org>
parents: 7687
diff changeset
53 ## a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],
e2a179415bac doc fixes
John W. Eaton <jwe@octave.org>
parents: 7687
diff changeset
54 ## [-1, 0, 1], m, n);
7681
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 ## x0 = a \ ones (m,1);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
56 ## s = spaugment (a);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
57 ## [L, U, P, Q] = lu (s);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
58 ## x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)])));
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
59 ## x1 = x1(end - n + 1 : end);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
60 ## @end group
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
61 ## @end example
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
62 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
63 ## To find the solution of an overdetermined problem needs an estimate
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
64 ## of the residual error @var{r} and so it is more complex to formulate
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 ## a minimum norm solution using the @code{spaugment} function.
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
66 ##
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 ## In general the left division operator is more stable and faster than
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
68 ## using the @code{spaugment} function.
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
69 ## @end deftypefn
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
70
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
71 function s = spaugment (a, c)
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
72 if (nargin < 2)
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
73 if (issparse (a))
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
74 c = max (max (abs (a))) / 1000;
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
75 else
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
76 if (ndims (a) != 2)
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
77 error ("spaugment: expecting 2-dimenisional matrix")
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
78 else
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 c = max (abs (a(:))) / 1000;
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
80 endif
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
81 endif
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
82 elseif (!isscalar (c))
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
83 error ("spaugment: c must be a scalar");
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
84 endif
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
85
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
86 [m, n] = size (a);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
87 s = [ c * speye(m, m), a; a', sparse(n, n)];
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
88 endfunction
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
89
8871
fb1c929dbbb7 tests vs. 64-bit indexing
John W. Eaton <jwe@octave.org>
parents: 8516
diff changeset
90 %!testif HAVE_UMFPACK
7681
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
91 %! m = 11; n = 10; mn = max(m ,n);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
92 %! a = spdiags ([ones(mn,1), 10*ones(mn,1), -ones(mn,1)],[-1,0,1], m, n);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
93 %! x0 = a \ ones (m,1);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
94 %! s = spaugment (a);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
95 %! [L, U, P, Q] = lu (s);
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
96 %! x1 = Q * (U \ (L \ (P * [ones(m,1); zeros(n,1)])));
b1c1133641ee Add the spaugment function
David Bateman <dbateman@free.fr>
parents:
diff changeset
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)