/* * MatrixContent.cpp * * Created on: Nov 14, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include "LinearAlgebra/RealSpaceMatrix.hpp" #include "Helpers/Assert.hpp" #include "Exceptions/NotInvertibleException.hpp" #include "Helpers/fast_functions.hpp" #include "Helpers/Assert.hpp" #include "LinearAlgebra/Vector.hpp" #include "LinearAlgebra/VectorContent.hpp" #include "LinearAlgebra/MatrixContent.hpp" #include #include #include #include #include #include #include using namespace std; /** Constructor for class MatrixContent. * \param rows number of rows * \param columns number of columns */ MatrixContent::MatrixContent(size_t _rows, size_t _columns) : rows(_rows), columns(_columns) { content = gsl_matrix_calloc(rows, columns); } /** Constructor for class MatrixContent. * \param rows number of rows * \param columns number of columns * \param *src array with components to initialize matrix with */ MatrixContent::MatrixContent(size_t _rows, size_t _columns, const double *src) : rows(_rows), columns(_columns) { content = gsl_matrix_calloc(rows, columns); set(0,0, src[0]); set(1,0, src[1]); set(2,0, src[2]); set(0,1, src[3]); set(1,1, src[4]); set(2,1, src[5]); set(0,2, src[6]); set(1,2, src[7]); set(2,2, src[8]); } /** Constructor for class MatrixContent. * We embed the given gls_matrix pointer within this class and set it to NULL * afterwards. * \param *src source gsl_matrix vector to embed within this class */ MatrixContent::MatrixContent(gsl_matrix *&src) : rows(src->size1), columns(src->size2) { content = gsl_matrix_alloc(src->size1, src->size2); gsl_matrix_memcpy(content,src); // content = src; // src = NULL; } /** Copy constructor for class MatrixContent. * \param &src reference to source MatrixContent */ MatrixContent::MatrixContent(const MatrixContent &src) : rows(src.rows), columns(src.columns) { content = gsl_matrix_alloc(src.rows, src.columns); gsl_matrix_memcpy(content,src.content); } /** Copy constructor for class MatrixContent. * \param *src pointer to source MatrixContent */ MatrixContent::MatrixContent(const MatrixContent *src) : rows(src->rows), columns(src->columns) { ASSERT(src != NULL, "MatrixContent::MatrixContent - pointer to source matrix is NULL!"); content = gsl_matrix_alloc(src->rows, src->columns); gsl_matrix_memcpy(content,src->content); } /** Destructor for class MatrixContent. */ MatrixContent::~MatrixContent() { gsl_matrix_free(content); } /** Set matrix to identity. */ void MatrixContent::setIdentity() { for(int i=rows;i--;){ for(int j=columns;j--;){ set(i,j,i==j); } } } /** Set all matrix components to zero. */ void MatrixContent::setZero() { for(int i=rows;i--;){ for(int j=columns;j--;){ set(i,j,0.); } } } /** Copy operator for MatrixContent with self-assignment check. * \param &src matrix to compare to * \return reference to this */ MatrixContent &MatrixContent::operator=(const MatrixContent &src) { if(&src!=this){ gsl_matrix_memcpy(content,src.content); } return *this; } /** Addition operator. * \param &rhs matrix to add * \return reference to this */ const MatrixContent &MatrixContent::operator+=(const MatrixContent &rhs) { gsl_matrix_add(content, rhs.content); return *this; } /** Subtraction operator. * \param &rhs matrix to subtract * \return reference to this */ const MatrixContent &MatrixContent::operator-=(const MatrixContent &rhs) { gsl_matrix_sub(content, rhs.content); return *this; } /** Multiplication operator. * Note that here matrix have to have same dimensions. * \param &rhs matrix to multiply with * \return reference to this */ const MatrixContent &MatrixContent::operator*=(const MatrixContent &rhs) { ASSERT(rows == rhs.rows, "MatrixContent::operator*=() - row dimension differ: "+toString(rows)+" != "+toString(rhs.rows)+"."); ASSERT(columns == rhs.columns, "MatrixContent::operator*=() - columns dimension differ: "+toString(columns)+" != "+toString(rhs.columns)+"."); (*this) = (*this)*rhs; return *this; } /** Multiplication with copy operator. * \param &rhs matrix to multiply with * \return reference to newly allocated MatrixContent */ const MatrixContent MatrixContent::operator*(const MatrixContent &rhs) const { gsl_matrix *res = gsl_matrix_alloc(rows, columns); gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content, rhs.content, 0.0, res); // gsl_matrix is taken over by constructor, hence no free MatrixContent tmp(res); gsl_matrix_free(res); return tmp; } /** Accessor for manipulating component (i,j). * \param i row number * \param j column number * \return reference to component (i,j) */ double &MatrixContent::at(size_t i, size_t j) { ASSERT((i>=0) && (i=0) && (j=0) && (i=0) && (j=0) && (i=0) && (jMYEPSILON){ return false; } } } return true; } return false; } Vector operator*(const MatrixContent &mat,const Vector &vec) { Vector res; gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content, vec.content->content, 0.0, res.content->content); return res; }