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