source: src/Matrix.cpp@ 36166d

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 36166d was 4b94bb, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Moved Vector-Matrix operations to matrix class

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Matrix.cpp
3 *
4 * Created on: Jun 25, 2010
5 * Author: crueger
6 */
7
8#include "Matrix.hpp"
9#include "Helpers/Assert.hpp"
10#include "Exceptions/NotInvertibleException.hpp"
11#include "Helpers/fast_functions.hpp"
12#include "Helpers/Assert.hpp"
13#include "vector.hpp"
14
15#include <gsl/gsl_blas.h>
16#include <cmath>
17#include <iostream>
18
19using namespace std;
20
21Matrix::Matrix()
22{
23 content = gsl_matrix_calloc(NDIM, NDIM);
24}
25
26Matrix::Matrix(const double* src){
27 content = gsl_matrix_alloc(NDIM, NDIM);
28 set(0,0, src[0]);
29 set(1,0, src[1]);
30 set(2,0, src[2]);
31
32 set(0,1, src[3]);
33 set(1,1, src[4]);
34 set(2,1, src[5]);
35
36 set(0,2, src[6]);
37 set(1,2, src[7]);
38 set(2,2, src[8]);
39}
40
41Matrix::Matrix(const Matrix &src){
42 content = gsl_matrix_alloc(NDIM, NDIM);
43 gsl_matrix_memcpy(content,src.content);
44}
45
46Matrix::Matrix(gsl_matrix* _content) :
47 content(_content)
48{}
49
50Matrix::~Matrix()
51{
52 gsl_matrix_free(content);
53}
54
55void Matrix::one(){
56 gsl_matrix_free(content);
57 content = gsl_matrix_calloc(NDIM, NDIM);
58 for(int i = NDIM;i--;){
59 set(i,i,1.);
60 }
61}
62
63Matrix &Matrix::operator=(const Matrix &src){
64 if(&src!=this){
65 gsl_matrix_memcpy(content,src.content);
66 }
67 return *this;
68}
69
70Matrix &Matrix::operator+=(const Matrix &rhs){
71 gsl_matrix_add(content, rhs.content);
72 return *this;
73}
74
75Matrix &Matrix::operator-=(const Matrix &rhs){
76 gsl_matrix_sub(content, rhs.content);
77 return *this;
78}
79
80Matrix &Matrix::operator*=(const Matrix &rhs){
81 (*this) = (*this)*rhs;
82 return *this;
83}
84
85Matrix Matrix::operator+(const Matrix &rhs) const{
86 Matrix tmp = *this;
87 tmp+=rhs;
88 return tmp;
89}
90
91Matrix Matrix::operator-(const Matrix &rhs) const{
92 Matrix tmp = *this;
93 tmp-=rhs;
94 return tmp;
95}
96
97Matrix Matrix::operator*(const Matrix &rhs) const{
98 gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM);
99 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, this->content, rhs.content, 0.0, res);
100 return Matrix(res);
101}
102
103double &Matrix::at(size_t i, size_t j){
104 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
105 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
106 return *gsl_matrix_ptr (content, i, j);
107}
108
109const double Matrix::at(size_t i, size_t j) const{
110 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
111 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
112 return gsl_matrix_get(content, i, j);
113}
114
115void Matrix::set(size_t i, size_t j, const double value){
116 ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
117 ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
118 gsl_matrix_set(content,i,j,value);
119}
120
121double Matrix::determinant() const{
122 return at(0,0)*at(1,1)*at(2,2)
123 + at(0,1)*at(1,2)*at(2,0)
124 + at(0,2)*at(1,0)*at(2,1)
125 - at(2,0)*at(1,1)*at(0,2)
126 - at(2,1)*at(1,2)*at(0,0)
127 - at(2,2)*at(1,0)*at(0,1);
128}
129
130Matrix Matrix::invert() const{
131 double det = determinant();
132 if(fabs(det)<MYEPSILON){
133 throw NotInvertibleException(__FILE__,__LINE__);
134 }
135
136 double detReci = 1./det;
137 Matrix res;
138 res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
139 res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
140 res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
141 res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
142 res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
143 res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
144 res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
145 res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
146 res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
147 return res;
148}
149
150Matrix &Matrix::operator*=(const double factor){
151 gsl_matrix_scale(content, factor);
152 return *this;
153}
154
155Matrix operator*(const double factor,const Matrix& mat){
156 Matrix tmp = mat;
157 tmp*=factor;
158 return tmp;
159}
160
161Matrix operator*(const Matrix &mat,const double factor){
162 return factor*mat;
163}
164
165/** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
166 * \param *symm 6-dim array of unique symmetric matrix components
167 * \return allocated NDIM*NDIM array with the symmetric matrix
168 */
169Matrix ReturnFullMatrixforSymmetric(const double * const symm)
170{
171 Matrix matrix;
172 matrix.set(0,0, symm[0]);
173 matrix.set(1,0, symm[1]);
174 matrix.set(2,0, symm[3]);
175 matrix.set(0,1, symm[1]);
176 matrix.set(1,1, symm[2]);
177 matrix.set(2,1, symm[4]);
178 matrix.set(0,2, symm[3]);
179 matrix.set(1,2, symm[4]);
180 matrix.set(2,2, symm[5]);
181 return matrix;
182};
183
184ostream &operator<<(ostream &ost,const Matrix &mat){
185 for(int i = 0;i<NDIM;++i){
186 ost << "\n";
187 for(int j = 0; j<NDIM;++j){
188 ost << mat.at(i,j);
189 if(j!=NDIM-1)
190 ost << "; ";
191 }
192 }
193 return ost;
194}
195
196Vector operator*(const Matrix &mat,const Vector &vec){
197 gsl_vector *res = gsl_vector_calloc(NDIM);
198 gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content, vec.content, 0.0, res);
199 return Vector(res);
200}
201
202Vector &operator*=(Vector& lhs,const Matrix &mat){
203 lhs = mat*lhs;
204 return lhs;
205}
206
Note: See TracBrowser for help on using the repository browser.