Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/null.m @ 20815:a260a6acb70f
fix test failures introduced by a22d8a2eb0e5
* scripts/ode/private/integrate_adaptive.m: fix stepping backwards, fix
invocation of OutputFcn, fix text of some error messages
* scripts/ode/private/integrate_const.m: remove use of option OutputSave
* scripts/ode/private/integrate_n_steps.m: remove use of option OutputSave
author | Carlo de Falco <carlo.defalco@polimi.it> |
---|---|
date | Sun, 11 Oct 2015 23:09:01 +0200 |
parents | 03b9d17a2d95 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1994-2015 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1026 | 18 |
3372 | 19 ## -*- texinfo -*- |
12584
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
20 ## @deftypefn {Function File} {} null (@var{A}) |
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {} null (@var{A}, @var{tol}) |
11470
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
22 ## Return an orthonormal basis of the null space of @var{A}. |
3426 | 23 ## |
20370
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
24 ## The dimension of the null space is taken as the number of singular values of |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## @var{A} not greater than @var{tol}. If the argument @var{tol} is missing, |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## it is computed as |
3426 | 27 ## |
3372 | 28 ## @example |
11470
eb9e0b597d61
Use common names for variables in documentation and code for a few more m-script files.
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
29 ## max (size (@var{A})) * max (svd (@var{A})) * eps |
3372 | 30 ## @end example |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
31 ## @seealso{orth} |
3372 | 32 ## @end deftypefn |
557 | 33 |
5428 | 34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 35 ## Created: 24 December 1993. |
36 ## Adapted-By: jwe | |
557 | 37 |
2312 | 38 function retval = null (A, tol) |
557 | 39 |
4371 | 40 if (isempty (A)) |
41 retval = []; | |
42 else | |
43 [U, S, V] = svd (A); | |
1065 | 44 |
4371 | 45 [rows, cols] = size (A); |
46 | |
47 [S_nr, S_nc] = size (S); | |
557 | 48 |
4371 | 49 if (S_nr == 1 || S_nc == 1) |
50 s = S(1); | |
51 else | |
52 s = diag (S); | |
53 endif | |
557 | 54 |
4371 | 55 if (nargin == 1) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
56 if (isa (A, "single")) |
10549 | 57 tol = max (size (A)) * s (1) * eps ("single"); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
58 else |
10549 | 59 tol = max (size (A)) * s (1) * eps; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
60 endif |
4371 | 61 elseif (nargin != 2) |
6046 | 62 print_usage (); |
4371 | 63 endif |
557 | 64 |
4371 | 65 rank = sum (s > tol); |
66 | |
67 if (rank < cols) | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
68 retval = V(:, rank+1:cols); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
69 if (isa (A, "single")) |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
70 retval (abs (retval) < eps ("single")) = 0; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
71 else |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
72 retval (abs (retval) < eps) = 0; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
73 endif |
4371 | 74 else |
75 retval = zeros (cols, 0); | |
76 endif | |
557 | 77 endif |
78 | |
79 endfunction | |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
80 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
81 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
82 ## FIXME: Need some tests for 'single' variables as well |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
84 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
85 %! A = 0; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 %! assert (null (A), 1); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
87 |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
88 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
89 %! A = 1; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 %! assert (null (A), zeros (1,0)); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
91 |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
92 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
93 %! A = [1 0; 0 1]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
94 %! assert (null (A), zeros (2,0)); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
95 |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
96 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
97 %! A = [1 0; 1 0]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
98 %! assert (null (A), [0 1]'); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
99 |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
100 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
101 %! A = [1 1; 0 0]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %! assert (null (A), [-1/sqrt(2) 1/sqrt(2)]', eps); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
103 |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
104 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
105 %! tol = 1e-4; |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
106 %! A = [1 0; 0 tol-eps]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %! assert (null (A,tol), [0; 1]); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
108 |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
109 %!test |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
110 %! tol = 1e-4; |
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
111 %! A = [1 0; 0 tol+eps]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
112 %! assert (null (A,tol), zeros (2,0)); |
12806
a85ddaf8f402
codesprint: Wrote 8 tests for null.m
Sean Young <seannz@gmail.com>
parents:
12584
diff
changeset
|
113 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
114 %!error null () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 |