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 | // include config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include "CodePatterns/Assert.hpp"
|
---|
17 |
|
---|
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 | *
|
---|
40 | */
|
---|
41 |
|
---|
42 | #include <gsl/gsl_matrix.h>
|
---|
43 |
|
---|
44 | #include <iosfwd>
|
---|
45 |
|
---|
46 | #include "MatrixVector_ops.hpp"
|
---|
47 |
|
---|
48 | class VectorContent;
|
---|
49 | namespace boost {
|
---|
50 | namespace serialization {
|
---|
51 | class access;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Dummy structure to create a unique signature.
|
---|
56 | *
|
---|
57 | */
|
---|
58 | struct MatrixBaseCase{};
|
---|
59 |
|
---|
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
|
---|
92 | {
|
---|
93 | friend std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat);
|
---|
94 | friend class RealSpaceMatrix;
|
---|
95 | friend class LinearSystemOfEquations;
|
---|
96 |
|
---|
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);
|
---|
100 | friend MatrixContent const operator+(const MatrixContent& a, const MatrixContent& b);
|
---|
101 | friend MatrixContent const operator-(const MatrixContent& a, const MatrixContent& b);
|
---|
102 |
|
---|
103 | // unit tests
|
---|
104 | friend class MatrixContentTest;
|
---|
105 | friend class MatrixContentSymmetricTest;
|
---|
106 | public:
|
---|
107 | MatrixContent(size_t rows, size_t columns);
|
---|
108 | MatrixContent(size_t _rows, size_t _columns, MatrixBaseCase);
|
---|
109 | MatrixContent(size_t rows, size_t columns, const double *src);
|
---|
110 | MatrixContent(size_t rows, size_t columns, std::istream &inputstream);
|
---|
111 | MatrixContent(gsl_matrix *&src);
|
---|
112 | MatrixContent(const MatrixContent &src);
|
---|
113 | MatrixContent(const MatrixContent *src);
|
---|
114 | virtual ~MatrixContent();
|
---|
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);
|
---|
120 |
|
---|
121 | // Initializing
|
---|
122 | void setFromDoubleArray(double * x);
|
---|
123 | void setIdentity();
|
---|
124 | void setValue(double _value);
|
---|
125 | void setZero();
|
---|
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);
|
---|
131 |
|
---|
132 | // Transformations
|
---|
133 | MatrixContent transposed() const;
|
---|
134 | MatrixContent &transpose();
|
---|
135 | gsl_vector* transformToEigenbasis();
|
---|
136 | void sortEigenbasis(gsl_vector *eigenvalues);
|
---|
137 | void performSingularValueDecomposition(MatrixContent &V, VectorContent &S);
|
---|
138 | VectorContent solveOverdeterminedLinearEquation(const VectorContent &b);
|
---|
139 | VectorContent solveOverdeterminedLinearEquation(MatrixContent &V, VectorContent &S, const VectorContent &b);
|
---|
140 |
|
---|
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 |
|
---|
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;
|
---|
155 | const MatrixContent &operator&=(const MatrixContent &rhs);
|
---|
156 | const MatrixContent operator&(const MatrixContent &rhs) const;
|
---|
157 | const MatrixContent &operator*=(const double factor);
|
---|
158 | bool operator==(const MatrixContent &rhs) const;
|
---|
159 |
|
---|
160 | VectorContent *getColumnVector(size_t column) const;
|
---|
161 | VectorContent *getRowVector(size_t row) const;
|
---|
162 | VectorContent *getDiagonalVector() const;
|
---|
163 |
|
---|
164 | static std::pair<size_t, size_t> preparseMatrixDimensions(std::istream &inputstream);
|
---|
165 | void write(std::ostream &ost) const;
|
---|
166 |
|
---|
167 | protected:
|
---|
168 | double *Pointer(size_t m, size_t n);
|
---|
169 | const double *const_Pointer(size_t m, size_t n) const;
|
---|
170 |
|
---|
171 | gsl_matrix * content;
|
---|
172 |
|
---|
173 | private:
|
---|
174 | bool free_content_on_exit;
|
---|
175 | };
|
---|
176 |
|
---|
177 | std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat);
|
---|
178 | const MatrixContent operator*(const double factor,const MatrixContent& mat);
|
---|
179 | const MatrixContent operator*(const MatrixContent &mat,const double factor);
|
---|
180 | MatrixContent const operator+(const MatrixContent& a, const MatrixContent& b);
|
---|
181 | MatrixContent const operator-(const MatrixContent& a, const MatrixContent& b);
|
---|
182 |
|
---|
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 |
|
---|
206 | #endif /* MATRIXCONTENT_HPP_ */
|
---|