| 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::operator*=(const double factor){ | 
|---|
| 216 | gsl_matrix_scale(content->content, factor); | 
|---|
| 217 | return *this; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | Matrix operator*(const double factor,const Matrix& mat){ | 
|---|
| 221 | Matrix tmp = mat; | 
|---|
| 222 | tmp*=factor; | 
|---|
| 223 | return tmp; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | Matrix operator*(const Matrix &mat,const double factor){ | 
|---|
| 227 | return factor*mat; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | bool Matrix::operator==(const Matrix &rhs) const{ | 
|---|
| 231 | for(int i=NDIM;i--;){ | 
|---|
| 232 | for(int j=NDIM;j--;){ | 
|---|
| 233 | if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){ | 
|---|
| 234 | return false; | 
|---|
| 235 | } | 
|---|
| 236 | } | 
|---|
| 237 | } | 
|---|
| 238 | return true; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix. | 
|---|
| 242 | * \param *symm 6-dim array of unique symmetric matrix components | 
|---|
| 243 | * \return allocated NDIM*NDIM array with the symmetric matrix | 
|---|
| 244 | */ | 
|---|
| 245 | Matrix ReturnFullMatrixforSymmetric(const double * const symm) | 
|---|
| 246 | { | 
|---|
| 247 | Matrix matrix; | 
|---|
| 248 | matrix.set(0,0, symm[0]); | 
|---|
| 249 | matrix.set(1,0, symm[1]); | 
|---|
| 250 | matrix.set(2,0, symm[3]); | 
|---|
| 251 | matrix.set(0,1, symm[1]); | 
|---|
| 252 | matrix.set(1,1, symm[2]); | 
|---|
| 253 | matrix.set(2,1, symm[4]); | 
|---|
| 254 | matrix.set(0,2, symm[3]); | 
|---|
| 255 | matrix.set(1,2, symm[4]); | 
|---|
| 256 | matrix.set(2,2, symm[5]); | 
|---|
| 257 | return matrix; | 
|---|
| 258 | }; | 
|---|
| 259 |  | 
|---|
| 260 | ostream &operator<<(ostream &ost,const Matrix &mat){ | 
|---|
| 261 | for(int i = 0;i<NDIM;++i){ | 
|---|
| 262 | ost << "\n"; | 
|---|
| 263 | for(int j = 0; j<NDIM;++j){ | 
|---|
| 264 | ost << mat.at(i,j); | 
|---|
| 265 | if(j!=NDIM-1) | 
|---|
| 266 | ost << "; "; | 
|---|
| 267 | } | 
|---|
| 268 | } | 
|---|
| 269 | return ost; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | Vector operator*(const Matrix &mat,const Vector &vec){ | 
|---|
| 273 | gsl_vector *res = gsl_vector_calloc(NDIM); | 
|---|
| 274 | gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res); | 
|---|
| 275 | VectorContent *content = new VectorContent(); | 
|---|
| 276 | content->content = res; | 
|---|
| 277 | return Vector(content); | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | Vector &operator*=(Vector& lhs,const Matrix &mat){ | 
|---|
| 281 | lhs = mat*lhs; | 
|---|
| 282 | return lhs; | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|