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