source: src/LinearAlgebra/Matrix.cpp@ db7cb0

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

Merge branch 'StructureRefactoring' into Shapes

Conflicts:

src/Box.cpp
src/Box.hpp
src/Descriptors/AtomShapeDescriptor.cpp
src/Descriptors/AtomShapeDescriptor.hpp
src/Descriptors/AtomShapeDescriptor_impl.hpp
src/LinearAlgebra/Line.cpp
src/LinearAlgebra/Line.hpp
src/LinearAlgebra/Matrix.cpp
src/LinearAlgebra/Matrix.hpp
src/Makefile.am
src/Shapes/BaseShapes.cpp
src/Shapes/BaseShapes_impl.hpp
src/Shapes/Shape.cpp
src/Shapes/Shape.hpp
src/Shapes/ShapeOps_impl.hpp
src/Shapes/Shape_impl.hpp
src/unittests/ShapeUnittest.cpp

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