annotate libinterp/corefcn/svd.cc @ 20597:bc6daa38ff50

eliminate more simple uses of error_state * graphics.cc: Eliminate simple uses of error_state.
author John W. Eaton <jwe@octave.org>
date Tue, 06 Oct 2015 14:47:41 -0400
parents b70cc4bd8109
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
1 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
2
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
3 Copyright (C) 1996-2015 John W. Eaton
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
4
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6620
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6620
diff changeset
10 option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
11
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
15 for more details.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6620
diff changeset
18 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6620
diff changeset
19 <http://www.gnu.org/licenses/>.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
20
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
21 */
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
22
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
24 #include <config.h>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
27 #include "CmplxSVD.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28 #include "dbleSVD.h"
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
29 #include "fCmplxSVD.h"
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
30 #include "floatSVD.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
31
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
32 #include "defun.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
33 #include "error.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
34 #include "gripes.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
35 #include "oct-obj.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
36 #include "pr-output.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
37 #include "utils.h"
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
38 #include "variables.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
39
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
40 static int Vsvd_driver = SVD::GESVD;
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
41
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
42 DEFUN (svd, args, nargout,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
43 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
44 @deftypefn {Built-in Function} {@var{s} =} svd (@var{A})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
45 @deftypefnx {Built-in Function} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
46 @deftypefnx {Built-in Function} {[@var{U}, @var{S}, @var{V}] =} svd (@var{A}, @var{econ})\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
47 @cindex singular value decomposition\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11533
diff changeset
48 Compute the singular value decomposition of @var{A}\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
49 @tex\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
50 $$\n\
12584
7ef7e20057fa Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
51 A = U S V^{\\dagger}\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
52 $$\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
53 @end tex\n\
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7814
diff changeset
54 @ifnottex\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
55 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
56 @example\n\
6620
bf4bdc21dc8d [project @ 2007-05-14 17:35:46 by jwe]
jwe
parents: 5823
diff changeset
57 A = U*S*V'\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
58 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10711
diff changeset
59 \n\
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7814
diff changeset
60 @end ifnottex\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
61 \n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10640
diff changeset
62 The function @code{svd} normally returns only the vector of singular values.\n\
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10640
diff changeset
63 When called with three return values, it computes\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
64 @tex\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
65 $U$, $S$, and $V$.\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
66 @end tex\n\
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7814
diff changeset
67 @ifnottex\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11533
diff changeset
68 @var{U}, @var{S}, and @var{V}.\n\
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 7814
diff changeset
69 @end ifnottex\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
70 For example,\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
71 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
72 @example\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
73 svd (hilb (3))\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
74 @end example\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
75 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
76 @noindent\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
77 returns\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
78 \n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
79 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
80 @group\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
81 ans =\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
82 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
83 1.4083189\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
84 0.1223271\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
85 0.0026873\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
86 @end group\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
87 @end example\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
88 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
89 @noindent\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
90 and\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
91 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
92 @example\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
93 [u, s, v] = svd (hilb (3))\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
94 @end example\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
95 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
96 @noindent\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
97 returns\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
98 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
99 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
100 @group\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
101 u =\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
102 \n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
103 -0.82704 0.54745 0.12766\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
104 -0.45986 -0.52829 -0.71375\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
105 -0.32330 -0.64901 0.68867\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
106 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
107 s =\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
108 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
109 1.40832 0.00000 0.00000\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
110 0.00000 0.12233 0.00000\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
111 0.00000 0.00000 0.00269\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
112 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
113 v =\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
114 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
115 -0.82704 0.54745 0.12766\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
116 -0.45986 -0.52829 -0.71375\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
117 -0.32330 -0.64901 0.68867\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
118 @end group\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
119 @end example\n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
120 \n\
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
121 If given a second argument, @code{svd} returns an economy-sized\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11533
diff changeset
122 decomposition, eliminating the unnecessary rows or columns of @var{U} or\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11533
diff changeset
123 @var{V}.\n\
16920
53eaa83e4181 doc: Add seealso links between various factorization forms.
Rik <rik@octave.org>
parents: 15888
diff changeset
124 @seealso{svd_driver, svds, eig, lu, chol, hess, qr, qz}\n\
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3014
diff changeset
125 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
126 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
127 octave_value_list retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
128
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
129 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
130
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
131 if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3)
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
132 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
133
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
134 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
135
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 4478
diff changeset
136 octave_idx_type nr = arg.rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 4478
diff changeset
137 octave_idx_type nc = arg.columns ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
138
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
139 if (arg.ndims () != 2)
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
140 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
141
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
142 bool isfloat = arg.is_single_type ();
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
143
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
144 SVD::type type = ((nargout == 0 || nargout == 1)
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
145 ? SVD::sigma_only
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
146 : (nargin == 2) ? SVD::economy : SVD::std);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
147
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
148 SVD::driver driver = static_cast<SVD::driver> (Vsvd_driver);
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
149
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
150 if (nr == 0 || nc == 0)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
151 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
152 if (isfloat)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
153 {
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
154 switch (type)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
155 {
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
156 case SVD::std:
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
157 retval(2) = FloatDiagMatrix (nc, nc, 1.0f);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
158 retval(1) = FloatMatrix (nr, nc);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
159 retval(0) = FloatDiagMatrix (nr, nr, 1.0f);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
160 break;
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
161
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
162 case SVD::economy:
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
163 retval(2) = FloatDiagMatrix (0, nc, 1.0f);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
164 retval(1) = FloatMatrix (0, 0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
165 retval(0) = FloatDiagMatrix (nr, 0, 1.0f);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
166 break;
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
167
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
168 case SVD::sigma_only: default:
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
169 retval(0) = FloatMatrix (0, 1);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
170 break;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
171 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
172 }
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
173 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
174 {
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
175 switch (type)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
176 {
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
177 case SVD::std:
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
178 retval(2) = DiagMatrix (nc, nc, 1.0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
179 retval(1) = Matrix (nr, nc);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
180 retval(0) = DiagMatrix (nr, nr, 1.0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
181 break;
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
182
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
183 case SVD::economy:
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
184 retval(2) = DiagMatrix (0, nc, 1.0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
185 retval(1) = Matrix (0, 0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
186 retval(0) = DiagMatrix (nr, 0, 1.0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
187 break;
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
188
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
189 case SVD::sigma_only: default:
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
190 retval(0) = Matrix (0, 1);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
191 break;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
192 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
193 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
194 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
195 else
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
196 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
197 if (isfloat)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
198 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
199 if (arg.is_real_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
200 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
201 FloatMatrix tmp = arg.float_matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
202
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
203 if (tmp.any_element_is_inf_or_nan ())
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
204 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
205
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
206 FloatSVD result (tmp, type, driver);
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
207
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
208 FloatDiagMatrix sigma = result.singular_values ();
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
209
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
210 if (nargout == 0 || nargout == 1)
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
211 retval(0) = sigma.extract_diag ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
212 else
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
213 {
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
214 retval(2) = result.right_singular_matrix ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
215 retval(1) = sigma;
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
216 retval(0) = result.left_singular_matrix ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
217 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
218 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
219 else if (arg.is_complex_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
220 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
221 FloatComplexMatrix ctmp = arg.float_complex_matrix_value ();
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
222
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
223 if (ctmp.any_element_is_inf_or_nan ())
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
224 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
225
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
226 FloatComplexSVD result (ctmp, type, driver);
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
227
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
228 FloatDiagMatrix sigma = result.singular_values ();
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
229
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
230 if (nargout == 0 || nargout == 1)
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
231 retval(0) = sigma.extract_diag ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
232 else
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
233 {
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
234 retval(2) = result.right_singular_matrix ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
235 retval(1) = sigma;
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
236 retval(0) = result.left_singular_matrix ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
237 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
238 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
239 }
4478
7afd4bf05aa8 [project @ 2003-07-30 19:15:31 by jwe]
jwe
parents: 3548
diff changeset
240 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
241 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
242 if (arg.is_real_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
243 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
244 Matrix tmp = arg.matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
245
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
246 if (tmp.any_element_is_inf_or_nan ())
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
247 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
248
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
249 SVD result (tmp, type, driver);
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
250
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
251 DiagMatrix sigma = result.singular_values ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
252
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
253 if (nargout == 0 || nargout == 1)
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
254 retval(0) = sigma.extract_diag ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
255 else
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
256 {
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
257 retval(2) = result.right_singular_matrix ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
258 retval(1) = sigma;
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
259 retval(0) = result.left_singular_matrix ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
260 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
261 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
262 else if (arg.is_complex_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
263 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
264 ComplexMatrix ctmp = arg.complex_matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
265
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
266 if (ctmp.any_element_is_inf_or_nan ())
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
267 error ("svd: cannot take SVD of matrix containing Inf or NaN values");
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
268
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
269 ComplexSVD result (ctmp, type, driver);
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
270
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
271 DiagMatrix sigma = result.singular_values ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
272
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
273 if (nargout == 0 || nargout == 1)
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
274 retval(0) = sigma.extract_diag ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
275 else
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
276 {
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
277 retval(2) = result.right_singular_matrix ();
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
278 retval(1) = sigma;
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
279 retval(0) = result.left_singular_matrix ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
280 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
281 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
282 else
20569
b70cc4bd8109 begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents: 20207
diff changeset
283 gripe_wrong_type_arg ("svd", arg);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9758
diff changeset
284 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
285 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
286
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
287 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
288 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
289
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
290 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
291 %!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
292
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
293 %!test
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 ([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
295 %! 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
296 %! 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
297 %! 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
298 %! 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
299
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
300 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
301 %! 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
302 %! [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
303 %! 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
304
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
305 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
306 %! 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
307 %! [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
308 %! 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
309
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
310 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
311 %! 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
312 %! [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
313 %! 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
314
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
315 %!test
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
316 %! 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
317 %! [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
318 %! 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
319
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
320 %!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
321
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
322 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
323 %! [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
324 %! 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
325 %! 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
326 %! 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
327 %! 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
328
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
329 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
330 %! 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
331 %! [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
332 %! 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
333
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
334 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
335 %! 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
336 %! [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
337 %! 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
338
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
339 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
340 %! 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
341 %! [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
342 %! 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
343
87865ed7405f Second set of single precision test code and fix of resulting bugs
David Bateman <dbateman@free.fr>
parents: 7789
diff changeset
344 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
345 %! 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
346 %! [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
347 %! 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
348
10185
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
349 %!test
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
350 %! a = zeros (0, 5);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
351 %! [u, s, v] = svd (a);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
352 %! assert (size (u), [0, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
353 %! assert (size (s), [0, 5]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
354 %! assert (size (v), [5, 5]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
355
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
356 %!test
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
357 %! a = zeros (5, 0);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
358 %! [u, s, v] = svd (a, 1);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
359 %! assert (size (u), [5, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
360 %! assert (size (s), [0, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
361 %! assert (size (v), [0, 0]);
455759a5fcbe fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 10155
diff changeset
362
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
363 %!error svd ()
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
364 %!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
365 %!error [u, v] = svd ([1, 2; 3, 4])
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
366 */
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
367
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
368 DEFUN (svd_driver, args, nargout,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
369 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
370 @deftypefn {Built-in Function} {@var{val} =} svd_driver ()\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
371 @deftypefnx {Built-in Function} {@var{old_val} =} svd_driver (@var{new_val})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
372 @deftypefnx {Built-in Function} {} svd_driver (@var{new_val}, \"local\")\n\
12584
7ef7e20057fa Improve documentation strings in Linear Algebra chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
373 Query or set the underlying @sc{lapack} driver used by @code{svd}.\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
374 \n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
375 Currently recognized values are @qcode{\"gesvd\"} and @qcode{\"gesdd\"}.\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16920
diff changeset
376 The default is @qcode{\"gesvd\"}.\n\
13951
79aa00a94e9e doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents: 13915
diff changeset
377 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16920
diff changeset
378 When called from inside a function with the @qcode{\"local\"} option, the\n\
20207
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19731
diff changeset
379 variable is changed locally for the function and any subroutines it calls.\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 16920
diff changeset
380 The original variable value is restored when exiting the function.\n\
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
381 @seealso{svd}\n\
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
382 @end deftypefn")
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
383 {
10640
5c594472f75e determine string enum length by trailing null rather than sizeof
Jaroslav Hajek <highegg@gmail.com>
parents: 10638
diff changeset
384 static const char *driver_names[] = { "gesvd", "gesdd", 0 };
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
385
10638
e1559a8a60b4 general mechanism for string enum variables
Jaroslav Hajek <highegg@gmail.com>
parents: 10601
diff changeset
386 return SET_INTERNAL_VARIABLE_CHOICES (svd_driver, driver_names);
10601
3ce0c530a9c9 implement svd_driver
Jaroslav Hajek <highegg@gmail.com>
parents: 10185
diff changeset
387 }
15887
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
388
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
389 /*
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
390 %!test
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
391 %! 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
392 %! old_driver = svd_driver ("gesvd");
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
393 %! [U1, S1, V1] = svd (A);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
394 %! svd_driver ("gesdd");
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
395 %! [U2, S2, V2] = svd (A);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
396 %! assert (U1, U2, 5*eps);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
397 %! assert (S1, S2, 5*eps);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
398 %! assert (V1, V2, 5*eps);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
399 %! svd_driver (old_driver);
8ced82e96b48 Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents: 14138
diff changeset
400 */