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