source: LinearAlgebra/src/LinearAlgebra/MatrixContent.hpp@ c5038e

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since c5038e was c5038e, checked in by Frederik Heber <heber@…>, 13 years ago

Made MatrixContent and VectorContent dimensions const by placing into extra class.

  • new classes MatrixDimension and VectorDimension.
  • they only displays getters to the outside. Thus, we may savely serialize them without having to disable the constness.
  • Property mode set to 100644
File size: 6.5 KB
Line 
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
48class VectorContent;
49namespace boost {
50 namespace serialization {
51 class access;
52 }
53}
54
55/** Dummy structure to create a unique signature.
56 *
57 */
58struct 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 */
66struct MatrixDimension {
67public:
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
73private:
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
86private:
87 size_t rows; // store for internal purposes
88 size_t columns; // store for internal purposes
89};
90
91class 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;
106public:
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
167protected:
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
173private:
174 bool free_content_on_exit;
175};
176
177std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat);
178const MatrixContent operator*(const double factor,const MatrixContent& mat);
179const MatrixContent operator*(const MatrixContent &mat,const double factor);
180MatrixContent const operator+(const MatrixContent& a, const MatrixContent& b);
181MatrixContent 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 */
192struct 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_ */
Note: See TracBrowser for help on using the repository browser.