Changeset 11
- Timestamp:
- Jun 3, 2003, 6:21:31 PM (20 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Makefile.am
r4 r11 5 5 lib_LIBRARIES = libc++_tools.a 6 6 7 libc___tools_a_SOURCES = SVD.cc7 libc___tools_a_SOURCES = matrix.cc vector.cc SVD.cc PCA.cc random_singleton.cc histogram.cc 8 8 9 9 INCLUDES = -I/usr/local_bio/include -I/usr/local_bio -
trunk/src/SVD.cc
r7 r11 2 2 3 3 #include "SVD.h" 4 #include <iostream>5 #include <cmath>6 #include <cstdlib> //To include srand() and rand()7 #include <ctime>8 4 using namespace thep_cpp_tools; 9 5 … … 13 9 14 10 //Constructor initializing the svd object with matrix A to be decomposed 15 SVD::SVD( const gsl::matrix& Ain ) : A_( Ain )11 SVD::SVD( const thep_gsl_api::matrix& Ain ) : A_( Ain ) 16 12 { 17 13 //Get dimensions 18 14 //////////////// 19 size_t n = Ain. get_rows();15 size_t n = Ain.cols(); 20 16 21 17 //Instantiate matrix with correct dimenstions 22 18 //and assign all elements the value zero 23 19 ///////////////////////////////////////////// 24 V_.set_dimensions( n, n ); 25 V_.set_zero(); 26 27 20 V_ = thep_gsl_api::matrix( n, n, true ); 21 28 22 //Assign vector s_ with correct dimenstion 29 23 ///////////////////////////////////////////////// 30 s_ = gsl::vector( n );24 s_ = thep_gsl_api::vector( n ); 31 25 32 26 loaded_ = true; … … 39 33 //Ax = b 40 34 //The x vector will be reached using the get_x() function. 41 SVD::SVD( const gsl::matrix& Ain, const gsl::vector& b ) : A_( Ain ), b_( b )35 SVD::SVD( const thep_gsl_api::matrix& Ain, const thep_gsl_api::vector& b ) : A_( Ain ), b_( b ) 42 36 { 43 37 //Get dimensions 44 38 //////////////// 45 size_t n = Ain. get_rows();39 size_t n = Ain.cols(); 46 40 47 41 //Instantiate matrix with correct dimenstions 48 42 //and assign all elements the value zero 49 43 ///////////////////////////////////////////// 50 V_.set_dimensions( n, n ); 51 V_.set_zero(); 44 V_ = thep_gsl_api::matrix( n, n, true ); 52 45 53 46 54 47 //Assign vector s_ with correct dimenstion 55 48 ///////////////////////////////////////////////// 56 s_ = gsl::vector( n );49 s_ = thep_gsl_api::vector( n ); 57 50 58 51 59 52 //An extra vector holding the answer to equation Ax = b is required 60 53 /////////////////////////////////////////////////////////////////// 61 x_ = gsl::vector( n );54 x_ = thep_gsl_api::vector( n ); 62 55 63 56 … … 120 113 //required here 121 114 /////////////////////////////// 122 gsl::vector w( A_.get_rows() );123 if( gsl_linalg_SV_decomp( A_.g slobj(), V_.gslobj(),124 s_.g slobj(), w.gslobj()115 thep_gsl_api::vector w( A_.cols() ); 116 if( gsl_linalg_SV_decomp( A_.get_gsl_matrix(), V_.get_gsl_matrix(), 117 s_.get_gsl_vector(), w.get_gsl_vector() 125 118 ) != 0 ) 126 119 { … … 141 134 //required here 142 135 /////////////////////////////// 143 size_t n = A_.get_rows(); 144 gsl::vector w( n ); 145 gsl::matrix X; 146 147 X.set_dimensions( n, n ); 148 149 if( gsl_linalg_SV_decomp_mod( A_.gslobj(), X.gslobj(), 150 V_.gslobj(), s_.gslobj(), 151 w.gslobj() 136 size_t n = A_.rows(); 137 thep_gsl_api::vector w( n ); 138 thep_gsl_api::matrix X( n, n, true ); 139 140 if( gsl_linalg_SV_decomp_mod( A_.get_gsl_matrix(), X.get_gsl_matrix(), 141 V_.get_gsl_matrix(), s_.get_gsl_vector(), 142 w.get_gsl_vector() 152 143 ) != 0 ) 153 144 { … … 167 158 //Perform SVD using GSL library 168 159 /////////////////////////////// 169 if( gsl_linalg_SV_decomp_jacobi( A_.g slobj(), V_.gslobj(),170 s_.g slobj()160 if( gsl_linalg_SV_decomp_jacobi( A_.get_gsl_matrix(), V_.get_gsl_matrix(), 161 s_.get_gsl_vector() 171 162 ) != 0 ) 172 163 { … … 188 179 189 180 //Solve ... 190 if( gsl_linalg_SV_solve( A_.g slobj(), V_.gslobj(),191 s_.g slobj(), b_.gslobj(),192 x_.g slobj()181 if( gsl_linalg_SV_solve( A_.get_gsl_matrix(), V_.get_gsl_matrix(), 182 s_.get_gsl_vector(), b_.get_gsl_vector(), 183 x_.get_gsl_vector() 193 184 ) != 0 ) 194 185 { … … 204 195 205 196 //This method will create a diagonal matrix of vector s_ 206 gsl::matrix SVD::get_s() const207 { 208 size_t n = A_. get_rows();197 thep_gsl_api::matrix SVD::get_s() const 198 { 199 size_t n = A_.rows(); 209 200 210 gsl::matrix S; 211 S.set_dimensions( n, n ); 212 S.set_zero(); 201 thep_gsl_api::matrix S( n, n, true ); 213 202 214 203 for( size_t i = 0; i < n; ++i ) 215 204 { 216 S.set _element( i, i, s_( i ) );205 S.set( i, i, s_.get( i ) ); 217 206 } 218 207 … … 236 225 /////////////////////////////// 237 226 const size_t DIM = 3; 238 gsl::matrix A( DIM, DIM );227 thep_gsl_api::matrix A( DIM, DIM ); 239 228 for( size_t i = 0; i < DIM; ++i ) 240 229 { 241 230 for( size_t j = 0; j < DIM; ++j ) 242 231 { 243 A ( i, j ) = 10 * (double) rand() / RAND_MAX; //Use random numbers between 0 and 10232 A.set( i, j, 10 * (double) rand() / RAND_MAX ); //Use random numbers between 0 and 10 244 233 } 245 234 } … … 249 238 ////////// 250 239 A_ = A; 251 size_t n = A_. get_rows();252 size_t m = A_. get_cols();240 size_t n = A_.rows(); 241 size_t m = A_.cols(); 253 242 n = m = DIM; 254 243 loaded_ = true; … … 257 246 //Set work matrices 258 247 /////////////////// 259 V_.set_dimensions( n, n ); 260 V_.set_zero(); 261 248 V_ = thep_gsl_api::matrix( n, n, true ); 262 249 263 250 //Assign vector s_ with correct dimensions 264 251 ///////////////////////////////////////////////// 265 s_ = gsl::vector( n );252 s_ = thep_gsl_api::vector( n ); 266 253 267 254 double error = 1; 268 gsl::matrix E, S;255 thep_gsl_api::matrix E, S; 269 256 270 257 … … 281 268 this->process(); 282 269 S = this->get_s(); 270 283 271 E = A - A_*S*V_.transpose(); 272 273 284 274 error = sqrt( E.norm( 2 ) ); 285 275 cout << "error(Unmodified method) = " << error << endl; -
trunk/src/SVD.h
r7 r11 4 4 #define _THEP_CPPTOOLS_SVD_ 5 5 6 //GSLwrap includes 7 ////////////////// 8 #include <gslwrap/matrix_double.h> 9 #include <gslwrap/vector_double.h> 6 // C++ tools include 7 ///////////////////// 8 #include "matrix.h" 9 #include "vector.h" 10 #include <gsl/gsl_vector.h> 11 12 13 // Standard includes 14 //////////////////// 15 #include <iostream> 16 #include <cmath> 17 #include <cstdlib> //To include srand() and rand() 18 #include <ctime> 10 19 11 20 … … 26 35 class SVD 27 36 { 28 static const double MAXTOL = 1E- 10;37 static const double MAXTOL = 1E-6; 29 38 30 39 enum SVDtypes … … 38 47 public: 39 48 /** 49 The default constructor should only be used for testing!!! 50 */ 51 SVD(); 52 53 54 /** 40 55 Constructs an SVD object using the matrix A as only 41 56 input. The input matrix is copied for further use in the 42 57 object. 43 58 */ 44 SVD( const gsl::matrix& );59 SVD( const thep_gsl_api::matrix& ); 45 60 46 61 /** 47 62 Constructor initializing the SVD object for Solver. 48 Solver requires an extra-vector paramter \a b: $Ax = b$49 The $x$ vector will be reached using the get_x()63 Solver requires an extra-vector paramter \a b: \f$Ax = b\f$ 64 The \f$x\f$ vector will be reached using the get_x() 50 65 function. 51 66 @see get_x() function. 52 67 */ 53 68 54 SVD( const gsl::matrix&, const gsl::vector& b);69 SVD( const thep_gsl_api::matrix&, const thep_gsl_api::vector& b); 55 70 ~SVD(); 56 71 … … 61 76 Modified: This method is faster when M>>N. (see GSL documentation 62 77 about gsl_linalg_SV_decomp_mod function)\n 63 Ja koby: Computes singular values to higer accuracy than default method78 Jacobi: Computes singular values to higer accuracy than default method 64 79 (see GSL documentation about gsl_linalg_SV_decomp_jacobi.\n 65 80 Solver: Using Unmodified to perform SVD and then solves the system \f$Ax=b\f$ … … 71 86 executed. 72 87 */ 73 gsl::matrix get_u() const { return A_; }88 thep_gsl_api::matrix get_u() const { return A_; } 74 89 75 90 /** … … 77 92 executed. 78 93 */ 79 gsl::matrix get_v() const { return V_; }94 thep_gsl_api::matrix get_v() const { return V_; } 80 95 81 96 … … 87 102 */ 88 103 89 gsl::vector get_x() const { return x_; }104 thep_gsl_api::vector get_x() const { return x_; } 90 105 91 106 /** … … 93 108 executed. 94 109 */ 95 gsl::matrix get_s() const; 110 thep_gsl_api::matrix get_s() const; 111 96 112 97 113 /** 98 This method will execute Default, Unmodified and Jakoby methods one at a time 114 Function returning diagonal matrix S in vector form. 115 */ 116 thep_gsl_api::vector get_s_vec() const { return s_; } 117 118 119 /** 120 This method will execute Default, Unmodified and Jacobi methods one at a time 99 121 and calculate the error, \f$ e = \left\Vert A - USV^{T} \right\Vert_{2} \f$. 100 The method will return "true" if \f$e < 10^{- 10}\f$ else false. A test program122 The method will return "true" if \f$e < 10^{-6}\f$ else false. A test program 101 123 is available that executes this method, c++_tools/test/svd_test. No test is 102 124 performed for solver but the aim is to add one in the future. … … 105 127 106 128 private: 107 SVD();108 129 109 gsl::matrix A_, V_;110 gsl::vector s_, b_, x_;130 thep_gsl_api::matrix A_, V_; 131 thep_gsl_api::vector s_, b_, x_; 111 132 bool loaded_, solver_; 112 133 -
trunk/test/Makefile.am
r5 r11 3 3 ## $Id$ 4 4 5 check_PROGRAMS = test_svd 5 check_PROGRAMS = test_rnd 6 #test_pca 7 #test_svd 6 8 7 test_ svd_SOURCES = test_svd.cc8 test_ svd_LDADD = -L@top_srcdir@/$(CPP_TOOLS_LIB_LOCATION) $(CPP_TOOLS_LIB) \9 test_rnd_SOURCES = test_rnd.cc 10 test_rnd_LDADD = -L@top_srcdir@/$(CPP_TOOLS_LIB_LOCATION) $(CPP_TOOLS_LIB) \ 9 11 -L/usr/local_bio/lib -L/usr/local_bio/lib/Linux_P4SSE2 \ 10 12 $(GSLWRAP_LIB) $(GSL_LIB) $(CBLAS_LIB) $(MATH_LIB) 11 13 14 #test_pca_SOURCES = test_pca.cc 15 #test_pca_LDADD = -L@top_srcdir@/$(CPP_TOOLS_LIB_LOCATION) $(CPP_TOOLS_LIB) \ 16 # -L/usr/local_bio/lib -L/usr/local_bio/lib/Linux_P4SSE2 \ 17 # $(GSLWRAP_LIB) $(GSL_LIB) $(CBLAS_LIB) $(MATH_LIB) 18 19 #test_svd_SOURCES = test_svd.cc 20 #test_svd_LDADD = -L@top_srcdir@/$(CPP_TOOLS_LIB_LOCATION) $(CPP_TOOLS_LIB) \ 21 # -L/usr/local_bio/lib -L/usr/local_bio/lib/Linux_P4SSE2 \ 22 # $(GSLWRAP_LIB) $(GSL_LIB) $(CBLAS_LIB) $(MATH_LIB) 23 12 24 INCLUDES = -I@top_srcdir@/$(CPP_TOOLS_HEADER_LOCATION) \ 13 25 -I/usr/local_bio/include
Note: See TracChangeset
for help on using the changeset viewer.