| [fee079] | 1 | /* | 
|---|
|  | 2 | * MatrixContent.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Jul 2, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef MATRIXCONTENT_HPP_ | 
|---|
|  | 9 | #define MATRIXCONTENT_HPP_ | 
|---|
|  | 10 |  | 
|---|
| [3bc926] | 11 | /** MatrixContent is a wrapper for gsl_matrix. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | * The GNU Scientific Library contaisn some very well written routines for | 
|---|
|  | 14 | * linear algebra problems. However, it's syntax is C and hence it does not | 
|---|
|  | 15 | * lend itself to readable written code, i.e. C = A * B, where A, B, and C | 
|---|
|  | 16 | * are all matrices. Writing code this way is very convenient, concise and | 
|---|
|  | 17 | * also in the same manner as one would type in MatLab. | 
|---|
|  | 18 | * However, with C++ and its feature to overload functions and its operator | 
|---|
|  | 19 | * functions such syntax is easy to create. | 
|---|
|  | 20 | * | 
|---|
|  | 21 | * Hence, this class is a C++ wrapper to gsl_matrix. There already some other | 
|---|
|  | 22 | * libraries, however they fall short for the following reasons: | 
|---|
|  | 23 | * -# gslwrap: not very well written and uses floats instead of doubles | 
|---|
|  | 24 | * -# gslcpp: last change is from 2007 and only very few commits | 
|---|
|  | 25 | * -# o2scl: basically, the same functionality as gsl only written in C++, | 
|---|
|  | 26 | *    however it uses GPLv3 license which is inconvenient for MoleCuilder. | 
|---|
|  | 27 | * | 
|---|
|  | 28 | * <h1>Howto use MatrixContent</h1> | 
|---|
|  | 29 | * | 
|---|
|  | 30 | * One should not use MatrixContent directly but either have it as a member | 
|---|
|  | 31 | * variable in a specialized class or inherit from it. | 
|---|
|  | 32 | * | 
|---|
| [fee079] | 33 | */ | 
|---|
|  | 34 |  | 
|---|
|  | 35 | #include <gsl/gsl_matrix.h> | 
|---|
|  | 36 |  | 
|---|
| [b4cf2b] | 37 | #include <iosfwd> | 
|---|
|  | 38 |  | 
|---|
| [3da2fb] | 39 | #include "LinearAlgebra/MatrixVector_ops.hpp" | 
|---|
|  | 40 |  | 
|---|
| [60dada] | 41 | class VectorContent; | 
|---|
| [3bc926] | 42 |  | 
|---|
| [8e9ce1] | 43 | /** Dummy structure to create a unique signature. | 
|---|
|  | 44 | * | 
|---|
|  | 45 | */ | 
|---|
|  | 46 | struct MatrixBaseCase{}; | 
|---|
|  | 47 |  | 
|---|
| [3bc926] | 48 | class MatrixContent | 
|---|
|  | 49 | { | 
|---|
| [b4cf2b] | 50 | friend std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat); | 
|---|
| [cca9ef] | 51 | friend class RealSpaceMatrix; | 
|---|
| [0d4424] | 52 | friend class LinearSystemOfEquations; | 
|---|
|  | 53 |  | 
|---|
| [3da2fb] | 54 | // matrix vector products | 
|---|
|  | 55 | friend VectorContent const operator*(const VectorContent& vec, const MatrixContent& mat); | 
|---|
|  | 56 | friend VectorContent const operator*(const MatrixContent& mat, const VectorContent& vec); | 
|---|
|  | 57 |  | 
|---|
|  | 58 | // unit tests | 
|---|
| [0d4424] | 59 | friend class MatrixContentTest; | 
|---|
|  | 60 | friend class MatrixContentSymmetricTest; | 
|---|
| [3bc926] | 61 | public: | 
|---|
|  | 62 | MatrixContent(size_t rows, size_t columns); | 
|---|
| [8e9ce1] | 63 | MatrixContent(size_t _rows, size_t _columns, MatrixBaseCase); | 
|---|
| [3bc926] | 64 | MatrixContent(size_t rows, size_t columns, const double *src); | 
|---|
|  | 65 | MatrixContent(gsl_matrix *&src); | 
|---|
|  | 66 | MatrixContent(const MatrixContent &src); | 
|---|
|  | 67 | MatrixContent(const MatrixContent *src); | 
|---|
| [8e9ce1] | 68 | virtual ~MatrixContent(); | 
|---|
| [3bc926] | 69 |  | 
|---|
|  | 70 | // Accessing | 
|---|
|  | 71 | double &at(size_t i, size_t j); | 
|---|
|  | 72 | const double at(size_t i, size_t j) const; | 
|---|
|  | 73 | void set(size_t i, size_t j, const double value); | 
|---|
| [bbf1bd] | 74 |  | 
|---|
|  | 75 | // Initializing | 
|---|
| [0d4424] | 76 | void setFromDoubleArray(double * x); | 
|---|
| [bbf1bd] | 77 | void setIdentity(); | 
|---|
|  | 78 | void setValue(double _value); | 
|---|
|  | 79 | void setZero(); | 
|---|
| [0d4424] | 80 |  | 
|---|
|  | 81 | // Exchanging elements | 
|---|
|  | 82 | bool SwapRows(size_t i, size_t j); | 
|---|
|  | 83 | bool SwapColumns(size_t i, size_t j); | 
|---|
|  | 84 | bool SwapRowColumn(size_t i, size_t j); | 
|---|
| [3bc926] | 85 |  | 
|---|
|  | 86 | // Transformations | 
|---|
|  | 87 | MatrixContent transpose() const; | 
|---|
|  | 88 | MatrixContent &transpose(); | 
|---|
|  | 89 | gsl_vector* transformToEigenbasis(); | 
|---|
| [e828c0] | 90 | void sortEigenbasis(gsl_vector *eigenvalues); | 
|---|
| [3bc926] | 91 |  | 
|---|
| [0d4424] | 92 | // Properties | 
|---|
|  | 93 | bool IsNull() const; | 
|---|
|  | 94 | bool IsPositive() const; | 
|---|
|  | 95 | bool IsNegative() const; | 
|---|
|  | 96 | bool IsNonNegative() const; | 
|---|
|  | 97 | bool IsPositiveDefinite() const; | 
|---|
|  | 98 | double Determinant() const; | 
|---|
|  | 99 |  | 
|---|
| [3bc926] | 100 | // Operators | 
|---|
|  | 101 | MatrixContent &operator=(const MatrixContent &src); | 
|---|
|  | 102 | const MatrixContent &operator+=(const MatrixContent &rhs); | 
|---|
|  | 103 | const MatrixContent &operator-=(const MatrixContent &rhs); | 
|---|
|  | 104 | const MatrixContent &operator*=(const MatrixContent &rhs); | 
|---|
|  | 105 | const MatrixContent operator*(const MatrixContent &rhs) const; | 
|---|
| [d85c28] | 106 | const MatrixContent &operator&=(const MatrixContent &rhs); | 
|---|
|  | 107 | const MatrixContent operator&(const MatrixContent &rhs) const; | 
|---|
| [3bc926] | 108 | const MatrixContent &operator*=(const double factor); | 
|---|
|  | 109 | bool operator==(const MatrixContent &rhs) const; | 
|---|
|  | 110 |  | 
|---|
| [35fbef] | 111 | const size_t getRows() const; | 
|---|
|  | 112 | const size_t getColumns() const; | 
|---|
| [60dada] | 113 |  | 
|---|
|  | 114 | VectorContent *getColumnVector(size_t column) const; | 
|---|
|  | 115 | VectorContent *getRowVector(size_t row) const; | 
|---|
|  | 116 | VectorContent *getDiagonalVector() const; | 
|---|
|  | 117 |  | 
|---|
| [0d4424] | 118 | protected: | 
|---|
|  | 119 | double *Pointer(size_t m, size_t n); | 
|---|
|  | 120 | const double *const_Pointer(size_t m, size_t n) const; | 
|---|
| [3bc926] | 121 |  | 
|---|
| [fee079] | 122 | gsl_matrix * content; | 
|---|
| [3bc926] | 123 | const size_t rows;      // store for internal purposes | 
|---|
|  | 124 | const size_t columns;  // store for internal purposes | 
|---|
| [8e9ce1] | 125 |  | 
|---|
|  | 126 | private: | 
|---|
| [fee079] | 127 | }; | 
|---|
|  | 128 |  | 
|---|
| [3bc926] | 129 | const MatrixContent operator*(const double factor,const MatrixContent& mat); | 
|---|
|  | 130 | const MatrixContent operator*(const MatrixContent &mat,const double factor); | 
|---|
| [b4cf2b] | 131 | std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat); | 
|---|
| [3bc926] | 132 |  | 
|---|
| [8e9ce1] | 133 | /** Matrix view. | 
|---|
|  | 134 | * Extension of MatrixContent to contain not a gsl_matrix but only a view on a | 
|---|
|  | 135 | * gsl_matrix | 
|---|
|  | 136 | * | 
|---|
|  | 137 | * We need the above MatrixBaseCase here: | 
|---|
|  | 138 | * content, i.e. the gsl_matrix, must not be allocated, as it is just a view | 
|---|
|  | 139 | * with an internal gsl_matrix_view. Hence, MatrixBaseCase is just dummy class | 
|---|
|  | 140 | * to give the constructor a unique signature. | 
|---|
|  | 141 | */ | 
|---|
|  | 142 | struct MatrixViewContent : public MatrixContent | 
|---|
|  | 143 | { | 
|---|
|  | 144 | MatrixViewContent(gsl_matrix_view _view) : | 
|---|
|  | 145 | MatrixContent(_view.matrix.size1, _view.matrix.size2, MatrixBaseCase()), | 
|---|
|  | 146 | view(_view) | 
|---|
|  | 147 | { | 
|---|
|  | 148 | content = &view.matrix; | 
|---|
|  | 149 | } | 
|---|
|  | 150 | ~MatrixViewContent(){ | 
|---|
|  | 151 | content=0; | 
|---|
|  | 152 | } | 
|---|
|  | 153 | gsl_matrix_view view; | 
|---|
|  | 154 | }; | 
|---|
|  | 155 |  | 
|---|
| [fee079] | 156 | #endif /* MATRIXCONTENT_HPP_ */ | 
|---|