/* * 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. */ /* * VectorContent.cpp * * Created on: Nov 15, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include #include #include #include #include "Helpers/Assert.hpp" #include "Helpers/defs.hpp" #include "LinearAlgebra/Vector.hpp" #include "LinearAlgebra/VectorContent.hpp" /** Constructor of class VectorContent. * Allocates GSL structures * \param _dim number of components */ VectorContent::VectorContent(size_t _dim) : dimension(_dim) { content = gsl_vector_calloc(dimension); } /** Constructor of class VectorContent. * We need this VectorBaseCase for the VectorContentView class. * There no content should be allocated, as it is just a view with an internal * gsl_vector_view. Hence, VectorBaseCase is just dummy class to give the * constructor a unique signature. * \param VectorBaseCase */ VectorContent::VectorContent(VectorBaseCase) {} /** Copy constructor of class VectorContent. * Allocates GSL structures and copies components from \a *src. * \param *src source vector */ VectorContent::VectorContent(const VectorContent * const src) : dimension(src->dimension) { content = gsl_vector_alloc(dimension); gsl_vector_memcpy (content, src->content); }; /** Copy constructor of class VectorContent. * Allocates GSL structures and copies components from \a *src. * \param *src source vector */ VectorContent::VectorContent(const VectorContent & src) : dimension(src.dimension) { content = gsl_vector_alloc(dimension); gsl_vector_memcpy (content, src.content); }; /** Copy constructor of class VectorContent. * No allocation, we just take over gsl_vector. * \param *src source gsl_vector */ VectorContent::VectorContent(gsl_vector * _src) : dimension(_src->size) { content = _src; } /** Destructor of class VectorContent. * Frees GSL structures */ VectorContent::~VectorContent() { if(content != NULL){ gsl_vector_free(content); content = NULL; } } /* ============================ Accessing =============================== */ /** Accessor for manipulating component (i). * \param i component number * \return reference to component (i) */ double &VectorContent::at(size_t i) { ASSERT((i>=0) && (i=0) && (icontent); } /** This function sets all the elements of the vector to zero. */ void VectorContent::setZero() { gsl_vector_set_zero (content); }; /** This function makes a basis vector by setting all the elements of the vector to zero except for the i-th element which is set to one. * \param i i-th component to set to unity (all other to zero) * \return vector set */ int VectorContent::setBasis(size_t i) { return gsl_vector_set_basis (content, i); }; /* ====================== Exchanging elements ============================ */ /** This function exchanges the \a i-th and \a j-th elements of the vector in-place. * \param i i-th element to swap with ... * \param j ... j-th element to swap against */ int VectorContent::SwapElements(size_t i, size_t j) { return gsl_vector_swap_elements (content, i, j); }; /** This function reverses the order of the elements of the vector. */ int VectorContent::Reverse() { return gsl_vector_reverse (content); }; /* ========================== Operators =============================== */ /** Accessor for manipulating component (i). * \param i component number * \return reference to component (i) */ double &VectorContent::operator[](size_t i) { ASSERT((i>=0) && (i=0) && (i MYEPSILON) && (fabs(norm2) > MYEPSILON)) angle = this->ScalarProduct(y)/norm1/norm2; // -1-MYEPSILON occured due to numerical imprecision, catch ... //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl; if (angle < -1) angle = -1; if (angle > 1) angle = 1; return acos(angle); } /* ======================== Properties ============================= */ /** Calculates scalar product between \a *this and \a b. * @param b other vector * @return \f$| (*this) \cdot (b)|^2\f$ */ const double VectorContent::operator*(const VectorContent& b) const { double res = 0.; gsl_blas_ddot(content, b.content, &res); return res; }; /** Getter for VectorContent::dimension. * @return VectorContent::dimension */ const size_t VectorContent::getDimension() const { return dimension; }