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