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