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