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