| [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 |  | 
|---|
| [3bc926] | 39 | class Vector; | 
|---|
|  | 40 |  | 
|---|
| [8e9ce1] | 41 | /** Dummy structure to create a unique signature. | 
|---|
|  | 42 | * | 
|---|
|  | 43 | */ | 
|---|
|  | 44 | struct MatrixBaseCase{}; | 
|---|
|  | 45 |  | 
|---|
| [3bc926] | 46 | class MatrixContent | 
|---|
|  | 47 | { | 
|---|
|  | 48 | friend Vector operator*(const MatrixContent &mat,const Vector &vec); | 
|---|
| [b4cf2b] | 49 | friend std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat); | 
|---|
| [cca9ef] | 50 | friend class RealSpaceMatrix; | 
|---|
| [0d4424] | 51 | friend class LinearSystemOfEquations; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | friend class MatrixContentTest; | 
|---|
|  | 54 | friend class MatrixContentSymmetricTest; | 
|---|
| [3bc926] | 55 | public: | 
|---|
|  | 56 | MatrixContent(size_t rows, size_t columns); | 
|---|
| [8e9ce1] | 57 | MatrixContent(size_t _rows, size_t _columns, MatrixBaseCase); | 
|---|
| [3bc926] | 58 | MatrixContent(size_t rows, size_t columns, const double *src); | 
|---|
|  | 59 | MatrixContent(gsl_matrix *&src); | 
|---|
|  | 60 | MatrixContent(const MatrixContent &src); | 
|---|
|  | 61 | MatrixContent(const MatrixContent *src); | 
|---|
| [8e9ce1] | 62 | virtual ~MatrixContent(); | 
|---|
| [3bc926] | 63 |  | 
|---|
|  | 64 | // Accessing | 
|---|
|  | 65 | double &at(size_t i, size_t j); | 
|---|
|  | 66 | const double at(size_t i, size_t j) const; | 
|---|
|  | 67 | void set(size_t i, size_t j, const double value); | 
|---|
| [bbf1bd] | 68 |  | 
|---|
|  | 69 | // Initializing | 
|---|
| [0d4424] | 70 | void setFromDoubleArray(double * x); | 
|---|
| [bbf1bd] | 71 | void setIdentity(); | 
|---|
|  | 72 | void setValue(double _value); | 
|---|
|  | 73 | void setZero(); | 
|---|
| [0d4424] | 74 |  | 
|---|
|  | 75 | // Exchanging elements | 
|---|
|  | 76 | bool SwapRows(size_t i, size_t j); | 
|---|
|  | 77 | bool SwapColumns(size_t i, size_t j); | 
|---|
|  | 78 | bool SwapRowColumn(size_t i, size_t j); | 
|---|
| [3bc926] | 79 |  | 
|---|
|  | 80 | // Transformations | 
|---|
|  | 81 | MatrixContent transpose() const; | 
|---|
|  | 82 | MatrixContent &transpose(); | 
|---|
|  | 83 | gsl_vector* transformToEigenbasis(); | 
|---|
|  | 84 |  | 
|---|
| [0d4424] | 85 | // Properties | 
|---|
|  | 86 | bool IsNull() const; | 
|---|
|  | 87 | bool IsPositive() const; | 
|---|
|  | 88 | bool IsNegative() const; | 
|---|
|  | 89 | bool IsNonNegative() const; | 
|---|
|  | 90 | bool IsPositiveDefinite() const; | 
|---|
|  | 91 | double Determinant() const; | 
|---|
|  | 92 |  | 
|---|
| [3bc926] | 93 | // Operators | 
|---|
|  | 94 | MatrixContent &operator=(const MatrixContent &src); | 
|---|
|  | 95 | const MatrixContent &operator+=(const MatrixContent &rhs); | 
|---|
|  | 96 | const MatrixContent &operator-=(const MatrixContent &rhs); | 
|---|
|  | 97 | const MatrixContent &operator*=(const MatrixContent &rhs); | 
|---|
|  | 98 | const MatrixContent operator*(const MatrixContent &rhs) const; | 
|---|
|  | 99 | const MatrixContent &operator*=(const double factor); | 
|---|
|  | 100 | bool operator==(const MatrixContent &rhs) const; | 
|---|
|  | 101 |  | 
|---|
| [0d4424] | 102 | protected: | 
|---|
|  | 103 | double *Pointer(size_t m, size_t n); | 
|---|
|  | 104 | const double *const_Pointer(size_t m, size_t n) const; | 
|---|
| [3bc926] | 105 |  | 
|---|
| [fee079] | 106 | gsl_matrix * content; | 
|---|
| [3bc926] | 107 | const size_t rows;      // store for internal purposes | 
|---|
|  | 108 | const size_t columns;  // store for internal purposes | 
|---|
| [8e9ce1] | 109 |  | 
|---|
|  | 110 | private: | 
|---|
| [fee079] | 111 | }; | 
|---|
|  | 112 |  | 
|---|
| [3bc926] | 113 | const MatrixContent operator*(const double factor,const MatrixContent& mat); | 
|---|
|  | 114 | const MatrixContent operator*(const MatrixContent &mat,const double factor); | 
|---|
|  | 115 | Vector operator*(const MatrixContent &mat,const Vector &vec); | 
|---|
| [b4cf2b] | 116 | std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat); | 
|---|
| [3bc926] | 117 |  | 
|---|
| [8e9ce1] | 118 | /** Matrix view. | 
|---|
|  | 119 | * Extension of MatrixContent to contain not a gsl_matrix but only a view on a | 
|---|
|  | 120 | * gsl_matrix | 
|---|
|  | 121 | * | 
|---|
|  | 122 | * We need the above MatrixBaseCase here: | 
|---|
|  | 123 | * content, i.e. the gsl_matrix, must not be allocated, as it is just a view | 
|---|
|  | 124 | * with an internal gsl_matrix_view. Hence, MatrixBaseCase is just dummy class | 
|---|
|  | 125 | * to give the constructor a unique signature. | 
|---|
|  | 126 | */ | 
|---|
|  | 127 | struct MatrixViewContent : public MatrixContent | 
|---|
|  | 128 | { | 
|---|
|  | 129 | MatrixViewContent(gsl_matrix_view _view) : | 
|---|
|  | 130 | MatrixContent(_view.matrix.size1, _view.matrix.size2, MatrixBaseCase()), | 
|---|
|  | 131 | view(_view) | 
|---|
|  | 132 | { | 
|---|
|  | 133 | content = &view.matrix; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | ~MatrixViewContent(){ | 
|---|
|  | 136 | content=0; | 
|---|
|  | 137 | } | 
|---|
|  | 138 | gsl_matrix_view view; | 
|---|
|  | 139 | }; | 
|---|
|  | 140 |  | 
|---|
| [fee079] | 141 | #endif /* MATRIXCONTENT_HPP_ */ | 
|---|