| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [325390] | 8 | /* | 
|---|
|  | 9 | * Matrix.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Jun 25, 2010 | 
|---|
|  | 12 | *      Author: crueger | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [bbbad5] | 20 | #include "Helpers/MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
| [57f243] | 22 | #include "LinearAlgebra/Matrix.hpp" | 
|---|
| [325390] | 23 | #include "Helpers/Assert.hpp" | 
|---|
|  | 24 | #include "Exceptions/NotInvertibleException.hpp" | 
|---|
|  | 25 | #include "Helpers/fast_functions.hpp" | 
|---|
| [e3ffd3] | 26 | #include "Helpers/Assert.hpp" | 
|---|
| [57f243] | 27 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [ce3d2b] | 28 | #include "VectorContent.hpp" | 
|---|
| [fee079] | 29 | #include "MatrixContent.hpp" | 
|---|
| [325390] | 30 |  | 
|---|
|  | 31 | #include <gsl/gsl_blas.h> | 
|---|
| [a439e5] | 32 | #include <gsl/gsl_eigen.h> | 
|---|
|  | 33 | #include <gsl/gsl_matrix.h> | 
|---|
|  | 34 | #include <gsl/gsl_multimin.h> | 
|---|
|  | 35 | #include <gsl/gsl_vector.h> | 
|---|
| [325390] | 36 | #include <cmath> | 
|---|
| [c49c96] | 37 | #include <iostream> | 
|---|
|  | 38 |  | 
|---|
|  | 39 | using namespace std; | 
|---|
| [325390] | 40 |  | 
|---|
|  | 41 | Matrix::Matrix() | 
|---|
|  | 42 | { | 
|---|
| [fee079] | 43 | content = new MatrixContent(); | 
|---|
|  | 44 | content->content = gsl_matrix_calloc(NDIM, NDIM); | 
|---|
| [3dbb9d] | 45 | createViews(); | 
|---|
| [325390] | 46 | } | 
|---|
|  | 47 |  | 
|---|
|  | 48 | Matrix::Matrix(const double* src){ | 
|---|
| [fee079] | 49 | content = new MatrixContent(); | 
|---|
|  | 50 | content->content = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
| [436f04] | 51 | set(0,0, src[0]); | 
|---|
|  | 52 | set(1,0, src[1]); | 
|---|
|  | 53 | set(2,0, src[2]); | 
|---|
| [325390] | 54 |  | 
|---|
| [436f04] | 55 | set(0,1, src[3]); | 
|---|
|  | 56 | set(1,1, src[4]); | 
|---|
|  | 57 | set(2,1, src[5]); | 
|---|
| [325390] | 58 |  | 
|---|
| [436f04] | 59 | set(0,2, src[6]); | 
|---|
|  | 60 | set(1,2, src[7]); | 
|---|
|  | 61 | set(2,2, src[8]); | 
|---|
| [3dbb9d] | 62 | createViews(); | 
|---|
| [325390] | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 | Matrix::Matrix(const Matrix &src){ | 
|---|
| [fee079] | 66 | content = new MatrixContent(); | 
|---|
|  | 67 | content->content = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
|  | 68 | gsl_matrix_memcpy(content->content,src.content->content); | 
|---|
| [3dbb9d] | 69 | createViews(); | 
|---|
| [325390] | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [fee079] | 72 | Matrix::Matrix(MatrixContent* _content) : | 
|---|
| [325390] | 73 | content(_content) | 
|---|
| [3dbb9d] | 74 | { | 
|---|
|  | 75 | createViews(); | 
|---|
|  | 76 | } | 
|---|
| [325390] | 77 |  | 
|---|
|  | 78 | Matrix::~Matrix() | 
|---|
|  | 79 | { | 
|---|
| [3dbb9d] | 80 | // delete all views | 
|---|
|  | 81 | for(int i=NDIM;i--;){ | 
|---|
|  | 82 | delete rows_ptr[i]; | 
|---|
|  | 83 | } | 
|---|
|  | 84 | for(int i=NDIM;i--;){ | 
|---|
|  | 85 | delete columns_ptr[i]; | 
|---|
|  | 86 | } | 
|---|
|  | 87 | delete diagonal_ptr; | 
|---|
| [fee079] | 88 | gsl_matrix_free(content->content); | 
|---|
|  | 89 | delete content; | 
|---|
| [325390] | 90 | } | 
|---|
|  | 91 |  | 
|---|
| [3dbb9d] | 92 | void Matrix::createViews(){ | 
|---|
|  | 93 | // create row views | 
|---|
|  | 94 | for(int i=NDIM;i--;){ | 
|---|
|  | 95 | VectorContent *rowContent = new VectorViewContent(gsl_matrix_row(content->content,i)); | 
|---|
|  | 96 | rows_ptr[i] = new Vector(rowContent); | 
|---|
|  | 97 | } | 
|---|
|  | 98 | // create column views | 
|---|
|  | 99 | for(int i=NDIM;i--;){ | 
|---|
|  | 100 | VectorContent *columnContent = new VectorViewContent(gsl_matrix_column(content->content,i)); | 
|---|
|  | 101 | columns_ptr[i] = new Vector(columnContent); | 
|---|
|  | 102 | } | 
|---|
|  | 103 | // create diagonal view | 
|---|
|  | 104 | VectorContent *diagonalContent = new VectorViewContent(gsl_matrix_diagonal(content->content)); | 
|---|
|  | 105 | diagonal_ptr = new Vector(diagonalContent); | 
|---|
|  | 106 | } | 
|---|
|  | 107 |  | 
|---|
| [1da5f5] | 108 | void Matrix::one(){ | 
|---|
| [3dbb9d] | 109 | for(int i=NDIM;i--;){ | 
|---|
|  | 110 | for(int j=NDIM;j--;){ | 
|---|
|  | 111 | set(i,j,i==j); | 
|---|
|  | 112 | } | 
|---|
| [1da5f5] | 113 | } | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
| [a439e5] | 116 | void Matrix::zero(){ | 
|---|
|  | 117 | for(int i=NDIM;i--;){ | 
|---|
|  | 118 | for(int j=NDIM;j--;){ | 
|---|
|  | 119 | set(i,j,0.); | 
|---|
|  | 120 | } | 
|---|
|  | 121 | } | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
| [31fb1d] | 124 | void Matrix::rotation(const double x, const double y, const double z) | 
|---|
|  | 125 | { | 
|---|
|  | 126 | set(0,0, cos(y)*cos(z)); | 
|---|
|  | 127 | set(0,1, cos(z)*sin(x)*sin(y) - cos(x)*sin(z)); | 
|---|
|  | 128 | set(0,2, cos(x)*cos(z)*sin(y) + sin(x) * sin(z)); | 
|---|
|  | 129 | set(1,0, cos(y)*sin(z)); | 
|---|
|  | 130 | set(1,1, cos(x)*cos(z) + sin(x)*sin(y)*sin(z)); | 
|---|
|  | 131 | set(1,2, -cos(z)*sin(x) + cos(x)*sin(y)*sin(z)); | 
|---|
|  | 132 | set(2,0, -sin(y)); | 
|---|
|  | 133 | set(2,1, cos(y)*sin(x)); | 
|---|
|  | 134 | set(2,2, cos(x)*cos(y)); | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [325390] | 137 | Matrix &Matrix::operator=(const Matrix &src){ | 
|---|
|  | 138 | if(&src!=this){ | 
|---|
| [fee079] | 139 | gsl_matrix_memcpy(content->content,src.content->content); | 
|---|
| [325390] | 140 | } | 
|---|
|  | 141 | return *this; | 
|---|
|  | 142 | } | 
|---|
|  | 143 |  | 
|---|
| [5b4605] | 144 | const Matrix &Matrix::operator+=(const Matrix &rhs){ | 
|---|
| [fee079] | 145 | gsl_matrix_add(content->content, rhs.content->content); | 
|---|
| [325390] | 146 | return *this; | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
| [5b4605] | 149 | const Matrix &Matrix::operator-=(const Matrix &rhs){ | 
|---|
| [fee079] | 150 | gsl_matrix_sub(content->content, rhs.content->content); | 
|---|
| [325390] | 151 | return *this; | 
|---|
|  | 152 | } | 
|---|
|  | 153 |  | 
|---|
| [5b4605] | 154 | const Matrix &Matrix::operator*=(const Matrix &rhs){ | 
|---|
| [325390] | 155 | (*this) = (*this)*rhs; | 
|---|
|  | 156 | return *this; | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
| [5b4605] | 159 | const Matrix Matrix::operator+(const Matrix &rhs) const{ | 
|---|
| [325390] | 160 | Matrix tmp = *this; | 
|---|
|  | 161 | tmp+=rhs; | 
|---|
|  | 162 | return tmp; | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
| [5b4605] | 165 | const Matrix Matrix::operator-(const Matrix &rhs) const{ | 
|---|
| [325390] | 166 | Matrix tmp = *this; | 
|---|
|  | 167 | tmp-=rhs; | 
|---|
|  | 168 | return tmp; | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
| [5b4605] | 171 | const Matrix Matrix::operator*(const Matrix &rhs) const{ | 
|---|
| [325390] | 172 | gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
| [fee079] | 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); | 
|---|
| [325390] | 177 | } | 
|---|
|  | 178 |  | 
|---|
|  | 179 | double &Matrix::at(size_t i, size_t j){ | 
|---|
| [e3ffd3] | 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"); | 
|---|
| [fee079] | 182 | return *gsl_matrix_ptr (content->content, i, j); | 
|---|
| [325390] | 183 | } | 
|---|
|  | 184 |  | 
|---|
| [436f04] | 185 | const double Matrix::at(size_t i, size_t j) const{ | 
|---|
| [e3ffd3] | 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"); | 
|---|
| [fee079] | 188 | return gsl_matrix_get(content->content, i, j); | 
|---|
| [436f04] | 189 | } | 
|---|
|  | 190 |  | 
|---|
| [3dbb9d] | 191 | Vector &Matrix::row(size_t i){ | 
|---|
|  | 192 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); | 
|---|
|  | 193 | return *rows_ptr[i]; | 
|---|
|  | 194 | } | 
|---|
|  | 195 |  | 
|---|
|  | 196 | const Vector &Matrix::row(size_t i) const{ | 
|---|
|  | 197 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); | 
|---|
|  | 198 | return *rows_ptr[i]; | 
|---|
|  | 199 | } | 
|---|
|  | 200 |  | 
|---|
|  | 201 | Vector &Matrix::column(size_t i){ | 
|---|
|  | 202 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); | 
|---|
|  | 203 | return *columns_ptr[i]; | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
|  | 206 | const Vector &Matrix::column(size_t i) const{ | 
|---|
|  | 207 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range"); | 
|---|
|  | 208 | return *columns_ptr[i]; | 
|---|
|  | 209 | } | 
|---|
|  | 210 |  | 
|---|
|  | 211 | Vector &Matrix::diagonal(){ | 
|---|
|  | 212 | return *diagonal_ptr; | 
|---|
|  | 213 | } | 
|---|
|  | 214 |  | 
|---|
|  | 215 | const Vector &Matrix::diagonal() const{ | 
|---|
|  | 216 | return *diagonal_ptr; | 
|---|
|  | 217 | } | 
|---|
|  | 218 |  | 
|---|
| [436f04] | 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"); | 
|---|
| [fee079] | 222 | gsl_matrix_set(content->content,i,j,value); | 
|---|
| [cadbc1] | 223 | } | 
|---|
|  | 224 |  | 
|---|
|  | 225 | double Matrix::determinant() const{ | 
|---|
| [325390] | 226 | return at(0,0)*at(1,1)*at(2,2) | 
|---|
|  | 227 | + at(0,1)*at(1,2)*at(2,0) | 
|---|
|  | 228 | + at(0,2)*at(1,0)*at(2,1) | 
|---|
|  | 229 | - at(2,0)*at(1,1)*at(0,2) | 
|---|
|  | 230 | - at(2,1)*at(1,2)*at(0,0) | 
|---|
|  | 231 | - at(2,2)*at(1,0)*at(0,1); | 
|---|
|  | 232 | } | 
|---|
|  | 233 |  | 
|---|
| [a439e5] | 234 | Matrix Matrix::transpose() const | 
|---|
|  | 235 | { | 
|---|
|  | 236 | Matrix copy(*this); | 
|---|
|  | 237 | copy.transpose(); | 
|---|
|  | 238 | return copy; | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
|  | 241 |  | 
|---|
| [31fb1d] | 242 | Matrix &Matrix::transpose() | 
|---|
| [a439e5] | 243 | { | 
|---|
|  | 244 | double tmp; | 
|---|
|  | 245 | for (int i=0;i<NDIM;i++) | 
|---|
|  | 246 | for (int j=i+1;j<NDIM;j++) { | 
|---|
|  | 247 | tmp = at(j,i); | 
|---|
| [31fb1d] | 248 | at(j,i) = at(i,j); | 
|---|
| [a439e5] | 249 | at(i,j) = tmp; | 
|---|
|  | 250 | } | 
|---|
| [31fb1d] | 251 | return *this; | 
|---|
| [a439e5] | 252 | } | 
|---|
|  | 253 |  | 
|---|
|  | 254 |  | 
|---|
| [cadbc1] | 255 | Matrix Matrix::invert() const{ | 
|---|
| [325390] | 256 | double det = determinant(); | 
|---|
|  | 257 | if(fabs(det)<MYEPSILON){ | 
|---|
|  | 258 | throw NotInvertibleException(__FILE__,__LINE__); | 
|---|
|  | 259 | } | 
|---|
|  | 260 |  | 
|---|
|  | 261 | double detReci = 1./det; | 
|---|
|  | 262 | Matrix res; | 
|---|
| [436f04] | 263 | res.set(0,0,  detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2)));    // A_11 | 
|---|
|  | 264 | res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2)));    // A_21 | 
|---|
|  | 265 | res.set(2,0,  detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1)));    // A_31 | 
|---|
|  | 266 | res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2)));    // A_12 | 
|---|
|  | 267 | res.set(1,1,  detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2)));    // A_22 | 
|---|
|  | 268 | res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1)));    // A_32 | 
|---|
|  | 269 | res.set(0,2,  detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2)));    // A_13 | 
|---|
|  | 270 | res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2)));    // A_23 | 
|---|
|  | 271 | res.set(2,2,  detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1)));    // A_33 | 
|---|
| [325390] | 272 | return res; | 
|---|
|  | 273 | } | 
|---|
|  | 274 |  | 
|---|
| [a439e5] | 275 | Vector Matrix::transformToEigenbasis() | 
|---|
|  | 276 | { | 
|---|
|  | 277 | gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM); | 
|---|
|  | 278 | gsl_vector *eval = gsl_vector_alloc(NDIM); | 
|---|
|  | 279 | gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
|  | 280 | gsl_eigen_symmv(content->content, eval, evec, T); | 
|---|
|  | 281 | gsl_eigen_symmv_free(T); | 
|---|
|  | 282 | gsl_matrix_memcpy(content->content, evec); | 
|---|
| [80cecb5] | 283 | gsl_matrix_free(evec); | 
|---|
| [a439e5] | 284 | Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2)); | 
|---|
| [80cecb5] | 285 | gsl_vector_free(eval); | 
|---|
| [a439e5] | 286 | return evalues; | 
|---|
|  | 287 | } | 
|---|
|  | 288 |  | 
|---|
| [5b4605] | 289 | const Matrix &Matrix::operator*=(const double factor){ | 
|---|
| [fee079] | 290 | gsl_matrix_scale(content->content, factor); | 
|---|
| [325390] | 291 | return *this; | 
|---|
|  | 292 | } | 
|---|
|  | 293 |  | 
|---|
| [5b4605] | 294 | const Matrix operator*(const double factor,const Matrix& mat){ | 
|---|
| [325390] | 295 | Matrix tmp = mat; | 
|---|
|  | 296 | tmp*=factor; | 
|---|
|  | 297 | return tmp; | 
|---|
|  | 298 | } | 
|---|
|  | 299 |  | 
|---|
| [5b4605] | 300 | const Matrix operator*(const Matrix &mat,const double factor){ | 
|---|
| [325390] | 301 | return factor*mat; | 
|---|
|  | 302 | } | 
|---|
| [d10eb6] | 303 |  | 
|---|
| [0eb2dc] | 304 | bool Matrix::operator==(const Matrix &rhs) const{ | 
|---|
|  | 305 | for(int i=NDIM;i--;){ | 
|---|
|  | 306 | for(int j=NDIM;j--;){ | 
|---|
|  | 307 | if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){ | 
|---|
|  | 308 | return false; | 
|---|
|  | 309 | } | 
|---|
|  | 310 | } | 
|---|
|  | 311 | } | 
|---|
|  | 312 | return true; | 
|---|
|  | 313 | } | 
|---|
|  | 314 |  | 
|---|
| [d10eb6] | 315 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix. | 
|---|
|  | 316 | * \param *symm 6-dim array of unique symmetric matrix components | 
|---|
|  | 317 | * \return allocated NDIM*NDIM array with the symmetric matrix | 
|---|
|  | 318 | */ | 
|---|
|  | 319 | Matrix ReturnFullMatrixforSymmetric(const double * const symm) | 
|---|
|  | 320 | { | 
|---|
|  | 321 | Matrix matrix; | 
|---|
| [436f04] | 322 | matrix.set(0,0, symm[0]); | 
|---|
|  | 323 | matrix.set(1,0, symm[1]); | 
|---|
|  | 324 | matrix.set(2,0, symm[3]); | 
|---|
|  | 325 | matrix.set(0,1, symm[1]); | 
|---|
|  | 326 | matrix.set(1,1, symm[2]); | 
|---|
|  | 327 | matrix.set(2,1, symm[4]); | 
|---|
|  | 328 | matrix.set(0,2, symm[3]); | 
|---|
|  | 329 | matrix.set(1,2, symm[4]); | 
|---|
|  | 330 | matrix.set(2,2, symm[5]); | 
|---|
| [d10eb6] | 331 | return matrix; | 
|---|
|  | 332 | }; | 
|---|
| [c49c96] | 333 |  | 
|---|
|  | 334 | ostream &operator<<(ostream &ost,const Matrix &mat){ | 
|---|
|  | 335 | for(int i = 0;i<NDIM;++i){ | 
|---|
|  | 336 | ost << "\n"; | 
|---|
|  | 337 | for(int j = 0; j<NDIM;++j){ | 
|---|
|  | 338 | ost << mat.at(i,j); | 
|---|
|  | 339 | if(j!=NDIM-1) | 
|---|
|  | 340 | ost << "; "; | 
|---|
|  | 341 | } | 
|---|
|  | 342 | } | 
|---|
|  | 343 | return ost; | 
|---|
|  | 344 | } | 
|---|
| [4b94bb] | 345 |  | 
|---|
|  | 346 | Vector operator*(const Matrix &mat,const Vector &vec){ | 
|---|
| [2a7457] | 347 | Vector res; | 
|---|
|  | 348 | gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res.content->content); | 
|---|
|  | 349 | return res; | 
|---|
| [4b94bb] | 350 | } | 
|---|
|  | 351 |  | 
|---|
|  | 352 | Vector &operator*=(Vector& lhs,const Matrix &mat){ | 
|---|
|  | 353 | lhs = mat*lhs; | 
|---|
|  | 354 | return lhs; | 
|---|
|  | 355 | } | 
|---|
|  | 356 |  | 
|---|