457
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
|
32 #include "dbleSVD.h" |
1368
|
33 #include "f77-uscore.h" |
457
|
34 #include "mx-inlines.cc" |
|
35 |
|
36 extern "C" |
|
37 { |
1253
|
38 int F77_FCN (dgesvd, DGESVD) (const char*, const char*, const int&, |
|
39 const int&, double*, const int&, |
|
40 double*, double*, const int&, double*, |
|
41 const int&, double*, const int&, int&, |
|
42 long, long); |
457
|
43 } |
|
44 |
1543
|
45 Matrix |
1545
|
46 SVD::left_singular_matrix (void) const |
1543
|
47 { |
1544
|
48 if (type_computed == SVD::sigma_only) |
1543
|
49 { |
|
50 (*current_liboctave_error_handler) |
|
51 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
|
52 return Matrix (); |
|
53 } |
|
54 else |
|
55 return left_sm; |
|
56 } |
|
57 |
|
58 Matrix |
1545
|
59 SVD::right_singular_matrix (void) const |
1543
|
60 { |
1544
|
61 if (type_computed == SVD::sigma_only) |
1543
|
62 { |
|
63 (*current_liboctave_error_handler) |
|
64 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
|
65 return Matrix (); |
|
66 } |
|
67 else |
|
68 return right_sm; |
|
69 } |
|
70 |
457
|
71 int |
537
|
72 SVD::init (const Matrix& a, SVD::type svd_type) |
457
|
73 { |
|
74 int info; |
|
75 |
|
76 int m = a.rows (); |
|
77 int n = a.cols (); |
|
78 |
|
79 double *tmp_data = dup (a.data (), a.length ()); |
|
80 |
|
81 int min_mn = m < n ? m : n; |
|
82 int max_mn = m > n ? m : n; |
|
83 |
1251
|
84 char *jobu = "A"; |
|
85 char *jobv = "A"; |
537
|
86 |
|
87 int ncol_u = m; |
|
88 int nrow_vt = n; |
|
89 int nrow_s = m; |
|
90 int ncol_s = n; |
|
91 |
1543
|
92 switch (svd_type) |
537
|
93 { |
1543
|
94 case SVD::economy: |
1251
|
95 jobu = jobv ="S"; |
537
|
96 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543
|
97 break; |
|
98 |
|
99 case SVD::sigma_only: |
|
100 jobu = jobv ="N"; |
1545
|
101 ncol_u = nrow_vt = 1; |
1543
|
102 break; |
|
103 |
|
104 default: |
|
105 break; |
537
|
106 } |
|
107 |
1544
|
108 type_computed = svd_type; |
|
109 |
1545
|
110 double *u = (*jobu == 'N' ? 0 : new double[m * ncol_u]); |
457
|
111 double *s_vec = new double[min_mn]; |
1545
|
112 double *vt = (*jobv == 'N' ? 0 : new double[nrow_vt * n]); |
457
|
113 |
|
114 int tmp1 = 3*min_mn + max_mn; |
|
115 int tmp2 = 5*min_mn - 4; |
|
116 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
117 double *work = new double[lwork]; |
|
118 |
1253
|
119 F77_FCN (dgesvd, DGESVD) (jobu, jobv, m, n, tmp_data, m, s_vec, u, |
|
120 m, vt, nrow_vt, work, lwork, info, 1L, |
|
121 1L); |
457
|
122 |
1545
|
123 if (*jobu != 'N') |
1543
|
124 left_sm = Matrix (u, m, ncol_u); |
|
125 |
537
|
126 sigma = DiagMatrix (s_vec, nrow_s, ncol_s); |
1543
|
127 |
1545
|
128 if (*jobv != 'N') |
1543
|
129 { |
|
130 Matrix vt_m (vt, nrow_vt, n); |
|
131 right_sm = vt_m.transpose (); |
|
132 } |
457
|
133 |
|
134 delete [] tmp_data; |
|
135 delete [] work; |
|
136 |
|
137 return info; |
|
138 } |
|
139 |
|
140 ostream& |
|
141 operator << (ostream& os, const SVD& a) |
|
142 { |
|
143 os << a.left_singular_matrix () << "\n"; |
|
144 os << a.singular_values () << "\n"; |
|
145 os << a.right_singular_matrix () << "\n"; |
|
146 |
|
147 return os; |
|
148 } |
|
149 |
|
150 /* |
|
151 ;;; Local Variables: *** |
|
152 ;;; mode: C++ *** |
|
153 ;;; page-delimiter: "^/\\*" *** |
|
154 ;;; End: *** |
|
155 */ |