[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 |
|
---|
[bbbad5] | 20 | #include "Helpers/MemDebug.hpp"
|
---|
| 21 |
|
---|
[cca9ef] | 22 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[325390] | 23 | #include "Helpers/Assert.hpp"
|
---|
| 24 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
| 25 | #include "Helpers/fast_functions.hpp"
|
---|
[e3ffd3] | 26 | #include "Helpers/Assert.hpp"
|
---|
[e4fe8d] | 27 | #include "Helpers/defs.hpp"
|
---|
[57f243] | 28 | #include "LinearAlgebra/Vector.hpp"
|
---|
[3bc926] | 29 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
| 30 | #include "LinearAlgebra/MatrixContent.hpp"
|
---|
[325390] | 31 |
|
---|
| 32 | #include <gsl/gsl_blas.h>
|
---|
[a439e5] | 33 | #include <gsl/gsl_eigen.h>
|
---|
| 34 | #include <gsl/gsl_matrix.h>
|
---|
| 35 | #include <gsl/gsl_multimin.h>
|
---|
| 36 | #include <gsl/gsl_vector.h>
|
---|
[325390] | 37 | #include <cmath>
|
---|
[c49c96] | 38 | #include <iostream>
|
---|
| 39 |
|
---|
| 40 | using namespace std;
|
---|
[325390] | 41 |
|
---|
[cca9ef] | 42 | RealSpaceMatrix::RealSpaceMatrix()
|
---|
[325390] | 43 | {
|
---|
[3bc926] | 44 | content = new MatrixContent(NDIM, NDIM);
|
---|
[3dbb9d] | 45 | createViews();
|
---|
[325390] | 46 | }
|
---|
| 47 |
|
---|
[cca9ef] | 48 | RealSpaceMatrix::RealSpaceMatrix(const double* src)
|
---|
[3bc926] | 49 | {
|
---|
| 50 | content = new MatrixContent(NDIM, NDIM, src);
|
---|
| 51 | createViews();
|
---|
| 52 | }
|
---|
[325390] | 53 |
|
---|
[cca9ef] | 54 | RealSpaceMatrix::RealSpaceMatrix(const RealSpaceMatrix &src)
|
---|
[3bc926] | 55 | {
|
---|
| 56 | content = new MatrixContent(src.content);
|
---|
[3dbb9d] | 57 | createViews();
|
---|
[325390] | 58 | }
|
---|
| 59 |
|
---|
[cca9ef] | 60 | RealSpaceMatrix::RealSpaceMatrix(const MatrixContent &src)
|
---|
[3bc926] | 61 | {
|
---|
| 62 | content = new MatrixContent(src);
|
---|
[3dbb9d] | 63 | createViews();
|
---|
[325390] | 64 | }
|
---|
| 65 |
|
---|
[cca9ef] | 66 | RealSpaceMatrix::RealSpaceMatrix(MatrixContent* _content)
|
---|
[3dbb9d] | 67 | {
|
---|
[3bc926] | 68 | content = new MatrixContent(_content);
|
---|
[3dbb9d] | 69 | createViews();
|
---|
| 70 | }
|
---|
[325390] | 71 |
|
---|
[cca9ef] | 72 | RealSpaceMatrix::~RealSpaceMatrix()
|
---|
[325390] | 73 | {
|
---|
[3dbb9d] | 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;
|
---|
[fee079] | 82 | delete content;
|
---|
[325390] | 83 | }
|
---|
| 84 |
|
---|
[cca9ef] | 85 | void RealSpaceMatrix::createViews(){
|
---|
[3dbb9d] | 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);
|
---|
[bbf1bd] | 90 | ASSERT(rowContent == NULL, "RealSpaceMatrix::createViews() - rowContent was not taken over as supposed to happen!");
|
---|
[3dbb9d] | 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);
|
---|
[bbf1bd] | 96 | ASSERT(columnContent == NULL, "RealSpaceMatrix::createViews() - columnContent was not taken over as supposed to happen!");
|
---|
[3dbb9d] | 97 | }
|
---|
| 98 | // create diagonal view
|
---|
| 99 | VectorContent *diagonalContent = new VectorViewContent(gsl_matrix_diagonal(content->content));
|
---|
| 100 | diagonal_ptr = new Vector(diagonalContent);
|
---|
[bbf1bd] | 101 | ASSERT(diagonalContent == NULL, "RealSpaceMatrix::createViews() - diagonalContent was not taken over as supposed to happen!");
|
---|
[3dbb9d] | 102 | }
|
---|
| 103 |
|
---|
[cca9ef] | 104 | void RealSpaceMatrix::setIdentity(){
|
---|
[3bc926] | 105 | content->setIdentity();
|
---|
[1da5f5] | 106 | }
|
---|
| 107 |
|
---|
[cca9ef] | 108 | void RealSpaceMatrix::setZero(){
|
---|
[3bc926] | 109 | content->setZero();
|
---|
[a439e5] | 110 | }
|
---|
| 111 |
|
---|
[cca9ef] | 112 | void RealSpaceMatrix::setRotation(const double x, const double y, const double z)
|
---|
[31fb1d] | 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 |
|
---|
[cca9ef] | 125 | RealSpaceMatrix &RealSpaceMatrix::operator=(const RealSpaceMatrix &src)
|
---|
[3bc926] | 126 | {
|
---|
| 127 | // MatrixContent checks for self-assignment
|
---|
| 128 | *content = *(src.content);
|
---|
[325390] | 129 | return *this;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[cca9ef] | 132 | const RealSpaceMatrix &RealSpaceMatrix::operator+=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 133 | {
|
---|
| 134 | *content += *(rhs.content);
|
---|
[325390] | 135 | return *this;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[cca9ef] | 138 | const RealSpaceMatrix &RealSpaceMatrix::operator-=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 139 | {
|
---|
| 140 | *content -= *(rhs.content);
|
---|
[325390] | 141 | return *this;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[cca9ef] | 144 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 145 | {
|
---|
| 146 | (*content) *= (*rhs.content);
|
---|
[325390] | 147 | return *this;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[cca9ef] | 150 | const RealSpaceMatrix RealSpaceMatrix::operator+(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 151 | {
|
---|
[cca9ef] | 152 | RealSpaceMatrix tmp = *this;
|
---|
[325390] | 153 | tmp+=rhs;
|
---|
| 154 | return tmp;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[cca9ef] | 157 | const RealSpaceMatrix RealSpaceMatrix::operator-(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 158 | {
|
---|
[cca9ef] | 159 | RealSpaceMatrix tmp = *this;
|
---|
[325390] | 160 | tmp-=rhs;
|
---|
| 161 | return tmp;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[cca9ef] | 164 | const RealSpaceMatrix RealSpaceMatrix::operator*(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 165 | {
|
---|
[cca9ef] | 166 | RealSpaceMatrix tmp(content);
|
---|
[3bc926] | 167 | tmp *= rhs;
|
---|
| 168 | return tmp;
|
---|
[325390] | 169 | }
|
---|
| 170 |
|
---|
[cca9ef] | 171 | double &RealSpaceMatrix::at(size_t i, size_t j)
|
---|
[3bc926] | 172 | {
|
---|
| 173 | return content->at(i,j);
|
---|
[325390] | 174 | }
|
---|
| 175 |
|
---|
[cca9ef] | 176 | const double RealSpaceMatrix::at(size_t i, size_t j) const
|
---|
[3bc926] | 177 | {
|
---|
| 178 | return content->at(i,j);
|
---|
[436f04] | 179 | }
|
---|
| 180 |
|
---|
[cca9ef] | 181 | Vector &RealSpaceMatrix::row(size_t i)
|
---|
[3bc926] | 182 | {
|
---|
[3dbb9d] | 183 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 184 | return *rows_ptr[i];
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[cca9ef] | 187 | const Vector &RealSpaceMatrix::row(size_t i) const
|
---|
[3bc926] | 188 | {
|
---|
[3dbb9d] | 189 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 190 | return *rows_ptr[i];
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[cca9ef] | 193 | Vector &RealSpaceMatrix::column(size_t i)
|
---|
[3bc926] | 194 | {
|
---|
[3dbb9d] | 195 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 196 | return *columns_ptr[i];
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[cca9ef] | 199 | const Vector &RealSpaceMatrix::column(size_t i) const
|
---|
[3bc926] | 200 | {
|
---|
[3dbb9d] | 201 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 202 | return *columns_ptr[i];
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[cca9ef] | 205 | Vector &RealSpaceMatrix::diagonal()
|
---|
[3bc926] | 206 | {
|
---|
[3dbb9d] | 207 | return *diagonal_ptr;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[cca9ef] | 210 | const Vector &RealSpaceMatrix::diagonal() const
|
---|
[3bc926] | 211 | {
|
---|
[3dbb9d] | 212 | return *diagonal_ptr;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[cca9ef] | 215 | void RealSpaceMatrix::set(size_t i, size_t j, const double value)
|
---|
[3bc926] | 216 | {
|
---|
| 217 | content->set(i,j, value);
|
---|
[cadbc1] | 218 | }
|
---|
| 219 |
|
---|
[cca9ef] | 220 | double RealSpaceMatrix::determinant() const{
|
---|
[325390] | 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 |
|
---|
[a439e5] | 229 |
|
---|
[cca9ef] | 230 | RealSpaceMatrix RealSpaceMatrix::invert() const{
|
---|
[325390] | 231 | double det = determinant();
|
---|
| 232 | if(fabs(det)<MYEPSILON){
|
---|
| 233 | throw NotInvertibleException(__FILE__,__LINE__);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | double detReci = 1./det;
|
---|
[cca9ef] | 237 | RealSpaceMatrix res;
|
---|
[436f04] | 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
|
---|
[325390] | 247 | return res;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[cca9ef] | 250 | RealSpaceMatrix RealSpaceMatrix::transpose() const
|
---|
[3bc926] | 251 | {
|
---|
[cca9ef] | 252 | RealSpaceMatrix res = RealSpaceMatrix(const_cast<const MatrixContent *>(content)->transpose());
|
---|
[41ea3c] | 253 | return res;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[cca9ef] | 256 | RealSpaceMatrix &RealSpaceMatrix::transpose()
|
---|
[6c438f] | 257 | {
|
---|
[3bc926] | 258 | content->transpose();
|
---|
[6c438f] | 259 | return *this;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[cca9ef] | 262 | Vector RealSpaceMatrix::transformToEigenbasis()
|
---|
[a439e5] | 263 | {
|
---|
[3bc926] | 264 | gsl_vector *eval = content->transformToEigenbasis();
|
---|
[a439e5] | 265 | Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
|
---|
[80cecb5] | 266 | gsl_vector_free(eval);
|
---|
[a439e5] | 267 | return evalues;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[cca9ef] | 270 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const double factor)
|
---|
[3bc926] | 271 | {
|
---|
| 272 | *content *= factor;
|
---|
[325390] | 273 | return *this;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[cca9ef] | 276 | const RealSpaceMatrix operator*(const double factor,const RealSpaceMatrix& mat)
|
---|
[3bc926] | 277 | {
|
---|
[cca9ef] | 278 | RealSpaceMatrix tmp = mat;
|
---|
[3bc926] | 279 | return tmp *= factor;
|
---|
[325390] | 280 | }
|
---|
| 281 |
|
---|
[cca9ef] | 282 | const RealSpaceMatrix operator*(const RealSpaceMatrix &mat,const double factor)
|
---|
[3bc926] | 283 | {
|
---|
[325390] | 284 | return factor*mat;
|
---|
| 285 | }
|
---|
[d10eb6] | 286 |
|
---|
[cca9ef] | 287 | bool RealSpaceMatrix::operator==(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 288 | {
|
---|
| 289 | return (*content == *(rhs.content));
|
---|
[0eb2dc] | 290 | }
|
---|
| 291 |
|
---|
[d10eb6] | 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 | */
|
---|
[cca9ef] | 296 | RealSpaceMatrix ReturnFullMatrixforSymmetric(const double * const symm)
|
---|
[d10eb6] | 297 | {
|
---|
[cca9ef] | 298 | RealSpaceMatrix matrix;
|
---|
[436f04] | 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]);
|
---|
[d10eb6] | 308 | return matrix;
|
---|
| 309 | };
|
---|
[c49c96] | 310 |
|
---|
[cca9ef] | 311 | ostream &operator<<(ostream &ost,const RealSpaceMatrix &mat)
|
---|
[3bc926] | 312 | {
|
---|
[c49c96] | 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 | }
|
---|
[4b94bb] | 323 |
|
---|
[cca9ef] | 324 | Vector operator*(const RealSpaceMatrix &mat,const Vector &vec)
|
---|
[3bc926] | 325 | {
|
---|
| 326 | return (*mat.content) * vec;
|
---|
[4b94bb] | 327 | }
|
---|
| 328 |
|
---|
[cca9ef] | 329 | Vector &operator*=(Vector& lhs,const RealSpaceMatrix &mat)
|
---|
[3bc926] | 330 | {
|
---|
[4b94bb] | 331 | lhs = mat*lhs;
|
---|
| 332 | return lhs;
|
---|
| 333 | }
|
---|
| 334 |
|
---|