1 | #ifndef _theplu_utility_svd_ |
---|
2 | #define _theplu_utility_svd_ |
---|
3 | |
---|
4 | // $Id: SVD.h 675 2006-10-10 12:08:45Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) The authors contributing to this file. |
---|
8 | |
---|
9 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 2 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "yat/utility/matrix.h" |
---|
28 | #include "yat/utility/vector.h" |
---|
29 | |
---|
30 | #include <gsl/gsl_linalg.h> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace utility { |
---|
34 | |
---|
35 | // Jari check that interface is complete |
---|
36 | |
---|
37 | /** |
---|
38 | Class encapsulating GSL methods for singular value decomposition, |
---|
39 | SVD. |
---|
40 | |
---|
41 | A = U S V' = (MxN)(NxN)(NxN) = (MxN)\n |
---|
42 | |
---|
43 | A = Matrix to be decomposed, size MxN\n |
---|
44 | U = Orthogonal matrix, size MxN\n |
---|
45 | S = Diagonal matrix of singular values, size NxN\n |
---|
46 | V = Orthogonal matrix, size NxN\n |
---|
47 | */ |
---|
48 | |
---|
49 | class SVD |
---|
50 | { |
---|
51 | public: |
---|
52 | |
---|
53 | /// |
---|
54 | /// A number of SVD algorithms are implemented in GSL. They have |
---|
55 | /// their strengths and weaknesses, check the GSL documentation. |
---|
56 | /// |
---|
57 | /// There are restrictions on the matrix dimensions depending on |
---|
58 | /// which SVD algorithm is used. From the GSL's SVD source code |
---|
59 | /// one finds that the Golub-Reinsch algorithm implementation will |
---|
60 | /// not work on matrices with fewer rows than columns, the same is |
---|
61 | /// also true for the modified Golub-Reinsch algorithm. |
---|
62 | /// |
---|
63 | /// @see GSL's SVD documentation. |
---|
64 | /// |
---|
65 | enum SVDalgorithm { |
---|
66 | GolubReinsch, |
---|
67 | ModifiedGolubReinsch, |
---|
68 | Jacobi |
---|
69 | }; |
---|
70 | |
---|
71 | /// |
---|
72 | /// Constructs an SVD object using the matrix A as only input. The |
---|
73 | /// input matrix is copied for further use in the object. |
---|
74 | /// |
---|
75 | inline SVD(const utility::matrix& Ain) |
---|
76 | : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns()) {} |
---|
77 | |
---|
78 | inline ~SVD(void) {} |
---|
79 | |
---|
80 | /// |
---|
81 | /// This function will perform SVD with the method specified by \a |
---|
82 | /// algo. |
---|
83 | /// |
---|
84 | /// @return Whatever GSL returns. |
---|
85 | /// |
---|
86 | int decompose(SVDalgorithm algo=GolubReinsch); |
---|
87 | |
---|
88 | /// |
---|
89 | /// Access to the s vector. |
---|
90 | /// |
---|
91 | /// @return A copy of the s vector. |
---|
92 | /// |
---|
93 | /// @note If decompose() has not been run the outcome of the call |
---|
94 | /// is undefined. |
---|
95 | /// |
---|
96 | inline const utility::vector& s(void) const { return s_; } |
---|
97 | |
---|
98 | /// |
---|
99 | /// Solve the system \f$ Ax=b \f$ using the decomposition of A. |
---|
100 | /// |
---|
101 | /// @note If decompose() has not been run the outcome of the call |
---|
102 | /// is undefined. |
---|
103 | /// |
---|
104 | /// @return Whatever GSL returns. |
---|
105 | /// |
---|
106 | inline int solve(utility::vector b, utility::vector x) |
---|
107 | { return gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(), |
---|
108 | s_.gsl_vector_p(), b.gsl_vector_p(), |
---|
109 | x.gsl_vector_p()); } |
---|
110 | |
---|
111 | /// |
---|
112 | /// Access to the U matrix. |
---|
113 | /// |
---|
114 | /// @return A copy of the U matrix. |
---|
115 | /// |
---|
116 | /// @note If decompose() has not been run the outcome of the call |
---|
117 | /// is undefined. |
---|
118 | /// |
---|
119 | inline const utility::matrix& U(void) const { return U_; } |
---|
120 | |
---|
121 | /// |
---|
122 | /// Access to the V matrix. |
---|
123 | /// |
---|
124 | /// @return A copy of the V matrix. |
---|
125 | /// |
---|
126 | /// @note If decompose() has not been run the outcome of the call |
---|
127 | /// is undefined. |
---|
128 | /// |
---|
129 | inline const utility::matrix& V(void) const { return V_; } |
---|
130 | |
---|
131 | private: |
---|
132 | inline int jacobi(void) |
---|
133 | { return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_p(), V_.gsl_matrix_p(), |
---|
134 | s_.gsl_vector_p()); } |
---|
135 | int golub_reinsch(void); |
---|
136 | int modified_golub_reinsch(void); |
---|
137 | |
---|
138 | utility::matrix U_, V_; |
---|
139 | utility::vector s_; |
---|
140 | }; |
---|
141 | |
---|
142 | }} // of namespace utility and namespace theplu |
---|
143 | |
---|
144 | #endif |
---|