| [6ac7ee] | 1 | /** \file vector.cpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Function implementations for the class vector. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | */ | 
|---|
|  | 6 |  | 
|---|
| [bf3817] | 7 | // include config.h | 
|---|
|  | 8 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 9 | #include <config.h> | 
|---|
|  | 10 | #endif | 
|---|
|  | 11 |  | 
|---|
| [112b09] | 12 | #include "Helpers/MemDebug.hpp" | 
|---|
| [edb93c] | 13 |  | 
|---|
| [57f243] | 14 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [ce3d2b] | 15 | #include "VectorContent.hpp" | 
|---|
| [952f38] | 16 | #include "Helpers/Verbose.hpp" | 
|---|
| [b34306] | 17 | #include "World.hpp" | 
|---|
| [0a4f7f] | 18 | #include "Helpers/Assert.hpp" | 
|---|
| [753f02] | 19 | #include "Helpers/fast_functions.hpp" | 
|---|
| [325390] | 20 | #include "Exceptions/MathException.hpp" | 
|---|
| [6ac7ee] | 21 |  | 
|---|
| [1bd79e] | 22 | #include <iostream> | 
|---|
| [923b6c] | 23 | #include <gsl/gsl_blas.h> | 
|---|
| [a439e5] | 24 | #include <gsl/gsl_vector.h> | 
|---|
| [923b6c] | 25 |  | 
|---|
| [1bd79e] | 26 |  | 
|---|
|  | 27 | using namespace std; | 
|---|
| [6ac7ee] | 28 |  | 
|---|
| [97498a] | 29 |  | 
|---|
| [6ac7ee] | 30 | /************************************ Functions for class vector ************************************/ | 
|---|
|  | 31 |  | 
|---|
|  | 32 | /** Constructor of class vector. | 
|---|
|  | 33 | */ | 
|---|
| [753f02] | 34 | Vector::Vector() | 
|---|
|  | 35 | { | 
|---|
| [ce3d2b] | 36 | content = new VectorContent(); | 
|---|
| [753f02] | 37 | }; | 
|---|
| [6ac7ee] | 38 |  | 
|---|
| [753f02] | 39 | /** | 
|---|
|  | 40 | * Copy constructor | 
|---|
| [821907] | 41 | */ | 
|---|
| [1bd79e] | 42 |  | 
|---|
| [753f02] | 43 | Vector::Vector(const Vector& src) | 
|---|
| [821907] | 44 | { | 
|---|
| [ce3d2b] | 45 | content = new VectorContent(); | 
|---|
|  | 46 | gsl_vector_memcpy(content->content, src.content->content); | 
|---|
| [1bd79e] | 47 | } | 
|---|
| [821907] | 48 |  | 
|---|
|  | 49 | /** Constructor of class vector. | 
|---|
|  | 50 | */ | 
|---|
| [753f02] | 51 | Vector::Vector(const double x1, const double x2, const double x3) | 
|---|
| [821907] | 52 | { | 
|---|
| [ce3d2b] | 53 | content = new VectorContent(); | 
|---|
|  | 54 | gsl_vector_set(content->content,0,x1); | 
|---|
|  | 55 | gsl_vector_set(content->content,1,x2); | 
|---|
|  | 56 | gsl_vector_set(content->content,2,x3); | 
|---|
| [821907] | 57 | }; | 
|---|
|  | 58 |  | 
|---|
| [d74077] | 59 | /** Constructor of class vector. | 
|---|
|  | 60 | */ | 
|---|
|  | 61 | Vector::Vector(const double x[3]) | 
|---|
|  | 62 | { | 
|---|
|  | 63 | content = new VectorContent(); | 
|---|
|  | 64 | gsl_vector_set(content->content,0,x[0]); | 
|---|
|  | 65 | gsl_vector_set(content->content,1,x[1]); | 
|---|
|  | 66 | gsl_vector_set(content->content,2,x[2]); | 
|---|
|  | 67 | }; | 
|---|
|  | 68 |  | 
|---|
| [ce3d2b] | 69 | Vector::Vector(VectorContent *_content) : | 
|---|
| [325390] | 70 | content(_content) | 
|---|
|  | 71 | {} | 
|---|
|  | 72 |  | 
|---|
| [0a4f7f] | 73 | /** | 
|---|
|  | 74 | * Assignment operator | 
|---|
| [6ac7ee] | 75 | */ | 
|---|
| [0a4f7f] | 76 | Vector& Vector::operator=(const Vector& src){ | 
|---|
|  | 77 | // check for self assignment | 
|---|
|  | 78 | if(&src!=this){ | 
|---|
| [ce3d2b] | 79 | gsl_vector_memcpy(content->content, src.content->content); | 
|---|
| [0a4f7f] | 80 | } | 
|---|
|  | 81 | return *this; | 
|---|
|  | 82 | } | 
|---|
| [6ac7ee] | 83 |  | 
|---|
|  | 84 | /** Desctructor of class vector. | 
|---|
|  | 85 | */ | 
|---|
| [d466f0] | 86 | Vector::~Vector() { | 
|---|
| [ce3d2b] | 87 | delete content; | 
|---|
| [d466f0] | 88 | }; | 
|---|
| [6ac7ee] | 89 |  | 
|---|
|  | 90 | /** Calculates square of distance between this and another vector. | 
|---|
|  | 91 | * \param *y array to second vector | 
|---|
|  | 92 | * \return \f$| x - y |^2\f$ | 
|---|
|  | 93 | */ | 
|---|
| [273382] | 94 | double Vector::DistanceSquared(const Vector &y) const | 
|---|
| [6ac7ee] | 95 | { | 
|---|
| [042f82] | 96 | double res = 0.; | 
|---|
|  | 97 | for (int i=NDIM;i--;) | 
|---|
| [d466f0] | 98 | res += (at(i)-y[i])*(at(i)-y[i]); | 
|---|
| [042f82] | 99 | return (res); | 
|---|
| [6ac7ee] | 100 | }; | 
|---|
|  | 101 |  | 
|---|
|  | 102 | /** Calculates distance between this and another vector. | 
|---|
|  | 103 | * \param *y array to second vector | 
|---|
|  | 104 | * \return \f$| x - y |\f$ | 
|---|
|  | 105 | */ | 
|---|
| [1513a74] | 106 | double Vector::distance(const Vector &y) const | 
|---|
| [6ac7ee] | 107 | { | 
|---|
| [273382] | 108 | return (sqrt(DistanceSquared(y))); | 
|---|
| [6ac7ee] | 109 | }; | 
|---|
|  | 110 |  | 
|---|
| [a439e5] | 111 | size_t Vector::GreatestComponent() const | 
|---|
|  | 112 | { | 
|---|
|  | 113 | int greatest = 0; | 
|---|
|  | 114 | for (int i=1;i<NDIM;i++) { | 
|---|
|  | 115 | if (at(i) > at(greatest)) | 
|---|
|  | 116 | greatest = i; | 
|---|
|  | 117 | } | 
|---|
|  | 118 | return greatest; | 
|---|
|  | 119 | } | 
|---|
|  | 120 |  | 
|---|
|  | 121 | size_t Vector::SmallestComponent() const | 
|---|
|  | 122 | { | 
|---|
|  | 123 | int smallest = 0; | 
|---|
|  | 124 | for (int i=1;i<NDIM;i++) { | 
|---|
|  | 125 | if (at(i) < at(smallest)) | 
|---|
|  | 126 | smallest = i; | 
|---|
|  | 127 | } | 
|---|
|  | 128 | return smallest; | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
|  | 131 |  | 
|---|
| [1513a74] | 132 | Vector Vector::getClosestPoint(const Vector &point) const{ | 
|---|
|  | 133 | // the closest point to a single point space is always the single point itself | 
|---|
|  | 134 | return *this; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [6ac7ee] | 137 | /** Calculates scalar product between this and another vector. | 
|---|
|  | 138 | * \param *y array to second vector | 
|---|
|  | 139 | * \return \f$\langle x, y \rangle\f$ | 
|---|
|  | 140 | */ | 
|---|
| [273382] | 141 | double Vector::ScalarProduct(const Vector &y) const | 
|---|
| [6ac7ee] | 142 | { | 
|---|
| [042f82] | 143 | double res = 0.; | 
|---|
| [ce3d2b] | 144 | gsl_blas_ddot(content->content, y.content->content, &res); | 
|---|
| [042f82] | 145 | return (res); | 
|---|
| [6ac7ee] | 146 | }; | 
|---|
|  | 147 |  | 
|---|
|  | 148 |  | 
|---|
|  | 149 | /** Calculates VectorProduct between this and another vector. | 
|---|
| [042f82] | 150 | *  -# returns the Product in place of vector from which it was initiated | 
|---|
|  | 151 | *  -# ATTENTION: Only three dim. | 
|---|
|  | 152 | *  \param *y array to vector with which to calculate crossproduct | 
|---|
|  | 153 | *  \return \f$ x \times y \f& | 
|---|
| [6ac7ee] | 154 | */ | 
|---|
| [273382] | 155 | void Vector::VectorProduct(const Vector &y) | 
|---|
| [6ac7ee] | 156 | { | 
|---|
| [042f82] | 157 | Vector tmp; | 
|---|
| [d466f0] | 158 | for(int i=NDIM;i--;) | 
|---|
|  | 159 | tmp[i] = at((i+1)%NDIM)*y[(i+2)%NDIM] - at((i+2)%NDIM)*y[(i+1)%NDIM]; | 
|---|
| [753f02] | 160 | (*this) = tmp; | 
|---|
| [6ac7ee] | 161 | }; | 
|---|
|  | 162 |  | 
|---|
|  | 163 |  | 
|---|
|  | 164 | /** projects this vector onto plane defined by \a *y. | 
|---|
|  | 165 | * \param *y normal vector of plane | 
|---|
|  | 166 | * \return \f$\langle x, y \rangle\f$ | 
|---|
|  | 167 | */ | 
|---|
| [273382] | 168 | void Vector::ProjectOntoPlane(const Vector &y) | 
|---|
| [6ac7ee] | 169 | { | 
|---|
| [042f82] | 170 | Vector tmp; | 
|---|
| [753f02] | 171 | tmp = y; | 
|---|
| [042f82] | 172 | tmp.Normalize(); | 
|---|
| [753f02] | 173 | tmp.Scale(ScalarProduct(tmp)); | 
|---|
|  | 174 | *this -= tmp; | 
|---|
| [2319ed] | 175 | }; | 
|---|
|  | 176 |  | 
|---|
| [821907] | 177 | /** Calculates the minimum distance of this vector to the plane. | 
|---|
|  | 178 | * \sa Vector::GetDistanceVectorToPlane() | 
|---|
|  | 179 | * \param *out output stream for debugging | 
|---|
|  | 180 | * \param *PlaneNormal normal of plane | 
|---|
|  | 181 | * \param *PlaneOffset offset of plane | 
|---|
|  | 182 | * \return distance to plane | 
|---|
|  | 183 | */ | 
|---|
| [d4c9ae] | 184 | double Vector::DistanceToSpace(const Space &space) const | 
|---|
| [821907] | 185 | { | 
|---|
| [d4c9ae] | 186 | return space.distance(*this); | 
|---|
| [c4d4df] | 187 | }; | 
|---|
|  | 188 |  | 
|---|
| [6ac7ee] | 189 | /** Calculates the projection of a vector onto another \a *y. | 
|---|
|  | 190 | * \param *y array to second vector | 
|---|
|  | 191 | */ | 
|---|
| [273382] | 192 | void Vector::ProjectIt(const Vector &y) | 
|---|
| [6ac7ee] | 193 | { | 
|---|
| [753f02] | 194 | (*this) += (-ScalarProduct(y))*y; | 
|---|
| [ef9df36] | 195 | }; | 
|---|
|  | 196 |  | 
|---|
|  | 197 | /** Calculates the projection of a vector onto another \a *y. | 
|---|
|  | 198 | * \param *y array to second vector | 
|---|
|  | 199 | * \return Vector | 
|---|
|  | 200 | */ | 
|---|
| [273382] | 201 | Vector Vector::Projection(const Vector &y) const | 
|---|
| [ef9df36] | 202 | { | 
|---|
| [753f02] | 203 | Vector helper = y; | 
|---|
|  | 204 | helper.Scale((ScalarProduct(y)/y.NormSquared())); | 
|---|
| [ef9df36] | 205 |  | 
|---|
|  | 206 | return helper; | 
|---|
| [6ac7ee] | 207 | }; | 
|---|
|  | 208 |  | 
|---|
|  | 209 | /** Calculates norm of this vector. | 
|---|
|  | 210 | * \return \f$|x|\f$ | 
|---|
|  | 211 | */ | 
|---|
|  | 212 | double Vector::Norm() const | 
|---|
|  | 213 | { | 
|---|
| [273382] | 214 | return (sqrt(NormSquared())); | 
|---|
| [6ac7ee] | 215 | }; | 
|---|
|  | 216 |  | 
|---|
| [d4d0dd] | 217 | /** Calculates squared norm of this vector. | 
|---|
|  | 218 | * \return \f$|x|^2\f$ | 
|---|
|  | 219 | */ | 
|---|
|  | 220 | double Vector::NormSquared() const | 
|---|
|  | 221 | { | 
|---|
| [273382] | 222 | return (ScalarProduct(*this)); | 
|---|
| [d4d0dd] | 223 | }; | 
|---|
|  | 224 |  | 
|---|
| [6ac7ee] | 225 | /** Normalizes this vector. | 
|---|
|  | 226 | */ | 
|---|
|  | 227 | void Vector::Normalize() | 
|---|
|  | 228 | { | 
|---|
| [1bd79e] | 229 | double factor = Norm(); | 
|---|
|  | 230 | (*this) *= 1/factor; | 
|---|
| [6ac7ee] | 231 | }; | 
|---|
|  | 232 |  | 
|---|
|  | 233 | /** Zeros all components of this vector. | 
|---|
|  | 234 | */ | 
|---|
|  | 235 | void Vector::Zero() | 
|---|
|  | 236 | { | 
|---|
| [753f02] | 237 | at(0)=at(1)=at(2)=0; | 
|---|
| [6ac7ee] | 238 | }; | 
|---|
|  | 239 |  | 
|---|
|  | 240 | /** Zeros all components of this vector. | 
|---|
|  | 241 | */ | 
|---|
| [776b64] | 242 | void Vector::One(const double one) | 
|---|
| [6ac7ee] | 243 | { | 
|---|
| [753f02] | 244 | at(0)=at(1)=at(2)=one; | 
|---|
| [6ac7ee] | 245 | }; | 
|---|
|  | 246 |  | 
|---|
| [9c20aa] | 247 | /** Checks whether vector has all components zero. | 
|---|
|  | 248 | * @return true - vector is zero, false - vector is not | 
|---|
|  | 249 | */ | 
|---|
| [54a746] | 250 | bool Vector::IsZero() const | 
|---|
| [9c20aa] | 251 | { | 
|---|
| [d466f0] | 252 | return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON); | 
|---|
| [54a746] | 253 | }; | 
|---|
|  | 254 |  | 
|---|
|  | 255 | /** Checks whether vector has length of 1. | 
|---|
|  | 256 | * @return true - vector is normalized, false - vector is not | 
|---|
|  | 257 | */ | 
|---|
|  | 258 | bool Vector::IsOne() const | 
|---|
|  | 259 | { | 
|---|
|  | 260 | return (fabs(Norm() - 1.) < MYEPSILON); | 
|---|
| [9c20aa] | 261 | }; | 
|---|
|  | 262 |  | 
|---|
| [ef9df36] | 263 | /** Checks whether vector is normal to \a *normal. | 
|---|
|  | 264 | * @return true - vector is normalized, false - vector is not | 
|---|
|  | 265 | */ | 
|---|
| [273382] | 266 | bool Vector::IsNormalTo(const Vector &normal) const | 
|---|
| [ef9df36] | 267 | { | 
|---|
|  | 268 | if (ScalarProduct(normal) < MYEPSILON) | 
|---|
|  | 269 | return true; | 
|---|
|  | 270 | else | 
|---|
|  | 271 | return false; | 
|---|
|  | 272 | }; | 
|---|
|  | 273 |  | 
|---|
| [b998c3] | 274 | /** Checks whether vector is normal to \a *normal. | 
|---|
|  | 275 | * @return true - vector is normalized, false - vector is not | 
|---|
|  | 276 | */ | 
|---|
| [273382] | 277 | bool Vector::IsEqualTo(const Vector &a) const | 
|---|
| [b998c3] | 278 | { | 
|---|
|  | 279 | bool status = true; | 
|---|
|  | 280 | for (int i=0;i<NDIM;i++) { | 
|---|
| [d466f0] | 281 | if (fabs(at(i) - a[i]) > MYEPSILON) | 
|---|
| [b998c3] | 282 | status = false; | 
|---|
|  | 283 | } | 
|---|
|  | 284 | return status; | 
|---|
|  | 285 | }; | 
|---|
|  | 286 |  | 
|---|
| [6ac7ee] | 287 | /** Calculates the angle between this and another vector. | 
|---|
|  | 288 | * \param *y array to second vector | 
|---|
|  | 289 | * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$ | 
|---|
|  | 290 | */ | 
|---|
| [273382] | 291 | double Vector::Angle(const Vector &y) const | 
|---|
| [6ac7ee] | 292 | { | 
|---|
| [753f02] | 293 | double norm1 = Norm(), norm2 = y.Norm(); | 
|---|
| [ef9df36] | 294 | double angle = -1; | 
|---|
| [d4d0dd] | 295 | if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON)) | 
|---|
|  | 296 | angle = this->ScalarProduct(y)/norm1/norm2; | 
|---|
| [02da9e] | 297 | // -1-MYEPSILON occured due to numerical imprecision, catch ... | 
|---|
| [e138de] | 298 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl; | 
|---|
| [02da9e] | 299 | if (angle < -1) | 
|---|
|  | 300 | angle = -1; | 
|---|
|  | 301 | if (angle > 1) | 
|---|
|  | 302 | angle = 1; | 
|---|
| [042f82] | 303 | return acos(angle); | 
|---|
| [6ac7ee] | 304 | }; | 
|---|
|  | 305 |  | 
|---|
| [0a4f7f] | 306 |  | 
|---|
|  | 307 | double& Vector::operator[](size_t i){ | 
|---|
| [753f02] | 308 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range"); | 
|---|
| [ce3d2b] | 309 | return *gsl_vector_ptr (content->content, i); | 
|---|
| [0a4f7f] | 310 | } | 
|---|
|  | 311 |  | 
|---|
|  | 312 | const double& Vector::operator[](size_t i) const{ | 
|---|
| [753f02] | 313 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range"); | 
|---|
| [ce3d2b] | 314 | return *gsl_vector_ptr (content->content, i); | 
|---|
| [0a4f7f] | 315 | } | 
|---|
|  | 316 |  | 
|---|
|  | 317 | double& Vector::at(size_t i){ | 
|---|
|  | 318 | return (*this)[i]; | 
|---|
|  | 319 | } | 
|---|
|  | 320 |  | 
|---|
|  | 321 | const double& Vector::at(size_t i) const{ | 
|---|
|  | 322 | return (*this)[i]; | 
|---|
|  | 323 | } | 
|---|
|  | 324 |  | 
|---|
| [ce3d2b] | 325 | VectorContent* Vector::get(){ | 
|---|
| [0c7ed8] | 326 | return content; | 
|---|
| [0a4f7f] | 327 | } | 
|---|
| [6ac7ee] | 328 |  | 
|---|
| [ef9df36] | 329 | /** Compares vector \a to vector \a b component-wise. | 
|---|
|  | 330 | * \param a base vector | 
|---|
|  | 331 | * \param b vector components to add | 
|---|
|  | 332 | * \return a == b | 
|---|
|  | 333 | */ | 
|---|
| [72e7fa] | 334 | bool Vector::operator==(const Vector& b) const | 
|---|
| [ef9df36] | 335 | { | 
|---|
| [1bd79e] | 336 | return IsEqualTo(b); | 
|---|
| [ef9df36] | 337 | }; | 
|---|
|  | 338 |  | 
|---|
| [fa5a6a] | 339 | bool Vector::operator!=(const Vector& b) const | 
|---|
|  | 340 | { | 
|---|
|  | 341 | return !IsEqualTo(b); | 
|---|
|  | 342 | } | 
|---|
|  | 343 |  | 
|---|
| [6ac7ee] | 344 | /** Sums vector \a to this lhs component-wise. | 
|---|
|  | 345 | * \param a base vector | 
|---|
|  | 346 | * \param b vector components to add | 
|---|
|  | 347 | * \return lhs + a | 
|---|
|  | 348 | */ | 
|---|
| [72e7fa] | 349 | const Vector& Vector::operator+=(const Vector& b) | 
|---|
| [6ac7ee] | 350 | { | 
|---|
| [273382] | 351 | this->AddVector(b); | 
|---|
| [72e7fa] | 352 | return *this; | 
|---|
| [6ac7ee] | 353 | }; | 
|---|
| [54a746] | 354 |  | 
|---|
|  | 355 | /** Subtracts vector \a from this lhs component-wise. | 
|---|
|  | 356 | * \param a base vector | 
|---|
|  | 357 | * \param b vector components to add | 
|---|
|  | 358 | * \return lhs - a | 
|---|
|  | 359 | */ | 
|---|
| [72e7fa] | 360 | const Vector& Vector::operator-=(const Vector& b) | 
|---|
| [54a746] | 361 | { | 
|---|
| [273382] | 362 | this->SubtractVector(b); | 
|---|
| [72e7fa] | 363 | return *this; | 
|---|
| [54a746] | 364 | }; | 
|---|
|  | 365 |  | 
|---|
| [6ac7ee] | 366 | /** factor each component of \a a times a double \a m. | 
|---|
|  | 367 | * \param a base vector | 
|---|
|  | 368 | * \param m factor | 
|---|
|  | 369 | * \return lhs.x[i] * m | 
|---|
|  | 370 | */ | 
|---|
| [b84d5d] | 371 | const Vector& operator*=(Vector& a, const double m) | 
|---|
| [6ac7ee] | 372 | { | 
|---|
| [042f82] | 373 | a.Scale(m); | 
|---|
|  | 374 | return a; | 
|---|
| [6ac7ee] | 375 | }; | 
|---|
|  | 376 |  | 
|---|
| [042f82] | 377 | /** Sums two vectors \a  and \b component-wise. | 
|---|
| [6ac7ee] | 378 | * \param a first vector | 
|---|
|  | 379 | * \param b second vector | 
|---|
|  | 380 | * \return a + b | 
|---|
|  | 381 | */ | 
|---|
| [72e7fa] | 382 | Vector const Vector::operator+(const Vector& b) const | 
|---|
| [6ac7ee] | 383 | { | 
|---|
| [72e7fa] | 384 | Vector x = *this; | 
|---|
| [273382] | 385 | x.AddVector(b); | 
|---|
| [b84d5d] | 386 | return x; | 
|---|
| [6ac7ee] | 387 | }; | 
|---|
|  | 388 |  | 
|---|
| [54a746] | 389 | /** Subtracts vector \a from \b component-wise. | 
|---|
|  | 390 | * \param a first vector | 
|---|
|  | 391 | * \param b second vector | 
|---|
|  | 392 | * \return a - b | 
|---|
|  | 393 | */ | 
|---|
| [72e7fa] | 394 | Vector const Vector::operator-(const Vector& b) const | 
|---|
| [54a746] | 395 | { | 
|---|
| [72e7fa] | 396 | Vector x = *this; | 
|---|
| [273382] | 397 | x.SubtractVector(b); | 
|---|
| [b84d5d] | 398 | return x; | 
|---|
| [54a746] | 399 | }; | 
|---|
|  | 400 |  | 
|---|
| [6ac7ee] | 401 | /** Factors given vector \a a times \a m. | 
|---|
|  | 402 | * \param a vector | 
|---|
|  | 403 | * \param m factor | 
|---|
| [54a746] | 404 | * \return m * a | 
|---|
| [6ac7ee] | 405 | */ | 
|---|
| [b84d5d] | 406 | Vector const operator*(const Vector& a, const double m) | 
|---|
| [6ac7ee] | 407 | { | 
|---|
| [b84d5d] | 408 | Vector x(a); | 
|---|
|  | 409 | x.Scale(m); | 
|---|
|  | 410 | return x; | 
|---|
| [6ac7ee] | 411 | }; | 
|---|
|  | 412 |  | 
|---|
| [54a746] | 413 | /** Factors given vector \a a times \a m. | 
|---|
|  | 414 | * \param m factor | 
|---|
|  | 415 | * \param a vector | 
|---|
|  | 416 | * \return m * a | 
|---|
|  | 417 | */ | 
|---|
| [b84d5d] | 418 | Vector const operator*(const double m, const Vector& a ) | 
|---|
| [54a746] | 419 | { | 
|---|
| [b84d5d] | 420 | Vector x(a); | 
|---|
|  | 421 | x.Scale(m); | 
|---|
|  | 422 | return x; | 
|---|
| [54a746] | 423 | }; | 
|---|
|  | 424 |  | 
|---|
| [9c20aa] | 425 | ostream& operator<<(ostream& ost, const Vector& m) | 
|---|
| [6ac7ee] | 426 | { | 
|---|
| [042f82] | 427 | ost << "("; | 
|---|
|  | 428 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 429 | ost << m[i]; | 
|---|
| [042f82] | 430 | if (i != 2) | 
|---|
|  | 431 | ost << ","; | 
|---|
|  | 432 | } | 
|---|
|  | 433 | ost << ")"; | 
|---|
|  | 434 | return ost; | 
|---|
| [6ac7ee] | 435 | }; | 
|---|
|  | 436 |  | 
|---|
|  | 437 |  | 
|---|
| [1bd79e] | 438 | void Vector::ScaleAll(const double *factor) | 
|---|
| [6ac7ee] | 439 | { | 
|---|
| [042f82] | 440 | for (int i=NDIM;i--;) | 
|---|
| [d466f0] | 441 | at(i) *= factor[i]; | 
|---|
| [6ac7ee] | 442 | }; | 
|---|
|  | 443 |  | 
|---|
| [b5bf84] | 444 | void Vector::ScaleAll(const Vector &factor){ | 
|---|
| [ce3d2b] | 445 | gsl_vector_mul(content->content, factor.content->content); | 
|---|
| [b5bf84] | 446 | } | 
|---|
| [6ac7ee] | 447 |  | 
|---|
| [1bd79e] | 448 |  | 
|---|
| [776b64] | 449 | void Vector::Scale(const double factor) | 
|---|
| [6ac7ee] | 450 | { | 
|---|
| [ce3d2b] | 451 | gsl_vector_scale(content->content,factor); | 
|---|
| [6ac7ee] | 452 | }; | 
|---|
|  | 453 |  | 
|---|
| [45ef76] | 454 | std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{ | 
|---|
|  | 455 | double factor = ScalarProduct(rhs)/rhs.NormSquared(); | 
|---|
|  | 456 | Vector res= factor * rhs; | 
|---|
|  | 457 | return make_pair(res,(*this)-res); | 
|---|
|  | 458 | } | 
|---|
|  | 459 |  | 
|---|
|  | 460 | std::pair<pointset,Vector> Vector::partition(const pointset &points) const{ | 
|---|
|  | 461 | Vector helper = *this; | 
|---|
|  | 462 | pointset res; | 
|---|
|  | 463 | for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){ | 
|---|
|  | 464 | pair<Vector,Vector> currPart = helper.partition(*iter); | 
|---|
|  | 465 | res.push_back(currPart.first); | 
|---|
|  | 466 | helper = currPart.second; | 
|---|
|  | 467 | } | 
|---|
|  | 468 | return make_pair(res,helper); | 
|---|
|  | 469 | } | 
|---|
|  | 470 |  | 
|---|
| [6ac7ee] | 471 | /** Creates this vector as the b y *factors' components scaled linear combination of the given three. | 
|---|
|  | 472 | * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2] | 
|---|
|  | 473 | * \param *x1 first vector | 
|---|
|  | 474 | * \param *x2 second vector | 
|---|
|  | 475 | * \param *x3 third vector | 
|---|
|  | 476 | * \param *factors three-component vector with the factor for each given vector | 
|---|
|  | 477 | */ | 
|---|
| [273382] | 478 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors) | 
|---|
| [6ac7ee] | 479 | { | 
|---|
| [273382] | 480 | (*this) = (factors[0]*x1) + | 
|---|
|  | 481 | (factors[1]*x2) + | 
|---|
|  | 482 | (factors[2]*x3); | 
|---|
| [6ac7ee] | 483 | }; | 
|---|
|  | 484 |  | 
|---|
|  | 485 | /** Calculates orthonormal vector to one given vectors. | 
|---|
|  | 486 | * Just subtracts the projection onto the given vector from this vector. | 
|---|
| [ef9df36] | 487 | * The removed part of the vector is Vector::Projection() | 
|---|
| [6ac7ee] | 488 | * \param *x1 vector | 
|---|
|  | 489 | * \return true - success, false - vector is zero | 
|---|
|  | 490 | */ | 
|---|
| [0a4f7f] | 491 | bool Vector::MakeNormalTo(const Vector &y1) | 
|---|
| [6ac7ee] | 492 | { | 
|---|
| [042f82] | 493 | bool result = false; | 
|---|
| [753f02] | 494 | double factor = y1.ScalarProduct(*this)/y1.NormSquared(); | 
|---|
| [45ef76] | 495 | Vector x1 = factor * y1; | 
|---|
| [753f02] | 496 | SubtractVector(x1); | 
|---|
| [042f82] | 497 | for (int i=NDIM;i--;) | 
|---|
| [d466f0] | 498 | result = result || (fabs(at(i)) > MYEPSILON); | 
|---|
| [6ac7ee] | 499 |  | 
|---|
| [042f82] | 500 | return result; | 
|---|
| [6ac7ee] | 501 | }; | 
|---|
|  | 502 |  | 
|---|
|  | 503 | /** Creates this vector as one of the possible orthonormal ones to the given one. | 
|---|
|  | 504 | * Just scan how many components of given *vector are unequal to zero and | 
|---|
|  | 505 | * try to get the skp of both to be zero accordingly. | 
|---|
|  | 506 | * \param *vector given vector | 
|---|
|  | 507 | * \return true - success, false - failure (null vector given) | 
|---|
|  | 508 | */ | 
|---|
| [273382] | 509 | bool Vector::GetOneNormalVector(const Vector &GivenVector) | 
|---|
| [6ac7ee] | 510 | { | 
|---|
| [042f82] | 511 | int Components[NDIM]; // contains indices of non-zero components | 
|---|
|  | 512 | int Last = 0;   // count the number of non-zero entries in vector | 
|---|
|  | 513 | int j;  // loop variables | 
|---|
|  | 514 | double norm; | 
|---|
|  | 515 |  | 
|---|
|  | 516 | for (j=NDIM;j--;) | 
|---|
|  | 517 | Components[j] = -1; | 
|---|
| [1829c4] | 518 |  | 
|---|
|  | 519 | // in two component-systems we need to find the one position that is zero | 
|---|
|  | 520 | int zeroPos = -1; | 
|---|
| [042f82] | 521 | // find two components != 0 | 
|---|
| [1829c4] | 522 | for (j=0;j<NDIM;j++){ | 
|---|
| [753f02] | 523 | if (fabs(GivenVector[j]) > MYEPSILON) | 
|---|
| [042f82] | 524 | Components[Last++] = j; | 
|---|
| [1829c4] | 525 | else | 
|---|
|  | 526 | // this our zero Position | 
|---|
|  | 527 | zeroPos = j; | 
|---|
|  | 528 | } | 
|---|
| [042f82] | 529 |  | 
|---|
|  | 530 | switch(Last) { | 
|---|
|  | 531 | case 3:  // threecomponent system | 
|---|
| [1829c4] | 532 | // the position of the zero is arbitrary in three component systems | 
|---|
|  | 533 | zeroPos = Components[2]; | 
|---|
| [042f82] | 534 | case 2:  // two component system | 
|---|
| [753f02] | 535 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]])); | 
|---|
| [1829c4] | 536 | at(zeroPos) = 0.; | 
|---|
| [042f82] | 537 | // in skp both remaining parts shall become zero but with opposite sign and third is zero | 
|---|
| [1829c4] | 538 | at(Components[1]) = -1./GivenVector[Components[1]] / norm; | 
|---|
|  | 539 | at(Components[0]) = 1./GivenVector[Components[0]] / norm; | 
|---|
| [042f82] | 540 | return true; | 
|---|
|  | 541 | break; | 
|---|
|  | 542 | case 1: // one component system | 
|---|
|  | 543 | // set sole non-zero component to 0, and one of the other zero component pendants to 1 | 
|---|
| [1829c4] | 544 | at((Components[0]+2)%NDIM) = 0.; | 
|---|
|  | 545 | at((Components[0]+1)%NDIM) = 1.; | 
|---|
|  | 546 | at(Components[0]) = 0.; | 
|---|
| [042f82] | 547 | return true; | 
|---|
|  | 548 | break; | 
|---|
|  | 549 | default: | 
|---|
|  | 550 | return false; | 
|---|
|  | 551 | } | 
|---|
| [6ac7ee] | 552 | }; | 
|---|
|  | 553 |  | 
|---|
|  | 554 | /** Adds vector \a *y componentwise. | 
|---|
|  | 555 | * \param *y vector | 
|---|
|  | 556 | */ | 
|---|
| [273382] | 557 | void Vector::AddVector(const Vector &y) | 
|---|
| [6ac7ee] | 558 | { | 
|---|
| [ce3d2b] | 559 | gsl_vector_add(content->content, y.content->content); | 
|---|
| [6ac7ee] | 560 | } | 
|---|
|  | 561 |  | 
|---|
|  | 562 | /** Adds vector \a *y componentwise. | 
|---|
|  | 563 | * \param *y vector | 
|---|
|  | 564 | */ | 
|---|
| [273382] | 565 | void Vector::SubtractVector(const Vector &y) | 
|---|
| [6ac7ee] | 566 | { | 
|---|
| [ce3d2b] | 567 | gsl_vector_sub(content->content, y.content->content); | 
|---|
| [ef9df36] | 568 | } | 
|---|
|  | 569 |  | 
|---|
| [005e18] | 570 |  | 
|---|
|  | 571 | // some comonly used vectors | 
|---|
|  | 572 | const Vector zeroVec(0,0,0); | 
|---|
|  | 573 | const Vector e1(1,0,0); | 
|---|
|  | 574 | const Vector e2(0,1,0); | 
|---|
|  | 575 | const Vector e3(0,0,1); | 
|---|