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