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