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