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