/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * RealSpaceMatrix.cpp * * Created on: Jun 25, 2010 * Author: crueger */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "Exceptions/NotInvertibleException.hpp" #include "CodePatterns/Assert.hpp" #include "Helpers/defs.hpp" #include "Helpers/fast_functions.hpp" #include "LinearAlgebra/MatrixContent.hpp" #include "LinearAlgebra/RealSpaceMatrix.hpp" #include "LinearAlgebra/Vector.hpp" #include "LinearAlgebra/VectorContent.hpp" #include "RandomNumbers/RandomNumberGeneratorFactory.hpp" #include "RandomNumbers/RandomNumberGenerator.hpp" #include #include #include #include #include #include #include using namespace std; RealSpaceMatrix::RealSpaceMatrix() { content = new MatrixContent(NDIM, NDIM); createViews(); } RealSpaceMatrix::RealSpaceMatrix(const double* src) { content = new MatrixContent(NDIM, NDIM, src); createViews(); } RealSpaceMatrix::RealSpaceMatrix(const RealSpaceMatrix &src) { content = new MatrixContent(src.content); createViews(); } RealSpaceMatrix::RealSpaceMatrix(const MatrixContent &src) { content = new MatrixContent(src); createViews(); } RealSpaceMatrix::RealSpaceMatrix(MatrixContent* _content) { content = new MatrixContent(_content); createViews(); } RealSpaceMatrix::~RealSpaceMatrix() { // delete all views for(int i=NDIM;i--;){ delete rows_ptr[i]; } for(int i=NDIM;i--;){ delete columns_ptr[i]; } delete diagonal_ptr; delete content; } void RealSpaceMatrix::createViews(){ // create row views for(int i=NDIM;i--;){ VectorContent *rowContent = content->getRowVector(i); rows_ptr[i] = new Vector(rowContent); ASSERT(rowContent == NULL, "RealSpaceMatrix::createViews() - rowContent was not taken over as supposed to happen!"); } // create column views for(int i=NDIM;i--;){ VectorContent *columnContent = content->getColumnVector(i); columns_ptr[i] = new Vector(columnContent); ASSERT(columnContent == NULL, "RealSpaceMatrix::createViews() - columnContent was not taken over as supposed to happen!"); } // create diagonal view VectorContent *diagonalContent = content->getDiagonalVector(); diagonal_ptr = new Vector(diagonalContent); ASSERT(diagonalContent == NULL, "RealSpaceMatrix::createViews() - diagonalContent was not taken over as supposed to happen!"); } void RealSpaceMatrix::setIdentity(){ content->setIdentity(); } void RealSpaceMatrix::setZero(){ content->setZero(); } void RealSpaceMatrix::setRotation(const double x, const double y, const double z) { set(0,0, cos(y)*cos(z)); set(0,1, cos(z)*sin(x)*sin(y) - cos(x)*sin(z)); set(0,2, cos(x)*cos(z)*sin(y) + sin(x) * sin(z)); set(1,0, cos(y)*sin(z)); set(1,1, cos(x)*cos(z) + sin(x)*sin(y)*sin(z)); set(1,2, -cos(z)*sin(x) + cos(x)*sin(y)*sin(z)); set(2,0, -sin(y)); set(2,1, cos(y)*sin(x)); set(2,2, cos(x)*cos(y)); } void RealSpaceMatrix::setRandomRotation() { double phi[NDIM]; RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator(); const double rng_min = random.min(); const double rng_max = random.max(); for (int i=0;iat(i,j); } const double RealSpaceMatrix::at(size_t i, size_t j) const { return content->at(i,j); } Vector &RealSpaceMatrix::row(size_t i) { ASSERT(i>=0&&i=0&&i=0&&i=0&&iset(i,j, value); } double RealSpaceMatrix::determinant() const{ return at(0,0)*at(1,1)*at(2,2) + at(0,1)*at(1,2)*at(2,0) + at(0,2)*at(1,0)*at(2,1) - at(2,0)*at(1,1)*at(0,2) - at(2,1)*at(1,2)*at(0,0) - at(2,2)*at(1,0)*at(0,1); } RealSpaceMatrix RealSpaceMatrix::invert() const{ double det = determinant(); if(fabs(det)(content)->transpose()); return res; } RealSpaceMatrix &RealSpaceMatrix::transpose() { content->transpose(); return *this; } Vector RealSpaceMatrix::transformToEigenbasis() { gsl_vector *eval = content->transformToEigenbasis(); Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2)); gsl_vector_free(eval); return evalues; } const RealSpaceMatrix &RealSpaceMatrix::operator*=(const double factor) { *content *= factor; return *this; } const RealSpaceMatrix operator*(const double factor,const RealSpaceMatrix& mat) { RealSpaceMatrix tmp = mat; return tmp *= factor; } const RealSpaceMatrix operator*(const RealSpaceMatrix &mat,const double factor) { return factor*mat; } bool RealSpaceMatrix::operator==(const RealSpaceMatrix &rhs) const { return (*content == *(rhs.content)); } /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix. * \param *symm 6-dim array of unique symmetric matrix components * \return allocated NDIM*NDIM array with the symmetric matrix */ RealSpaceMatrix ReturnFullMatrixforSymmetric(const double * const symm) { RealSpaceMatrix matrix; matrix.set(0,0, symm[0]); matrix.set(1,0, symm[1]); matrix.set(2,0, symm[3]); matrix.set(0,1, symm[1]); matrix.set(1,1, symm[2]); matrix.set(2,1, symm[4]); matrix.set(0,2, symm[3]); matrix.set(1,2, symm[4]); matrix.set(2,2, symm[5]); return matrix; }; ostream &operator<<(ostream &ost,const RealSpaceMatrix &mat) { for(int i = 0;i