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