- Timestamp:
- Nov 27, 2005, 11:40:18 PM (18 years ago)
- Location:
- branches/better_matrix_class/lib/gslapi
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/better_matrix_class/lib/gslapi/matrix.cc
r404 r407 7 7 8 8 #include <cmath> 9 #include <iostream>10 9 #include <sstream> 11 #include <string>12 10 #include <vector> 13 11 … … 22 20 23 21 matrix::matrix(void) 24 : m_(NULL) 22 : m_(NULL), view_(NULL) 25 23 { 26 24 } … … 29 27 30 28 matrix::matrix(const size_t& r, const size_t& c, double init_value) 29 : view_(NULL) 31 30 { 32 31 m_ = gsl_matrix_alloc(r,c); … … 37 36 38 37 matrix::matrix(const matrix& other) 38 : view_(NULL) 39 39 { 40 40 m_ = other.gsl_matrix_copy(); … … 43 43 44 44 45 matrix(matrix& m, size_t offset_row, size_t offset_column, size_t n_row, 46 size_t n_column); 47 { 48 view_ = new gsl_matrix_view(gsl_matrix_submatrix(v.m_,offset_row, 49 offset_column,n_row, 50 n_column)); 51 v_ = &(view_->matrix); 52 } 53 54 55 45 56 // Constructor that gets data from istream 46 matrix::matrix(std::istream& is) 57 matrix::matrix(std::istream& is) throw (utility::IO_error,std::exception) 58 : view_(NULL) 47 59 { 48 60 // read the data file and store in stl vectors (dynamically … … 53 65 std::vector<double> v; 54 66 for (nof_rows = 0; utility::read_to_double(is, v); nof_rows++) { 55 56 67 // Ignoring empty lines 57 if (!v.size()){68 if (!v.size()) { 58 69 nof_rows--; 59 70 continue; 60 71 } 61 62 if(nof_columns==0) 72 if (!nof_columns) 63 73 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); 74 else if (v.size()!=nof_columns) { 75 std::ostringstream s; 76 s << "matrix::matrix(std::istream&) data file error: " 77 << "line" << nof_rows+1 << " has " << v.size() 78 << " columns; expected " << nof_columns << " columns."; 79 throw utility::IO_error(s.str()); 71 80 } 72 81 data_matrix.push_back(v); … … 86 95 matrix::~matrix(void) 87 96 { 97 if (view_) 98 delete view_; 88 99 if (m_) { 89 100 gsl_matrix_free(m_); … … 100 111 for (size_t i=0; i<rows(); i++) 101 112 for (size_t j=0; j<columns(); j++) 102 // two last cond. are to check for nans113 // The two last condition checks are needed for NaN detection 103 114 if (fabs( (*this)(i,j)-other(i,j) ) > d || 104 115 (*this)(i,j)!=(*this)(i,j) || other(i,j)!=other(i,j)) 105 116 return false; 106 107 117 return true; 108 118 } … … 127 137 return res; 128 138 } 129 */130 139 131 140 … … 151 160 152 161 153 /*154 162 matrix matrix::operator*( const matrix &other ) const 155 163 { -
branches/better_matrix_class/lib/gslapi/matrix.h
r403 r407 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 /// \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 /// 22 34 /// @note All GSL matrix related functions are not implement but all 23 35 /// functionality, except binary file support, defined for GSL … … 31 43 /// The default constructor. 32 44 /// 45 /// This contructor does not initialize underlying (essential) 46 /// structures. 47 /// 33 48 matrix(void); 34 49 … … 46 61 /// 47 62 matrix(const matrix&); 63 64 /// 65 /// The matrix view constructor. 66 /// 67 /// Create a view of matrix \a m, with starting row \a offset_row, 68 /// starting column \a offset_column, row size \a n_row, and 69 /// column size \a n_column. 70 /// 71 /// A matrix view can be used as any matrix with the difference 72 /// that changes made to the view will also change the object that 73 /// is viewed. Also, using the copy constructor will create a new 74 /// matrix object that is a copy of whatever is viewed. If a copy 75 /// of the view is needed then you should use this consturctor to 76 /// get one. 77 /// 78 /// @note If the object viewed by the view goes out of scope or is 79 /// deleted, the view becomes invalid and the result of further 80 /// use is undefined. 81 /// 82 matrix(matrix& m, size_t offset_row, size_t offset_column, size_t n_row, 83 size_t n_column); 48 84 49 85 /// … … 80 116 /// @return Whatever GSL returns. 81 117 /// 82 inline int add_constant(const double d)83 118 inline int 119 add_constant(const double d) { return gsl_matrix_add_constant(m_,d); } 84 120 85 121 /// … … 95 131 /// @return Whatever GSL returns. 96 132 /// 97 inline int div_elements(const matrix& b) 98 { return gsl_matrix_div_elements(m_,b.m_); } 99 100 /// 101 /// @return true if each element deviates less or equal than \a 102 /// d. If any matrix contain a nan false is always returned. 103 /// 104 bool equal(const matrix&, const double d=0) const; 133 inline int 134 div_elements(const matrix& b) { return gsl_matrix_div_elements(m_,b.m_); } 135 136 /// 137 /// Check whether matrices are equal within a user defined 138 /// precision, set by \a precision. 139 /// 140 /// @return True if each element deviates less or equal than \a 141 /// d. If any matrix contain a NaN, false is always returned. 142 /// 143 bool equal(const matrix&, const double precision=0) const; 105 144 106 145 /// … … 109 148 /// 110 149 inline bool isnull(void) const { return gsl_matrix_isnull(m_); } 150 151 /// 152 /// Check if the matrix object is a view (sub-matrix) to another 153 /// matrix. 154 /// 155 /// @return True if the object is a view, false othwerwise. 156 /// 157 inline bool isview(void) const { return view_; } 111 158 112 159 /// … … 158 205 /// @return Whatever GSL returns. 159 206 /// 160 inline int mul_elements(const matrix& b)161 207 inline int 208 mul_elements(const matrix& b) { return gsl_matrix_mul_elements(m_,b.m_); } 162 209 163 210 /// … … 307 354 /// Add and assign operator. 308 355 /// 309 inline const matrix& operator+=(const double d)310 356 inline const matrix& 357 operator+=(const double d) { add_constant(d); return *this; } 311 358 312 359 /// … … 329 376 330 377 private: 378 331 379 /// 332 380 /// Create a new copy of the internal GSL matrix. … … 352 400 353 401 gsl_matrix* m_; 354 402 gsl_matrix_view* view_; 355 403 }; 356 404 … … 360 408 std::ostream& operator<< (std::ostream& s, const matrix&); 361 409 410 362 411 }} // of namespace gslapi and namespace theplu 363 412
Note: See TracChangeset
for help on using the changeset viewer.