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