annotate scripts/linear-algebra/subspace.m @ 8920:eb63fbe60fab

update copyright notices
author John W. Eaton <jwe@octave.org>
date Sat, 07 Mar 2009 10:41:27 -0500
parents e07e93c04080
children c776f063fefe
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: 8664
diff changeset
1 ## Copyright (C) 2008, 2009 VZLU Prague, a.s., Czech Republic
7611
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ##
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 ##
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{angle} =} subspace (@var{a}, @var{B})
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 ## Determine the largest principal angle between two subspaces
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22 ## spanned by columns of matrices @var{a} and @var{b}.
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23 ## @end deftypefn
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 ## Author: Jaroslav Hajek <highegg@gmail.com>
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27 ## reference:
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 ## [1] Andrew V. Knyazev, Merico E. Argentati:
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 ## Principal Angles between Subspaces in an A-Based Scalar Product:
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 ## Algorithms and Perturbation Estimates.
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
31 ## SIAM Journal on Scientific Computing, Vol. 23 no. 6, pp. 2008-2040
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32 ##
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 ## other texts are also around...
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 function ang = subspace (a, b)
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36
7612
c1702f963a5e error check for subspace.m
Jaroslav Hajek <highegg@gmail.com>
parents: 7611
diff changeset
37 if (nargin != 2)
c1702f963a5e error check for subspace.m
Jaroslav Hajek <highegg@gmail.com>
parents: 7611
diff changeset
38 print_usage ();
c1702f963a5e error check for subspace.m
Jaroslav Hajek <highegg@gmail.com>
parents: 7611
diff changeset
39 elseif (ndims (a) != 2 || ndims (b) != 2)
c1702f963a5e error check for subspace.m
Jaroslav Hajek <highegg@gmail.com>
parents: 7611
diff changeset
40 error ("subspace: expecting A and B to be 2-dimensional arrays");
7616
fb4fa07bc364 more checks for subspace
Jaroslav Hajek <highegg@gmail.com>
parents: 7612
diff changeset
41 elseif (rows (a) != rows (b))
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7616
diff changeset
42 error ("subspace: column dimensions of a and b must match");
7612
c1702f963a5e error check for subspace.m
Jaroslav Hajek <highegg@gmail.com>
parents: 7611
diff changeset
43 endif
c1702f963a5e error check for subspace.m
Jaroslav Hajek <highegg@gmail.com>
parents: 7611
diff changeset
44
7611
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 a = orth (a);
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 b = orth (b);
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
47 c = a'*b;
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
48 scos = min (svd (c));
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
49 if (scos^2 > 1/2)
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
50 if (columns (a) >= columns (b))
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 c = b - a*c;
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52 else
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 c = a - b*c';
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 endif
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 ssin = max (svd (c));
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
56 ang = asin (min (ssin, 1));
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
57 else
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58 ang = acos (scos);
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 endif
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60
4f903c303c3c implement subspace function
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
61 endfunction