457
|
1 /* |
|
2 |
1882
|
3 Copyright (C) 1996 John W. Eaton |
457
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
457
|
20 |
|
21 */ |
|
22 |
1296
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
457
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
457
|
29 #endif |
|
30 |
1631
|
31 #include <iostream.h> |
|
32 |
457
|
33 #include "dbleSVD.h" |
1847
|
34 #include "f77-fcn.h" |
457
|
35 #include "mx-inlines.cc" |
|
36 |
|
37 extern "C" |
|
38 { |
1253
|
39 int F77_FCN (dgesvd, DGESVD) (const char*, const char*, const int&, |
|
40 const int&, double*, const int&, |
|
41 double*, double*, const int&, double*, |
|
42 const int&, double*, const int&, int&, |
|
43 long, long); |
457
|
44 } |
|
45 |
1543
|
46 Matrix |
1545
|
47 SVD::left_singular_matrix (void) const |
1543
|
48 { |
1544
|
49 if (type_computed == SVD::sigma_only) |
1543
|
50 { |
|
51 (*current_liboctave_error_handler) |
|
52 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
|
53 return Matrix (); |
|
54 } |
|
55 else |
|
56 return left_sm; |
|
57 } |
|
58 |
|
59 Matrix |
1545
|
60 SVD::right_singular_matrix (void) const |
1543
|
61 { |
1544
|
62 if (type_computed == SVD::sigma_only) |
1543
|
63 { |
|
64 (*current_liboctave_error_handler) |
|
65 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
|
66 return Matrix (); |
|
67 } |
|
68 else |
|
69 return right_sm; |
|
70 } |
|
71 |
457
|
72 int |
537
|
73 SVD::init (const Matrix& a, SVD::type svd_type) |
457
|
74 { |
|
75 int info; |
|
76 |
|
77 int m = a.rows (); |
|
78 int n = a.cols (); |
|
79 |
1930
|
80 Matrix atmp = a; |
|
81 double *tmp_data = atmp.fortran_vec (); |
457
|
82 |
|
83 int min_mn = m < n ? m : n; |
|
84 int max_mn = m > n ? m : n; |
|
85 |
1930
|
86 char jobu = 'A'; |
|
87 char jobv = 'A'; |
537
|
88 |
|
89 int ncol_u = m; |
|
90 int nrow_vt = n; |
|
91 int nrow_s = m; |
|
92 int ncol_s = n; |
|
93 |
1543
|
94 switch (svd_type) |
537
|
95 { |
1543
|
96 case SVD::economy: |
1930
|
97 jobu = jobv = 'S'; |
537
|
98 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543
|
99 break; |
|
100 |
|
101 case SVD::sigma_only: |
2621
|
102 |
|
103 // Note: for this case, both jobu and jobv should be 'N', but |
|
104 // there seems to be a bug in dgesvd from Lapack V2.0. To |
|
105 // demonstrate the bug, set both jobu and jobv to 'N' and find |
|
106 // the singular values of [eye(3), eye(3)]. The result is |
|
107 // [-sqrt(2), -sqrt(2), -sqrt(2)]. |
|
108 |
|
109 jobu = 'O'; |
|
110 jobv = 'N'; |
1545
|
111 ncol_u = nrow_vt = 1; |
1543
|
112 break; |
|
113 |
|
114 default: |
|
115 break; |
537
|
116 } |
|
117 |
1544
|
118 type_computed = svd_type; |
|
119 |
2621
|
120 if (! (jobu == 'N' || jobu == 'O')) |
1931
|
121 left_sm.resize (m, ncol_u); |
1930
|
122 |
|
123 double *u = left_sm.fortran_vec (); |
|
124 |
|
125 sigma.resize (nrow_s, ncol_s); |
|
126 double *s_vec = sigma.fortran_vec (); |
|
127 |
2621
|
128 if (! (jobv == 'N' || jobv == 'O')) |
1930
|
129 right_sm.resize (nrow_vt, n); |
|
130 |
|
131 double *vt = right_sm.fortran_vec (); |
457
|
132 |
|
133 int tmp1 = 3*min_mn + max_mn; |
|
134 int tmp2 = 5*min_mn - 4; |
|
135 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
1930
|
136 |
|
137 Array<double> work (lwork); |
|
138 double *pwork = work.fortran_vec (); |
457
|
139 |
1930
|
140 F77_XFCN (dgesvd, DGESVD, (&jobu, &jobv, m, n, tmp_data, m, s_vec, |
|
141 u, m, vt, nrow_vt, pwork, lwork, info, |
|
142 1L, 1L)); |
1543
|
143 |
1930
|
144 if (f77_exception_encountered) |
|
145 (*current_liboctave_error_handler) ("unrecoverable error in dgesvd"); |
|
146 else |
1543
|
147 { |
2621
|
148 if (! (jobv == 'N' || jobv == 'O')) |
1930
|
149 right_sm = right_sm.transpose (); |
1543
|
150 } |
457
|
151 |
|
152 return info; |
|
153 } |
|
154 |
|
155 ostream& |
|
156 operator << (ostream& os, const SVD& a) |
|
157 { |
|
158 os << a.left_singular_matrix () << "\n"; |
|
159 os << a.singular_values () << "\n"; |
|
160 os << a.right_singular_matrix () << "\n"; |
|
161 |
|
162 return os; |
|
163 } |
|
164 |
|
165 /* |
|
166 ;;; Local Variables: *** |
|
167 ;;; mode: C++ *** |
|
168 ;;; End: *** |
|
169 */ |