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