source: src/LinearAlgebra/Matrix.cpp@ 31fb1d

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

(un)select-atoms-inside-cuboid: cuboid may be rotated.

  • Matrix::rotate() sets contents to arbitrary rotation matrix.
  • BUGFIX: Matrix::transpose() was working only for symmetric matrices.
  • TEST: unit test cases for rotation() and transpose().
  • new options "angle-x", "angle-y", "angle-z"
  • (un)select-atoms-inside-cuboid: have these as optional arguments.
  • Property mode set to 100644
File size: 8.5 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
117void Matrix::rotation(const double x, const double y, const double z)
118{
119 set(0,0, cos(y)*cos(z));
120 set(0,1, cos(z)*sin(x)*sin(y) - cos(x)*sin(z));
121 set(0,2, cos(x)*cos(z)*sin(y) + sin(x) * sin(z));
122 set(1,0, cos(y)*sin(z));
123 set(1,1, cos(x)*cos(z) + sin(x)*sin(y)*sin(z));
124 set(1,2, -cos(z)*sin(x) + cos(x)*sin(y)*sin(z));
125 set(2,0, -sin(y));
126 set(2,1, cos(y)*sin(x));
127 set(2,2, cos(x)*cos(y));
128}
129
130Matrix &Matrix::operator=(const Matrix &src){
131 if(&src!=this){
132 gsl_matrix_memcpy(content->content,src.content->content);
133 }
134 return *this;
135}
136
137const Matrix &Matrix::operator+=(const Matrix &rhs){
138 gsl_matrix_add(content->content, rhs.content->content);
139 return *this;
140}
141
142const Matrix &Matrix::operator-=(const Matrix &rhs){
143 gsl_matrix_sub(content->content, rhs.content->content);
144 return *this;
145}
146
147const Matrix &Matrix::operator*=(const Matrix &rhs){
148 (*this) = (*this)*rhs;
149 return *this;
150}
151
152const Matrix Matrix::operator+(const Matrix &rhs) const{
153 Matrix tmp = *this;
154 tmp+=rhs;
155 return tmp;
156}
157
158const Matrix Matrix::operator-(const Matrix &rhs) const{
159 Matrix tmp = *this;
160 tmp-=rhs;
161 return tmp;
162}
163
164const Matrix Matrix::operator*(const Matrix &rhs) const{
165 gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM);
166 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content->content, rhs.content->content, 0.0, res);
167 MatrixContent *content= new MatrixContent();
168 content->content = res;
169 return Matrix(content);
170}
171
172double &Matrix::at(size_t i, size_t j){
173 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
174 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
175 return *gsl_matrix_ptr (content->content, i, j);
176}
177
178const double Matrix::at(size_t i, size_t j) const{
179 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
180 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
181 return gsl_matrix_get(content->content, i, j);
182}
183
184Vector &Matrix::row(size_t i){
185 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
186 return *rows_ptr[i];
187}
188
189const Vector &Matrix::row(size_t i) const{
190 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
191 return *rows_ptr[i];
192}
193
194Vector &Matrix::column(size_t i){
195 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
196 return *columns_ptr[i];
197}
198
199const Vector &Matrix::column(size_t i) const{
200 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
201 return *columns_ptr[i];
202}
203
204Vector &Matrix::diagonal(){
205 return *diagonal_ptr;
206}
207
208const Vector &Matrix::diagonal() const{
209 return *diagonal_ptr;
210}
211
212void Matrix::set(size_t i, size_t j, const double value){
213 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
214 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
215 gsl_matrix_set(content->content,i,j,value);
216}
217
218double Matrix::determinant() const{
219 return at(0,0)*at(1,1)*at(2,2)
220 + at(0,1)*at(1,2)*at(2,0)
221 + at(0,2)*at(1,0)*at(2,1)
222 - at(2,0)*at(1,1)*at(0,2)
223 - at(2,1)*at(1,2)*at(0,0)
224 - at(2,2)*at(1,0)*at(0,1);
225}
226
227Matrix Matrix::transpose() const
228{
229 Matrix copy(*this);
230 copy.transpose();
231 return copy;
232}
233
234
235Matrix &Matrix::transpose()
236{
237 double tmp;
238 for (int i=0;i<NDIM;i++)
239 for (int j=i+1;j<NDIM;j++) {
240 tmp = at(j,i);
241 at(j,i) = at(i,j);
242 at(i,j) = tmp;
243 }
244 return *this;
245}
246
247
248Matrix Matrix::invert() const{
249 double det = determinant();
250 if(fabs(det)<MYEPSILON){
251 throw NotInvertibleException(__FILE__,__LINE__);
252 }
253
254 double detReci = 1./det;
255 Matrix res;
256 res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
257 res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
258 res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
259 res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
260 res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
261 res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
262 res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
263 res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
264 res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
265 return res;
266}
267
268Vector Matrix::transformToEigenbasis()
269{
270 gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
271 gsl_vector *eval = gsl_vector_alloc(NDIM);
272 gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
273 gsl_eigen_symmv(content->content, eval, evec, T);
274 gsl_eigen_symmv_free(T);
275 gsl_matrix_memcpy(content->content, evec);
276 Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
277 return evalues;
278}
279
280const Matrix &Matrix::operator*=(const double factor){
281 gsl_matrix_scale(content->content, factor);
282 return *this;
283}
284
285const Matrix operator*(const double factor,const Matrix& mat){
286 Matrix tmp = mat;
287 tmp*=factor;
288 return tmp;
289}
290
291const Matrix operator*(const Matrix &mat,const double factor){
292 return factor*mat;
293}
294
295bool Matrix::operator==(const Matrix &rhs) const{
296 for(int i=NDIM;i--;){
297 for(int j=NDIM;j--;){
298 if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){
299 return false;
300 }
301 }
302 }
303 return true;
304}
305
306/** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
307 * \param *symm 6-dim array of unique symmetric matrix components
308 * \return allocated NDIM*NDIM array with the symmetric matrix
309 */
310Matrix ReturnFullMatrixforSymmetric(const double * const symm)
311{
312 Matrix matrix;
313 matrix.set(0,0, symm[0]);
314 matrix.set(1,0, symm[1]);
315 matrix.set(2,0, symm[3]);
316 matrix.set(0,1, symm[1]);
317 matrix.set(1,1, symm[2]);
318 matrix.set(2,1, symm[4]);
319 matrix.set(0,2, symm[3]);
320 matrix.set(1,2, symm[4]);
321 matrix.set(2,2, symm[5]);
322 return matrix;
323};
324
325ostream &operator<<(ostream &ost,const Matrix &mat){
326 for(int i = 0;i<NDIM;++i){
327 ost << "\n";
328 for(int j = 0; j<NDIM;++j){
329 ost << mat.at(i,j);
330 if(j!=NDIM-1)
331 ost << "; ";
332 }
333 }
334 return ost;
335}
336
337Vector operator*(const Matrix &mat,const Vector &vec){
338 gsl_vector *res = gsl_vector_calloc(NDIM);
339 gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res);
340 VectorContent *content = new VectorContent();
341 content->content = res;
342 return Vector(content);
343}
344
345Vector &operator*=(Vector& lhs,const Matrix &mat){
346 lhs = mat*lhs;
347 return lhs;
348}
349
Note: See TracBrowser for help on using the repository browser.