source: src/LinearAlgebra/Matrix.cpp@ 3bc926

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 Candidate_v1.7.0 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 3bc926 was 3bc926, checked in by Frederik Heber <heber@…>, 15 years ago

Extended MatrixContent class.

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