[bbf1bd] | 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 | * VectorContent.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Nov 15, 2010
|
---|
| 12 | * Author: heber
|
---|
| 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 <gsl/gsl_vector.h>
|
---|
| 23 | #include <cmath>
|
---|
| 24 | #include <iostream>
|
---|
| 25 |
|
---|
| 26 | #include "Helpers/Assert.hpp"
|
---|
| 27 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 28 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
| 29 |
|
---|
| 30 | /** Constructor of class VectorContent.
|
---|
| 31 | * Allocates GSL structures
|
---|
| 32 | * \param _dim number of components
|
---|
| 33 | */
|
---|
| 34 | VectorContent::VectorContent(size_t _dim) :
|
---|
| 35 | dimension(_dim)
|
---|
| 36 | {
|
---|
| 37 | content = gsl_vector_calloc(dimension);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /** Constructor of class VectorContent.
|
---|
| 41 | * We need this BaseCase for the VectorContentView class.
|
---|
| 42 | * There no content should be allocated, as it is just a view with an internal
|
---|
| 43 | * gsl_vector_view. Hence, BaseCase is just dummy class to give the constructor
|
---|
| 44 | * a unique signature.
|
---|
| 45 | * \param BaseCase
|
---|
| 46 | */
|
---|
| 47 | VectorContent::VectorContent(BaseCase)
|
---|
| 48 | {}
|
---|
| 49 |
|
---|
| 50 | /** Copy constructor of class VectorContent.
|
---|
| 51 | * Allocates GSL structures and copies components from \a *src.
|
---|
| 52 | * \param *src source vector
|
---|
| 53 | */
|
---|
| 54 | VectorContent::VectorContent(const VectorContent * const src) :
|
---|
| 55 | dimension(src->dimension)
|
---|
| 56 | {
|
---|
| 57 | content = gsl_vector_alloc(dimension);
|
---|
| 58 | gsl_vector_memcpy (content, src->content);
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | /** Copy constructor of class VectorContent.
|
---|
| 62 | * Allocates GSL structures and copies components from \a *src.
|
---|
| 63 | * \param *src source vector
|
---|
| 64 | */
|
---|
| 65 | VectorContent::VectorContent(const VectorContent & src) :
|
---|
| 66 | dimension(src.dimension)
|
---|
| 67 | {
|
---|
| 68 | content = gsl_vector_alloc(dimension);
|
---|
| 69 | gsl_vector_memcpy (content, src.content);
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | /** Destructor of class VectorContent.
|
---|
| 73 | * Frees GSL structures
|
---|
| 74 | */
|
---|
| 75 | VectorContent::~VectorContent()
|
---|
| 76 | {
|
---|
| 77 | if(content != NULL){
|
---|
| 78 | gsl_vector_free(content);
|
---|
| 79 | content = NULL;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /* ============================ Accessing =============================== */
|
---|
| 84 | /** Accessor for manipulating component (i).
|
---|
| 85 | * \param i component number
|
---|
| 86 | * \return reference to component (i)
|
---|
| 87 | */
|
---|
| 88 | double &VectorContent::at(size_t i)
|
---|
| 89 | {
|
---|
| 90 | ASSERT((i>=0) && (i<dimension),
|
---|
| 91 | "VectorContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
| 92 | return *gsl_vector_ptr (content, i);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Constant accessor for (value of) component (i).
|
---|
| 96 | * \param i component number
|
---|
| 97 | * \return const component (i)
|
---|
| 98 | */
|
---|
| 99 | const double VectorContent::at(size_t i) const
|
---|
| 100 | {
|
---|
| 101 | ASSERT((i>=0) && (i<dimension),
|
---|
| 102 | "VectorContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
| 103 | return gsl_vector_get(content, i);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** These functions return a pointer to the \a m-th element of a vector.
|
---|
| 107 | * If \a m lies outside the allowed range of 0 to VectorContent::dimension-1 then the error handler is invoked and a null pointer is returned.
|
---|
| 108 | * \param m m-th element
|
---|
| 109 | * \return pointer to \a m-th element
|
---|
| 110 | */
|
---|
| 111 | double *VectorContent::Pointer(size_t m) const
|
---|
| 112 | {
|
---|
| 113 | return gsl_vector_ptr (content, m);
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | /** These functions return a constant pointer to the \a m-th element of a vector.
|
---|
| 117 | * If \a m lies outside the allowed range of 0 to VectorContent::dimension-1 then the error handler is invoked and a null pointer is returned.
|
---|
| 118 | * \param m m-th element
|
---|
| 119 | * \return const pointer to \a m-th element
|
---|
| 120 | */
|
---|
| 121 | const double *VectorContent::const_Pointer(size_t m) const
|
---|
| 122 | {
|
---|
| 123 | return gsl_vector_const_ptr (content, m);
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | /** Assignment operator.
|
---|
| 127 | * \param &src source vector to assign \a *this to
|
---|
| 128 | * \return reference to \a *this
|
---|
| 129 | */
|
---|
| 130 | VectorContent& VectorContent::operator=(const VectorContent& src)
|
---|
| 131 | {
|
---|
| 132 | ASSERT(dimension == src.dimension, "Dimensions have to be the same to assign VectorContent onto another!");
|
---|
| 133 | // check for self assignment
|
---|
| 134 | if(&src!=this){
|
---|
| 135 | gsl_vector_memcpy(content, src.content);
|
---|
| 136 | }
|
---|
| 137 | return *this;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* ========================== Initializing =============================== */
|
---|
| 141 | /** This function sets all the elements of the vector to the value \a x.
|
---|
| 142 | * \param x value to set to
|
---|
| 143 | */
|
---|
| 144 | void VectorContent::setValue(double x)
|
---|
| 145 | {
|
---|
| 146 | gsl_vector_set_all (content, x);
|
---|
| 147 | };
|
---|
| 148 |
|
---|
| 149 | /** This function sets the vector from a double array.
|
---|
| 150 | * Creates a vector view of the array and performs a memcopy.
|
---|
| 151 | * \param *x array of values (no dimension check is performed)
|
---|
| 152 | */
|
---|
| 153 | void VectorContent::setFromDoubleArray(double * x)
|
---|
| 154 | {
|
---|
| 155 | gsl_vector_view m = gsl_vector_view_array (x, dimension);
|
---|
| 156 | gsl_vector_memcpy (content, &m.vector);
|
---|
| 157 | };
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
| 160 | * This function sets the GSLvector from an ordinary vector.
|
---|
| 161 | *
|
---|
| 162 | * Takes access to the internal gsl_vector and copies it
|
---|
| 163 | */
|
---|
| 164 | void VectorContent::setFromVector(Vector &v)
|
---|
| 165 | {
|
---|
| 166 | gsl_vector_memcpy (content, v.get()->content);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** This function sets all the elements of the vector to zero.
|
---|
| 170 | */
|
---|
| 171 | void VectorContent::setZero()
|
---|
| 172 | {
|
---|
| 173 | gsl_vector_set_zero (content);
|
---|
| 174 | };
|
---|
| 175 |
|
---|
| 176 | /** 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.
|
---|
| 177 | * \param i i-th component to set to unity (all other to zero)
|
---|
| 178 | * \return vector set
|
---|
| 179 | */
|
---|
| 180 | int VectorContent::setBasis(size_t i)
|
---|
| 181 | {
|
---|
| 182 | return gsl_vector_set_basis (content, i);
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | /* ====================== Exchanging elements ============================ */
|
---|
| 186 | /** This function exchanges the \a i-th and \a j-th elements of the vector in-place.
|
---|
| 187 | * \param i i-th element to swap with ...
|
---|
| 188 | * \param j ... j-th element to swap against
|
---|
| 189 | */
|
---|
| 190 | int VectorContent::SwapElements(size_t i, size_t j)
|
---|
| 191 | {
|
---|
| 192 | return gsl_vector_swap_elements (content, i, j);
|
---|
| 193 | };
|
---|
| 194 |
|
---|
| 195 | /** This function reverses the order of the elements of the vector.
|
---|
| 196 | */
|
---|
| 197 | int VectorContent::Reverse()
|
---|
| 198 | {
|
---|
| 199 | return gsl_vector_reverse (content);
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | /* ========================== Operators =============================== */
|
---|
| 204 | /** Accessor for manipulating component (i).
|
---|
| 205 | * \param i component number
|
---|
| 206 | * \return reference to component (i)
|
---|
| 207 | */
|
---|
| 208 | double &VectorContent::operator[](size_t i)
|
---|
| 209 | {
|
---|
| 210 | ASSERT((i>=0) && (i<dimension),
|
---|
| 211 | "VectorContent::operator[]() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
| 212 | return *gsl_vector_ptr (content, i);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /** Constant accessor for (value of) component (i).
|
---|
| 216 | * \param i component number
|
---|
| 217 | * \return const component (i)
|
---|
| 218 | */
|
---|
| 219 | const double VectorContent::operator[](size_t i) const
|
---|
| 220 | {
|
---|
| 221 | ASSERT((i>=0) && (i<dimension),
|
---|
| 222 | "VectorContent::operator[]() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(dimension)+"]");
|
---|
| 223 | return gsl_vector_get(content, i);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | /** Compares VectorContent \a to VectorContent \a b component-wise.
|
---|
| 228 | * \param a base VectorContent
|
---|
| 229 | * \param b VectorContent components to add
|
---|
| 230 | * \return a == b
|
---|
| 231 | */
|
---|
| 232 | bool VectorContent::operator==(const VectorContent& b) const
|
---|
| 233 | {
|
---|
| 234 | bool status = true;
|
---|
| 235 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
| 236 | for (size_t i=0;i<dimension;i++)
|
---|
| 237 | status = status && (fabs(at(i) - b.at(i)) < MYEPSILON);
|
---|
| 238 | return status;
|
---|
| 239 | };
|
---|
| 240 |
|
---|
| 241 | /** Sums VectorContent \a to this lhs component-wise.
|
---|
| 242 | * \param a base VectorContent
|
---|
| 243 | * \param b VectorContent components to add
|
---|
| 244 | * \return lhs + a
|
---|
| 245 | */
|
---|
| 246 | const VectorContent& VectorContent::operator+=(const VectorContent& b)
|
---|
| 247 | {
|
---|
| 248 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
| 249 | for (size_t i=0;i<dimension;i++)
|
---|
| 250 | at(i) = at(i)+b.at(i);
|
---|
| 251 | return *this;
|
---|
| 252 | };
|
---|
| 253 |
|
---|
| 254 | /** Subtracts VectorContent \a from this lhs component-wise.
|
---|
| 255 | * \param a base VectorContent
|
---|
| 256 | * \param b VectorContent components to add
|
---|
| 257 | * \return lhs - a
|
---|
| 258 | */
|
---|
| 259 | const VectorContent& VectorContent::operator-=(const VectorContent& b)
|
---|
| 260 | {
|
---|
| 261 | ASSERT(dimension == b.dimension, "Dimenions of VectorContents to compare differ");
|
---|
| 262 | for (size_t i=0;i<dimension;i++)
|
---|
| 263 | at(i) = at(i)-b.at(i);
|
---|
| 264 | return *this;
|
---|
| 265 | };
|
---|
| 266 |
|
---|
| 267 | /** factor each component of \a a times a double \a m.
|
---|
| 268 | * \param a base VectorContent
|
---|
| 269 | * \param m factor
|
---|
| 270 | * \return lhs.Get(i) * m
|
---|
| 271 | */
|
---|
| 272 | const VectorContent& VectorContent::operator*=(const double m)
|
---|
| 273 | {
|
---|
| 274 | for (size_t i=0;i<dimension;i++)
|
---|
| 275 | at(i) = at(i)*m;
|
---|
| 276 | return *this;
|
---|
| 277 | };
|
---|
| 278 |
|
---|
| 279 | /** Sums two VectorContents \a and \b component-wise.
|
---|
| 280 | * \param a first VectorContent
|
---|
| 281 | * \param b second VectorContent
|
---|
| 282 | * \return a + b
|
---|
| 283 | */
|
---|
| 284 | VectorContent const operator+(const VectorContent& a, const VectorContent& b)
|
---|
| 285 | {
|
---|
| 286 | ASSERT(a.dimension == b.dimension, "VectorContent::operator+() - dimensions have to match: "+toString(a.dimension)+" != "+toString(b.dimension)+"!");
|
---|
| 287 | VectorContent x(a);
|
---|
| 288 | for (size_t i=0;i<a.dimension;i++)
|
---|
| 289 | x.at(i) = a.at(i)+b.at(i);
|
---|
| 290 | return x;
|
---|
| 291 | };
|
---|
| 292 |
|
---|
| 293 | /** Subtracts VectorContent \a from \b component-wise.
|
---|
| 294 | * \param a first VectorContent
|
---|
| 295 | * \param b second VectorContent
|
---|
| 296 | * \return a - b
|
---|
| 297 | */
|
---|
| 298 | VectorContent const operator-(const VectorContent& a, const VectorContent& b)
|
---|
| 299 | {
|
---|
| 300 | ASSERT(a.dimension == b.dimension, "VectorContent::operator-() - dimensions have to match: "+toString(a.dimension)+" != "+toString(b.dimension)+"!");
|
---|
| 301 | VectorContent x(a);
|
---|
| 302 | for (size_t i=0;i<a.dimension;i++)
|
---|
| 303 | x.at(i) = a.at(i)-b.at(i);
|
---|
| 304 | return x;
|
---|
| 305 | };
|
---|
| 306 |
|
---|
| 307 | /** Factors given VectorContent \a a times \a m.
|
---|
| 308 | * \param a VectorContent
|
---|
| 309 | * \param m factor
|
---|
| 310 | * \return m * a
|
---|
| 311 | */
|
---|
| 312 | VectorContent const operator*(const VectorContent& a, const double m)
|
---|
| 313 | {
|
---|
| 314 | VectorContent x(a);
|
---|
| 315 | for (size_t i=0;i<a.dimension;i++)
|
---|
| 316 | x.at(i) = a.at(i)*m;
|
---|
| 317 | return x;
|
---|
| 318 | };
|
---|
| 319 |
|
---|
| 320 | /** Factors given VectorContent \a a times \a m.
|
---|
| 321 | * \param m factor
|
---|
| 322 | * \param a VectorContent
|
---|
| 323 | * \return m * a
|
---|
| 324 | */
|
---|
| 325 | VectorContent const operator*(const double m, const VectorContent& a )
|
---|
| 326 | {
|
---|
| 327 | VectorContent x(a);
|
---|
| 328 | for (size_t i=0;i<a.dimension;i++)
|
---|
| 329 | x.at(i) = a.at(i)*m;
|
---|
| 330 | return x;
|
---|
| 331 | };
|
---|
| 332 |
|
---|
| 333 | ostream& operator<<(ostream& ost, const VectorContent& m)
|
---|
| 334 | {
|
---|
| 335 | ost << "(";
|
---|
| 336 | for (size_t i=0;i<m.dimension;i++) {
|
---|
| 337 | ost << m.at(i);
|
---|
| 338 | if (i != 2)
|
---|
| 339 | ost << ",";
|
---|
| 340 | }
|
---|
| 341 | ost << ")";
|
---|
| 342 | return ost;
|
---|
| 343 | };
|
---|
| 344 |
|
---|
| 345 | /* ====================== Checking State ============================ */
|
---|
| 346 | /** Checks whether vector has all components zero.
|
---|
| 347 | * TODO: This might see some numerical instabilities for huge dimension and small number.
|
---|
| 348 | * For stability one should sort the order of summing!
|
---|
| 349 | * @return true - vector is zero, false - vector is not
|
---|
| 350 | */
|
---|
| 351 | bool VectorContent::IsZero() const
|
---|
| 352 | {
|
---|
| 353 | double result = 0.;
|
---|
| 354 | for (size_t i = dimension; i--; )
|
---|
| 355 | result += fabs(at(i));
|
---|
| 356 | return (result < MYEPSILON);
|
---|
| 357 | };
|
---|
| 358 |
|
---|
| 359 | /** Checks whether vector has length of 1.
|
---|
| 360 | * @return true - vector is normalized, false - vector is not
|
---|
| 361 | */
|
---|
| 362 | bool VectorContent::IsOne() const
|
---|
| 363 | {
|
---|
| 364 | double NormValue = 0.;
|
---|
| 365 | for (size_t i=dimension;--i;)
|
---|
| 366 | NormValue += at(i)*at(i);
|
---|
| 367 | return (fabs(NormValue - 1.) < MYEPSILON);
|
---|
| 368 | };
|
---|
| 369 |
|
---|