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