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