Changeset 420
- Timestamp:
- Dec 2, 2005, 1:15:50 AM (18 years ago)
- Location:
- trunk
- Files:
-
- 30 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/gslapi/matrix.cc
r357 r420 7 7 8 8 #include <cmath> 9 #include <iostream>10 9 #include <sstream> 11 #include <string>12 10 #include <vector> 13 11 14 12 #include <gsl/gsl_blas.h> 15 #include <gsl/gsl_linalg.h>16 13 17 14 … … 21 18 22 19 23 matrix::matrix( void)24 : m_(NULL)20 matrix::matrix(matrix& m, size_t offset_row, size_t offset_column, 21 size_t n_row, size_t n_column) 25 22 { 26 } 27 28 29 30 matrix::matrix(const size_t& r, const size_t& c, double init_value) 31 { 32 m_ = gsl_matrix_alloc(r,c); 33 set_all(init_value); 34 } 35 36 37 38 matrix::matrix(const matrix& other) 39 { 40 m_ = other.gsl_matrix_copy(); 23 // Jari, exception handling needed here. Failure in setting up a 24 // proper gsl_matrix_view is communicated by NULL pointer in the 25 // view structure (cf. GSL manual). How about GSL error state? 26 view_ = new gsl_matrix_view(gsl_matrix_submatrix(m.m_, 27 offset_row,offset_column, 28 n_row,n_column)); 29 m_ = &(view_->matrix); 41 30 } 42 31 … … 44 33 45 34 // Constructor that gets data from istream 46 matrix::matrix(std::istream& is) 35 matrix::matrix(std::istream& is) throw (utility::IO_error,std::exception) 36 : view_(NULL) 47 37 { 48 38 // read the data file and store in stl vectors (dynamically … … 53 43 std::vector<double> v; 54 44 for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) { 55 56 45 // Ignoring empty lines 57 if (!v.size()){46 if (!v.size()) { 58 47 nof_rows--; 59 48 continue; 60 49 } 61 62 if(nof_columns==0) 50 if (!nof_columns) 63 51 nof_columns=v.size(); 64 else if(v.size()!=nof_columns) { 65 std::cerr << "matrix::matrix(std::istream&) data file error: " 66 << "line" << nof_rows+1 << " has " << v.size() 67 << " columns; expected " << nof_columns 68 << " columns" 69 << std::endl; 70 exit(1); 52 else if (v.size()!=nof_columns) { 53 std::ostringstream s; 54 s << "matrix::matrix(std::istream&) data file error: " 55 << "line" << nof_rows+1 << " has " << v.size() 56 << " columns; expected " << nof_columns << " columns."; 57 throw utility::IO_error(s.str()); 71 58 } 72 59 data_matrix.push_back(v); … … 77 64 // convert the data to a gsl matrix 78 65 m_ = gsl_matrix_alloc ( nof_rows, nof_columns ); 79 for(u_int i=0;i<nof_rows;i++) 80 for(u_int j=0;j<nof_columns;j++) 66 for(u_int i=0;i<nof_rows;i++) 67 for(u_int j=0;j<nof_columns;j++) 81 68 gsl_matrix_set( m_, i, j, data_matrix[i][j] ); 82 69 } … … 86 73 matrix::~matrix(void) 87 74 { 88 if (m_) { 75 if (view_) 76 delete view_; 77 else if (m_) 89 78 gsl_matrix_free(m_); 90 m_=NULL; 79 m_=NULL; 80 } 81 82 83 84 bool matrix::equal(const matrix& other, const double d) const 85 { 86 if (columns()!=other.columns() || rows()!=other.rows()) 87 return false; 88 for (size_t i=0; i<rows(); i++) 89 for (size_t j=0; j<columns(); j++) 90 // The two last condition checks are needed for NaN detection 91 if (fabs( (*this)(i,j)-other(i,j) ) > d || 92 (*this)(i,j)!=(*this)(i,j) || other(i,j)!=other(i,j)) 93 return false; 94 return true; 95 } 96 97 98 99 gsl_matrix* matrix::create_gsl_matrix_copy(void) const 100 { 101 gsl_matrix* m = gsl_matrix_alloc(rows(),columns()); 102 gsl_matrix_memcpy(m,m_); // Jari, a GSL return value is ignored here 103 return m; 104 } 105 106 107 108 // Jari, checkout GSL transpose support in GSL manual 8.4.9 109 void matrix::transpose(void) 110 { 111 if (columns()==rows()) 112 gsl_matrix_transpose(m_); 113 else { 114 gsl_matrix* transposed = gsl_matrix_alloc(columns(),rows()); 115 gsl_matrix_transpose_memcpy(transposed,m_); 116 gsl_matrix_free(m_); 117 m_=transposed; 91 118 } 92 119 } … … 94 121 95 122 96 gsl_matrix* matrix::gsl_matrix_copy(void) const 97 { 98 gsl_matrix* m = gsl_matrix_alloc(rows(),columns()); 99 gsl_matrix_memcpy(m,m_); 100 return m; 101 } 102 103 bool matrix::equal(const matrix& other, const double d) const 104 { 105 if (columns()!=other.columns() || rows()!=other.rows()) 106 return false; 107 for (size_t i=0; i<rows(); i++) 108 for (size_t j=0; j<columns(); j++) 109 // two last cond. are to check for nans 110 if (fabs( (*this)(i,j)-other(i,j) ) > d || 111 (*this)(i,j)!=(*this)(i,j) || other(i,j)!=other(i,j)) 112 return false; 113 114 return true; 115 } 116 117 vector matrix::TEMP_col_return(size_t column) const 118 { 119 vector vec(rows()); 120 for (size_t i=0; i<rows(); ++i) 121 vec(i)=operator()(i,column); 122 return vec; 123 } 124 125 126 127 vector matrix::operator[](size_t row) const 128 { 129 vector vec(columns()); 130 for (size_t i=0; i<columns(); ++i) 131 vec(i)=operator()(row,i); 132 return vec; 133 } 134 135 136 137 matrix matrix::operator*( const matrix &other ) const 138 { 139 matrix res(rows(), other.columns()); 140 gsl_linalg_matmult(m_,other.m_,res.m_); 141 return res; 142 } 143 144 145 146 vector matrix::operator*( const vector &other ) const 147 { 148 gsl_vector* gslvec=gsl_vector_alloc(rows()); 149 gsl_blas_dgemv(CblasNoTrans, 1.0, m_, other.gsl_vector_pointer(), 0.0, 150 gslvec ); 151 vector res(gslvec); 152 return res; 153 } 154 155 156 157 matrix matrix::operator+( const matrix &other ) const 158 { 159 matrix res( *this ); 160 gsl_matrix_add( res.m_, other.m_ ); 161 return res; 162 } 163 164 165 166 matrix matrix::operator-( const matrix &other ) const 167 { 168 matrix res( *this ); 169 gsl_matrix_sub( res.m_, other.m_ ); 170 return res; 171 } 172 173 174 175 matrix& matrix::operator=( const matrix& other ) 123 const matrix& matrix::operator=( const matrix& other ) 176 124 { 177 125 if ( this != &other ) { 178 if ( m_ ) 179 gsl_matrix_free( m_ ); 180 m_ = other.gsl_matrix_copy(); 181 } 126 if (view_) 127 delete view_; 128 else if (m_) 129 gsl_matrix_free(m_); 130 m_ = other.create_gsl_matrix_copy(); 131 } 182 132 return *this; 183 133 } … … 185 135 186 136 187 matrix matrix::transpose(void) const137 const matrix& matrix::operator*=(const matrix& other) 188 138 { 189 matrix res(columns(),rows()); 190 gsl_matrix_transpose_memcpy(res.m_,m_); 191 return res; 139 gsl_matrix* result = gsl_matrix_alloc(rows(),other.columns()); 140 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, m_, other.m_, 0.0, result); 141 gsl_matrix_free(m_); 142 m_=result; 143 return *this; 192 144 } 193 145 … … 196 148 std::ostream& operator<<(std::ostream& s, const matrix& m) 197 149 { 198 using namespace std; 199 s.setf( ios::dec ); 150 s.setf(std::ios::dec); 200 151 s.precision(12); 201 for(size_t i=0, j=0; i<m.rows(); i++) 152 for(size_t i=0, j=0; i<m.rows(); i++) 202 153 for (j=0; j<m.columns(); j++) { 203 154 s << m(i,j); … … 207 158 s << "\n"; 208 159 } 209 160 return s; 210 161 } 211 162 -
trunk/lib/gslapi/matrix.h
r390 r420 1 1 // $Id$ 2 2 3 #ifndef _theplu_gslapi_matrix_ 4 #define _theplu_gslapi_matrix_ 3 #ifndef _theplu_gslapi_matrix_ 4 #define _theplu_gslapi_matrix_ 5 5 6 6 #include <c++_tools/gslapi/vector.h> 7 #include <c++_tools/utility/Exception.h> 7 8 8 9 #include <gsl/gsl_matrix.h> 9 10 #include <iostream> 10 11 11 12 12 namespace theplu { … … 20 20 /// well in the future. 21 21 /// 22 class matrix 23 { 24 public: 22 /// \par[File streams] Reading and writing vectors to file streams 23 /// are of course supported. These are implemented without using GSL 24 /// functionality, and thus binary read and write to streams are not 25 /// supported. 26 /// 27 /// \par[Matrix views] GSL matrix views are supported and these are 28 /// disguised as ordinary gslapi::matrix'es. A support function is 29 /// added, gslapi::matrix::isview(), that can be used to check if a 30 /// matrix object is a view. Note that view matricess do not own the 31 /// undelying data, and a view is not valid if the matrix owning the 32 /// data is deallocated. 33 /// 34 /// @note All GSL matrix related functions are not implement but 35 /// most functionality defined for GSL matrices can be achieved with 36 /// this interface class. Most notable GSL functionality not 37 /// supported are no binary file support and views on arrays, 38 /// gslapi::vectors, gsl_vectors, diagonals, subdiagonals, and 39 /// superdiagonals. If there is an interest from the user community, 40 /// the omitted functionality could be included. 41 /// 42 /// @todo Maybe it would be smart to create temporary objects need 43 /// for BLAS in constructors. As it is now temporary objects are 44 /// called before BLAS functionality iss used, cf. const matrix& 45 /// matrix::operator*=(const matrix& other) 46 /// 47 class matrix 48 { 49 public: 25 50 26 51 /// 27 52 /// The default constructor. 28 /// 29 matrix(void); 53 /// 54 /// This contructor does not initialize underlying (essential) 55 /// structures. 56 /// 57 inline matrix(void) : m_(NULL), view_(NULL) {} 30 58 31 59 /// 32 60 /// Constructor. Allocates memory space for \a r times \a c 33 61 /// elements, and sets all elements to \a init_value. 34 /// 35 matrix(const size_t& r, const size_t& c, const double init_value=0); 36 37 /// 62 /// 63 inline matrix(const size_t& r, const size_t& c, double init_value=0) 64 : view_(NULL) { m_ = gsl_matrix_alloc(r,c); set_all(init_value); } 65 66 /// 38 67 /// The copy constructor. 39 /// 40 matrix(const matrix&); 68 /// 69 /// @note If the object to be copied is a matrix view, the values 70 /// of the view will be copied, i.e. the view is not copied. 71 /// 72 inline matrix(const matrix& o) : view_(NULL) {m_=o.create_gsl_matrix_copy();} 73 74 /// 75 /// The matrix view constructor. 76 /// 77 /// Create a view of matrix \a m, with starting row \a offset_row, 78 /// starting column \a offset_column, row size \a n_row, and 79 /// column size \a n_column. 80 /// 81 /// A matrix view can be used as any matrix with the difference 82 /// that changes made to the view will also change the object that 83 /// is viewed. Also, using the copy constructor will create a new 84 /// matrix object that is a copy of whatever is viewed. If a copy 85 /// of the view is needed then you should use this constructor to 86 /// obtain a copy. 87 /// 88 /// @note If the object viewed by the view goes out of scope or is 89 /// deleted, the view becomes invalid and the result of further 90 /// use is undefined. 91 /// 92 matrix(matrix& m, size_t offset_row, size_t offset_column, size_t n_row, 93 size_t n_column); 41 94 42 95 /// … … 46 99 /// column elements in a row must be separated with white space 47 100 /// characters except the new line (\\n) character. Rows, and 48 /// columns, are read conse quently and only rectangular matrices101 /// columns, are read consecutively and only rectangular matrices 49 102 /// are supported. Empty lines are ignored. End of input is at end 50 103 /// of file marker. 51 104 /// 52 explicit matrix(std::istream &) ;53 54 105 explicit matrix(std::istream &) throw (utility::IO_error,std::exception); 106 107 /// 55 108 /// The destructor. 56 109 /// 57 ~matrix(void); 58 59 /// 110 ~matrix(void); 111 112 /// 113 /// Elementwise addition of the elements of matrix \a b to the 114 /// elements of the calling matrix ,\f$a_{ij} = a_{ij} + b_{ij} \; 115 /// \forall i,j\f$. The result is stored into the calling matrix. 116 /// 117 /// @return Whatever GSL returns. 118 /// 119 inline int add(const matrix& b) { return gsl_matrix_add(m_,b.m_); } 120 121 /// 122 /// Add the scalar value \a d to the elements of the calling 123 /// matrix, \f$a_{ij} = a_{ij} + d \; \forall i,j\f$. The result 124 /// is stored into the calling matrix. 125 /// 126 /// @return Whatever GSL returns. 127 /// 128 inline int 129 add_constant(const double d) { return gsl_matrix_add_constant(m_,d); } 130 131 /// 60 132 /// @return The number of columns in the matrix. 61 133 /// 62 inline size_t columns(void) const { return m_->size2; } 63 64 /// 65 /// Swap column \a i with \a j. 66 /// 67 inline void column_swap(const size_t& i,const size_t& j) 68 { gsl_matrix_swap_columns(m_,i,j); } 69 70 /// 71 /// @return true if each element deviates less or equal than \a 72 /// d. If any matrix contain a nan false is always returned. 73 /// 74 bool equal(const matrix&, const double d=0) const; 134 inline size_t columns(void) const { return m_->size2; } 135 136 /// 137 /// Elementwise division of the elemnts of the calling matrix by 138 /// the elements of matrix \a b, \f$a_{ij} = a_{ij} / b_{ij} \; 139 /// \forall i,j\f$. The result is stored into the calling matrix. 140 /// 141 /// @return Whatever GSL returns. 142 /// 143 inline int 144 div_elements(const matrix& b) { return gsl_matrix_div_elements(m_,b.m_); } 145 146 /// 147 /// Check whether matrices are equal within a user defined 148 /// precision, set by \a precision. 149 /// 150 /// @return True if each element deviates less or equal than \a 151 /// d. If any matrix contain a NaN, false is always returned. 152 /// 153 bool equal(const matrix&, const double precision=0) const; 154 155 /// 156 /// @return A const pointer to the internal GSL matrix. 157 /// 158 inline const gsl_matrix* gsl_matrix_p(void) const { return m_; } 159 160 /// 161 /// @return A pointer to the internal GSL matrix. 162 /// 163 // Jari, is this needed? 164 inline gsl_matrix* gsl_matrix_p(void) { return m_; }; 165 166 /// 167 /// @return True if all elements in the matrix is zero, false 168 /// othwerwise; 169 /// 170 inline bool isnull(void) const { return gsl_matrix_isnull(m_); } 171 172 /// 173 /// Check if the matrix object is a view (sub-matrix) to another 174 /// matrix. 175 /// 176 /// @return True if the object is a view, false othwerwise. 177 /// 178 inline bool isview(void) const { return view_; } 179 180 /// 181 /// @return The maximum value of the matrix. 182 /// 183 inline double max(void) const { return gsl_matrix_max(m_); } 184 185 /// 186 /// @return The index to the element with the maximum value of the 187 /// matrix. The lowest index has precedence (searching in 188 /// row-major order). 189 /// 190 inline std::pair<size_t,size_t> max_index(void) const; 191 192 /// 193 /// @return The minimum value of the matrix. 194 /// 195 inline double min(void) const { return gsl_matrix_min(m_); } 196 197 /// 198 /// @return The index to the element with the minimum value of the 199 /// matrix. The lowest index has precedence (searching in 200 /// row-major order). 201 /// 202 inline std::pair<size_t,size_t> min_index(void) const; 203 204 /// 205 /// @return The indecies to the element with the minimum and 206 /// maximum values of the matrix, respectively. The lowest index 207 /// has precedence (searching in row-major order). 208 /// 209 inline void minmax_index(std::pair<size_t,size_t>& min, 210 std::pair<size_t,size_t>& max) const 211 { gsl_matrix_minmax_index(m_,&min.first,&min.second, 212 &max.first,&max.second); } 213 214 /// 215 /// @return The minimum and maximum values of the matrix, as the 216 /// \a first and \a second member of the returned \a pair, 217 /// respectively. 218 /// 219 std::pair<double,double> minmax(void) const; 220 221 /// 222 /// Multiply the elements of matrix \a b with the elements of the 223 /// calling matrix ,\f$a_{ij} = a_{ij} * b_{ij} \; \forall 224 /// i,j\f$. The result is stored into the calling matrix. 225 /// 226 /// @return Whatever GSL returns. 227 /// 228 inline int 229 mul_elements(const matrix& b) { return gsl_matrix_mul_elements(m_,b.m_); } 230 231 /// 232 /// @return The number of rows in the matrix. 233 /// 234 inline size_t rows(void) const { return m_->size1; } 235 236 /// 237 /// Multiply the elements of the calling matrix with a scalar \a 238 /// d, \f$a_{ij} = d * a_{ij} \; \forall i,j\f$. The result is 239 /// stored into the calling matrix. 240 /// 241 /// @return Whatever GSL returns. 242 /// 243 inline int scale(const double d) { return gsl_matrix_scale(m_,d); } 244 245 /// 246 /// Set element values to values in \a mat. This function is 247 /// needed for assignment of viewed elements. 248 /// 249 /// @return Whatever GSL returns. 250 /// 251 /// @note No check on size is done. 252 /// 253 /// @see const matrix& operator=(const matrix&) 254 /// 255 inline int set(const matrix& mat) { return gsl_matrix_memcpy(m_,mat.m_); } 256 257 /// 258 /// Set all elements to \a value. 259 /// 260 inline void set_all(const double value) { gsl_matrix_set_all(m_,value); } 261 262 /// 263 /// Set \a column values to values in \a vec. 264 /// 265 /// @return Whatever GSL returns. 266 /// 267 /// @note No check on size is done. 268 /// 269 inline int set_column(const size_t column, const vector& vec) 270 { return gsl_matrix_set_col(m_, column, vec.gsl_vector_p()); } 271 272 /// 273 /// Set \a row values to values in \a vec. 274 /// 275 /// @return Whatever GSL returns. 276 /// 277 /// @note No check on size is done. 278 /// 279 inline int set_row(const size_t row, const vector& vec) 280 { return gsl_matrix_set_row(m_, row, vec.gsl_vector_p()); } 281 282 /// 283 /// Subtract the elements of matrix \a b from the elements of the 284 /// calling matrix ,\f$a_{ij} = a_{ij} - b_{ij} \; \forall 285 /// i,j\f$. The result is stored into the calling matrix. 286 /// 287 /// @return Whatever GSL returns. 288 /// 289 inline int sub(const matrix& b) { return gsl_matrix_sub(m_,b.m_); } 290 291 /// 292 /// Exchange the elements of the this and \a other by copying. The 293 /// two matrices must have the same size. 294 /// 295 /// @return Whatever GSL returns. 296 /// 297 inline int swap(matrix& other) { return gsl_matrix_swap(m_,other.m_); } 298 299 /// 300 /// Swap columns \a i and \a j. 301 /// 302 inline int swap_columns(const size_t i,const size_t j) 303 { return gsl_matrix_swap_columns(m_,i,j); } 304 305 /// 306 /// Swap row \a i and column \a j. 307 /// 308 inline int swap_rowcol(const size_t i,const size_t j) 309 { return gsl_matrix_swap_rowcol(m_,i,j); } 310 311 /// 312 /// Swap rows \a i and \a j. 313 /// 314 inline int swap_rows(const size_t i, const size_t j) 315 { return gsl_matrix_swap_rows(m_,i,j); } 316 317 /// 318 /// Transpose the matrix. 319 /// 320 void transpose(void); 321 322 /// 323 /// @return Reference to the element position (\a row, \a column). 324 /// 325 inline double& operator()(size_t row,size_t column) 326 { return (*gsl_matrix_ptr(m_,row,column)); } 327 328 /// 329 /// @return Const reference to the element position (\a row, \a 330 /// column). 331 /// 332 inline const double& operator()(size_t row,size_t column) const 333 { return (*gsl_matrix_const_ptr(m_,row,column)); } 334 335 /// 336 /// Matrix-vector multiplication. 337 /// 338 /// @return The resulting vector. 339 /// 340 // Jari, where should this go? 341 const vector operator*(const vector&) const; 342 343 /// 344 /// Comparison operator. 345 /// 346 /// @return True if all elements are equal otherwise False. 347 /// 348 /// @see equal 349 /// 350 inline bool operator==(const matrix& other) const { return equal(other); } 351 352 /// 353 /// Comparison operator. 354 /// 355 /// @return False if all elements are equal otherwise True. 356 /// 357 /// @see equal 358 /// 359 inline bool operator!=(const matrix& other) const { return !equal(other); } 360 361 /// 362 /// The assignment operator. There is no requirements on 363 /// dimensions, i.e. the matrix is remapped in memory if 364 /// necessary. This implies that in general views cannot be 365 /// assigned using this operator. Views will be mutated into 366 /// normal matrices. The only exception to this behaviour on views 367 /// is when self-assignemnt is done, since self-assignment is 368 /// ignored. 369 /// 370 /// @return A const reference to the resulting matrix. 371 /// 372 /// @see int set(const matrix&) 373 /// 374 const matrix& operator=(const matrix& other); 375 376 /// 377 /// Add and assign operator. 378 /// 379 inline const matrix& operator+=(const matrix& m) { add(m); return *this; } 380 381 /// 382 /// Add and assign operator. 383 /// 384 inline const matrix& 385 operator+=(const double d) { add_constant(d); return *this; } 386 387 /// 388 /// Subtract and assign operator. 389 /// 390 inline const matrix& operator-=(const matrix& m) { sub(m); return *this; } 391 392 /// 393 /// Multiply and assigment operator. 394 /// 395 /// @return Const reference to the resulting matrix. 396 /// 397 const matrix& operator*=(const matrix&); 398 399 /// 400 /// Multiply and assign operator. 401 /// 402 inline const matrix& operator*=(const double d) { scale(d); return *this; } 403 404 405 private: 75 406 76 407 /// … … 82 413 /// @return A pointer to a copy of the internal GSL matrix. 83 414 /// 84 gsl_matrix* gsl_matrix_copy(void) const; 85 86 /// 87 /// @return A const pointer to the internal GSL matrix. 88 /// 89 inline const gsl_matrix* gsl_matrix_pointer(void) const { return m_; } 90 91 /// 92 /// @return A pointer to the internal GSL matrix. 93 /// 94 inline gsl_matrix* gsl_matrix_pointer(void) { return m_; }; 95 96 /// 97 /// This function multiplies all elements between two matrices and 98 /// the result is stored back into the calling object, \f$a_{ij} = 99 /// a_{ij} * b_{ij} \; \forall i,j\f$. 100 /// 101 inline void mul_elements(const matrix& b) 102 { gsl_matrix_mul_elements(m_,b.m_); } 103 104 /// 105 /// Swap row \a i with \a j. 106 /// 107 inline void row_swap( const size_t& i, const size_t& j) 108 { gsl_matrix_swap_rows(m_,i,j); } 109 110 /// 111 /// @return The number of rows in the matrix. 112 /// 113 inline size_t rows(void) const { return m_->size1; } 114 115 /// 116 /// Set all elements to \a value. 117 /// 118 inline void set_all(const double value) { gsl_matrix_set_all(m_,value); } 119 120 /// 121 /// Set column \a i to \a vec. 122 /// 123 inline void set_column(const size_t i, const vector vec) 124 {gsl_matrix_set_col(m_, i, vec.gsl_vector_pointer());} 125 126 /// 127 /// Set row \a i to \a vec. 128 /// 129 inline void set_row(const size_t i, const vector vec) 130 {gsl_matrix_set_row(m_, i, vec.gsl_vector_pointer());} 131 132 /// 133 /// Set column \a i to \a vec 134 /// 135 //void matrix set_column(const size_t i, const vector vec) const; 136 137 /// 138 /// Transpose the matrix. 139 /// 140 /// @return Transpose of the matrix. 141 /// 142 matrix transpose(void) const; 143 144 /// 145 /// @return Reference to the element position (\a row, \a column). 146 /// 147 inline double& operator()(size_t row,size_t column) 148 { return (*gsl_matrix_ptr(m_,row,column)); } 149 150 /// 151 /// @return Const reference to the element position (\a row, \a 152 /// column). 153 /// 154 inline const double& operator()(size_t row,size_t column) const 155 { return (*gsl_matrix_const_ptr(m_,row,column)); } 156 157 /// 158 /// @todo to be properly implemented. Should return a reference to a vector 159 /// object. Underlying GSL supports only GSL_vector views, thus 160 /// some redesign of the vector class is needed. 161 /// 162 /// inline vector& operator[](size_t i); 163 /// So for now, stupid copying is done, and actually a matrix 164 /// row is returned using this function. 165 vector operator[](size_t row) const; 166 167 /// 168 /// @todo to be properly implemented. Should return a reference to 169 /// a vector object (matrix row). Underlying GSL supports only GSL_vector 170 /// views, thus some redesign of the vector class is needed. 171 /// 172 /// inline const vector& operator[](size_t i) const; 173 /// 174 /// So for now, stupid copying is done, and actually a matrix 175 /// column is returned using this function. 176 vector TEMP_col_return(size_t column) const; 177 178 /// 179 /// Matrix multiplication. 180 /// 181 /// @return The resulting matrix. 182 /// 183 matrix operator*(const matrix&) const; 184 185 /// 186 /// Matrix-vector multiplication. 187 /// 188 /// @return The resulting vector. 189 /// 190 vector operator*(const vector&) const; 191 192 /// 193 /// Matrix addition. 194 /// 195 /// @return The resulting matrix. 196 /// 197 matrix operator+(const matrix&) const; 198 199 /// 200 /// @todo Matrix addition. 201 /// 202 /// @return The resulting matrix. 203 /// 204 matrix operator+=(const matrix&); 205 206 /// 207 /// Matrix subtraction. 208 /// 209 /// @return The resulting matrix. 210 /// 211 matrix operator-(const matrix&) const; 212 213 /// 214 /// @todo Matrix subtraction. 215 /// 216 /// @return The resulting matrix. 217 /// 218 matrix operator-=(const matrix&) const; 219 220 /// 221 /// Comparison operator. 222 /// 223 /// @return True if all elements are equal otherwise False. 224 /// 225 /// @see equal 226 /// 227 inline bool operator==( const matrix& other ) const { return equal(other);} 228 229 /// 230 /// Comparison operator. 231 /// 232 /// @return False if all elements are equal otherwise True. 233 /// 234 /// @see equal 235 /// 236 inline bool operator!=( const matrix& other ) const {return !equal(other);} 237 238 /// 239 /// The assignment operator. There is no requirements on 240 /// dimensions. 241 /// 242 matrix& operator=(const matrix& other); 243 244 /// 245 /// Subtract and assign operator. 246 /// 247 inline matrix& operator-=(const matrix& m) 248 { gsl_matrix_sub(m_,m.m_); return *this; } 249 250 /// 251 /// Multiply and assign operator. 252 /// 253 inline matrix& operator*=(const double d) 254 { gsl_matrix_scale(m_,d); return *this; } 255 256 257 private: 258 259 gsl_matrix* m_; 260 415 gsl_matrix* create_gsl_matrix_copy(void) const; 416 417 gsl_matrix* m_; 418 gsl_matrix_view* view_; 261 419 }; 262 420 263 421 /// 264 422 /// The output operator for the matrix class. 265 /// 266 std::ostream& operator<< (std::ostream& s, const matrix&); 267 423 /// 424 std::ostream& operator<< (std::ostream& s, const matrix&); 268 425 269 426 270 427 }} // of namespace gslapi and namespace theplu 271 428 272 #endif 429 #endif -
trunk/lib/gslapi/vector.cc
r357 r420 2 2 3 3 #include <c++_tools/gslapi/vector.h> 4 #include <c++_tools/gslapi/matrix.h> 4 5 #include <c++_tools/utility/stl_utility.h> 5 6 6 #include <iostream>7 7 #include <sstream> 8 #include <string>9 8 #include <vector> 10 9 #include <utility> … … 16 15 17 16 18 vector::vector(void)19 : v_(NULL), view_(NULL)20 {21 }22 23 24 25 vector::vector(const size_t n,const double init_value)26 : view_(NULL)27 {28 v_ = gsl_vector_alloc(n);29 set_all(init_value);30 }31 32 33 34 vector::vector(const vector& other)35 : view_(NULL)36 {37 v_ = other.TEMP_gsl_vector_copy();38 }39 40 41 42 17 vector::vector(vector& v, size_t offset, size_t n, size_t stride) 43 { 18 : const_view_(NULL) 19 { 20 // Jari, exception handling needed here. Failure in setting up a 21 // proper gsl_vector_view is communicated by NULL pointer in the 22 // view structure (cf. GSL manual). How about GSL error state? 44 23 view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset, 45 24 stride,n)); … … 49 28 50 29 51 vector::vector(gsl_vector* vec) 52 : v_(vec), view_(NULL) 53 { 30 vector::vector(const vector& v, size_t offset, size_t n, size_t stride) 31 : view_(NULL) 32 { 33 // Jari, exception handling needed here. Failure in setting up a 34 // proper gsl_vector_view is communicated by NULL pointer in the 35 // view structure (cf. GSL manual). How about GSL error state? 36 const_view_ = new gsl_vector_const_view( 37 gsl_vector_const_subvector_with_stride(v.v_,offset,stride,n)); 38 v_ = const_cast<gsl_vector*>(&(const_view_->vector)); 39 } 40 41 42 43 vector::vector(matrix& m, size_t i, bool row) 44 : const_view_(NULL) 45 { 46 view_=new gsl_vector_view(row ? 47 gsl_matrix_row (m.gsl_matrix_p(),i) : 48 gsl_matrix_column(m.gsl_matrix_p(),i) ); 49 v_ = &(view_->vector); 50 } 51 52 53 54 vector::vector(const matrix& m, size_t i, bool row) 55 : view_(NULL) 56 { 57 const_view_ = new gsl_vector_const_view(row ? 58 gsl_matrix_const_row (m.gsl_matrix_p(),i) : 59 gsl_matrix_const_column(m.gsl_matrix_p(),i) ); 60 v_ = const_cast<gsl_vector*>(&(const_view_->vector)); 54 61 } 55 62 … … 57 64 58 65 vector::vector(std::istream& is) throw (utility::IO_error,std::exception) 59 : view_(NULL) 66 : view_(NULL), const_view_(NULL) 60 67 { 61 68 // read the data file and store in stl vectors (dynamically … … 79 86 << ") and columns (" << nof_columns 80 87 << ").\n Expected a row or a column vector."; 81 std::string m=s.str(); 82 throw utility::IO_error(m); 88 throw utility::IO_error(s.str()); 83 89 } 84 else if (v.size()!=nof_columns) {90 else if (v.size()!=nof_columns) { 85 91 std::ostringstream s; 86 92 s << "vector::vector(std::istream&) data file error:\n" 87 93 << " Line " << (nof_rows+1) << " has " << v.size() 88 94 << " columns; expected " << nof_columns << " column."; 89 std::string m=s.str(); 90 throw utility::IO_error(m); 95 throw utility::IO_error(s.str()); 91 96 } 92 97 data_matrix.push_back(v); … … 109 114 if (view_) 110 115 delete view_; 116 else if (const_view_) 117 delete const_view_; 111 118 else if (v_) 112 119 gsl_vector_free(v_); … … 116 123 117 124 118 gsl_vector* vector:: TEMP_gsl_vector_copy(void) const125 gsl_vector* vector::create_gsl_vector_copy(void) const 119 126 { 120 127 gsl_vector* vec = gsl_vector_alloc(size()); … … 159 166 for ( size_t i = 0; i < size(); ++i ) 160 167 res += other[i] * (*this)[i]; 161 return res;162 }163 164 165 166 vector vector::operator+(const vector &other) const167 {168 vector res(*this);169 gsl_vector_add(res.v_,other.v_);170 return res;171 }172 173 174 175 vector vector::operator-( const vector &other ) const176 {177 vector res( *this );178 gsl_vector_sub(res.v_,other.v_);179 168 return res; 180 169 } … … 197 186 { 198 187 if( this != &other ) { 199 if ( v_ ) 188 if (view_) 189 delete view_; 190 else if (const_view_) 191 delete const_view_; 192 else if ( v_ ) 200 193 gsl_vector_free( v_ ); 201 v_ = other. TEMP_gsl_vector_copy();194 v_ = other.create_gsl_vector_copy(); 202 195 } 203 196 return *this; … … 208 201 std::ostream& operator<<(std::ostream& s, const vector& a) 209 202 { 210 s.setf(std::ios:: fixed);203 s.setf(std::ios::dec); 211 204 s.precision(12); 212 205 for (size_t j = 0; j < a.size(); ++j) { -
trunk/lib/gslapi/vector.h
r357 r420 1 1 // $Id$ 2 2 3 #ifndef _theplu_gslapi_vector_ 3 #ifndef _theplu_gslapi_vector_ 4 4 #define _theplu_gslapi_vector_ 5 6 #include <c++_tools/utility/Exception.h> 5 7 6 8 #include <iostream> … … 9 11 10 12 #include <gsl/gsl_vector.h> 11 #include <c++_tools/utility/Exception.h>12 13 13 14 namespace theplu { 14 15 namespace gslapi { 16 17 class matrix; 18 15 19 /// 16 20 /// This is the C++ tools interface to GSL vector. 'double' is the … … 25 29 /// \par[Vector views] GSL vector views are supported and these are 26 30 /// disguised as ordinary gslapi::vectors. A support function is 27 /// added, gslapi::vector::is_view(), that can be used to check if 28 /// the vector object is a view. Note that view vectors do not own 29 /// the undelying data, and is not valid if the original vector is 30 /// deallocated. 31 /// added, gslapi::vector::isview(), that can be used to check if a 32 /// vector object is a view. Note that view vectors do not own the 33 /// undelying data, and a view is not valid if the vector owning the 34 /// data is deallocated. 35 /// 36 /// @note Missing support to underlying GSL vector features can in 37 /// principle be included to this class if requested by the user 38 /// community. 31 39 /// 32 40 class vector … … 37 45 /// The default constructor. 38 46 /// 39 vector(void);47 inline vector(void) : v_(NULL), view_(NULL), const_view_(NULL) {} 40 48 41 49 /// … … 43 51 /// sets all elements to \a init_value. 44 52 /// 45 vector(const size_t n, const double init_value=0); 53 inline vector(const size_t n,const double init_value=0) 54 : view_(NULL), const_view_(NULL) 55 { v_ = gsl_vector_alloc(n); set_all(init_value); } 46 56 47 57 /// … … 51 61 /// of the view will be copied, i.e. the view is not copied. 52 62 /// 53 vector(const vector&); 54 55 /// 56 /// The vector view constructor. 63 inline vector(const vector& other) : view_(NULL), const_view_(NULL) 64 { v_ = other.create_gsl_vector_copy(); } 65 66 /// 67 /// Vector view constructor. 57 68 /// 58 69 /// Create a view of vector \a v, with starting index \a offset, … … 63 74 /// is viewed. Also, using the copy constructor will create a new 64 75 /// vector object that is a copy of whatever is viewed. If a copy 65 /// of the view is needed the you should use this consturctor to66 /// get one.76 /// of the view is needed then you should use this constructor to 77 /// obtain a copy. 67 78 /// 68 79 /// @note If the object viewed by the view goes out of scope or is 69 /// deleted, the view becomes invalid. 70 /// 71 vector(vector& v, size_t offset, size_t n, size_t stride=1); 72 73 /// 74 /// Constructor that imports a GSL vector. The GSL object is owned 75 /// by the created object. 76 /// 77 vector(gsl_vector*); 80 /// deleted, the view becomes invalid and the result of further 81 /// use is undefined. 82 /// 83 vector(vector& v, size_t offset, size_t n, size_t stride=1); 84 85 /// 86 /// Vector const view constructor. 87 /// 88 /// Create a view of vector \a v, with starting index \a offset, 89 /// size \a n, and an optional \a stride. 90 /// 91 /// A vector view can be used as any const vector. Using the copy 92 /// constructor will create a new vector object that is a copy of 93 /// whatever is viewed. If a copy of the view is needed then you 94 /// should use this constructor to obtain a copy. 95 /// 96 /// @note If the object viewed by the view goes out of scope or is 97 /// deleted, the view becomes invalid and the result of further 98 /// use is undefined. 99 /// 100 vector(const vector& v, size_t offset, size_t n, size_t stride=1); 101 102 /// 103 /// Matrix row/column view constructor. 104 /// 105 /// Create a row/column vector view of matrix \a m, pointing at 106 /// row/column \a i. The parameter \a row is used to set whether 107 /// the view should be a row or column view. If \a row is set to 108 /// true, the view will be a row view (default behaviour), and, 109 /// naturally, a column view otherwise. 110 /// 111 /// A vector view can be used as any vector with the difference 112 /// that changes made to the view will also change the object that 113 /// is viewed. Also, using the copy constructor will create a new 114 /// vector object that is a copy of whatever is viewed. If a copy 115 /// of the view is needed then you should use the vector view 116 /// constructor to obtain a copy. 117 /// 118 /// @note If the object viewed by the view goes out of scope or is 119 /// deleted, the view becomes invalid and the result of further 120 /// use is undefined. 121 /// 122 vector(matrix& m, size_t i, bool row=true); 123 124 /// 125 /// Matrix row/column const view constructor. 126 /// 127 /// Create a row/column vector view of matrix \a m, pointing at 128 /// row/column \a i. The parameter \a row is used to set whether 129 /// the view should be a row or column view. If \a row is set to 130 /// true, the view will be a row view (default behaviour), and, 131 /// naturally, a column view otherwise. 132 /// 133 /// A const vector view can be used as any const vector. Using the 134 /// copy constructor will create a new vector object that is a 135 /// copy of whatever is viewed. If a copy of the view is needed 136 /// then you should use the vector view constructor to obtain a 137 /// copy. 138 /// 139 /// @note If the object viewed by the view goes out of scope or is 140 /// deleted, the view becomes invalid and the result of further 141 /// use is undefined. 142 /// 143 vector(const matrix& m, size_t i, bool row=true); 78 144 79 145 /// … … 117 183 118 184 /// 185 /// @return A const pointer to the internal GSL vector, 186 /// 187 inline const gsl_vector* gsl_vector_p(void) const { return v_; } 188 189 /// 190 /// @return A pointer to the internal GSL vector, 191 /// 192 inline gsl_vector* gsl_vector_p(void) { return v_; } 193 194 /// 119 195 /// @return True if all elements in the vector is zero, false 120 196 /// othwerwise; … … 123 199 124 200 /// 125 /// Check if the vector object is a view (sub 201 /// Check if the vector object is a view (sub-vector) to another 126 202 /// vector. 127 203 /// 128 /// @return True if the object is a view, false othwerwise; 129 /// 130 inline bool isview(void) const { return view_; } 131 132 /// 133 /// @return A const pointer to the internal GSL vector, 134 /// 135 inline const gsl_vector* gsl_vector_pointer(void) const { return v_; } 136 137 /// 138 /// @return A pointer to the internal GSL vector, 139 /// 140 inline gsl_vector* TEMP_gsl_vector_pointer(void) { return v_; } 204 /// @return True if the object is a view, false othwerwise. 205 /// 206 inline bool isview(void) const { return view_ || const_view_; } 141 207 142 208 /// … … 208 274 209 275 /// 276 /// Set element values to values in \a vec. This function is 277 /// needed for assignment of viewed elements. 278 /// 279 /// @return Whatever GSL returns. 280 /// 281 /// @note No check on size is done. 282 /// 283 /// @see const vector& operator=(const vector&) 284 /// 285 inline int set(const vector& vec) { return gsl_vector_memcpy(v_,vec.v_); } 286 287 /// 210 288 /// Set all elements to \a value. 211 289 /// … … 279 357 /// 280 358 // Jari, doxygen group as Accessing vector elements 281 inline const double& operator()(size_t i) const282 283 359 inline const double& 360 operator()(size_t i) const { return *gsl_vector_const_ptr(v_,i); } 361 284 362 /// 285 363 /// Element access operator. … … 296 374 /// 297 375 // Jari, doxygen group as Accessing vector elements 298 inline const double& operator[](size_t i) const299 300 376 inline const double& 377 operator[](size_t i) const { return *gsl_vector_const_ptr(v_,i); } 378 301 379 /// 302 380 /// @return The dot product. 303 381 /// 304 382 double operator*( const vector &other ) const; 305 306 ///307 /// Vector with scalar multiplication.308 ///309 /// @note This operator is not implemented!310 ///311 vector operator*(const double d) const;312 313 ///314 /// Vector addition.315 ///316 /// @return The resulting vector.317 ///318 vector operator+( const vector& other ) const;319 320 ///321 /// Vector subtraction.322 ///323 /// @return The resulting vector.324 ///325 vector operator-( const vector& other ) const;326 383 327 384 /// … … 335 392 /// 336 393 /// The assignment operator. There is no requirements on 337 /// dimensions. 394 /// dimensions, i.e. the vector is remapped in memory if 395 /// necessary. This implies that in general views cannot be 396 /// assigned using this operator. Views will be mutated into 397 /// normal vectors. The only exception to this behaviour on views 398 /// is when self-assignemnt is done, since self-assignment is 399 /// ignored. 338 400 /// 339 401 /// @return A const reference to the resulting vector. 340 402 /// 403 /// @see int set(const vector&) 404 /// 341 405 const vector& operator=(const vector&); 342 406 … … 346 410 /// @return A const reference to the resulting vector. 347 411 /// 348 inline const vector& operator+=( const vector& other )349 { gsl_vector_add(v_,other.v_); return *this;}412 inline const vector& 413 operator+=(const vector& other) { gsl_vector_add(v_,other.v_); return *this;} 350 414 351 415 /// … … 354 418 /// @return A const reference to the resulting vector. 355 419 /// 356 inline const vector& operator-=( const vector& other )357 { gsl_vector_sub(v_,other.v_); return *this;}420 inline const vector& 421 operator-=(const vector& other) { gsl_vector_sub(v_,other.v_); return *this;} 358 422 359 423 /// … … 362 426 /// @return A const reference to the resulting vector. 363 427 /// 364 inline const vector& operator*=(const double d) 365 { gsl_vector_scale(v_,d); return *this; }366 367 368 428 inline const vector& 429 operator*=(const double d) { gsl_vector_scale(v_,d); return *this; } 430 431 432 private: 369 433 370 434 /// … … 376 440 /// @return A pointer to a copy of the internal GSL vector. 377 441 /// 378 gsl_vector* TEMP_gsl_vector_copy(void) const;379 380 442 gsl_vector* create_gsl_vector_copy(void) const; 443 444 gsl_vector* v_; 381 445 gsl_vector_view* view_; 382 }; 446 gsl_vector_const_view* const_view_; 447 }; 383 448 384 449 /// 385 450 /// The output operator for the vector class. 386 /// 451 /// 387 452 std::ostream& operator<<(std::ostream&, const vector& ); 388 453 … … 390 455 }} // of namespace gslapi and namespace theplu 391 456 392 #endif 457 #endif -
trunk/lib/statistics/Averager.cc
r295 r420 11 11 12 12 13 Averager::Averager(void)14 : n_(0), x_(0), xx_(0)15 {16 }17 18 Averager::Averager(const double x,const double xx,const long n)19 : n_(n), x_(x), xx_(xx)20 {21 }22 23 Averager::Averager(const Averager& a)24 : n_(a.n_), x_(a.x_), xx_(a.xx_)25 {26 }27 28 13 const Averager& Averager::operator+=(const Averager& a) 29 14 { … … 34 19 } 35 20 21 36 22 }} // of namespace statistics and namespace theplu -
trunk/lib/statistics/Averager.h
r349 r420 26 26 /// Default constructor 27 27 /// 28 Averager(void);28 inline Averager(void) : n_(0), x_(0), xx_(0) {} 29 29 30 30 /// … … 32 32 /// number of samples \a n. 33 33 /// 34 Averager(const double x, const double xx, const long n); 34 inline Averager(const double x,const double xx,const long n) 35 : n_(n), x_(x), xx_(xx) {} 35 36 36 37 /// 37 38 /// Copy constructor 38 39 /// 39 Averager(const Averager&);40 40 inline Averager(const Averager& a) : n_(a.n_), x_(a.x_), xx_(a.xx_) {} 41 41 42 /// 42 43 /// Adding \a n (default=1) number of data point(s) with value \a d. 43 44 /// 44 inline void add(const double d,const long n=1) 45 {n_+=n; x_+=n*d; xx_+=n*d*d;} 45 inline void add(const double d,const long n=1) { n_+=n; x_+=n*d; xx_+=n*d*d;} 46 46 47 47 /// -
trunk/lib/statistics/AveragerPair.cc
r295 r420 10 10 11 11 12 AveragerPair::AveragerPair(void)13 : x_(Averager()), y_(Averager()), xy_(0.0)14 {15 }16 17 AveragerPair::AveragerPair(const double x, const double xx, const double y,18 const double yy, const double xy, const long n)19 : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy)20 {21 }22 23 AveragerPair::AveragerPair(const AveragerPair& a)24 : x_(a.x_averager()), y_(a.y_averager()), xy_(a.sum_xy())25 {26 }27 28 12 const AveragerPair& AveragerPair::operator+=(const AveragerPair& a) 29 13 { … … 34 18 } 35 19 20 36 21 }} // of namespace statistics and namespace theplu -
trunk/lib/statistics/AveragerPair.h
r355 r420 25 25 /// Default constructor 26 26 /// 27 AveragerPair(void);27 inline AveragerPair(void) : x_(Averager()), y_(Averager()), xy_(0.0) {} 28 28 29 29 /// … … 31 31 /// number of pair of values \a n 32 32 /// 33 AveragerPair(const double x, const double xx, const double y, 34 const double yy, const double xy, const long); 33 inline AveragerPair(const double x, const double xx, const double y, 34 const double yy, const double xy, const long n) 35 : x_(Averager(x,xx,n)), y_(Averager(y,yy,n)), xy_(xy) {} 35 36 36 37 /// 37 38 /// Copy constructor 38 39 /// 39 AveragerPair(const AveragerPair&); 40 inline AveragerPair(const AveragerPair& a) 41 : x_(a.x_averager()), y_(a.y_averager()), xy_(a.sum_xy()) {} 40 42 41 43 /// -
trunk/lib/statistics/AveragerWeighted.cc
r397 r420 11 11 12 12 13 AveragerWeighted::AveragerWeighted(void)14 : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0)15 {16 }17 18 AveragerWeighted::AveragerWeighted(const AveragerWeighted& a)19 : w_(Averager(a.sum_w(),a.sum_ww(),1)),20 wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)),21 wwx_(a.sum_wwx()),22 wxx_(a.sum_wxx())23 {24 }25 26 13 AveragerWeighted AveragerWeighted::operator+=(const AveragerWeighted& a) 27 14 { -
trunk/lib/statistics/AveragerWeighted.h
r414 r420 24 24 /// 25 25 /// Default constructor 26 /// 27 AveragerWeighted(void); 26 /// 27 inline AveragerWeighted(void) 28 : w_(Averager()), wx_(Averager()), wwx_(0), wxx_(0) {} 28 29 29 30 /// 30 31 /// Copy constructor 31 32 /// 32 AveragerWeighted(const AveragerWeighted&); 33 inline AveragerWeighted(const AveragerWeighted& a) 34 : w_(Averager(a.sum_w(),a.sum_ww(),1)), 35 wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), wwx_(a.sum_wwx()), 36 wxx_(a.sum_wxx()) {} 33 37 34 38 /// -
trunk/lib/statistics/Linear.h
r389 r420 32 32 /// Destructor 33 33 /// 34 virtual ~Linear(void) {};34 inline virtual ~Linear(void) {}; 35 35 36 36 /// -
trunk/lib/statistics/MultiDimensional.cc
r386 r420 10 10 11 11 12 MultiDimensional::~MultiDimensional(void)13 {14 if (work_)15 gsl_multifit_linear_free(work_);16 }17 18 19 20 12 void MultiDimensional::fit(const gslapi::matrix& x, const gslapi::vector& y) 21 13 { … … 25 17 gsl_multifit_linear_free(work_); 26 18 work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size()); 27 gsl_multifit_linear(x.gsl_matrix_p ointer(),y.gsl_vector_pointer(),28 fit_parameters_. TEMP_gsl_vector_pointer(),29 covariance_.gsl_matrix_p ointer(),&chisquare_,work_);19 gsl_multifit_linear(x.gsl_matrix_p(),y.gsl_vector_p(), 20 fit_parameters_.gsl_vector_p(), 21 covariance_.gsl_matrix_p(),&chisquare_,work_); 30 22 } 31 23 -
trunk/lib/statistics/MultiDimensional.h
r389 r420 29 29 /// @brief Destructor 30 30 /// 31 ~MultiDimensional(void);31 inline ~MultiDimensional(void) { if (work_) gsl_multifit_linear_free(work_);} 32 32 33 33 /// -
trunk/lib/svm/InputRanker.cc
r301 r420 14 14 namespace svm { 15 15 16 InputRanker::InputRanker() 17 :train_set_(std::vector<size_t>()), 18 id_(std::vector<size_t>()), 19 rank_(std::vector<size_t>()) 20 21 { 22 } 23 16 24 17 InputRanker::InputRanker(const gslapi::matrix& data, 25 18 const gslapi::vector& target, … … 41 34 //scoring each input 42 35 std::vector<std::pair<size_t, double> > score; 43 for (size_t i=0; i<nof_genes; i++) {44 double area = score_object.score(target, data[i], train_set_);36 for (size_t i=0; i<nof_genes; i++) { 37 double area = score_object.score(target,gslapi::vector(data,i),train_set_); 45 38 std::pair<size_t, double> tmp(i,area); 46 39 score.push_back(tmp); … … 56 49 } 57 50 } 51 52 58 53 59 54 InputRanker::InputRanker(const gslapi::matrix& data, … … 76 71 //scoring each input 77 72 std::vector<std::pair<size_t, double> > score; 78 for (size_t i=0; i<nof_genes; i++) {79 double area = score_object.score(target, data[i],80 weight[i], train_set_);73 for (size_t i=0; i<nof_genes; i++) { 74 double area = score_object.score(target, gslapi::vector(data,i), 75 gslapi::vector(weight,i), train_set_); 81 76 std::pair<size_t, double> tmp(i,area); 82 77 score.push_back(tmp); … … 91 86 } 92 87 } 93 88 89 94 90 }} // of namespace svm and namespace theplu -
trunk/lib/svm/InputRanker.h
r295 r420 21 21 class InputRanker 22 22 { 23 23 24 24 public: 25 25 /// 26 /// Default constructor. Not implemented.26 /// Default constructor. 27 27 /// 28 InputRanker(); 28 inline InputRanker(void) : train_set_(std::vector<size_t>()), 29 id_(std::vector<size_t>()), 30 rank_(std::vector<size_t>()) {} 29 31 30 32 /// … … 54 56 /// 55 57 inline size_t id(const size_t i) const {return id_[i];} 56 58 57 59 /// 58 60 /// highest ranked gene is ranked as number zero @return rank for … … 66 68 std::vector<size_t> id_; 67 69 std::vector<size_t> rank_; 70 }; 68 71 69 70 };71 72 72 73 }} // of namespace svm and namespace theplu 73 74 74 75 #endif 75 -
trunk/lib/svm/Kernel_MEV.cc
r336 r420 3 3 #include <c++_tools/svm/Kernel_MEV.h> 4 4 5 #include <c++_tools/svm/KernelFunction.h>6 #include <c++_tools/gslapi/matrix.h>7 #include <c++_tools/gslapi/vector.h>8 9 5 namespace theplu { 10 6 namespace svm { 11 7 12 Kernel_MEV::Kernel_MEV(const gslapi::matrix& data, const KernelFunction& kf)13 : Kernel(data, kf), data_(data), kf_(&kf)14 {15 }16 17 18 19 Kernel_MEV::~Kernel_MEV(void)20 {21 }22 23 24 25 8 26 9 }} // of namespace svm and namespace theplu -
trunk/lib/svm/Kernel_MEV.h
r350 r420 30 30 /// Default constructor (not implemented) 31 31 /// 32 Kernel_MEV( );32 Kernel_MEV(void); 33 33 34 34 /// … … 37 37 /// sample. @note Can not handle NaNs. 38 38 /// 39 Kernel_MEV(const gslapi::matrix&, const KernelFunction&); 40 39 inline Kernel_MEV(const gslapi::matrix& data, const KernelFunction& kf) 40 : Kernel(data,kf), data_(data), kf_(&kf) {} 41 41 42 /// 42 43 /// @todo Constructor taking the \a data matrix, the KernelFunction and a … … 54 55 /// Destructor 55 56 /// 56 virtual ~Kernel_MEV(void);57 inline virtual ~Kernel_MEV(void) {}; 57 58 58 59 /// 59 /// @return element at position (\a row, \a column) of the Kernel60 /// @return Element at position (\a row, \a column) of the Kernel 60 61 /// matrix 61 62 /// 62 double operator()(const size_t row,const size_t column) const 63 { return (*kf_)(data_.TEMP_col_return(row),data_.TEMP_col_return(column)); } 63 inline double operator()(const size_t row, const size_t column) const 64 { return (*kf_)(gslapi::vector(data_,row,false), 65 gslapi::vector(data_,column,false)); } 64 66 65 67 /// -
trunk/lib/svm/Kernel_SEV.cc
r336 r420 11 11 namespace svm { 12 12 13 13 14 Kernel_SEV::Kernel_SEV(const gslapi::matrix& data, const KernelFunction& kf) 14 15 : Kernel(data,kf), data_(data), kf_(&kf) … … 17 18 for (size_t i=0; i<kernel_matrix_.rows(); i++) 18 19 for (size_t j=i; j<kernel_matrix_.columns(); j++) 19 kernel_matrix_(i,j) = kernel_matrix_(j,i) = 20 (*kf_)(data_.TEMP_col_return(i),data_.TEMP_col_return(j)); 21 20 kernel_matrix_(i,j) = kernel_matrix_(j,i) = 21 (*kf_)(gslapi::vector(data_,i,false),gslapi::vector(data_,j,false)); 22 22 } 23 23 24 Kernel_SEV::~Kernel_SEV(void)25 {26 }27 24 28 25 }} // of namespace svm and namespace theplu -
trunk/lib/svm/Kernel_SEV.h
r336 r420 48 48 /// Destructor 49 49 /// 50 virtual ~Kernel_SEV(void);50 inline virtual ~Kernel_SEV(void) {}; 51 51 52 52 /// -
trunk/lib/utility/Exception.h
r411 r420 17 17 public: 18 18 IO_error(void) throw() : std::runtime_error("IO_error:") {} 19 IO_error(std::string &message) throw()19 IO_error(std::string message) throw() 20 20 : std::runtime_error("IO_error: " + message) {} 21 21 }; -
trunk/lib/utility/PCA.cc
r301 r420 12 12 namespace utility { 13 13 14 //PCA::PCA() : process_( false ), explained_calc_( false ){}15 16 PCA::PCA( const gslapi::matrix& A ) : A_( A ),17 process_( false ),18 explained_calc_( false )19 {20 }21 14 22 15 void PCA::process() … … 35 28 36 29 // Read the eigenvectors and eigenvalues 37 eigenvectors_ = U.transpose(); 38 eigenvalues_ = pSVD->s(); 30 eigenvectors_ = U; 31 eigenvectors_ .transpose(); 32 eigenvalues_ = pSVD->s(); 39 33 40 34 // T … … 50 44 // make sure that the i:th element is in its correct 51 45 // position (N element --> Ordo( N*N )) 52 for( size_t i = 0; i < eigenvalues_.size(); ++i ) 53 { 54 for( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 55 { 56 if( eigenvalues_[ j ] > eigenvalues_[ i ] ) 57 { 58 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 59 eigenvectors_.row_swap(i,j); 60 } 61 } 62 } 63 } 46 for ( size_t i = 0; i < eigenvalues_.size(); ++i ) 47 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 48 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) { 49 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 50 eigenvectors_.swap_rows(i,j); 51 } 52 } 53 64 54 65 55 … … 73 63 // Transform into SVD friendly dimension 74 64 //////////////////////////////////////// 75 A_ = A_.transpose();76 A_center = A_center.transpose();65 A_.transpose(); 66 A_center.transpose(); 77 67 78 68 // Single value decompose the data matrix … … 83 73 84 74 // Read the eigenvectors and eigenvalues 85 eigenvectors_ = V.transpose();//U.transpose(); 75 eigenvectors_=V; 76 eigenvectors_.transpose(); 86 77 eigenvalues_ = pSVD->s(); 87 78 … … 89 80 // (used V insted of U now for eigenvectors) 90 81 //////////////////////////////////////////// 91 A_ = A_.transpose(); 92 A_center = A_center.transpose(); 93 82 A_.transpose(); 83 A_center.transpose(); 94 84 95 85 // T … … 105 95 // make sure that the i:th element is in its correct 106 96 // position (N element --> Ordo( N*N )) 107 for( size_t i = 0; i < eigenvalues_.size(); ++i ) 108 { 109 for( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 110 { 111 if( eigenvalues_[ j ] > eigenvalues_[ i ] ) 112 { 113 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 114 eigenvectors_.row_swap(i,j); 115 } 116 } 117 } 118 } 97 for ( size_t i = 0; i < eigenvalues_.size(); ++i ) 98 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 99 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) { 100 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 101 eigenvectors_.swap_rows(i,j); 102 } 103 } 104 119 105 120 106 … … 127 113 gslapi::vector A_row_sum(A_.rows()); 128 114 for (size_t i=0; i<A_row_sum.size(); ++i) 129 A_row_sum(i)= A_[i].sum();115 A_row_sum(i)=gslapi::vector(A_,i).sum(); 130 116 for( size_t i = 0; i < A_center.rows(); ++i ) 131 117 { … … 135 121 } 136 122 } 123 137 124 138 125 … … 162 149 163 150 151 164 152 gslapi::matrix PCA::projection_transposed( const gslapi::matrix& 165 153 samples ) const … … 205 193 } 206 194 195 196 207 197 double PCA::get_explained_intensity( const size_t& k ) 208 198 { … … 215 205 } 216 206 207 217 208 }} // of namespace utility and namespace theplu -
trunk/lib/utility/PCA.h
r334 r420 6 6 #include <c++_tools/gslapi/matrix.h> 7 7 #include <c++_tools/gslapi/vector.h> 8 9 // Standard C++ includes10 ////////////////////////11 // #include <vector>12 // #include <iostream>13 // #include <memory>14 // #include <cstdlib>15 8 16 9 … … 29 22 projection_transposed()... 30 23 */ 31 32 24 class PCA 33 25 { … … 36 28 Default constructor (not implemented) 37 29 */ 38 PCA( );30 PCA(void); 39 31 40 32 /** … … 42 34 should have been performed and no products. 43 35 */ 44 explicit PCA( const gslapi::matrix& ); 36 inline explicit PCA(const gslapi::matrix& A) 37 : A_(A), process_(false), explained_calc_(false) {} 45 38 46 47 39 /** 48 40 Will perform PCA according to the following scheme: \n … … 51 43 3: Calculate eigenvalues according to \n 52 44 \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n 53 4: Sort eigenvectors (from matrix V) according to descending eigenvalues 45 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n 54 46 */ 55 void process( );47 void process(void); 56 48 57 49 /** … … 62 54 projection_transposed() method. 63 55 */ 64 void process_transposed_problem( );65 56 void process_transposed_problem(void); 57 66 58 /** 67 Returns eigenvector \a i59 @return Eigenvector \a i. 68 60 */ 69 // Jari, change this to 70 // const gslapi::vector& get_eigenvector( const size_t& i ) const 71 const gslapi::vector get_eigenvector( const size_t& i ) const 72 { 73 return eigenvectors_[i]; 74 } 61 inline gslapi::vector get_eigenvector(const size_t& i) const 62 { return gslapi::vector(eigenvectors_,i); } 75 63 76 64 /** … … 78 66 \f$ C = \frac{1}{N^2}A^TA \f$ 79 67 */ 80 double get_eigenvalue( const size_t& i ) const 81 { 82 return eigenvalues_[ i ]; 83 } 68 inline double 69 get_eigenvalue(const size_t& i) const { return eigenvalues_[i]; } 84 70 85 71 /** … … 90 76 double get_explained_intensity( const size_t& k ); 91 77 92 93 94 /** 95 This function will project data onto the new coordinate-system 96 where the axes are the calculated eigenvectors. This means that 97 PCA must have been run before this function can be used! 98 Output is presented as coordinates in the N-dimensional room 99 spanned by the eigenvectors. 78 /** 79 This function will project data onto the new coordinate-system 80 where the axes are the calculated eigenvectors. This means that 81 PCA must have been run before this function can be used! 82 Output is presented as coordinates in the N-dimensional room 83 spanned by the eigenvectors. 100 84 */ 101 85 gslapi::matrix projection( const gslapi::matrix& ) const; 102 103 /** 104 Same as projection() but works when used process_transposed_problem() 86 87 /** 88 Same as projection() but works when used 89 process_transposed_problem(). 105 90 */ 106 91 gslapi::matrix projection_transposed( const gslapi::matrix& ) const; 107 92 108 109 93 110 94 private: 111 gslapi::matrix 95 gslapi::matrix A_; 112 96 gslapi::matrix eigenvectors_; 113 97 gslapi::vector eigenvalues_; … … 128 112 */ 129 113 void calculate_explained_intensity(); 114 }; // class PCA 130 115 131 132 }; // class PCA 133 116 134 117 }} // of namespace utility and namespace theplu 135 118 136 119 #endif 137 -
trunk/lib/utility/SVD.cc
r301 r420 6 6 namespace theplu { 7 7 namespace utility { 8 9 10 SVD::SVD(const gslapi::matrix& Ain)11 : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns())12 {13 }14 15 16 17 SVD::~SVD(void)18 {19 }20 21 8 22 9 … … 42 29 { 43 30 gslapi::vector w(U_.columns()); 44 return gsl_linalg_SV_decomp(U_.gsl_matrix_pointer(), 45 V_.gsl_matrix_pointer(), 46 s_.TEMP_gsl_vector_pointer(), 47 w.TEMP_gsl_vector_pointer()); 31 return gsl_linalg_SV_decomp(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 32 s_.gsl_vector_p(), w.gsl_vector_p()); 48 33 } 49 34 … … 54 39 gslapi::vector w(U_.columns()); 55 40 gslapi::matrix X(U_.columns(),U_.columns()); 56 return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_pointer(), 57 X.gsl_matrix_pointer(), 58 V_.gsl_matrix_pointer(), 59 s_.TEMP_gsl_vector_pointer(), 60 w.TEMP_gsl_vector_pointer()); 41 return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_p(), X.gsl_matrix_p(), 42 V_.gsl_matrix_p(), s_.gsl_vector_p(), 43 w.gsl_vector_p()); 61 44 } 62 45 -
trunk/lib/utility/SVD.h
r303 r420 52 52 /// input matrix is copied for further use in the object. 53 53 /// 54 SVD(const gslapi::matrix& ); 54 inline SVD(const gslapi::matrix& Ain) 55 : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns()) {} 55 56 56 ~SVD(void);57 inline ~SVD(void) {} 57 58 58 59 /// … … 72 73 /// is undefined. 73 74 /// 74 gslapi::vectors(void) const { return s_; }75 inline const gslapi::vector& s(void) const { return s_; } 75 76 76 77 /// … … 83 84 /// 84 85 inline int solve(gslapi::vector b, gslapi::vector x) 85 { return gsl_linalg_SV_solve(U_.gsl_matrix_pointer(), 86 V_.gsl_matrix_pointer(), 87 s_.gsl_vector_pointer(), 88 b.gsl_vector_pointer(), 89 x.TEMP_gsl_vector_pointer()); 90 } 86 { return gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 87 s_.gsl_vector_p(), b.gsl_vector_p(), 88 x.gsl_vector_p()); } 91 89 92 90 /// … … 98 96 /// is undefined. 99 97 /// 100 gslapi::matrixU(void) const { return U_; }98 inline const gslapi::matrix& U(void) const { return U_; } 101 99 102 100 /// … … 108 106 /// is undefined. 109 107 /// 110 gslapi::matrixV(void) const { return V_; }108 inline const gslapi::matrix& V(void) const { return V_; } 111 109 112 110 private: 113 111 inline int jacobi(void) 114 { return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_pointer(), 115 V_.gsl_matrix_pointer(), 116 s_.TEMP_gsl_vector_pointer()); 117 } 112 { return gsl_linalg_SV_decomp_jacobi(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 113 s_.gsl_vector_p()); } 118 114 int golub_reinsch(void); 119 115 int modified_golub_reinsch(void); -
trunk/lib/utility/stl_utility.cc
r341 r420 20 20 for (size_t i=0; i<vec_str.size(); i++) { 21 21 if (!is_float(vec_str[i])){ 22 std::cerr << "Warning: '" << vec_str[i] 23 << "' is not a number." << std::endl; 22 // Jari, this should be communicated with as an exception. 23 // std::cerr << "Warning: '" << vec_str[i] 24 // << "' is not a number." << std::endl; 24 25 } 25 26 else … … 41 42 for (size_t i=0; i<vec_str.size(); i++) { 42 43 if (!is_int(vec_str[i])){ 43 std::cerr << "Warning: '" << vec_str[i] 44 << "' is not an integer." << std::endl; 44 // Jari, this should be communicated with as an exception. 45 // std::cerr << "Warning: '" << vec_str[i] 46 // << "' is not an integer." << std::endl; 45 47 } 46 48 else -
trunk/test/data/knni_matrix.data
r175 r420 1 1 2 .2348 .234773 .098934 .21627 .32782 .2328 .347454 .25784 .345376 .56582 .1234 2 3 .2346 .786345 .234734 .34935 .23733 .2347 .21347 .286758 .456454 .97732 .1234 3 4 .8964 .471294 .947234 .38272 .48328 .2874 .489892 .23478 .45645 .23899 .1234 4 .4372 .382144 .763342 .32924 .27823 .3827 .234764 .384734 .398795 .23498 .1234 5 .4372 .382144 .763342 .32924 .27823 .3827 .234764 .384734 .398795 .23498 .1234 6 5 7 .8624 .286482 .83248 .72846 .28468 .2347 .23234 .98973 .29874 .32894 .1234 6 8 .8624 .286482 .83248 .72846 .28468 .2347 .23234 .98973 .29874 .32894 .1234 7 9 .2342 .23878 .97437 .76236 .32764 .3474 .43276 .34555 .29948 .34598 .1234 8 10 .2347 .56783 .78435 .35763 .23468 .7384 .23578 .345533 .235948 .32458 .1234 9 .2234 .77639 .38783 .28953 .62376 .5445 .32489 .22388 .238746 .23468 .1234 11 .2234 .77639 .38783 .28953 .62376 .5445 .32489 .22388 .238746 .23468 .1234 10 12 .2388 .234688 .43687 .86456 .65654 .6864 .879645 .98454 .98731 .164897 .1234 11 13 .1968 .153987 .13684 .14685 .316135 .5286 .69435 .13634 .18351 .1984531 .1234 12 14 .6865 .213687 .12368 .18564 .897654 .4945 .789654 .45547 .98466 .66136 .1234 13 .9835 .963154 .964664 .132369 .983316 .3276 .376513 .34941 .387433 .31685 .123415 .9835 .963154 .964664 .132369 .983316 .3276 .376513 .34941 .387433 .31685 .1234 14 16 .9783 .973154 .897432 .643344 .64343 .6731 .69436 .765313 .547865 .89713 .1234 15 17 .9783 .973154 .897432 .643344 .64343 .6731 .69436 .765313 .547865 .89713 .1234 -
trunk/test/matrix_test.cc
r399 r420 3 3 #include <c++_tools/gslapi/matrix.h> 4 4 5 #include <unistd.h> 5 6 #include <fstream> 6 7 #include <iostream> 7 8 9 class matrixwrapper 10 { 11 public: 12 matrixwrapper(size_t i,size_t j,double value=7) 13 : m_(i,j,value) {} 14 15 inline theplu::gslapi::vector 16 row(const size_t& i) { return theplu::gslapi::vector(m_,i); } 17 18 inline const theplu::gslapi::matrix& matrix(void) const { return m_; } 19 20 private: 21 theplu::gslapi::matrix m_; 22 }; 23 24 8 25 int main(const int argc,const char* argv[]) 9 { 26 { 10 27 using namespace theplu; 11 28 std::ostream* error; … … 17 34 std::cout << "matrix_test -v : for printing extra information\n"; 18 35 } 19 *error << "testing matrix" << std::endl; 36 37 *error << "Testing matrix class" << std::endl; 20 38 bool ok = true; 21 39 40 *error << "\tcopy constructor and operator!=" << std::endl; 22 41 gslapi::matrix m(3,3,9); 23 42 gslapi::matrix m2(m); 24 43 if (m2!=m) 25 44 ok=false; 45 46 *error << "\toutput operator and istream constructor" << std::endl; 47 // Checking that the matrix output operator writes a file that the 48 // input operator can read. 26 49 std::ofstream my_out("data/tmp_test_matrix.txt"); 27 50 my_out << m2; 28 51 my_out.close(); 29 30 52 std::ifstream is("data/tmp_test_matrix.txt"); 31 53 gslapi::matrix m3(is); … … 33 55 if (m3!=m2) 34 56 ok=false; 35 57 unlink("data/tmp_test_matrix.txt"); 58 59 *error << "\toperator*(double)" << std::endl; 36 60 gslapi::matrix m4(3,3,1); 37 61 m4 *= 9; 38 39 if (m4!=m){ 62 if (m4!=m) { 40 63 ok=false; 41 64 *error << "error operator*=(double)" << std::endl; 42 65 } 43 66 67 *error << "\tsub-matrix" << std::endl; 68 // Checking that sub-matrices work, i.e. mutation to the view are 69 // reflected in the viewed object. 70 is.open("data/knni_matrix.data"); 71 // The stream input is a proper matrix file, with some stray empty 72 // lines and other whitespaces. The file is not expected to break 73 // things. 74 gslapi::matrix m5(is); 75 is.close(); 76 double m5_sum=0; 77 for (size_t i=0; i<m5.rows(); ++i) 78 for (size_t j=0; j<m5.columns(); ++j) 79 m5_sum+=m5(i,j); 80 gslapi::matrix* m5sub=new gslapi::matrix(m5,3,3,3,3); 81 double m5sub_sum=0; 82 for (size_t i=0; i<m5sub->rows(); ++i) 83 for (size_t j=0; j<m5sub->columns(); ++j) { 84 m5sub_sum+=(*m5sub)(i,j); 85 (*m5sub)(i,j)=1; 86 } 87 delete m5sub; 88 double m5_sum2=0; 89 for (size_t i=0; i<m5.rows(); ++i) 90 for (size_t j=0; j<m5.columns(); ++j) 91 m5_sum2+=m5(i,j); 92 if (m5_sum2-m5_sum-9+m5sub_sum>1e-13) { 93 ok=false; 94 *error << "error sub-matrix test" << std::endl; 95 } 96 97 *error << "\tsub-(row)vector" << std::endl; 98 // Checking that the row view works, i.e. mutation to the view are 99 // reflected in the viewed object. 100 gslapi::vector v5subrow(m5,3); 101 double v5subrow_sum=0; 102 for (size_t i=0; i<v5subrow.size(); ++i) { 103 v5subrow_sum+=v5subrow(i); 104 v5subrow[i]=0; 105 } 106 double m5_sum3=0; 107 for (size_t i=0; i<m5.rows(); ++i) 108 for (size_t j=0; j<m5.columns(); ++j) 109 m5_sum3+=m5(i,j); 110 if (m5_sum3-m5_sum2+v5subrow_sum>1e-13) { 111 ok=false; 112 *error << "error sub-vector test 1" << std::endl; 113 } 114 115 *error << "\tsub-(column)vector" << std::endl; 116 // Checking that the column view works, i.e. mutation to the view 117 // are reflected in the viewed object. 118 gslapi::vector v5subcolumn(m5,0,false); 119 double v5subcolumn_sum=0; 120 for (size_t i=0; i<v5subcolumn.size(); ++i) { 121 v5subcolumn_sum+=v5subcolumn(i); 122 v5subcolumn(i)=1; 123 } 124 double m5_sum4=0; 125 for (size_t i=0; i<m5.rows(); ++i) 126 for (size_t j=0; j<m5.columns(); ++j) 127 m5_sum4+=m5(i,j); 128 if (m5_sum4-m5_sum3-v5subcolumn.size()+v5subcolumn_sum>1e-13) { 129 ok=false; 130 *error << "error sub-vector test 2" << std::endl; 131 } 132 // Checking that the column view above mutates the values in the row 133 // view. 134 double v5subrow_sum2=0; 135 for (size_t i=0; i<v5subrow.size(); ++i) 136 v5subrow_sum2+=v5subrow(i); 137 if (v5subrow_sum2-v5subcolumn(3)>1e-13) { 138 ok=false; 139 *error << "error sub-vector test 3" << std::endl; 140 } 141 142 *error << "\tsub-vector and vector copying" << std::endl; 143 // Checking that a view is not inherited through the copy 144 // contructor. 145 gslapi::vector v6(v5subrow); 146 v6.set_all(2); 147 double v5subrow_sum3=0; 148 for (size_t i=0; i<v5subrow.size(); ++i) 149 v5subrow_sum3+=v5subrow(i); 150 if (v5subrow_sum3-v5subrow_sum2>1e-13) { 151 ok=false; 152 *error << "error sub-vector test 4" << std::endl; 153 } 154 // Checking that values in a vector is copied into a viewed matrix. 155 v5subrow.set(v6); 156 double m5_sum5=0; 157 for (size_t i=0; i<m5.rows(); ++i) 158 for (size_t j=0; j<m5.columns(); ++j) 159 m5_sum5+=m5(i,j); 160 if (m5_sum5-m5_sum4-v5subrow.size()*2+v5subrow_sum3>1e-13) { 161 ok=false; 162 *error << "error sub-vector test 5" << std::endl; 163 } 164 // Checking that vector::operator= indeed makes a view to become a 165 // "normal" vector. 166 v5subrow=gslapi::vector(23,22); 167 double m5_sum6=0; 168 for (size_t i=0; i<m5.rows(); ++i) 169 for (size_t j=0; j<m5.columns(); ++j) 170 m5_sum6+=m5(i,j); 171 if (m5_sum6-m5_sum5>1e-13) { 172 ok=false; 173 *error << "error sub-vector test 6" << std::endl; 174 } 175 176 // Checking that the memberfunction matrixwrapper::row() returns a 177 // view 178 *error << "\tthat class member returns a view" << std::endl; 179 matrixwrapper mw(5,2); 180 gslapi::vector mwrow=mw.row(2); 181 if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) { 182 ok=false; 183 *error << "error sub-vector test 7" << std::endl; 184 } 185 186 *error << "\tsub-matrix of a sub-matrix" << std::endl; 187 // Checking that a sub-matrix of a sub-matrix can be created. 188 gslapi::matrix sub(m5,3,3,3,3); 189 gslapi::matrix subsub(sub,2,1,1,2); 190 subsub(0,0)=23221121; 191 if (&sub(2,1)!=&subsub(0,0) || subsub(0,0)!=m5(5,4) || &subsub(0,0)!=&m5(5,4)){ 192 ok=false; 193 *error << "error sub-matrix test 1" << std::endl; 194 } 44 195 45 196 if (error!=&std::cerr) … … 47 198 48 199 return (ok ? 0 : -1); 49 50 200 } -
trunk/test/score_test.cc
r414 r420 79 79 const double tol = 0.001; 80 80 for (size_t i=0; i<data.rows(); i++){ 81 gslapi::vector vec = data[i];81 gslapi::vector vec(data,i); 82 82 area = roc.score(target2,vec); 83 83 if (area<correct_area(i)-tol || area>correct_area(i)+tol){ … … 90 90 gslapi::vector weight(target2.size(),1); 91 91 for (size_t i=0; i<data.rows(); i++){ 92 gslapi::vector vec = data[i];92 gslapi::vector vec(data,i); 93 93 area = roc.score(target2, vec, weight); 94 94 if (area<correct_area(i)-tol || area>correct_area(i)+tol){ -
trunk/test/svd_test.cc
r377 r420 37 37 for (size_t i=0; i<s.size(); ++i) 38 38 S(i,i)=s[i]; 39 gslapi::matrix V=svd.V(); 40 gslapi::matrix U=svd.U(); 41 double error = this_norm(A-U*S*V.transpose()); 39 gslapi::matrix Vtranspose=svd.V(); 40 Vtranspose.transpose(); 41 // Reconstructing A = U*S*Vtranspose 42 gslapi::matrix Areconstruct=svd.U(); 43 Areconstruct*=S; 44 Areconstruct*=Vtranspose; 45 Areconstruct-=A; // Expect null matrix 46 double error = this_norm(Areconstruct); 42 47 bool testerror=false; 43 48 if (error>MAXTOL) { … … 49 54 } 50 55 51 error=this_norm(V.transpose()*V)-n; 56 Vtranspose*=svd.V(); // Expect unity matrix 57 error=this_norm(Vtranspose)-n; 52 58 if (error>MAXTOL) { 53 59 std::cerr << "test_svd: FAILED, algorithm " << algo … … 57 63 } 58 64 59 error=this_norm(U.transpose()*U)-n; 65 gslapi::matrix Utranspose=svd.U(); 66 Utranspose.transpose(); 67 Utranspose*=svd.U(); // Expect unity matrix 68 error=this_norm(Utranspose)-n; 60 69 if (error>MAXTOL) { 61 70 std::cerr << "test_svd: FAILED, algorithm " << algo -
trunk/test/svm_test.cc
r337 r420 82 82 83 83 // Comparing alpha to alpha_matlab 84 theplu::gslapi::vector diff_alpha = alpha - alpha_matlab; 84 theplu::gslapi::vector diff_alpha(alpha); 85 diff_alpha-=alpha_matlab; 85 86 if (diff_alpha*diff_alpha> 1e-10 ){ 86 87 if (print) … … 90 91 91 92 // Comparing output to target 92 theplu::gslapi::vector output = svm.output();93 theplu::gslapi::vector output(svm.output()); 93 94 double slack = 0; 94 95 for (unsigned int i=0; i<target.size(); i++){
Note: See TracChangeset
for help on using the changeset viewer.