| 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 "CodePatterns/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "CodePatterns/Assert.hpp"
|
|---|
| 23 | #include "LinearAlgebra/defs.hpp"
|
|---|
| 24 | #include "LinearAlgebra/Exceptions.hpp"
|
|---|
| 25 | #include "LinearAlgebra/fast_functions.hpp"
|
|---|
| 26 | #include "LinearAlgebra/MatrixContent.hpp"
|
|---|
| 27 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
|---|
| 28 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 29 | #include "LinearAlgebra/VectorContent.hpp"
|
|---|
| 30 |
|
|---|
| 31 | #include <gsl/gsl_blas.h>
|
|---|
| 32 | #include <gsl/gsl_eigen.h>
|
|---|
| 33 | #include <gsl/gsl_matrix.h>
|
|---|
| 34 | #include <gsl/gsl_multimin.h>
|
|---|
| 35 | #include <gsl/gsl_vector.h>
|
|---|
| 36 | #include <cmath>
|
|---|
| 37 | #include <iostream>
|
|---|
| 38 | #include <limits>
|
|---|
| 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 = content->getRowVector(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 = content->getColumnVector(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 = content->getDiagonalVector();
|
|---|
| 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 | // double phi[NDIM];
|
|---|
| 124 | // phi[0] = x;
|
|---|
| 125 | // phi[0] = y;
|
|---|
| 126 | // phi[0] = z;
|
|---|
| 127 | // setRotation(phi);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void RealSpaceMatrix::setRotation(const double phi[NDIM])
|
|---|
| 131 | {
|
|---|
| 132 | setRotation(phi[0], phi[1], phi[2]);
|
|---|
| 133 | // set(0,0, cos(phi[0]) *cos(phi[2]) + (sin(phi[0])*sin(phi[1])*sin(phi[2])));
|
|---|
| 134 | // set(0,1, sin(phi[0]) *cos(phi[2]) - (cos(phi[0])*sin(phi[1])*sin(phi[2])));
|
|---|
| 135 | // set(0,2, cos(phi[1])*sin(phi[2]) );
|
|---|
| 136 | // set(1,0, -sin(phi[0])*cos(phi[1]) );
|
|---|
| 137 | // set(1,1, cos(phi[0])*cos(phi[1]) );
|
|---|
| 138 | // set(1,2, sin(phi[1]) );
|
|---|
| 139 | // set(2,0, -cos(phi[0]) *sin(phi[2]) + (sin(phi[0])*sin(phi[1])*cos(phi[2])));
|
|---|
| 140 | // set(2,1, -sin(phi[0]) *sin(phi[2]) - (cos(phi[0])*sin(phi[1])*cos(phi[2])));
|
|---|
| 141 | // set(2,2, cos(phi[1])*cos(phi[2]) );
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | RealSpaceMatrix &RealSpaceMatrix::operator=(const RealSpaceMatrix &src)
|
|---|
| 145 | {
|
|---|
| 146 | // MatrixContent checks for self-assignment
|
|---|
| 147 | *content = *(src.content);
|
|---|
| 148 | return *this;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | const RealSpaceMatrix &RealSpaceMatrix::operator+=(const RealSpaceMatrix &rhs)
|
|---|
| 152 | {
|
|---|
| 153 | *content += *(rhs.content);
|
|---|
| 154 | return *this;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | const RealSpaceMatrix &RealSpaceMatrix::operator-=(const RealSpaceMatrix &rhs)
|
|---|
| 158 | {
|
|---|
| 159 | *content -= *(rhs.content);
|
|---|
| 160 | return *this;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const RealSpaceMatrix &rhs)
|
|---|
| 164 | {
|
|---|
| 165 | (*content) *= (*rhs.content);
|
|---|
| 166 | return *this;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | const RealSpaceMatrix RealSpaceMatrix::operator+(const RealSpaceMatrix &rhs) const
|
|---|
| 170 | {
|
|---|
| 171 | RealSpaceMatrix tmp = *this;
|
|---|
| 172 | tmp+=rhs;
|
|---|
| 173 | return tmp;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | const RealSpaceMatrix RealSpaceMatrix::operator-(const RealSpaceMatrix &rhs) const
|
|---|
| 177 | {
|
|---|
| 178 | RealSpaceMatrix tmp = *this;
|
|---|
| 179 | tmp-=rhs;
|
|---|
| 180 | return tmp;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | const RealSpaceMatrix RealSpaceMatrix::operator*(const RealSpaceMatrix &rhs) const
|
|---|
| 184 | {
|
|---|
| 185 | RealSpaceMatrix tmp(content);
|
|---|
| 186 | tmp *= rhs;
|
|---|
| 187 | return tmp;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | double &RealSpaceMatrix::at(size_t i, size_t j)
|
|---|
| 191 | {
|
|---|
| 192 | return content->at(i,j);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | const double RealSpaceMatrix::at(size_t i, size_t j) const
|
|---|
| 196 | {
|
|---|
| 197 | return content->at(i,j);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | Vector &RealSpaceMatrix::row(size_t i)
|
|---|
| 201 | {
|
|---|
| 202 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
|---|
| 203 | return *rows_ptr[i];
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | const Vector &RealSpaceMatrix::row(size_t i) const
|
|---|
| 207 | {
|
|---|
| 208 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
|---|
| 209 | return *rows_ptr[i];
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | Vector &RealSpaceMatrix::column(size_t i)
|
|---|
| 213 | {
|
|---|
| 214 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
|---|
| 215 | return *columns_ptr[i];
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | const Vector &RealSpaceMatrix::column(size_t i) const
|
|---|
| 219 | {
|
|---|
| 220 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
|---|
| 221 | return *columns_ptr[i];
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | Vector &RealSpaceMatrix::diagonal()
|
|---|
| 225 | {
|
|---|
| 226 | return *diagonal_ptr;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | const Vector &RealSpaceMatrix::diagonal() const
|
|---|
| 230 | {
|
|---|
| 231 | return *diagonal_ptr;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | void RealSpaceMatrix::set(size_t i, size_t j, const double value)
|
|---|
| 235 | {
|
|---|
| 236 | content->set(i,j, value);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | double RealSpaceMatrix::determinant() const{
|
|---|
| 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 |
|
|---|
| 248 |
|
|---|
| 249 | RealSpaceMatrix RealSpaceMatrix::invert() const{
|
|---|
| 250 | double det = determinant();
|
|---|
| 251 | if(fabs(det) <= LINALG_MYEPSILON()){
|
|---|
| 252 | throw NotInvertibleException() << LinearAlgebraDeterminant(det) << LinearAlgebraMatrixContent(this->content);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | double detReci = 1./det;
|
|---|
| 256 | RealSpaceMatrix res;
|
|---|
| 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
|
|---|
| 266 | return res;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | RealSpaceMatrix RealSpaceMatrix::transpose() const
|
|---|
| 270 | {
|
|---|
| 271 | RealSpaceMatrix res = RealSpaceMatrix(const_cast<const MatrixContent *>(content)->transpose());
|
|---|
| 272 | return res;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | RealSpaceMatrix &RealSpaceMatrix::transpose()
|
|---|
| 276 | {
|
|---|
| 277 | content->transpose();
|
|---|
| 278 | return *this;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | Vector RealSpaceMatrix::transformToEigenbasis()
|
|---|
| 282 | {
|
|---|
| 283 | gsl_vector *eval = content->transformToEigenbasis();
|
|---|
| 284 | Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
|
|---|
| 285 | gsl_vector_free(eval);
|
|---|
| 286 | return evalues;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const double factor)
|
|---|
| 290 | {
|
|---|
| 291 | *content *= factor;
|
|---|
| 292 | return *this;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | const RealSpaceMatrix operator*(const double factor,const RealSpaceMatrix& mat)
|
|---|
| 296 | {
|
|---|
| 297 | RealSpaceMatrix tmp = mat;
|
|---|
| 298 | return tmp *= factor;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | const RealSpaceMatrix operator*(const RealSpaceMatrix &mat,const double factor)
|
|---|
| 302 | {
|
|---|
| 303 | return factor*mat;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | bool RealSpaceMatrix::operator==(const RealSpaceMatrix &rhs) const
|
|---|
| 307 | {
|
|---|
| 308 | return (*content == *(rhs.content));
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 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 | */
|
|---|
| 315 | RealSpaceMatrix ReturnFullMatrixforSymmetric(const double * const symm)
|
|---|
| 316 | {
|
|---|
| 317 | RealSpaceMatrix matrix;
|
|---|
| 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]);
|
|---|
| 327 | return matrix;
|
|---|
| 328 | };
|
|---|
| 329 |
|
|---|
| 330 | ostream &operator<<(ostream &ost,const RealSpaceMatrix &mat)
|
|---|
| 331 | {
|
|---|
| 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 | }
|
|---|
| 342 |
|
|---|
| 343 | Vector operator*(const RealSpaceMatrix &mat,const Vector &vec)
|
|---|
| 344 | {
|
|---|
| 345 | return (*mat.content) * vec;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | Vector &operator*=(Vector& lhs,const RealSpaceMatrix &mat)
|
|---|
| 349 | {
|
|---|
| 350 | lhs = mat*lhs;
|
|---|
| 351 | return lhs;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|