Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/null.m @ 10509:ddbd812d09aa
properly compress sparse matrices after assembly
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Apr 2010 12:57:44 +0200 |
parents | eb63fbe60fab |
children | 95c3e38098bf |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2003, 2005, 2006, |
8920 | 2 ## 2007, 2008 John W. Eaton |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
1026 | 19 |
3372 | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} null (@var{a}, @var{tol}) | |
22 ## Return an orthonormal basis of the null space of @var{a}. | |
3426 | 23 ## |
2311 | 24 ## The dimension of the null space is taken as the number of singular |
3372 | 25 ## values of @var{a} not greater than @var{tol}. If the argument @var{tol} |
26 ## is missing, it is computed as | |
3426 | 27 ## |
3372 | 28 ## @example |
29 ## max (size (@var{a})) * max (svd (@var{a})) * eps | |
30 ## @end example | |
31 ## @end deftypefn | |
557 | 32 |
5428 | 33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 34 ## Created: 24 December 1993. |
35 ## Adapted-By: jwe | |
557 | 36 |
2312 | 37 function retval = null (A, tol) |
557 | 38 |
4371 | 39 if (isempty (A)) |
40 retval = []; | |
41 else | |
42 [U, S, V] = svd (A); | |
1065 | 43 |
4371 | 44 [rows, cols] = size (A); |
45 | |
46 [S_nr, S_nc] = size (S); | |
557 | 47 |
4371 | 48 if (S_nr == 1 || S_nc == 1) |
49 s = S(1); | |
50 else | |
51 s = diag (S); | |
52 endif | |
557 | 53 |
4371 | 54 if (nargin == 1) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
55 if (isa (A, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
56 tol = max (size (A)) * s (1) * eps ("single"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
57 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
58 tol = max (size (A)) * s (1) * eps; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
59 endif |
4371 | 60 elseif (nargin != 2) |
6046 | 61 print_usage (); |
4371 | 62 endif |
557 | 63 |
4371 | 64 rank = sum (s > tol); |
65 | |
66 if (rank < cols) | |
67 retval = V (:, rank+1:cols); | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
68 if (isa (A, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
69 retval(abs (retval) < eps ("single")) = 0; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
70 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
71 retval(abs (retval) < eps) = 0; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
72 endif |
4371 | 73 else |
74 retval = zeros (cols, 0); | |
75 endif | |
557 | 76 endif |
77 | |
78 endfunction |