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