Mercurial > hg > octave-nkf
annotate scripts/linear-algebra/orth.m @ 20770:c1a6c31ac29a
eliminate more simple uses of error_state
* ov-classdef.cc: Eliminate simple uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 06 Oct 2015 00:20:02 -0400 |
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} {} orth (@var{A}) |
7ef7e20057fa
Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## @deftypefnx {Function File} {} orth (@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 range 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 range space is taken as the number of singular values |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## of @var{A} greater than @var{tol}. If the argument @var{tol} is missing, it |
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## 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{null} |
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 = orth (A, tol) |
557 | 39 |
3141 | 40 if (nargin == 1 || nargin == 2) |
557 | 41 |
12906
d7a91b3fb7f9
Return empty matrix if the argument to orth is empty (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
42 if (isempty (A)) |
d7a91b3fb7f9
Return empty matrix if the argument to orth is empty (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
43 retval = []; |
d7a91b3fb7f9
Return empty matrix if the argument to orth is empty (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
44 return; |
d7a91b3fb7f9
Return empty matrix if the argument to orth is empty (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
45 endif |
d7a91b3fb7f9
Return empty matrix if the argument to orth is empty (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12584
diff
changeset
|
46 |
3141 | 47 [U, S, V] = svd (A); |
557 | 48 |
3141 | 49 [rows, cols] = size (A); |
1065 | 50 |
3141 | 51 [S_nr, S_nc] = size (S); |
52 | |
53 if (S_nr == 1 || S_nc == 1) | |
54 s = S(1); | |
55 else | |
56 s = diag (S); | |
57 endif | |
557 | 58 |
3141 | 59 if (nargin == 1) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
60 if (isa (A, "single")) |
10549 | 61 tol = max (size (A)) * s (1) * eps ("single"); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
62 else |
10549 | 63 tol = max (size (A)) * s (1) * eps; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
64 endif |
3141 | 65 endif |
66 | |
67 rank = sum (s > tol); | |
557 | 68 |
3141 | 69 if (rank > 0) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
70 retval = -U(:, 1:rank); |
3141 | 71 else |
72 retval = zeros (rows, 0); | |
73 endif | |
557 | 74 |
75 else | |
3141 | 76 |
6046 | 77 print_usage (); |
3141 | 78 |
557 | 79 endif |
80 | |
81 endfunction | |
13047
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
82 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 |
13047
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
84 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %! for i = 1:20 |
13047
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
86 %! A = rand (10, 10); |
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
87 %! V = orth (A); |
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
88 %! if (det (A) != 0) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %! assert (V'*V, eye (10), 100*eps); |
13047
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
90 %! endif |
69a4609e61e2
codesprint: Add a test to orth.m
Carlo de Falco <kingcrimson@tiscali.it>
parents:
12906
diff
changeset
|
91 %! endfor |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 |