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