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