| 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 |  * gslvector.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Jan 8, 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 <cassert>
 | 
|---|
| 23 | #include <cmath>
 | 
|---|
| 24 | #include <iostream>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "LinearAlgebra/gslvector.hpp"
 | 
|---|
| 27 | #include "defs.hpp"
 | 
|---|
| 28 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 29 | #include "VectorContent.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | /** Constructor of class GSLVector.
 | 
|---|
| 32 |  * Allocates GSL structures
 | 
|---|
| 33 |  * \param m dimension of vector
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | GSLVector::GSLVector(size_t m) : dimension(m)
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   vector = gsl_vector_calloc(dimension);
 | 
|---|
| 38 | };
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /** Copy constructor of class GSLVector.
 | 
|---|
| 41 |  * Allocates GSL structures and copies components from \a *src.
 | 
|---|
| 42 |  * \param *src source vector
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | GSLVector::GSLVector(const GSLVector * const src) : dimension(src->dimension)
 | 
|---|
| 45 | {
 | 
|---|
| 46 |   vector = gsl_vector_alloc(dimension);
 | 
|---|
| 47 |   gsl_vector_memcpy (vector, src->vector);
 | 
|---|
| 48 | };
 | 
|---|
| 49 | 
 | 
|---|
| 50 | /** Copy constructor of class GSLVector.
 | 
|---|
| 51 |  * Allocates GSL structures and copies components from \a *src.
 | 
|---|
| 52 |  * \param *src source vector
 | 
|---|
| 53 |  */
 | 
|---|
| 54 | GSLVector::GSLVector(const GSLVector & src) : dimension(src.dimension)
 | 
|---|
| 55 | {
 | 
|---|
| 56 |   vector = gsl_vector_alloc(dimension);
 | 
|---|
| 57 |   gsl_vector_memcpy (vector, src.vector);
 | 
|---|
| 58 | };
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /** Destructor of class GSLVector.
 | 
|---|
| 61 |  * Frees GSL structures
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | GSLVector::~GSLVector()
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   gsl_vector_free(vector);
 | 
|---|
| 66 | };
 | 
|---|
| 67 | 
 | 
|---|
| 68 | /* ============================ Accessing =============================== */
 | 
|---|
| 69 | /** This function sets the vector from a double array.
 | 
|---|
| 70 |  * Creates a vector view of the array and performs a memcopy.
 | 
|---|
| 71 |  * \param *x array of values (no dimension check is performed)
 | 
|---|
| 72 |  */
 | 
|---|
| 73 | void GSLVector::SetFromDoubleArray(double * x)
 | 
|---|
| 74 | {
 | 
|---|
| 75 |   gsl_vector_view m = gsl_vector_view_array (x, dimension);
 | 
|---|
| 76 |   gsl_vector_memcpy (vector, &m.vector);
 | 
|---|
| 77 | };
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /**
 | 
|---|
| 80 |  * This function sets the GSLvector from an ordinary vector.
 | 
|---|
| 81 |  *
 | 
|---|
| 82 |  * Takes access to the internal gsl_vector and copies it
 | 
|---|
| 83 |  */
 | 
|---|
| 84 | void GSLVector::SetFromVector(Vector &v){
 | 
|---|
| 85 |   gsl_vector_memcpy (vector, v.get()->content);
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | /** This function returns the i-th element of a vector.
 | 
|---|
| 89 |  * If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked and 0 is returned.
 | 
|---|
| 90 |  * \param m m-th element
 | 
|---|
| 91 |  * \return  m-th element of vector
 | 
|---|
| 92 |  */
 | 
|---|
| 93 | double GSLVector::Get(size_t m) const
 | 
|---|
| 94 | {
 | 
|---|
| 95 |   return gsl_vector_get (vector, m);
 | 
|---|
| 96 | };
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /** This function sets the value of the \a m -th element of a vector to \a x.
 | 
|---|
| 99 |  *  If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked.
 | 
|---|
| 100 |  * \param m-th element to set
 | 
|---|
| 101 |  * \param x value to set to
 | 
|---|
| 102 |  */
 | 
|---|
| 103 | void GSLVector::Set(size_t m, double x)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   gsl_vector_set (vector, m, x);
 | 
|---|
| 106 | };
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /** These functions return a pointer to the \a m-th element of a vector.
 | 
|---|
| 109 |  *  If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked and a null pointer is returned.
 | 
|---|
| 110 |  * \param m m-th element
 | 
|---|
| 111 |  * \return pointer to \a m-th element
 | 
|---|
| 112 |  */
 | 
|---|
| 113 | double *GSLVector::Pointer(size_t m) const
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   return gsl_vector_ptr (vector, m);
 | 
|---|
| 116 | };
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /** These functions return a constant pointer to the \a m-th element of a vector.
 | 
|---|
| 119 |  *  If \a m lies outside the allowed range of 0 to GSLVector::dimension-1 then the error handler is invoked and a null pointer is returned.
 | 
|---|
| 120 |  * \param m m-th element
 | 
|---|
| 121 |  * \return const pointer to \a m-th element
 | 
|---|
| 122 |  */
 | 
|---|
| 123 | const double *GSLVector::const_Pointer(size_t m) const
 | 
|---|
| 124 | {
 | 
|---|
| 125 |   return gsl_vector_const_ptr (vector, m);
 | 
|---|
| 126 | };
 | 
|---|
| 127 | 
 | 
|---|
| 128 | /** Returns the dimension of the vector.
 | 
|---|
| 129 |  * \return dimension of vector
 | 
|---|
| 130 |  */
 | 
|---|
| 131 | #ifdef HAVE_INLINE
 | 
|---|
| 132 | inline
 | 
|---|
| 133 | #endif
 | 
|---|
| 134 | size_t GSLVector::GetDimension() const
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   return dimension;
 | 
|---|
| 137 | };
 | 
|---|
| 138 | 
 | 
|---|
| 139 | /* ========================== Initializing =============================== */
 | 
|---|
| 140 | /** This function sets all the elements of the vector to the value \a x.
 | 
|---|
| 141 |  * \param *x
 | 
|---|
| 142 |  */
 | 
|---|
| 143 | void GSLVector::SetAll(double x)
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   gsl_vector_set_all (vector, x);
 | 
|---|
| 146 | };
 | 
|---|
| 147 | 
 | 
|---|
| 148 | /** This function sets all the elements of the vector to zero.
 | 
|---|
| 149 |  */
 | 
|---|
| 150 | void GSLVector::SetZero()
 | 
|---|
| 151 | {
 | 
|---|
| 152 |   gsl_vector_set_zero (vector);
 | 
|---|
| 153 | };
 | 
|---|
| 154 | 
 | 
|---|
| 155 | /** 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.
 | 
|---|
| 156 |  * \param i i-th component to set to unity (all other to zero)
 | 
|---|
| 157 |  * \return vector set
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | int GSLVector::SetBasis(size_t i)
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   return gsl_vector_set_basis (vector, i);
 | 
|---|
| 162 | };
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /* ====================== Exchanging elements ============================ */
 | 
|---|
| 165 | /** This function exchanges the \a i-th and \a j-th elements of the vector in-place.
 | 
|---|
| 166 |  * \param i i-th element to swap with ...
 | 
|---|
| 167 |  * \param j ... j-th element to swap against
 | 
|---|
| 168 |  */
 | 
|---|
| 169 | int GSLVector::SwapElements(size_t i, size_t j)
 | 
|---|
| 170 | {
 | 
|---|
| 171 |   return gsl_vector_swap_elements (vector, i, j);
 | 
|---|
| 172 | };
 | 
|---|
| 173 | 
 | 
|---|
| 174 | /** This function reverses the order of the elements of the vector.
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | int GSLVector::Reverse()
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   return gsl_vector_reverse (vector);
 | 
|---|
| 179 | };
 | 
|---|
| 180 | 
 | 
|---|
| 181 | 
 | 
|---|
| 182 | /* ========================== Operators =============================== */
 | 
|---|
| 183 | /** Compares GSLVector \a to GSLVector \a b component-wise.
 | 
|---|
| 184 |  * \param a base GSLVector
 | 
|---|
| 185 |  * \param b GSLVector components to add
 | 
|---|
| 186 |  * \return a == b
 | 
|---|
| 187 |  */
 | 
|---|
| 188 | bool operator==(const GSLVector& a, const GSLVector& b)
 | 
|---|
| 189 | {
 | 
|---|
| 190 |   bool status = true;
 | 
|---|
| 191 |   assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
 | 
|---|
| 192 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 193 |     status = status && (fabs(a.Get(i) - b.Get(i)) < MYEPSILON);
 | 
|---|
| 194 |   return status;
 | 
|---|
| 195 | };
 | 
|---|
| 196 | 
 | 
|---|
| 197 | /** Sums GSLVector \a to this lhs component-wise.
 | 
|---|
| 198 |  * \param a base GSLVector
 | 
|---|
| 199 |  * \param b GSLVector components to add
 | 
|---|
| 200 |  * \return lhs + a
 | 
|---|
| 201 |  */
 | 
|---|
| 202 | const GSLVector& operator+=(GSLVector& a, const GSLVector& b)
 | 
|---|
| 203 | {
 | 
|---|
| 204 |   assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
 | 
|---|
| 205 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 206 |     a.Set(i,a.Get(i)+b.Get(i));
 | 
|---|
| 207 |   return a;
 | 
|---|
| 208 | };
 | 
|---|
| 209 | 
 | 
|---|
| 210 | /** Subtracts GSLVector \a from this lhs component-wise.
 | 
|---|
| 211 |  * \param a base GSLVector
 | 
|---|
| 212 |  * \param b GSLVector components to add
 | 
|---|
| 213 |  * \return lhs - a
 | 
|---|
| 214 |  */
 | 
|---|
| 215 | const GSLVector& operator-=(GSLVector& a, const GSLVector& b)
 | 
|---|
| 216 | {
 | 
|---|
| 217 |   assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
 | 
|---|
| 218 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 219 |     a.Set(i,a.Get(i)-b.Get(i));
 | 
|---|
| 220 |   return a;
 | 
|---|
| 221 | };
 | 
|---|
| 222 | 
 | 
|---|
| 223 | /** factor each component of \a a times a double \a m.
 | 
|---|
| 224 |  * \param a base GSLVector
 | 
|---|
| 225 |  * \param m factor
 | 
|---|
| 226 |  * \return lhs.Get(i) * m
 | 
|---|
| 227 |  */
 | 
|---|
| 228 | const GSLVector& operator*=(GSLVector& a, const double m)
 | 
|---|
| 229 | {
 | 
|---|
| 230 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 231 |     a.Set(i,a.Get(i)*m);
 | 
|---|
| 232 |   return a;
 | 
|---|
| 233 | };
 | 
|---|
| 234 | 
 | 
|---|
| 235 | /** Sums two GSLVectors \a  and \b component-wise.
 | 
|---|
| 236 |  * \param a first GSLVector
 | 
|---|
| 237 |  * \param b second GSLVector
 | 
|---|
| 238 |  * \return a + b
 | 
|---|
| 239 |  */
 | 
|---|
| 240 | GSLVector const operator+(const GSLVector& a, const GSLVector& b)
 | 
|---|
| 241 | {
 | 
|---|
| 242 |   GSLVector x(a);
 | 
|---|
| 243 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 244 |     x.Set(i,a.Get(i)+b.Get(i));
 | 
|---|
| 245 |   return x;
 | 
|---|
| 246 | };
 | 
|---|
| 247 | 
 | 
|---|
| 248 | /** Subtracts GSLVector \a from \b component-wise.
 | 
|---|
| 249 |  * \param a first GSLVector
 | 
|---|
| 250 |  * \param b second GSLVector
 | 
|---|
| 251 |  * \return a - b
 | 
|---|
| 252 |  */
 | 
|---|
| 253 | GSLVector const operator-(const GSLVector& a, const GSLVector& b)
 | 
|---|
| 254 | {
 | 
|---|
| 255 |   assert(a.GetDimension() == b.GetDimension() && "Dimenions of GSLVectors to compare differ");
 | 
|---|
| 256 |   GSLVector x(a);
 | 
|---|
| 257 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 258 |     x.Set(i,a.Get(i)-b.Get(i));
 | 
|---|
| 259 |   return x;
 | 
|---|
| 260 | };
 | 
|---|
| 261 | 
 | 
|---|
| 262 | /** Factors given GSLVector \a a times \a m.
 | 
|---|
| 263 |  * \param a GSLVector
 | 
|---|
| 264 |  * \param m factor
 | 
|---|
| 265 |  * \return m * a
 | 
|---|
| 266 |  */
 | 
|---|
| 267 | GSLVector const operator*(const GSLVector& a, const double m)
 | 
|---|
| 268 | {
 | 
|---|
| 269 |   GSLVector x(a);
 | 
|---|
| 270 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 271 |     x.Set(i,a.Get(i)*m);
 | 
|---|
| 272 |   return x;
 | 
|---|
| 273 | };
 | 
|---|
| 274 | 
 | 
|---|
| 275 | /** Factors given GSLVector \a a times \a m.
 | 
|---|
| 276 |  * \param m factor
 | 
|---|
| 277 |  * \param a GSLVector
 | 
|---|
| 278 |  * \return m * a
 | 
|---|
| 279 |  */
 | 
|---|
| 280 | GSLVector const operator*(const double m, const GSLVector& a )
 | 
|---|
| 281 | {
 | 
|---|
| 282 |   GSLVector x(a);
 | 
|---|
| 283 |   for (size_t i=0;i<a.GetDimension();i++)
 | 
|---|
| 284 |     x.Set(i,a.Get(i)*m);
 | 
|---|
| 285 |   return x;
 | 
|---|
| 286 | };
 | 
|---|
| 287 | 
 | 
|---|
| 288 | ostream& operator<<(ostream& ost, const GSLVector& m)
 | 
|---|
| 289 | {
 | 
|---|
| 290 |   ost << "(";
 | 
|---|
| 291 |   for (size_t i=0;i<m.GetDimension();i++) {
 | 
|---|
| 292 |     ost << m.Get(i);
 | 
|---|
| 293 |     if (i != 2)
 | 
|---|
| 294 |       ost << ",";
 | 
|---|
| 295 |   }
 | 
|---|
| 296 |   ost << ")";
 | 
|---|
| 297 |   return ost;
 | 
|---|
| 298 | };
 | 
|---|
| 299 | 
 | 
|---|
| 300 | /* ====================== Checking State ============================ */
 | 
|---|
| 301 | /** Checks whether vector has all components zero.
 | 
|---|
| 302 |  * @return true - vector is zero, false - vector is not
 | 
|---|
| 303 |  */
 | 
|---|
| 304 | bool GSLVector::IsZero() const
 | 
|---|
| 305 | {
 | 
|---|
| 306 |   return (fabs(Get(0))+fabs(Get(1))+fabs(Get(2)) < MYEPSILON);
 | 
|---|
| 307 | };
 | 
|---|
| 308 | 
 | 
|---|
| 309 | /** Checks whether vector has length of 1.
 | 
|---|
| 310 |  * @return true - vector is normalized, false - vector is not
 | 
|---|
| 311 |  */
 | 
|---|
| 312 | bool GSLVector::IsOne() const
 | 
|---|
| 313 | {
 | 
|---|
| 314 |   double NormValue = 0.;
 | 
|---|
| 315 |   for (size_t i=dimension;--i;)
 | 
|---|
| 316 |     NormValue += Get(i)*Get(i);
 | 
|---|
| 317 |   return (fabs(NormValue - 1.) < MYEPSILON);
 | 
|---|
| 318 | };
 | 
|---|
| 319 | 
 | 
|---|