source: src/LinearAlgebra/Matrix.cpp@ bbbad5

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 bbbad5 was bbbad5, checked in by Frederik Heber <heber@…>, 15 years ago

Added MemDebug.hpp to each and every .cpp file (were it was still missing).

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