Changeset 3bc926
- Timestamp:
- Dec 4, 2010, 11:54:32 PM (14 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- cca9ef
- Parents:
- 9eb7580
- git-author:
- Frederik Heber <heber@…> (11/15/10 12:36:18)
- git-committer:
- Frederik Heber <heber@…> (12/04/10 23:54:32)
- Location:
- src
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/LinearAlgebra/Makefile.am
r9eb7580 r3bc926 19 19 linearsystemofequations.cpp \ 20 20 Matrix.cpp \ 21 MatrixContent.cpp \ 21 22 Plane.cpp \ 22 23 Space.cpp \ … … 34 35 linearsystemofequations.hpp \ 35 36 Matrix.hpp \ 37 MatrixContent.hpp \ 36 38 Plane.hpp \ 37 39 Space.hpp \ 38 40 vector_ops.hpp \ 39 41 Vector.hpp \ 42 VectorContent.hpp \ 40 43 VectorInterface.hpp 41 44 -
src/LinearAlgebra/Matrix.cpp
r9eb7580 r3bc926 26 26 #include "Helpers/Assert.hpp" 27 27 #include "LinearAlgebra/Vector.hpp" 28 #include " VectorContent.hpp"29 #include " MatrixContent.hpp"28 #include "LinearAlgebra/VectorContent.hpp" 29 #include "LinearAlgebra/MatrixContent.hpp" 30 30 31 31 #include <gsl/gsl_blas.h> … … 41 41 Matrix::Matrix() 42 42 { 43 content = new MatrixContent(); 44 content->content = gsl_matrix_calloc(NDIM, NDIM); 45 createViews(); 46 } 47 48 Matrix::Matrix(const double* src){ 49 content = new MatrixContent(); 50 content->content = gsl_matrix_alloc(NDIM, NDIM); 51 set(0,0, src[0]); 52 set(1,0, src[1]); 53 set(2,0, src[2]); 54 55 set(0,1, src[3]); 56 set(1,1, src[4]); 57 set(2,1, src[5]); 58 59 set(0,2, src[6]); 60 set(1,2, src[7]); 61 set(2,2, src[8]); 62 createViews(); 63 } 64 65 Matrix::Matrix(const Matrix &src){ 66 content = new MatrixContent(); 67 content->content = gsl_matrix_alloc(NDIM, NDIM); 68 gsl_matrix_memcpy(content->content,src.content->content); 69 createViews(); 70 } 71 72 Matrix::Matrix(MatrixContent* _content) : 73 content(_content) 74 { 43 content = new MatrixContent(NDIM, NDIM); 44 createViews(); 45 } 46 47 Matrix::Matrix(const double* src) 48 { 49 content = new MatrixContent(NDIM, NDIM, src); 50 createViews(); 51 } 52 53 Matrix::Matrix(const Matrix &src) 54 { 55 content = new MatrixContent(src.content); 56 createViews(); 57 } 58 59 Matrix::Matrix(const MatrixContent &src) 60 { 61 content = new MatrixContent(src); 62 createViews(); 63 } 64 65 Matrix::Matrix(MatrixContent* _content) 66 { 67 content = new MatrixContent(_content); 75 68 createViews(); 76 69 } … … 86 79 } 87 80 delete diagonal_ptr; 88 gsl_matrix_free(content->content);89 81 delete content; 90 82 } … … 107 99 108 100 void Matrix::setIdentity(){ 109 for(int i=NDIM;i--;){ 110 for(int j=NDIM;j--;){ 111 set(i,j,i==j); 112 } 113 } 101 content->setIdentity(); 114 102 } 115 103 116 104 void Matrix::setZero(){ 117 for(int i=NDIM;i--;){ 118 for(int j=NDIM;j--;){ 119 set(i,j,0.); 120 } 121 } 105 content->setZero(); 122 106 } 123 107 … … 135 119 } 136 120 137 Matrix &Matrix::operator=(const Matrix &src){ 138 if(&src!=this){ 139 gsl_matrix_memcpy(content->content,src.content->content); 140 } 141 return *this; 142 } 143 144 const Matrix &Matrix::operator+=(const Matrix &rhs){ 145 gsl_matrix_add(content->content, rhs.content->content); 146 return *this; 147 } 148 149 const Matrix &Matrix::operator-=(const Matrix &rhs){ 150 gsl_matrix_sub(content->content, rhs.content->content); 151 return *this; 152 } 153 154 const Matrix &Matrix::operator*=(const Matrix &rhs){ 155 (*this) = (*this)*rhs; 156 return *this; 157 } 158 159 const Matrix Matrix::operator+(const Matrix &rhs) const{ 121 Matrix &Matrix::operator=(const Matrix &src) 122 { 123 // MatrixContent checks for self-assignment 124 *content = *(src.content); 125 return *this; 126 } 127 128 const Matrix &Matrix::operator+=(const Matrix &rhs) 129 { 130 *content += *(rhs.content); 131 return *this; 132 } 133 134 const Matrix &Matrix::operator-=(const Matrix &rhs) 135 { 136 *content -= *(rhs.content); 137 return *this; 138 } 139 140 const Matrix &Matrix::operator*=(const Matrix &rhs) 141 { 142 (*content) *= (*rhs.content); 143 return *this; 144 } 145 146 const Matrix Matrix::operator+(const Matrix &rhs) const 147 { 160 148 Matrix tmp = *this; 161 149 tmp+=rhs; … … 163 151 } 164 152 165 const Matrix Matrix::operator-(const Matrix &rhs) const{ 153 const Matrix Matrix::operator-(const Matrix &rhs) const 154 { 166 155 Matrix tmp = *this; 167 156 tmp-=rhs; … … 169 158 } 170 159 171 const Matrix Matrix::operator*(const Matrix &rhs) const{ 172 gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM); 173 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content->content, rhs.content->content, 0.0, res); 174 MatrixContent *content= new MatrixContent(); 175 content->content = res; 176 return Matrix(content); 177 } 178 179 double &Matrix::at(size_t i, size_t j){ 180 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 181 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range"); 182 return *gsl_matrix_ptr (content->content, i, j); 183 } 184 185 const double Matrix::at(size_t i, size_t j) const{ 186 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 187 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range"); 188 return gsl_matrix_get(content->content, i, j); 189 } 190 191 Vector &Matrix::row(size_t i){ 160 const Matrix Matrix::operator*(const Matrix &rhs) const 161 { 162 Matrix tmp(content); 163 tmp *= rhs; 164 return tmp; 165 } 166 167 double &Matrix::at(size_t i, size_t j) 168 { 169 return content->at(i,j); 170 } 171 172 const double Matrix::at(size_t i, size_t j) const 173 { 174 return content->at(i,j); 175 } 176 177 Vector &Matrix::row(size_t i) 178 { 192 179 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 193 180 return *rows_ptr[i]; 194 181 } 195 182 196 const Vector &Matrix::row(size_t i) const{ 183 const Vector &Matrix::row(size_t i) const 184 { 197 185 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 198 186 return *rows_ptr[i]; 199 187 } 200 188 201 Vector &Matrix::column(size_t i){ 189 Vector &Matrix::column(size_t i) 190 { 202 191 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 203 192 return *columns_ptr[i]; 204 193 } 205 194 206 const Vector &Matrix::column(size_t i) const{ 195 const Vector &Matrix::column(size_t i) const 196 { 207 197 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 208 198 return *columns_ptr[i]; 209 199 } 210 200 211 Vector &Matrix::diagonal(){ 201 Vector &Matrix::diagonal() 202 { 212 203 return *diagonal_ptr; 213 204 } 214 205 215 const Vector &Matrix::diagonal() const{ 206 const Vector &Matrix::diagonal() const 207 { 216 208 return *diagonal_ptr; 217 209 } 218 210 219 void Matrix::set(size_t i, size_t j, const double value){ 220 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); 221 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range"); 222 gsl_matrix_set(content->content,i,j,value); 211 void Matrix::set(size_t i, size_t j, const double value) 212 { 213 content->set(i,j, value); 223 214 } 224 215 … … 253 244 } 254 245 255 Matrix Matrix::transpose() const{ 256 MatrixContent *newContent = new MatrixContent(); 257 newContent->content = gsl_matrix_alloc(NDIM, NDIM); 258 gsl_matrix_transpose_memcpy(newContent->content, content->content); 259 Matrix res = Matrix(newContent); 246 Matrix Matrix::transpose() const 247 { 248 std::cout << "const Matrix::transpose()." << std::endl; 249 Matrix res = Matrix(const_cast<const MatrixContent *>(content)->transpose()); 260 250 return res; 261 251 } … … 263 253 Matrix &Matrix::transpose() 264 254 { 265 double tmp; 266 for (int i=0;i<NDIM;i++) 267 for (int j=i+1;j<NDIM;j++) { 268 tmp = at(j,i); 269 at(j,i) = at(i,j); 270 at(i,j) = tmp; 271 } 272 return *this; 273 } 274 255 std::cout << "Matrix::transpose()." << std::endl; 256 content->transpose(); 257 return *this; 258 } 275 259 276 260 Vector Matrix::transformToEigenbasis() 277 261 { 278 gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM); 279 gsl_vector *eval = gsl_vector_alloc(NDIM); 280 gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM); 281 gsl_eigen_symmv(content->content, eval, evec, T); 282 gsl_eigen_symmv_free(T); 283 gsl_matrix_memcpy(content->content, evec); 284 gsl_matrix_free(evec); 262 gsl_vector *eval = content->transformToEigenbasis(); 285 263 Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2)); 286 264 gsl_vector_free(eval); … … 288 266 } 289 267 290 const Matrix &Matrix::operator*=(const double factor){ 291 gsl_matrix_scale(content->content, factor); 292 return *this; 293 } 294 295 const Matrix operator*(const double factor,const Matrix& mat){ 268 const Matrix &Matrix::operator*=(const double factor) 269 { 270 *content *= factor; 271 return *this; 272 } 273 274 const Matrix operator*(const double factor,const Matrix& mat) 275 { 296 276 Matrix tmp = mat; 297 tmp*=factor;298 return tmp; 299 } 300 301 const Matrix operator*(const Matrix &mat,const double factor){277 return tmp *= factor; 278 } 279 280 const Matrix operator*(const Matrix &mat,const double factor) 281 { 302 282 return factor*mat; 303 283 } 304 284 305 bool Matrix::operator==(const Matrix &rhs) const{ 306 for(int i=NDIM;i--;){ 307 for(int j=NDIM;j--;){ 308 if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){ 309 return false; 310 } 311 } 312 } 313 return true; 285 bool Matrix::operator==(const Matrix &rhs) const 286 { 287 return (*content == *(rhs.content)); 314 288 } 315 289 … … 333 307 }; 334 308 335 ostream &operator<<(ostream &ost,const Matrix &mat){ 309 ostream &operator<<(ostream &ost,const Matrix &mat) 310 { 336 311 for(int i = 0;i<NDIM;++i){ 337 312 ost << "\n"; … … 345 320 } 346 321 347 Vector operator*(const Matrix &mat,const Vector &vec) {348 Vector res; 349 gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res.content->content);350 return res; 351 } 352 353 Vector &operator*=(Vector& lhs,const Matrix &mat){322 Vector operator*(const Matrix &mat,const Vector &vec) 323 { 324 return (*mat.content) * vec; 325 } 326 327 Vector &operator*=(Vector& lhs,const Matrix &mat) 328 { 354 329 lhs = mat*lhs; 355 330 return lhs; -
src/LinearAlgebra/Matrix.hpp
r9eb7580 r3bc926 43 43 Matrix(const double*); 44 44 Matrix(const Matrix&); 45 Matrix(const MatrixContent&); 45 46 virtual ~Matrix(); 46 47 -
src/LinearAlgebra/MatrixContent.hpp
r9eb7580 r3bc926 9 9 #define MATRIXCONTENT_HPP_ 10 10 11 /** 12 * !file 13 * The way GSL works does not allow for forward definitions of the structures. 14 * Because of this the pointer to the gsl_matrix struct is wrapped inside another 15 * (dumb) object that allows for forward definitions. All methods of this class 16 * are empty, to allow for a maximum of flexibility. 11 /** MatrixContent is a wrapper for gsl_matrix. 12 * 13 * The GNU Scientific Library contaisn some very well written routines for 14 * linear algebra problems. However, it's syntax is C and hence it does not 15 * lend itself to readable written code, i.e. C = A * B, where A, B, and C 16 * are all matrices. Writing code this way is very convenient, concise and 17 * also in the same manner as one would type in MatLab. 18 * However, with C++ and its feature to overload functions and its operator 19 * functions such syntax is easy to create. 20 * 21 * Hence, this class is a C++ wrapper to gsl_matrix. There already some other 22 * libraries, however they fall short for the following reasons: 23 * -# gslwrap: not very well written and uses floats instead of doubles 24 * -# gslcpp: last change is from 2007 and only very few commits 25 * -# o2scl: basically, the same functionality as gsl only written in C++, 26 * however it uses GPLv3 license which is inconvenient for MoleCuilder. 27 * 28 * <h1>Howto use MatrixContent</h1> 29 * 30 * One should not use MatrixContent directly but either have it as a member 31 * variable in a specialized class or inherit from it. 32 * 17 33 */ 18 34 19 35 #include <gsl/gsl_matrix.h> 20 36 21 struct MatrixContent{ 37 class Vector; 38 39 class MatrixContent 40 { 41 friend Vector operator*(const MatrixContent &mat,const Vector &vec); 42 friend class Matrix; 43 public: 44 MatrixContent(size_t rows, size_t columns); 45 MatrixContent(size_t rows, size_t columns, const double *src); 46 MatrixContent(gsl_matrix *&src); 47 MatrixContent(const MatrixContent &src); 48 MatrixContent(const MatrixContent *src); 49 ~MatrixContent(); 50 51 // Accessing 52 double &at(size_t i, size_t j); 53 const double at(size_t i, size_t j) const; 54 void set(size_t i, size_t j, const double value); 55 56 // Transformations 57 MatrixContent transpose() const; 58 MatrixContent &transpose(); 59 gsl_vector* transformToEigenbasis(); 60 61 // Initializing 62 void setIdentity(); 63 void setZero(); 64 65 // Operators 66 MatrixContent &operator=(const MatrixContent &src); 67 const MatrixContent &operator+=(const MatrixContent &rhs); 68 const MatrixContent &operator-=(const MatrixContent &rhs); 69 const MatrixContent &operator*=(const MatrixContent &rhs); 70 const MatrixContent operator*(const MatrixContent &rhs) const; 71 const MatrixContent &operator*=(const double factor); 72 bool operator==(const MatrixContent &rhs) const; 73 74 75 private: 22 76 gsl_matrix * content; 77 const size_t rows; // store for internal purposes 78 const size_t columns; // store for internal purposes 23 79 }; 24 80 81 const MatrixContent operator*(const double factor,const MatrixContent& mat); 82 const MatrixContent operator*(const MatrixContent &mat,const double factor); 83 Vector operator*(const MatrixContent &mat,const Vector &vec); 84 25 85 #endif /* MATRIXCONTENT_HPP_ */ -
src/LinearAlgebra/Vector.hpp
r9eb7580 r3bc926 23 23 class Vector; 24 24 class Matrix; 25 class MatrixContent; 25 26 struct VectorContent; 26 27 … … 31 32 */ 32 33 class Vector : public Space{ 33 friend Vector operator*(const Matrix &,const Vector&);34 friend Vector operator*(const MatrixContent&,const Vector&); 34 35 friend class Matrix; 35 36 public: -
src/LinearAlgebra/gslmatrix.cpp
r9eb7580 r3bc926 27 27 28 28 #include <cassert> 29 #include <iostream> 29 30 #include <gsl/gsl_linalg.h> 31 #include <gsl/gsl_blas.h> 32 #include <gsl/gsl_eigen.h> 33 #include <gsl/gsl_matrix.h> 34 #include <gsl/gsl_vector.h> 30 35 31 36 /** Constructor of class GSLMatrix. … … 46 51 matrix = gsl_matrix_alloc(rows, columns); 47 52 gsl_matrix_memcpy (matrix, src->matrix); 53 }; 54 55 /** Copy constructor of class GSLMatrix. 56 * We take over pointer to gsl_matrix and set the parameter pointer to NULL 57 * afterwards. 58 * \param m row count 59 * \param n column count 60 * \param *&src source gsl_matrix 61 */ 62 GSLMatrix::GSLMatrix(size_t m, size_t n, gsl_matrix *&src) : 63 rows(m), 64 columns(n) 65 { 66 matrix = src; 67 src = NULL; 48 68 }; 49 69 … … 90 110 * \return (m,n)-th element of matrix 91 111 */ 92 double GSLMatrix::Get(size_t m, size_t n) 112 double GSLMatrix::Get(size_t m, size_t n) const 93 113 { 94 114 return gsl_matrix_get (matrix, m, n); … … 121 141 * \return const pointer to \a m-th element 122 142 */ 123 const double *GSLMatrix::const_Pointer(size_t m, size_t n) 143 const double *GSLMatrix::const_Pointer(size_t m, size_t n) const 124 144 { 125 145 return gsl_matrix_const_ptr (matrix, m, n); 126 146 }; 147 148 /** Returns the number of rows of the matrix. 149 * \return number of rows 150 */ 151 size_t GSLMatrix::getRowCount() const 152 { 153 return rows; 154 } 155 156 /** Returns the number of columns of the matrix. 157 * \return number of columns 158 */ 159 size_t GSLMatrix::getColumnCount() const 160 { 161 return columns; 162 } 127 163 128 164 /* ========================== Initializing =============================== */ … … 204 240 * \return true - is null, false - else 205 241 */ 206 bool GSLMatrix::IsNull() 242 bool GSLMatrix::IsNull() const 207 243 { 208 244 return gsl_matrix_isnull (matrix); … … 212 248 * \return true - is positive, false - else 213 249 */ 214 bool GSLMatrix::IsPositive() 250 bool GSLMatrix::IsPositive() const 215 251 { 216 252 return gsl_matrix_ispos (matrix); … … 220 256 * \return true - is negative, false - else 221 257 */ 222 bool GSLMatrix::IsNegative() 258 bool GSLMatrix::IsNegative() const 223 259 { 224 260 return gsl_matrix_isneg (matrix); … … 228 264 * \return true - is non-negative, false - else 229 265 */ 230 bool GSLMatrix::IsNonNegative() 266 bool GSLMatrix::IsNonNegative() const 231 267 { 232 268 return gsl_matrix_isnonneg (matrix); … … 237 273 * \return true - matrix is positive-definite, false - else 238 274 */ 239 bool GSLMatrix::IsPositiveDefinite() 275 bool GSLMatrix::IsPositiveDefinite() const 240 276 { 241 277 if (rows != columns) // only possible for square matrices. … … 249 285 * if matrix is square, uses LU decomposition. 250 286 */ 251 double GSLMatrix::Determinant() 287 double GSLMatrix::Determinant() const 252 288 { 253 289 int signum = 0; … … 259 295 }; 260 296 297 298 /* ============================ Properties ============================== */ 299 300 const GSLMatrix &GSLMatrix::operator+=(const GSLMatrix &rhs) 301 { 302 gsl_matrix_add(matrix, rhs.matrix); 303 return *this; 304 } 305 306 const GSLMatrix &GSLMatrix::operator-=(const GSLMatrix &rhs) 307 { 308 gsl_matrix_sub(matrix, rhs.matrix); 309 return *this; 310 } 311 312 const GSLMatrix &GSLMatrix::operator*=(const GSLMatrix &rhs) 313 { 314 (*this) = (*this)*rhs; 315 return *this; 316 } 317 318 const GSLMatrix GSLMatrix::operator+(const GSLMatrix &rhs) const 319 { 320 GSLMatrix tmp = *this; 321 tmp+=rhs; 322 return tmp; 323 } 324 325 const GSLMatrix GSLMatrix::operator-(const GSLMatrix &rhs) const 326 { 327 GSLMatrix tmp = *this; 328 tmp-=rhs; 329 return tmp; 330 } 331 332 const GSLMatrix GSLMatrix::operator*(const GSLMatrix &rhs) const 333 { 334 gsl_matrix *res = gsl_matrix_alloc(rhs.rows, rhs.columns); 335 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, matrix, rhs.matrix, 0.0, res); 336 return GSLMatrix(rhs.rows, rhs.columns, res); 337 } 338 339 /** Print matrix in a matlab style manner. 340 * \param &ost reference to output stream 341 * \param &mat reference to matrix to print 342 * \return reference to obtained output stream for concatenation 343 */ 344 ostream &operator<<(ostream &ost,const GSLMatrix &mat) 345 { 346 ost << "["; 347 for(size_t i = 0;i<mat.rows;++i){ 348 for(size_t j = 0; j<mat.columns;++j){ 349 ost << mat.Get(i,j); 350 if(j!=mat.columns-1) 351 ost << " "; 352 } 353 if(i!=mat.rows-1) 354 ost << "; "; 355 } 356 ost << "]"; 357 return ost; 358 } 359 -
src/LinearAlgebra/gslmatrix.hpp
r9eb7580 r3bc926 9 9 #define GSLMATRIX_HPP_ 10 10 11 using namespace std;12 13 11 /*********************************************** includes ***********************************/ 14 12 … … 18 16 #endif 19 17 18 #include <iosfwd> 19 20 20 #include <gsl/gsl_matrix.h> 21 21 22 22 23 /****************************************** forward declarations *****************************/ … … 28 29 class GSLMatrix { 29 30 friend class LinearSystemOfEquations; 31 friend std::ostream &operator<<(std::ostream &ost,const GSLMatrix &mat); 30 32 31 33 public: 32 34 GSLMatrix(size_t m, size_t n); 33 35 GSLMatrix(const GSLMatrix * const src); 36 GSLMatrix(size_t m, size_t n, gsl_matrix *&src); 34 37 ~GSLMatrix(); 35 38 36 39 // Accessing 37 40 void SetFromDoubleArray(double * x); 38 double Get(size_t m, size_t n) ;41 double Get(size_t m, size_t n) const; 39 42 void Set(size_t m, size_t n, double x); 40 43 double *Pointer(size_t m, size_t n); 41 const double *const_Pointer(size_t m, size_t n); 44 const double *const_Pointer(size_t m, size_t n) const; 45 size_t getRowCount() const; 46 size_t getColumnCount() const; 42 47 43 48 // Initializing … … 50 55 bool SwapColumns(size_t i, size_t j); 51 56 bool SwapRowColumn(size_t i, size_t j); 57 58 // Properties 52 59 bool Transpose(); 53 bool IsNull() ;54 bool IsPositive() ;55 bool IsNegative() ;56 bool IsNonNegative() ;57 bool IsPositiveDefinite() ;58 double Determinant() ;60 bool IsNull() const; 61 bool IsPositive() const; 62 bool IsNegative() const; 63 bool IsNonNegative() const; 64 bool IsPositiveDefinite() const; 65 double Determinant() const; 59 66 60 GSLMatrix& operator=(const GSLMatrix& rhs); 67 // Operators 68 GSLMatrix &operator=(const GSLMatrix &src); 69 const GSLMatrix &operator+=(const GSLMatrix &rhs); 70 const GSLMatrix &operator-=(const GSLMatrix &rhs); 71 const GSLMatrix &operator*=(const GSLMatrix &rhs); 72 const GSLMatrix operator+(const GSLMatrix &rhs) const; 73 const GSLMatrix operator-(const GSLMatrix &rhs) const; 74 const GSLMatrix operator*(const GSLMatrix &rhs) const; 61 75 62 76 private: … … 67 81 }; 68 82 83 std::ostream &operator<<(std::ostream &ost,const GSLMatrix &mat); 69 84 70 85 #endif /* GSLMATRIX_HPP_ */ -
src/unittests/MatrixUnittest.cpp
r9eb7580 r3bc926 38 38 void MatrixUnittest::setUp(){ 39 39 zero = new Matrix(); 40 for(int i =NDIM;i--;) { 41 for(int j =NDIM;j--;) { 42 zero->at(i,j)=0.; 43 } 44 } 40 45 one = new Matrix(); 41 46 for(int i =NDIM;i--;){ … … 172 177 // transpose of unit is unit 173 178 res.setIdentity(); 174 (const Matrix)res.transpose();179 res.transpose(); 175 180 CPPUNIT_ASSERT_EQUAL(res,*one); 176 181 … … 185 190 186 191 res =(*zero) *(*zero); 192 std::cout << *zero << " times " << *zero << " is " << res << std::endl; 187 193 CPPUNIT_ASSERT_EQUAL(res,*zero); 188 194 res =(*zero) *(*one);
Note:
See TracChangeset
for help on using the changeset viewer.