[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"
|
---|
[57f243] | 27 | #include "LinearAlgebra/Vector.hpp"
|
---|
[3bc926] | 28 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
| 29 | #include "LinearAlgebra/MatrixContent.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--;){
|
---|
| 87 | VectorContent *rowContent = new VectorViewContent(gsl_matrix_row(content->content,i));
|
---|
| 88 | rows_ptr[i] = new Vector(rowContent);
|
---|
| 89 | }
|
---|
| 90 | // create column views
|
---|
| 91 | for(int i=NDIM;i--;){
|
---|
| 92 | VectorContent *columnContent = new VectorViewContent(gsl_matrix_column(content->content,i));
|
---|
| 93 | columns_ptr[i] = new Vector(columnContent);
|
---|
| 94 | }
|
---|
| 95 | // create diagonal view
|
---|
| 96 | VectorContent *diagonalContent = new VectorViewContent(gsl_matrix_diagonal(content->content));
|
---|
| 97 | diagonal_ptr = new Vector(diagonalContent);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[cca9ef] | 100 | void RealSpaceMatrix::setIdentity(){
|
---|
[3bc926] | 101 | content->setIdentity();
|
---|
[1da5f5] | 102 | }
|
---|
| 103 |
|
---|
[cca9ef] | 104 | void RealSpaceMatrix::setZero(){
|
---|
[3bc926] | 105 | content->setZero();
|
---|
[a439e5] | 106 | }
|
---|
| 107 |
|
---|
[cca9ef] | 108 | void RealSpaceMatrix::setRotation(const double x, const double y, const double z)
|
---|
[31fb1d] | 109 | {
|
---|
| 110 | set(0,0, cos(y)*cos(z));
|
---|
| 111 | set(0,1, cos(z)*sin(x)*sin(y) - cos(x)*sin(z));
|
---|
| 112 | set(0,2, cos(x)*cos(z)*sin(y) + sin(x) * sin(z));
|
---|
| 113 | set(1,0, cos(y)*sin(z));
|
---|
| 114 | set(1,1, cos(x)*cos(z) + sin(x)*sin(y)*sin(z));
|
---|
| 115 | set(1,2, -cos(z)*sin(x) + cos(x)*sin(y)*sin(z));
|
---|
| 116 | set(2,0, -sin(y));
|
---|
| 117 | set(2,1, cos(y)*sin(x));
|
---|
| 118 | set(2,2, cos(x)*cos(y));
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[cca9ef] | 121 | RealSpaceMatrix &RealSpaceMatrix::operator=(const RealSpaceMatrix &src)
|
---|
[3bc926] | 122 | {
|
---|
| 123 | // MatrixContent checks for self-assignment
|
---|
| 124 | *content = *(src.content);
|
---|
[325390] | 125 | return *this;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[cca9ef] | 128 | const RealSpaceMatrix &RealSpaceMatrix::operator+=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 129 | {
|
---|
| 130 | *content += *(rhs.content);
|
---|
[325390] | 131 | return *this;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[cca9ef] | 134 | const RealSpaceMatrix &RealSpaceMatrix::operator-=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 135 | {
|
---|
| 136 | *content -= *(rhs.content);
|
---|
[325390] | 137 | return *this;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[cca9ef] | 140 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 141 | {
|
---|
| 142 | (*content) *= (*rhs.content);
|
---|
[325390] | 143 | return *this;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[cca9ef] | 146 | const RealSpaceMatrix RealSpaceMatrix::operator+(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 147 | {
|
---|
[cca9ef] | 148 | RealSpaceMatrix tmp = *this;
|
---|
[325390] | 149 | tmp+=rhs;
|
---|
| 150 | return tmp;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[cca9ef] | 153 | const RealSpaceMatrix RealSpaceMatrix::operator-(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 154 | {
|
---|
[cca9ef] | 155 | RealSpaceMatrix tmp = *this;
|
---|
[325390] | 156 | tmp-=rhs;
|
---|
| 157 | return tmp;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[cca9ef] | 160 | const RealSpaceMatrix RealSpaceMatrix::operator*(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 161 | {
|
---|
[cca9ef] | 162 | RealSpaceMatrix tmp(content);
|
---|
[3bc926] | 163 | tmp *= rhs;
|
---|
| 164 | return tmp;
|
---|
[325390] | 165 | }
|
---|
| 166 |
|
---|
[cca9ef] | 167 | double &RealSpaceMatrix::at(size_t i, size_t j)
|
---|
[3bc926] | 168 | {
|
---|
| 169 | return content->at(i,j);
|
---|
[325390] | 170 | }
|
---|
| 171 |
|
---|
[cca9ef] | 172 | const double RealSpaceMatrix::at(size_t i, size_t j) const
|
---|
[3bc926] | 173 | {
|
---|
| 174 | return content->at(i,j);
|
---|
[436f04] | 175 | }
|
---|
| 176 |
|
---|
[cca9ef] | 177 | Vector &RealSpaceMatrix::row(size_t i)
|
---|
[3bc926] | 178 | {
|
---|
[3dbb9d] | 179 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 180 | return *rows_ptr[i];
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[cca9ef] | 183 | const Vector &RealSpaceMatrix::row(size_t i) const
|
---|
[3bc926] | 184 | {
|
---|
[3dbb9d] | 185 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 186 | return *rows_ptr[i];
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[cca9ef] | 189 | Vector &RealSpaceMatrix::column(size_t i)
|
---|
[3bc926] | 190 | {
|
---|
[3dbb9d] | 191 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 192 | return *columns_ptr[i];
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[cca9ef] | 195 | const Vector &RealSpaceMatrix::column(size_t i) const
|
---|
[3bc926] | 196 | {
|
---|
[3dbb9d] | 197 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 198 | return *columns_ptr[i];
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[cca9ef] | 201 | Vector &RealSpaceMatrix::diagonal()
|
---|
[3bc926] | 202 | {
|
---|
[3dbb9d] | 203 | return *diagonal_ptr;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[cca9ef] | 206 | const Vector &RealSpaceMatrix::diagonal() const
|
---|
[3bc926] | 207 | {
|
---|
[3dbb9d] | 208 | return *diagonal_ptr;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[cca9ef] | 211 | void RealSpaceMatrix::set(size_t i, size_t j, const double value)
|
---|
[3bc926] | 212 | {
|
---|
| 213 | content->set(i,j, value);
|
---|
[cadbc1] | 214 | }
|
---|
| 215 |
|
---|
[cca9ef] | 216 | double RealSpaceMatrix::determinant() const{
|
---|
[325390] | 217 | return at(0,0)*at(1,1)*at(2,2)
|
---|
| 218 | + at(0,1)*at(1,2)*at(2,0)
|
---|
| 219 | + at(0,2)*at(1,0)*at(2,1)
|
---|
| 220 | - at(2,0)*at(1,1)*at(0,2)
|
---|
| 221 | - at(2,1)*at(1,2)*at(0,0)
|
---|
| 222 | - at(2,2)*at(1,0)*at(0,1);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
[a439e5] | 225 |
|
---|
[cca9ef] | 226 | RealSpaceMatrix RealSpaceMatrix::invert() const{
|
---|
[325390] | 227 | double det = determinant();
|
---|
| 228 | if(fabs(det)<MYEPSILON){
|
---|
| 229 | throw NotInvertibleException(__FILE__,__LINE__);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | double detReci = 1./det;
|
---|
[cca9ef] | 233 | RealSpaceMatrix res;
|
---|
[436f04] | 234 | res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
|
---|
| 235 | res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
|
---|
| 236 | res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
|
---|
| 237 | res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
|
---|
| 238 | res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
|
---|
| 239 | res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
|
---|
| 240 | res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
|
---|
| 241 | res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
|
---|
| 242 | res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
|
---|
[325390] | 243 | return res;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[cca9ef] | 246 | RealSpaceMatrix RealSpaceMatrix::transpose() const
|
---|
[3bc926] | 247 | {
|
---|
[cca9ef] | 248 | RealSpaceMatrix res = RealSpaceMatrix(const_cast<const MatrixContent *>(content)->transpose());
|
---|
[41ea3c] | 249 | return res;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[cca9ef] | 252 | RealSpaceMatrix &RealSpaceMatrix::transpose()
|
---|
[6c438f] | 253 | {
|
---|
[3bc926] | 254 | content->transpose();
|
---|
[6c438f] | 255 | return *this;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[cca9ef] | 258 | Vector RealSpaceMatrix::transformToEigenbasis()
|
---|
[a439e5] | 259 | {
|
---|
[3bc926] | 260 | gsl_vector *eval = content->transformToEigenbasis();
|
---|
[a439e5] | 261 | Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
|
---|
[80cecb5] | 262 | gsl_vector_free(eval);
|
---|
[a439e5] | 263 | return evalues;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[cca9ef] | 266 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const double factor)
|
---|
[3bc926] | 267 | {
|
---|
| 268 | *content *= factor;
|
---|
[325390] | 269 | return *this;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[cca9ef] | 272 | const RealSpaceMatrix operator*(const double factor,const RealSpaceMatrix& mat)
|
---|
[3bc926] | 273 | {
|
---|
[cca9ef] | 274 | RealSpaceMatrix tmp = mat;
|
---|
[3bc926] | 275 | return tmp *= factor;
|
---|
[325390] | 276 | }
|
---|
| 277 |
|
---|
[cca9ef] | 278 | const RealSpaceMatrix operator*(const RealSpaceMatrix &mat,const double factor)
|
---|
[3bc926] | 279 | {
|
---|
[325390] | 280 | return factor*mat;
|
---|
| 281 | }
|
---|
[d10eb6] | 282 |
|
---|
[cca9ef] | 283 | bool RealSpaceMatrix::operator==(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 284 | {
|
---|
| 285 | return (*content == *(rhs.content));
|
---|
[0eb2dc] | 286 | }
|
---|
| 287 |
|
---|
[d10eb6] | 288 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
|
---|
| 289 | * \param *symm 6-dim array of unique symmetric matrix components
|
---|
| 290 | * \return allocated NDIM*NDIM array with the symmetric matrix
|
---|
| 291 | */
|
---|
[cca9ef] | 292 | RealSpaceMatrix ReturnFullMatrixforSymmetric(const double * const symm)
|
---|
[d10eb6] | 293 | {
|
---|
[cca9ef] | 294 | RealSpaceMatrix matrix;
|
---|
[436f04] | 295 | matrix.set(0,0, symm[0]);
|
---|
| 296 | matrix.set(1,0, symm[1]);
|
---|
| 297 | matrix.set(2,0, symm[3]);
|
---|
| 298 | matrix.set(0,1, symm[1]);
|
---|
| 299 | matrix.set(1,1, symm[2]);
|
---|
| 300 | matrix.set(2,1, symm[4]);
|
---|
| 301 | matrix.set(0,2, symm[3]);
|
---|
| 302 | matrix.set(1,2, symm[4]);
|
---|
| 303 | matrix.set(2,2, symm[5]);
|
---|
[d10eb6] | 304 | return matrix;
|
---|
| 305 | };
|
---|
[c49c96] | 306 |
|
---|
[cca9ef] | 307 | ostream &operator<<(ostream &ost,const RealSpaceMatrix &mat)
|
---|
[3bc926] | 308 | {
|
---|
[c49c96] | 309 | for(int i = 0;i<NDIM;++i){
|
---|
| 310 | ost << "\n";
|
---|
| 311 | for(int j = 0; j<NDIM;++j){
|
---|
| 312 | ost << mat.at(i,j);
|
---|
| 313 | if(j!=NDIM-1)
|
---|
| 314 | ost << "; ";
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | return ost;
|
---|
| 318 | }
|
---|
[4b94bb] | 319 |
|
---|
[cca9ef] | 320 | Vector operator*(const RealSpaceMatrix &mat,const Vector &vec)
|
---|
[3bc926] | 321 | {
|
---|
| 322 | return (*mat.content) * vec;
|
---|
[4b94bb] | 323 | }
|
---|
| 324 |
|
---|
[cca9ef] | 325 | Vector &operator*=(Vector& lhs,const RealSpaceMatrix &mat)
|
---|
[3bc926] | 326 | {
|
---|
[4b94bb] | 327 | lhs = mat*lhs;
|
---|
| 328 | return lhs;
|
---|
| 329 | }
|
---|
| 330 |
|
---|