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