source: src/LinearAlgebra/Matrix.cpp@ bf3817

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

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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