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