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