source: src/Matrix.cpp@ c550dd

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 c550dd was c49c96, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added an output function for matrices

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