source: src/Matrix.cpp@ 3839e5

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 3839e5 was 0eb2dc, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Added comparison Operator to matrix class

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Matrix.cpp
3 *
4 * Created on: Jun 25, 2010
5 * Author: crueger
6 */
7
8#include "Matrix.hpp"
9#include "Helpers/Assert.hpp"
10#include "Exceptions/NotInvertibleException.hpp"
11#include "Helpers/fast_functions.hpp"
12#include "Helpers/Assert.hpp"
13#include "vector.hpp"
14#include "VectorContent.hpp"
15#include "MatrixContent.hpp"
16
17#include <gsl/gsl_blas.h>
18#include <cmath>
19#include <iostream>
20
21using namespace std;
22
23Matrix::Matrix()
24{
25 content = new MatrixContent();
26 content->content = gsl_matrix_calloc(NDIM, NDIM);
27 createViews();
28}
29
30Matrix::Matrix(const double* src){
31 content = new MatrixContent();
32 content->content = gsl_matrix_alloc(NDIM, NDIM);
33 set(0,0, src[0]);
34 set(1,0, src[1]);
35 set(2,0, src[2]);
36
37 set(0,1, src[3]);
38 set(1,1, src[4]);
39 set(2,1, src[5]);
40
41 set(0,2, src[6]);
42 set(1,2, src[7]);
43 set(2,2, src[8]);
44 createViews();
45}
46
47Matrix::Matrix(const Matrix &src){
48 content = new MatrixContent();
49 content->content = gsl_matrix_alloc(NDIM, NDIM);
50 gsl_matrix_memcpy(content->content,src.content->content);
51 createViews();
52}
53
54Matrix::Matrix(MatrixContent* _content) :
55 content(_content)
56{
57 createViews();
58}
59
60Matrix::~Matrix()
61{
62 // delete all views
63 for(int i=NDIM;i--;){
64 delete rows_ptr[i];
65 }
66 for(int i=NDIM;i--;){
67 delete columns_ptr[i];
68 }
69 delete diagonal_ptr;
70 gsl_matrix_free(content->content);
71 delete content;
72}
73
74void Matrix::createViews(){
75 // create row views
76 for(int i=NDIM;i--;){
77 VectorContent *rowContent = new VectorViewContent(gsl_matrix_row(content->content,i));
78 rows_ptr[i] = new Vector(rowContent);
79 }
80 // create column views
81 for(int i=NDIM;i--;){
82 VectorContent *columnContent = new VectorViewContent(gsl_matrix_column(content->content,i));
83 columns_ptr[i] = new Vector(columnContent);
84 }
85 // create diagonal view
86 VectorContent *diagonalContent = new VectorViewContent(gsl_matrix_diagonal(content->content));
87 diagonal_ptr = new Vector(diagonalContent);
88}
89
90void Matrix::one(){
91 for(int i=NDIM;i--;){
92 for(int j=NDIM;j--;){
93 set(i,j,i==j);
94 }
95 }
96}
97
98Matrix &Matrix::operator=(const Matrix &src){
99 if(&src!=this){
100 gsl_matrix_memcpy(content->content,src.content->content);
101 }
102 return *this;
103}
104
105Matrix &Matrix::operator+=(const Matrix &rhs){
106 gsl_matrix_add(content->content, rhs.content->content);
107 return *this;
108}
109
110Matrix &Matrix::operator-=(const Matrix &rhs){
111 gsl_matrix_sub(content->content, rhs.content->content);
112 return *this;
113}
114
115Matrix &Matrix::operator*=(const Matrix &rhs){
116 (*this) = (*this)*rhs;
117 return *this;
118}
119
120Matrix Matrix::operator+(const Matrix &rhs) const{
121 Matrix tmp = *this;
122 tmp+=rhs;
123 return tmp;
124}
125
126Matrix Matrix::operator-(const Matrix &rhs) const{
127 Matrix tmp = *this;
128 tmp-=rhs;
129 return tmp;
130}
131
132Matrix Matrix::operator*(const Matrix &rhs) const{
133 gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM);
134 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content->content, rhs.content->content, 0.0, res);
135 MatrixContent *content= new MatrixContent();
136 content->content = res;
137 return Matrix(content);
138}
139
140double &Matrix::at(size_t i, size_t j){
141 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
142 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
143 return *gsl_matrix_ptr (content->content, i, j);
144}
145
146const double Matrix::at(size_t i, size_t j) const{
147 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
148 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
149 return gsl_matrix_get(content->content, i, j);
150}
151
152Vector &Matrix::row(size_t i){
153 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
154 return *rows_ptr[i];
155}
156
157const Vector &Matrix::row(size_t i) const{
158 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
159 return *rows_ptr[i];
160}
161
162Vector &Matrix::column(size_t i){
163 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
164 return *columns_ptr[i];
165}
166
167const Vector &Matrix::column(size_t i) const{
168 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
169 return *columns_ptr[i];
170}
171
172Vector &Matrix::diagonal(){
173 return *diagonal_ptr;
174}
175
176const Vector &Matrix::diagonal() const{
177 return *diagonal_ptr;
178}
179
180void Matrix::set(size_t i, size_t j, const double value){
181 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
182 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
183 gsl_matrix_set(content->content,i,j,value);
184}
185
186double Matrix::determinant() const{
187 return at(0,0)*at(1,1)*at(2,2)
188 + at(0,1)*at(1,2)*at(2,0)
189 + at(0,2)*at(1,0)*at(2,1)
190 - at(2,0)*at(1,1)*at(0,2)
191 - at(2,1)*at(1,2)*at(0,0)
192 - at(2,2)*at(1,0)*at(0,1);
193}
194
195Matrix Matrix::invert() const{
196 double det = determinant();
197 if(fabs(det)<MYEPSILON){
198 throw NotInvertibleException(__FILE__,__LINE__);
199 }
200
201 double detReci = 1./det;
202 Matrix res;
203 res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
204 res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
205 res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
206 res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
207 res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
208 res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
209 res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
210 res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
211 res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
212 return res;
213}
214
215Matrix &Matrix::operator*=(const double factor){
216 gsl_matrix_scale(content->content, factor);
217 return *this;
218}
219
220Matrix operator*(const double factor,const Matrix& mat){
221 Matrix tmp = mat;
222 tmp*=factor;
223 return tmp;
224}
225
226Matrix operator*(const Matrix &mat,const double factor){
227 return factor*mat;
228}
229
230bool Matrix::operator==(const Matrix &rhs) const{
231 for(int i=NDIM;i--;){
232 for(int j=NDIM;j--;){
233 if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){
234 return false;
235 }
236 }
237 }
238 return true;
239}
240
241/** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
242 * \param *symm 6-dim array of unique symmetric matrix components
243 * \return allocated NDIM*NDIM array with the symmetric matrix
244 */
245Matrix ReturnFullMatrixforSymmetric(const double * const symm)
246{
247 Matrix matrix;
248 matrix.set(0,0, symm[0]);
249 matrix.set(1,0, symm[1]);
250 matrix.set(2,0, symm[3]);
251 matrix.set(0,1, symm[1]);
252 matrix.set(1,1, symm[2]);
253 matrix.set(2,1, symm[4]);
254 matrix.set(0,2, symm[3]);
255 matrix.set(1,2, symm[4]);
256 matrix.set(2,2, symm[5]);
257 return matrix;
258};
259
260ostream &operator<<(ostream &ost,const Matrix &mat){
261 for(int i = 0;i<NDIM;++i){
262 ost << "\n";
263 for(int j = 0; j<NDIM;++j){
264 ost << mat.at(i,j);
265 if(j!=NDIM-1)
266 ost << "; ";
267 }
268 }
269 return ost;
270}
271
272Vector operator*(const Matrix &mat,const Vector &vec){
273 gsl_vector *res = gsl_vector_calloc(NDIM);
274 gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res);
275 VectorContent *content = new VectorContent();
276 content->content = res;
277 return Vector(content);
278}
279
280Vector &operator*=(Vector& lhs,const Matrix &mat){
281 lhs = mat*lhs;
282 return lhs;
283}
284
Note: See TracBrowser for help on using the repository browser.