| [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 | 
 | 
|---|
| [325390] | 117 | Matrix &Matrix::operator=(const Matrix &src){
 | 
|---|
 | 118 |   if(&src!=this){
 | 
|---|
| [fee079] | 119 |     gsl_matrix_memcpy(content->content,src.content->content);
 | 
|---|
| [325390] | 120 |   }
 | 
|---|
 | 121 |   return *this;
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | 
 | 
|---|
| [5b4605] | 124 | const Matrix &Matrix::operator+=(const Matrix &rhs){
 | 
|---|
| [fee079] | 125 |   gsl_matrix_add(content->content, rhs.content->content);
 | 
|---|
| [325390] | 126 |   return *this;
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
| [5b4605] | 129 | const Matrix &Matrix::operator-=(const Matrix &rhs){
 | 
|---|
| [fee079] | 130 |   gsl_matrix_sub(content->content, rhs.content->content);
 | 
|---|
| [325390] | 131 |   return *this;
 | 
|---|
 | 132 | }
 | 
|---|
 | 133 | 
 | 
|---|
| [5b4605] | 134 | const Matrix &Matrix::operator*=(const Matrix &rhs){
 | 
|---|
| [325390] | 135 |   (*this) = (*this)*rhs;
 | 
|---|
 | 136 |   return *this;
 | 
|---|
 | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
| [5b4605] | 139 | const Matrix Matrix::operator+(const Matrix &rhs) const{
 | 
|---|
| [325390] | 140 |   Matrix tmp = *this;
 | 
|---|
 | 141 |   tmp+=rhs;
 | 
|---|
 | 142 |   return tmp;
 | 
|---|
 | 143 | }
 | 
|---|
 | 144 | 
 | 
|---|
| [5b4605] | 145 | const Matrix Matrix::operator-(const Matrix &rhs) const{
 | 
|---|
| [325390] | 146 |   Matrix tmp = *this;
 | 
|---|
 | 147 |   tmp-=rhs;
 | 
|---|
 | 148 |   return tmp;
 | 
|---|
 | 149 | }
 | 
|---|
 | 150 | 
 | 
|---|
| [5b4605] | 151 | const Matrix Matrix::operator*(const Matrix &rhs) const{
 | 
|---|
| [325390] | 152 |   gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM);
 | 
|---|
| [fee079] | 153 |   gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content->content, rhs.content->content, 0.0, res);
 | 
|---|
 | 154 |   MatrixContent *content= new MatrixContent();
 | 
|---|
 | 155 |   content->content = res;
 | 
|---|
 | 156 |   return Matrix(content);
 | 
|---|
| [325390] | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | double &Matrix::at(size_t i, size_t j){
 | 
|---|
| [e3ffd3] | 160 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 161 |   ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
 | 
|---|
| [fee079] | 162 |   return *gsl_matrix_ptr (content->content, i, j);
 | 
|---|
| [325390] | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
| [436f04] | 165 | const double Matrix::at(size_t i, size_t j) const{
 | 
|---|
| [e3ffd3] | 166 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 167 |   ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
 | 
|---|
| [fee079] | 168 |   return gsl_matrix_get(content->content, i, j);
 | 
|---|
| [436f04] | 169 | }
 | 
|---|
 | 170 | 
 | 
|---|
| [3dbb9d] | 171 | Vector &Matrix::row(size_t i){
 | 
|---|
 | 172 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 173 |   return *rows_ptr[i];
 | 
|---|
 | 174 | }
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 | const Vector &Matrix::row(size_t i) const{
 | 
|---|
 | 177 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 178 |   return *rows_ptr[i];
 | 
|---|
 | 179 | }
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 | Vector &Matrix::column(size_t i){
 | 
|---|
 | 182 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 183 |   return *columns_ptr[i];
 | 
|---|
 | 184 | }
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 | const Vector &Matrix::column(size_t i) const{
 | 
|---|
 | 187 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 188 |   return *columns_ptr[i];
 | 
|---|
 | 189 | }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 | Vector &Matrix::diagonal(){
 | 
|---|
 | 192 |   return *diagonal_ptr;
 | 
|---|
 | 193 | }
 | 
|---|
 | 194 | 
 | 
|---|
 | 195 | const Vector &Matrix::diagonal() const{
 | 
|---|
 | 196 |   return *diagonal_ptr;
 | 
|---|
 | 197 | }
 | 
|---|
 | 198 | 
 | 
|---|
| [436f04] | 199 | void Matrix::set(size_t i, size_t j, const double value){
 | 
|---|
 | 200 |   ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
 | 
|---|
 | 201 |   ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
 | 
|---|
| [fee079] | 202 |   gsl_matrix_set(content->content,i,j,value);
 | 
|---|
| [cadbc1] | 203 | }
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 | double Matrix::determinant() const{
 | 
|---|
| [325390] | 206 |   return at(0,0)*at(1,1)*at(2,2)
 | 
|---|
 | 207 |        + at(0,1)*at(1,2)*at(2,0)
 | 
|---|
 | 208 |        + at(0,2)*at(1,0)*at(2,1)
 | 
|---|
 | 209 |        - at(2,0)*at(1,1)*at(0,2)
 | 
|---|
 | 210 |        - at(2,1)*at(1,2)*at(0,0)
 | 
|---|
 | 211 |        - at(2,2)*at(1,0)*at(0,1);
 | 
|---|
 | 212 | }
 | 
|---|
 | 213 | 
 | 
|---|
| [a439e5] | 214 | Matrix Matrix::transpose() const
 | 
|---|
 | 215 | {
 | 
|---|
 | 216 |   Matrix copy(*this);
 | 
|---|
 | 217 |   copy.transpose();
 | 
|---|
 | 218 |   return copy;
 | 
|---|
 | 219 | }
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 | void Matrix::transpose()
 | 
|---|
 | 223 | {
 | 
|---|
 | 224 |   double tmp;
 | 
|---|
 | 225 |   for (int i=0;i<NDIM;i++)
 | 
|---|
 | 226 |     for (int j=i+1;j<NDIM;j++) {
 | 
|---|
 | 227 |       tmp = at(j,i);
 | 
|---|
 | 228 |       at(i,j) = tmp;
 | 
|---|
 | 229 |       at(j,i) = tmp;
 | 
|---|
 | 230 |     }
 | 
|---|
 | 231 | }
 | 
|---|
 | 232 | 
 | 
|---|
 | 233 | 
 | 
|---|
| [cadbc1] | 234 | Matrix Matrix::invert() const{
 | 
|---|
| [325390] | 235 |   double det = determinant();
 | 
|---|
 | 236 |   if(fabs(det)<MYEPSILON){
 | 
|---|
 | 237 |     throw NotInvertibleException(__FILE__,__LINE__);
 | 
|---|
 | 238 |   }
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 |   double detReci = 1./det;
 | 
|---|
 | 241 |   Matrix res;
 | 
|---|
| [436f04] | 242 |   res.set(0,0,  detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2)));    // A_11
 | 
|---|
 | 243 |   res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2)));    // A_21
 | 
|---|
 | 244 |   res.set(2,0,  detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1)));    // A_31
 | 
|---|
 | 245 |   res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2)));    // A_12
 | 
|---|
 | 246 |   res.set(1,1,  detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2)));    // A_22
 | 
|---|
 | 247 |   res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1)));    // A_32
 | 
|---|
 | 248 |   res.set(0,2,  detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2)));    // A_13
 | 
|---|
 | 249 |   res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2)));    // A_23
 | 
|---|
 | 250 |   res.set(2,2,  detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1)));    // A_33
 | 
|---|
| [325390] | 251 |   return res;
 | 
|---|
 | 252 | }
 | 
|---|
 | 253 | 
 | 
|---|
| [a439e5] | 254 | Vector Matrix::transformToEigenbasis()
 | 
|---|
 | 255 | {
 | 
|---|
 | 256 |   gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
 | 
|---|
 | 257 |   gsl_vector *eval = gsl_vector_alloc(NDIM);
 | 
|---|
 | 258 |   gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
 | 
|---|
 | 259 |   gsl_eigen_symmv(content->content, eval, evec, T);
 | 
|---|
 | 260 |   gsl_eigen_symmv_free(T);
 | 
|---|
 | 261 |   gsl_matrix_memcpy(content->content, evec);
 | 
|---|
 | 262 |   Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
 | 
|---|
 | 263 |   return evalues;
 | 
|---|
 | 264 | }
 | 
|---|
 | 265 | 
 | 
|---|
| [5b4605] | 266 | const Matrix &Matrix::operator*=(const double factor){
 | 
|---|
| [fee079] | 267 |   gsl_matrix_scale(content->content, factor);
 | 
|---|
| [325390] | 268 |   return *this;
 | 
|---|
 | 269 | }
 | 
|---|
 | 270 | 
 | 
|---|
| [5b4605] | 271 | const Matrix operator*(const double factor,const Matrix& mat){
 | 
|---|
| [325390] | 272 |   Matrix tmp = mat;
 | 
|---|
 | 273 |   tmp*=factor;
 | 
|---|
 | 274 |   return tmp;
 | 
|---|
 | 275 | }
 | 
|---|
 | 276 | 
 | 
|---|
| [5b4605] | 277 | const Matrix operator*(const Matrix &mat,const double factor){
 | 
|---|
| [325390] | 278 |   return factor*mat;
 | 
|---|
 | 279 | }
 | 
|---|
| [d10eb6] | 280 | 
 | 
|---|
| [0eb2dc] | 281 | bool Matrix::operator==(const Matrix &rhs) const{
 | 
|---|
 | 282 |   for(int i=NDIM;i--;){
 | 
|---|
 | 283 |     for(int j=NDIM;j--;){
 | 
|---|
 | 284 |       if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){
 | 
|---|
 | 285 |         return false;
 | 
|---|
 | 286 |       }
 | 
|---|
 | 287 |     }
 | 
|---|
 | 288 |   }
 | 
|---|
 | 289 |   return true;
 | 
|---|
 | 290 | }
 | 
|---|
 | 291 | 
 | 
|---|
| [d10eb6] | 292 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
 | 
|---|
 | 293 |  * \param *symm 6-dim array of unique symmetric matrix components
 | 
|---|
 | 294 |  * \return allocated NDIM*NDIM array with the symmetric matrix
 | 
|---|
 | 295 |  */
 | 
|---|
 | 296 | Matrix ReturnFullMatrixforSymmetric(const double * const symm)
 | 
|---|
 | 297 | {
 | 
|---|
 | 298 |   Matrix matrix;
 | 
|---|
| [436f04] | 299 |   matrix.set(0,0, symm[0]);
 | 
|---|
 | 300 |   matrix.set(1,0, symm[1]);
 | 
|---|
 | 301 |   matrix.set(2,0, symm[3]);
 | 
|---|
 | 302 |   matrix.set(0,1, symm[1]);
 | 
|---|
 | 303 |   matrix.set(1,1, symm[2]);
 | 
|---|
 | 304 |   matrix.set(2,1, symm[4]);
 | 
|---|
 | 305 |   matrix.set(0,2, symm[3]);
 | 
|---|
 | 306 |   matrix.set(1,2, symm[4]);
 | 
|---|
 | 307 |   matrix.set(2,2, symm[5]);
 | 
|---|
| [d10eb6] | 308 |   return matrix;
 | 
|---|
 | 309 | };
 | 
|---|
| [c49c96] | 310 | 
 | 
|---|
 | 311 | ostream &operator<<(ostream &ost,const Matrix &mat){
 | 
|---|
 | 312 |   for(int i = 0;i<NDIM;++i){
 | 
|---|
 | 313 |     ost << "\n";
 | 
|---|
 | 314 |     for(int j = 0; j<NDIM;++j){
 | 
|---|
 | 315 |       ost << mat.at(i,j);
 | 
|---|
 | 316 |       if(j!=NDIM-1)
 | 
|---|
 | 317 |         ost << "; ";
 | 
|---|
 | 318 |     }
 | 
|---|
 | 319 |   }
 | 
|---|
 | 320 |   return ost;
 | 
|---|
 | 321 | }
 | 
|---|
| [4b94bb] | 322 | 
 | 
|---|
 | 323 | Vector operator*(const Matrix &mat,const Vector &vec){
 | 
|---|
 | 324 |   gsl_vector *res = gsl_vector_calloc(NDIM);
 | 
|---|
| [fee079] | 325 |   gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res);
 | 
|---|
| [ce3d2b] | 326 |   VectorContent *content = new VectorContent();
 | 
|---|
 | 327 |   content->content = res;
 | 
|---|
 | 328 |   return Vector(content);
 | 
|---|
| [4b94bb] | 329 | }
 | 
|---|
 | 330 | 
 | 
|---|
 | 331 | Vector &operator*=(Vector& lhs,const Matrix &mat){
 | 
|---|
 | 332 |   lhs = mat*lhs;
 | 
|---|
 | 333 |   return lhs;
 | 
|---|
 | 334 | }
 | 
|---|
 | 335 | 
 | 
|---|