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