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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
457
|
26 #endif |
|
27 |
|
28 #include "dbleSVD.h" |
|
29 #include "mx-inlines.cc" |
|
30 #include "f77-uscore.h" |
|
31 |
|
32 extern "C" |
|
33 { |
1251
|
34 int F77_FCN (dgesvd) (const char*, const char*, const int&, |
|
35 const int&, double*, const int&, double*, |
|
36 double*, const int&, double*, const int&, |
|
37 double*, const int&, int&, long, long); |
457
|
38 } |
|
39 |
|
40 int |
537
|
41 SVD::init (const Matrix& a, SVD::type svd_type) |
457
|
42 { |
|
43 int info; |
|
44 |
|
45 int m = a.rows (); |
|
46 int n = a.cols (); |
|
47 |
|
48 double *tmp_data = dup (a.data (), a.length ()); |
|
49 |
|
50 int min_mn = m < n ? m : n; |
|
51 int max_mn = m > n ? m : n; |
|
52 |
1251
|
53 char *jobu = "A"; |
|
54 char *jobv = "A"; |
537
|
55 |
|
56 int ncol_u = m; |
|
57 int nrow_vt = n; |
|
58 int nrow_s = m; |
|
59 int ncol_s = n; |
|
60 |
|
61 if (svd_type == SVD::economy) |
|
62 { |
1251
|
63 jobu = jobv ="S"; |
537
|
64 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
|
65 } |
|
66 |
|
67 double *u = new double[m * ncol_u]; |
457
|
68 double *s_vec = new double[min_mn]; |
537
|
69 double *vt = new double[nrow_vt * n]; |
457
|
70 |
|
71 int tmp1 = 3*min_mn + max_mn; |
|
72 int tmp2 = 5*min_mn - 4; |
|
73 int lwork = tmp1 > tmp2 ? tmp1 : tmp2; |
|
74 double *work = new double[lwork]; |
|
75 |
1251
|
76 F77_FCN (dgesvd) (jobu, jobv, m, n, tmp_data, m, s_vec, u, m, |
|
77 vt, nrow_vt, work, lwork, info, 1L, 1L); |
457
|
78 |
537
|
79 left_sm = Matrix (u, m, ncol_u); |
|
80 sigma = DiagMatrix (s_vec, nrow_s, ncol_s); |
|
81 Matrix vt_m (vt, nrow_vt, n); |
|
82 right_sm = vt_m.transpose (); |
457
|
83 |
|
84 delete [] tmp_data; |
|
85 delete [] work; |
|
86 |
|
87 return info; |
|
88 } |
|
89 |
|
90 ostream& |
|
91 operator << (ostream& os, const SVD& a) |
|
92 { |
|
93 os << a.left_singular_matrix () << "\n"; |
|
94 os << a.singular_values () << "\n"; |
|
95 os << a.right_singular_matrix () << "\n"; |
|
96 |
|
97 return os; |
|
98 } |
|
99 |
|
100 /* |
|
101 ;;; Local Variables: *** |
|
102 ;;; mode: C++ *** |
|
103 ;;; page-delimiter: "^/\\*" *** |
|
104 ;;; End: *** |
|
105 */ |