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