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 |
1631
|
32 #include <iostream.h> |
|
33 |
457
|
34 #include "dbleSVD.h" |
1368
|
35 #include "f77-uscore.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 |
|
81 double *tmp_data = dup (a.data (), a.length ()); |
|
82 |
|
83 int min_mn = m < n ? m : n; |
|
84 int max_mn = m > n ? m : n; |
|
85 |
1251
|
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: |
1251
|
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: |
|
102 jobu = jobv ="N"; |
1545
|
103 ncol_u = nrow_vt = 1; |
1543
|
104 break; |
|
105 |
|
106 default: |
|
107 break; |
537
|
108 } |
|
109 |
1544
|
110 type_computed = svd_type; |
|
111 |
1545
|
112 double *u = (*jobu == 'N' ? 0 : new double[m * ncol_u]); |
457
|
113 double *s_vec = new double[min_mn]; |
1545
|
114 double *vt = (*jobv == 'N' ? 0 : new double[nrow_vt * n]); |
457
|
115 |
|
116 int tmp1 = 3*min_mn + max_mn; |
|
117 int tmp2 = 5*min_mn - 4; |
|
118 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
119 double *work = new double[lwork]; |
|
120 |
1253
|
121 F77_FCN (dgesvd, DGESVD) (jobu, jobv, m, n, tmp_data, m, s_vec, u, |
|
122 m, vt, nrow_vt, work, lwork, info, 1L, |
|
123 1L); |
457
|
124 |
1545
|
125 if (*jobu != 'N') |
1543
|
126 left_sm = Matrix (u, m, ncol_u); |
|
127 |
537
|
128 sigma = DiagMatrix (s_vec, nrow_s, ncol_s); |
1543
|
129 |
1545
|
130 if (*jobv != 'N') |
1543
|
131 { |
|
132 Matrix vt_m (vt, nrow_vt, n); |
|
133 right_sm = vt_m.transpose (); |
|
134 } |
457
|
135 |
|
136 delete [] tmp_data; |
|
137 delete [] work; |
|
138 |
|
139 return info; |
|
140 } |
|
141 |
|
142 ostream& |
|
143 operator << (ostream& os, const SVD& a) |
|
144 { |
|
145 os << a.left_singular_matrix () << "\n"; |
|
146 os << a.singular_values () << "\n"; |
|
147 os << a.right_singular_matrix () << "\n"; |
|
148 |
|
149 return os; |
|
150 } |
|
151 |
|
152 /* |
|
153 ;;; Local Variables: *** |
|
154 ;;; mode: C++ *** |
|
155 ;;; page-delimiter: "^/\\*" *** |
|
156 ;;; End: *** |
|
157 */ |