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