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 |
|
45 int |
537
|
46 SVD::init (const Matrix& a, SVD::type svd_type) |
457
|
47 { |
|
48 int info; |
|
49 |
|
50 int m = a.rows (); |
|
51 int n = a.cols (); |
|
52 |
|
53 double *tmp_data = dup (a.data (), a.length ()); |
|
54 |
|
55 int min_mn = m < n ? m : n; |
|
56 int max_mn = m > n ? m : n; |
|
57 |
1251
|
58 char *jobu = "A"; |
|
59 char *jobv = "A"; |
537
|
60 |
|
61 int ncol_u = m; |
|
62 int nrow_vt = n; |
|
63 int nrow_s = m; |
|
64 int ncol_s = n; |
|
65 |
|
66 if (svd_type == SVD::economy) |
|
67 { |
1251
|
68 jobu = jobv ="S"; |
537
|
69 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
|
70 } |
|
71 |
|
72 double *u = new double[m * ncol_u]; |
457
|
73 double *s_vec = new double[min_mn]; |
537
|
74 double *vt = new double[nrow_vt * n]; |
457
|
75 |
|
76 int tmp1 = 3*min_mn + max_mn; |
|
77 int tmp2 = 5*min_mn - 4; |
|
78 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
79 double *work = new double[lwork]; |
|
80 |
1253
|
81 F77_FCN (dgesvd, DGESVD) (jobu, jobv, m, n, tmp_data, m, s_vec, u, |
|
82 m, vt, nrow_vt, work, lwork, info, 1L, |
|
83 1L); |
457
|
84 |
537
|
85 left_sm = Matrix (u, m, ncol_u); |
|
86 sigma = DiagMatrix (s_vec, nrow_s, ncol_s); |
|
87 Matrix vt_m (vt, nrow_vt, n); |
|
88 right_sm = vt_m.transpose (); |
457
|
89 |
|
90 delete [] tmp_data; |
|
91 delete [] work; |
|
92 |
|
93 return info; |
|
94 } |
|
95 |
|
96 ostream& |
|
97 operator << (ostream& os, const SVD& a) |
|
98 { |
|
99 os << a.left_singular_matrix () << "\n"; |
|
100 os << a.singular_values () << "\n"; |
|
101 os << a.right_singular_matrix () << "\n"; |
|
102 |
|
103 return os; |
|
104 } |
|
105 |
|
106 /* |
|
107 ;;; Local Variables: *** |
|
108 ;;; mode: C++ *** |
|
109 ;;; page-delimiter: "^/\\*" *** |
|
110 ;;; End: *** |
|
111 */ |