Mercurial > octave
annotate libinterp/corefcn/svd.cc @ 25054:6652d3823428 stable
maint: Update copyright dates in all source files.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 30 Mar 2018 09:19:05 -0400 |
parents | 194eb4bd202b |
children | 078b795c5219 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
3 Copyright (C) 1996-2018 John W. Eaton |
2928 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22709
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22709
diff
changeset
|
10 (at your option) any later version. |
2928 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22709
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22709
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22709
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22709
diff
changeset
|
15 GNU General Public License for more details. |
2928 | 16 |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23795
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
2928 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21273
diff
changeset
|
24 # include "config.h" |
2928 | 25 #endif |
26 | |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
27 #include "svd.h" |
2928 | 28 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
29 #include "defun.h" |
2928 | 30 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
31 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20939
diff
changeset
|
32 #include "ovl.h" |
2928 | 33 #include "pr-output.h" |
34 #include "utils.h" | |
10638
e1559a8a60b4
general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents:
10601
diff
changeset
|
35 #include "variables.h" |
2928 | 36 |
22977
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
37 static std::string Vsvd_driver = "gesdd"; |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
38 |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
39 template <typename T> |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
40 static typename octave::math::svd<T>::Type |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
41 svd_type (int nargin, int nargout, const octave_value_list & args, const T & A) |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
42 { |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
43 if (nargout == 0 || nargout == 1) |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
44 return octave::math::svd<T>::Type::sigma_only; |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
45 else if (nargin == 1) |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
46 return octave::math::svd<T>::Type::std; |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
47 else |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
48 if (! args(1).is_real_scalar ()) |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
49 return octave::math::svd<T>::Type::economy; |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
50 else |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
51 { |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
52 if (A.rows () > A.columns ()) |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
53 return octave::math::svd<T>::Type::economy; |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
54 else |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
55 return octave::math::svd<T>::Type::std; |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
56 } |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
57 } |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
58 |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
59 template <typename T> |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
60 static typename octave::math::svd<T>::Driver |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
61 svd_driver (void) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
62 { |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
63 return (Vsvd_driver == "gesvd" |
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
64 ? octave::math::svd<T>::Driver::GESVD |
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
65 : octave::math::svd<T>::Driver::GESDD); |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
66 } |
10601 | 67 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
68 DEFUN (svd, args, nargout, |
22893
5ff6716cf157
allow dispatch types to be declared for built-in functions
John W. Eaton <jwe@octave.org>
parents:
22755
diff
changeset
|
69 classes: double single |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
70 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
71 @deftypefn {} {@var{s} =} svd (@var{A}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
72 @deftypefnx {} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}) |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
73 @deftypefnx {} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}, "econ") |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
74 @deftypefnx {} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}, 0) |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
75 @cindex singular value decomposition |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
76 Compute the singular value decomposition of @var{A} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
77 @tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
78 $$ |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
79 A = U S V^{\dagger} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
80 $$ |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
81 @end tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
82 @ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
83 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
84 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
85 A = U*S*V' |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
86 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
87 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
88 @end ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
89 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
90 The function @code{svd} normally returns only the vector of singular values. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
91 When called with three return values, it computes |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
92 @tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
93 $U$, $S$, and $V$. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
94 @end tex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
95 @ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
96 @var{U}, @var{S}, and @var{V}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
97 @end ifnottex |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
98 For example, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
99 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
100 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
101 svd (hilb (3)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
102 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
103 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
104 @noindent |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
105 returns |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
106 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
107 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
108 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
109 ans = |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
110 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
111 1.4083189 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
112 0.1223271 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
113 0.0026873 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
114 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
115 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
116 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
117 @noindent |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
118 and |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
119 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
120 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
121 [u, s, v] = svd (hilb (3)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
122 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
123 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
124 @noindent |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
125 returns |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
126 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
127 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
128 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
129 u = |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
130 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
131 -0.82704 0.54745 0.12766 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
132 -0.45986 -0.52829 -0.71375 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
133 -0.32330 -0.64901 0.68867 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
134 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
135 s = |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
136 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
137 1.40832 0.00000 0.00000 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
138 0.00000 0.12233 0.00000 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
139 0.00000 0.00000 0.00269 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
140 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
141 v = |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
142 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
143 -0.82704 0.54745 0.12766 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
144 -0.45986 -0.52829 -0.71375 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
145 -0.32330 -0.64901 0.68867 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
146 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
147 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
148 |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
149 When given a second argument that is not 0, @code{svd} returns an economy-sized |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
150 decomposition, eliminating the unnecessary rows or columns of @var{U} or |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
151 @var{V}. |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
152 |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
153 If the second argument is exactly 0, then the choice of decomposition is based |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
154 on the matrix @var{A}. If @var{A} has more rows than columns then an |
22709
5c04055aa767
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
22615
diff
changeset
|
155 economy-sized decomposition is returned, otherwise a regular decomposition |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
156 is calculated. |
22977
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
157 |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
158 Algorithm Notes: When calculating the full decomposition (left and right |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
159 singular matrices in addition to singular values) there is a choice of two |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
160 routines in @sc{lapack}. The default routine used by Octave is @code{gesdd} |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
161 which is 5X faster than the alternative @code{gesvd}, but may use more memory |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
162 and may be less accurate for some matrices. See the documentation for |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
163 @code{svd_driver} for more information. |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
164 @seealso{svd_driver, svds, eig, lu, chol, hess, qr, qz} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
165 @end deftypefn */) |
2928 | 166 { |
167 int nargin = args.length (); | |
168 | |
169 if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3) | |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
170 print_usage (); |
2928 | 171 |
172 octave_value arg = args(0); | |
173 | |
10185
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
174 if (arg.ndims () != 2) |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
175 error ("svd: A must be a 2-D matrix"); |
10185
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
176 |
20939
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20873
diff
changeset
|
177 octave_value_list retval; |
b17fda023ca6
maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents:
20873
diff
changeset
|
178 |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
179 bool isfloat = arg.is_single_type (); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
180 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
181 if (isfloat) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
182 { |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
183 if (arg.isreal ()) |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
184 { |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
185 FloatMatrix tmp = arg.float_matrix_value (); |
10638
e1559a8a60b4
general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents:
10601
diff
changeset
|
186 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
187 if (tmp.any_element_is_inf_or_nan ()) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
188 error ("svd: cannot take SVD of matrix containing Inf or NaN values"); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
189 |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
190 octave::math::svd<FloatMatrix> result |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
191 (tmp, |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
192 svd_type<FloatMatrix> (nargin, nargout, args, tmp), |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
193 svd_driver<FloatMatrix> ()); |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
194 |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
195 FloatDiagMatrix sigma = result.singular_values (); |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
196 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
197 if (nargout == 0 || nargout == 1) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
198 retval(0) = sigma.extract_diag (); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
199 else |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
200 retval = ovl (result.left_singular_matrix (), |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
201 sigma, |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
202 result.right_singular_matrix ()); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
203 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
204 else if (arg.iscomplex ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
205 { |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
206 FloatComplexMatrix ctmp = arg.float_complex_matrix_value (); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
207 |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
208 if (ctmp.any_element_is_inf_or_nan ()) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
209 error ("svd: cannot take SVD of matrix containing Inf or NaN values"); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
210 |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
211 octave::math::svd<FloatComplexMatrix> result |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
212 (ctmp, |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
213 svd_type<FloatComplexMatrix> (nargin, nargout, args, ctmp), |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
214 svd_driver<FloatComplexMatrix> ()); |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
215 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
216 FloatDiagMatrix sigma = result.singular_values (); |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
217 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
218 if (nargout == 0 || nargout == 1) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
219 retval(0) = sigma.extract_diag (); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
220 else |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
221 retval = ovl (result.left_singular_matrix (), |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
222 sigma, |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
223 result.right_singular_matrix ()); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
224 } |
2928 | 225 } |
226 else | |
227 { | |
23582
0cc2011d800e
maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
228 if (arg.isreal ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
229 { |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
230 Matrix tmp = arg.matrix_value (); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
231 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
232 if (tmp.any_element_is_inf_or_nan ()) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
233 error ("svd: cannot take SVD of matrix containing Inf or NaN values"); |
4478 | 234 |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
235 octave::math::svd<Matrix> result |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
236 (tmp, |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
237 svd_type<Matrix> (nargin, nargout, args, tmp), |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
238 svd_driver<Matrix> ()); |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
20172
diff
changeset
|
239 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
240 DiagMatrix sigma = result.singular_values (); |
4478 | 241 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
242 if (nargout == 0 || nargout == 1) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
243 retval(0) = sigma.extract_diag (); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
244 else |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
245 retval = ovl (result.left_singular_matrix (), |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
246 sigma, |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
247 result.right_singular_matrix ()); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
248 } |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23573
diff
changeset
|
249 else if (arg.iscomplex ()) |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
250 { |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
251 ComplexMatrix ctmp = arg.complex_matrix_value (); |
4478 | 252 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
253 if (ctmp.any_element_is_inf_or_nan ()) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
254 error ("svd: cannot take SVD of matrix containing Inf or NaN values"); |
4478 | 255 |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
256 octave::math::svd<ComplexMatrix> result |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
257 (ctmp, |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
258 svd_type<ComplexMatrix> (nargin, nargout, args, ctmp), |
22317
6ca3acf5fad8
move some new numeric classes to namespace octave::math
John W. Eaton <jwe@octave.org>
parents:
22204
diff
changeset
|
259 svd_driver<ComplexMatrix> ()); |
4478 | 260 |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
261 DiagMatrix sigma = result.singular_values (); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
262 |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
263 if (nargout == 0 || nargout == 1) |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
264 retval(0) = sigma.extract_diag (); |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
265 else |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
266 retval = ovl (result.left_singular_matrix (), |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
267 sigma, |
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
268 result.right_singular_matrix ()); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9758
diff
changeset
|
269 } |
4478 | 270 else |
21273
cbced1c09916
better use of templates for svd classes
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
271 err_wrong_type_arg ("svd", arg); |
2928 | 272 } |
273 | |
274 return retval; | |
275 } | |
276 | |
277 /* | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
278 %!assert (svd ([1, 2; 2, 1]), [3; 1], sqrt (eps)) |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
279 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
280 %!test |
22592
ee0df00e12d6
fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents:
22407
diff
changeset
|
281 a = [1, 2; 3, 4] + [5, 6; 7, 8]*i; |
ee0df00e12d6
fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents:
22407
diff
changeset
|
282 [u,s,v] = svd (a); |
ee0df00e12d6
fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents:
22407
diff
changeset
|
283 assert (a, u * s * v', 128 * eps); |
ee0df00e12d6
fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents:
22407
diff
changeset
|
284 |
ee0df00e12d6
fix complex svd (bug #49232)
John W. Eaton <jwe@octave.org>
parents:
22407
diff
changeset
|
285 %!test |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
286 %! [u, s, v] = svd ([1, 2; 2, 1]); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
287 %! x = 1 / sqrt (2); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
288 %! assert (u, [-x, -x; -x, x], sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
289 %! assert (s, [3, 0; 0, 1], sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
290 %! assert (v, [-x, x; -x, -x], sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
291 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
292 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
293 %! a = [1, 2, 3; 4, 5, 6]; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
294 %! [u, s, v] = svd (a); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
295 %! assert (u * s * v', a, sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
296 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
297 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
298 %! a = [1, 2; 3, 4; 5, 6]; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
299 %! [u, s, v] = svd (a); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
300 %! assert (u * s * v', a, sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
301 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
302 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
303 %! a = [1, 2, 3; 4, 5, 6]; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
304 %! [u, s, v] = svd (a, 1); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
305 %! assert (u * s * v', a, sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
306 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
307 %!test |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
308 %! a = [1, 2; 3, 4; 5, 6]; |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
309 %! [u, s, v] = svd (a, 1); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
310 %! assert (u * s * v', a, sqrt (eps)); |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
311 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
312 %!assert (svd (single ([1, 2; 2, 1])), single ([3; 1]), sqrt (eps ("single"))) |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
313 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
314 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
315 %! [u, s, v] = svd (single ([1, 2; 2, 1])); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
316 %! x = single (1 / sqrt (2)); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
317 %! assert (u, [-x, -x; -x, x], sqrt (eps ("single"))); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
318 %! assert (s, single ([3, 0; 0, 1]), sqrt (eps ("single"))); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
319 %! assert (v, [-x, x; -x, -x], sqrt (eps ("single"))); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
320 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
321 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
322 %! a = single ([1, 2, 3; 4, 5, 6]); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
323 %! [u, s, v] = svd (a); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
324 %! assert (u * s * v', a, sqrt (eps ("single"))); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
325 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
326 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
327 %! a = single ([1, 2; 3, 4; 5, 6]); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
328 %! [u, s, v] = svd (a); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
329 %! assert (u * s * v', a, sqrt (eps ("single"))); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
330 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
331 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
332 %! a = single ([1, 2, 3; 4, 5, 6]); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
333 %! [u, s, v] = svd (a, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
334 %! assert (u * s * v', a, sqrt (eps ("single"))); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
335 |
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
336 %!test |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
337 %! a = single ([1, 2; 3, 4; 5, 6]); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
338 %! [u, s, v] = svd (a, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
339 %! assert (u * s * v', a, sqrt (eps ("single"))); |
7814
87865ed7405f
Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents:
7789
diff
changeset
|
340 |
10185
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
341 %!test |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
342 %! a = zeros (0, 5); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
343 %! [u, s, v] = svd (a); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
344 %! assert (size (u), [0, 0]); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
345 %! assert (size (s), [0, 5]); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
346 %! assert (size (v), [5, 5]); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
347 |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
348 %!test |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
349 %! a = zeros (5, 0); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
350 %! [u, s, v] = svd (a, 1); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
351 %! assert (size (u), [5, 0]); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
352 %! assert (size (s), [0, 0]); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
353 %! assert (size (v), [0, 0]); |
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
354 |
23573
1b4f4ec53b4a
use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents:
23572
diff
changeset
|
355 %!test <*49309> |
22615
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
356 %! [~,~,v] = svd ([1, 1, 1], 0); |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
357 %! assert (size (v), [3 3]); |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
358 %! [~,~,v] = svd ([1, 1, 1], "econ"); |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
359 %! assert (size (v), [3 1]); |
68e9bdb7cde3
Accept second input '0' to svd (bug #49309).
Rik <rik@octave.org>
parents:
22592
diff
changeset
|
360 |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
361 %!error svd () |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
362 %!error svd ([1, 2; 4, 5], 2, 3) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
363 %!error [u, v] = svd ([1, 2; 3, 4]) |
2928 | 364 */ |
10601 | 365 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14501
diff
changeset
|
366 DEFUN (svd_driver, args, nargout, |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
367 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
368 @deftypefn {} {@var{val} =} svd_driver () |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
369 @deftypefnx {} {@var{old_val} =} svd_driver (@var{new_val}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
370 @deftypefnx {} {} svd_driver (@var{new_val}, "local") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
371 Query or set the underlying @sc{lapack} driver used by @code{svd}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
372 |
22977
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
373 Currently recognized values are @qcode{"gesdd"} and @qcode{"gesvd"}. |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
374 The default is @qcode{"gesdd"}. |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
375 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
376 When called from inside a function with the @qcode{"local"} option, the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
377 variable is changed locally for the function and any subroutines it calls. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
378 The original variable value is restored when exiting the function. |
22977
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
379 |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
380 Algorithm Notes: The @sc{lapack} library provides two routines for calculating |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
381 the full singular value decomposition (left and right singular matrices as |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
382 well as singular values). When calculating just the singular values the |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
383 following discussion is not relevant. |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
384 |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
385 The default routine use by Octave is the newer @code{gesdd} which is based on a |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
386 Divide-and-Conquer algorithm that is 5X faster than the alternative |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
387 @code{gesvd}, which is based on QR factorization. However, the new algorithm |
23567
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
388 can use significantly more memory. For an @nospell{MxN} input matrix the |
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
389 memory usage is of order O(min(M,N) ^ 2), whereas the alternative is of order |
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
390 O(max(M,N)). In general, modern computers have abundant memory so Octave has |
71bfd507663c
doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
391 chosen to prioritize speed. |
22977
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
392 |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
393 In addition, there have been instances in the past where some input matrices |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
394 were not accurately decomposed by @code{gesdd}. This appears to have been |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
395 resolved with modern versions of @sc{lapack}. However, if certainty is |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
396 required the accuracy of the decomposition can always be tested after the fact |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
397 with |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
398 |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
399 @example |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
400 @group |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
401 [@var{u}, @var{s}, @var{v}] = svd (@var{x}); |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
402 norm (@var{x} - @var{u}*@var{s}*@var{v'}, "fro") |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
403 @end group |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
404 @end example |
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
405 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
406 @seealso{svd} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
407 @end deftypefn */) |
10601 | 408 { |
23795
980f39c3ab90
Use C++11 nullptr rather than 0 in code (bug #51565).
Rik <rik@octave.org>
parents:
23582
diff
changeset
|
409 static const char *driver_names[] = { "gesvd", "gesdd", nullptr }; |
10601 | 410 |
10638
e1559a8a60b4
general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents:
10601
diff
changeset
|
411 return SET_INTERNAL_VARIABLE_CHOICES (svd_driver, driver_names); |
10601 | 412 } |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
413 |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
414 /* |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
415 %!test |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
416 %! A = [1+1i, 1-1i, 0; 0, 2, 0; 1i, 1i, 1+2i]; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
417 %! old_driver = svd_driver ("gesvd"); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
418 %! [U1, S1, V1] = svd (A); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
419 %! svd_driver ("gesdd"); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
420 %! [U2, S2, V2] = svd (A); |
22977
750c8b4b7164
Switch svd_driver to gesdd for performance (bug #49940).
Rik <rik@octave.org>
parents:
22893
diff
changeset
|
421 %! svd_driver (old_driver); |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
422 %! assert (U1, U2, 5*eps); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
423 %! assert (S1, S2, 5*eps); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
424 %! assert (V1, V2, 5*eps); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
425 */ |