| 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 "verbose.hpp"
 | 
|---|
| 11 | #include "World.hpp"
 | 
|---|
| 12 | #include "Helpers/Assert.hpp"
 | 
|---|
| 13 | #include "Helpers/fast_functions.hpp"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <iostream>
 | 
|---|
| 16 | 
 | 
|---|
| 17 | using namespace std;
 | 
|---|
| 18 | 
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /************************************ Functions for class vector ************************************/
 | 
|---|
| 21 | 
 | 
|---|
| 22 | /** Constructor of class vector.
 | 
|---|
| 23 |  */
 | 
|---|
| 24 | Vector::Vector()
 | 
|---|
| 25 | {
 | 
|---|
| 26 |   content = gsl_vector_calloc (NDIM);
 | 
|---|
| 27 | };
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /**
 | 
|---|
| 30 |  * Copy constructor
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | 
 | 
|---|
| 33 | Vector::Vector(const Vector& src)
 | 
|---|
| 34 | {
 | 
|---|
| 35 |   content = gsl_vector_alloc(NDIM);
 | 
|---|
| 36 |   gsl_vector_set(content,0,src[0]);
 | 
|---|
| 37 |   gsl_vector_set(content,1,src[1]);
 | 
|---|
| 38 |   gsl_vector_set(content,2,src[2]);
 | 
|---|
| 39 | }
 | 
|---|
| 40 | 
 | 
|---|
| 41 | /** Constructor of class vector.
 | 
|---|
| 42 |  */
 | 
|---|
| 43 | Vector::Vector(const double x1, const double x2, const double x3)
 | 
|---|
| 44 | {
 | 
|---|
| 45 |   content = gsl_vector_alloc(NDIM);
 | 
|---|
| 46 |   gsl_vector_set(content,0,x1);
 | 
|---|
| 47 |   gsl_vector_set(content,1,x2);
 | 
|---|
| 48 |   gsl_vector_set(content,2,x3);
 | 
|---|
| 49 | };
 | 
|---|
| 50 | 
 | 
|---|
| 51 | /**
 | 
|---|
| 52 |  * Assignment operator
 | 
|---|
| 53 |  */
 | 
|---|
| 54 | Vector& Vector::operator=(const Vector& src){
 | 
|---|
| 55 |   // check for self assignment
 | 
|---|
| 56 |   if(&src!=this){
 | 
|---|
| 57 |     gsl_vector_set(content,0,src[0]);
 | 
|---|
| 58 |     gsl_vector_set(content,1,src[1]);
 | 
|---|
| 59 |     gsl_vector_set(content,2,src[2]);
 | 
|---|
| 60 |   }
 | 
|---|
| 61 |   return *this;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | /** Desctructor of class vector.
 | 
|---|
| 65 |  */
 | 
|---|
| 66 | Vector::~Vector() {
 | 
|---|
| 67 |   gsl_vector_free(content);
 | 
|---|
| 68 | };
 | 
|---|
| 69 | 
 | 
|---|
| 70 | /** Calculates square of distance between this and another vector.
 | 
|---|
| 71 |  * \param *y array to second vector
 | 
|---|
| 72 |  * \return \f$| x - y |^2\f$
 | 
|---|
| 73 |  */
 | 
|---|
| 74 | double Vector::DistanceSquared(const Vector &y) const
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   double res = 0.;
 | 
|---|
| 77 |   for (int i=NDIM;i--;)
 | 
|---|
| 78 |     res += (at(i)-y[i])*(at(i)-y[i]);
 | 
|---|
| 79 |   return (res);
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | /** Calculates distance between this and another vector.
 | 
|---|
| 83 |  * \param *y array to second vector
 | 
|---|
| 84 |  * \return \f$| x - y |\f$
 | 
|---|
| 85 |  */
 | 
|---|
| 86 | double Vector::distance(const Vector &y) const
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   return (sqrt(DistanceSquared(y)));
 | 
|---|
| 89 | };
 | 
|---|
| 90 | 
 | 
|---|
| 91 | Vector Vector::getClosestPoint(const Vector &point) const{
 | 
|---|
| 92 |   // the closest point to a single point space is always the single point itself
 | 
|---|
| 93 |   return *this;
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | /** Calculates distance between this and another vector in a periodic cell.
 | 
|---|
| 97 |  * \param *y array to second vector
 | 
|---|
| 98 |  * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
 | 
|---|
| 99 |  * \return \f$| x - y |\f$
 | 
|---|
| 100 |  */
 | 
|---|
| 101 | double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
 | 
|---|
| 102 | {
 | 
|---|
| 103 |   double res = distance(y), tmp, matrix[NDIM*NDIM];
 | 
|---|
| 104 |     Vector Shiftedy, TranslationVector;
 | 
|---|
| 105 |     int N[NDIM];
 | 
|---|
| 106 |     matrix[0] = cell_size[0];
 | 
|---|
| 107 |     matrix[1] = cell_size[1];
 | 
|---|
| 108 |     matrix[2] = cell_size[3];
 | 
|---|
| 109 |     matrix[3] = cell_size[1];
 | 
|---|
| 110 |     matrix[4] = cell_size[2];
 | 
|---|
| 111 |     matrix[5] = cell_size[4];
 | 
|---|
| 112 |     matrix[6] = cell_size[3];
 | 
|---|
| 113 |     matrix[7] = cell_size[4];
 | 
|---|
| 114 |     matrix[8] = cell_size[5];
 | 
|---|
| 115 |     // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
 | 
|---|
| 116 |     for (N[0]=-1;N[0]<=1;N[0]++)
 | 
|---|
| 117 |       for (N[1]=-1;N[1]<=1;N[1]++)
 | 
|---|
| 118 |         for (N[2]=-1;N[2]<=1;N[2]++) {
 | 
|---|
| 119 |           // create the translation vector
 | 
|---|
| 120 |           TranslationVector.Zero();
 | 
|---|
| 121 |           for (int i=NDIM;i--;)
 | 
|---|
| 122 |             TranslationVector[i] = (double)N[i];
 | 
|---|
| 123 |           TranslationVector.MatrixMultiplication(matrix);
 | 
|---|
| 124 |           // add onto the original vector to compare with
 | 
|---|
| 125 |           Shiftedy = y + TranslationVector;
 | 
|---|
| 126 |           // get distance and compare with minimum so far
 | 
|---|
| 127 |           tmp = distance(Shiftedy);
 | 
|---|
| 128 |           if (tmp < res) res = tmp;
 | 
|---|
| 129 |         }
 | 
|---|
| 130 |     return (res);
 | 
|---|
| 131 | };
 | 
|---|
| 132 | 
 | 
|---|
| 133 | /** Calculates distance between this and another vector in a periodic cell.
 | 
|---|
| 134 |  * \param *y array to second vector
 | 
|---|
| 135 |  * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
 | 
|---|
| 136 |  * \return \f$| x - y |^2\f$
 | 
|---|
| 137 |  */
 | 
|---|
| 138 | double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
 | 
|---|
| 141 |     Vector Shiftedy, TranslationVector;
 | 
|---|
| 142 |     int N[NDIM];
 | 
|---|
| 143 |     matrix[0] = cell_size[0];
 | 
|---|
| 144 |     matrix[1] = cell_size[1];
 | 
|---|
| 145 |     matrix[2] = cell_size[3];
 | 
|---|
| 146 |     matrix[3] = cell_size[1];
 | 
|---|
| 147 |     matrix[4] = cell_size[2];
 | 
|---|
| 148 |     matrix[5] = cell_size[4];
 | 
|---|
| 149 |     matrix[6] = cell_size[3];
 | 
|---|
| 150 |     matrix[7] = cell_size[4];
 | 
|---|
| 151 |     matrix[8] = cell_size[5];
 | 
|---|
| 152 |     // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
 | 
|---|
| 153 |     for (N[0]=-1;N[0]<=1;N[0]++)
 | 
|---|
| 154 |       for (N[1]=-1;N[1]<=1;N[1]++)
 | 
|---|
| 155 |         for (N[2]=-1;N[2]<=1;N[2]++) {
 | 
|---|
| 156 |           // create the translation vector
 | 
|---|
| 157 |           TranslationVector.Zero();
 | 
|---|
| 158 |           for (int i=NDIM;i--;)
 | 
|---|
| 159 |             TranslationVector[i] = (double)N[i];
 | 
|---|
| 160 |           TranslationVector.MatrixMultiplication(matrix);
 | 
|---|
| 161 |           // add onto the original vector to compare with
 | 
|---|
| 162 |           Shiftedy = y + TranslationVector;
 | 
|---|
| 163 |           // get distance and compare with minimum so far
 | 
|---|
| 164 |           tmp = DistanceSquared(Shiftedy);
 | 
|---|
| 165 |           if (tmp < res) res = tmp;
 | 
|---|
| 166 |         }
 | 
|---|
| 167 |     return (res);
 | 
|---|
| 168 | };
 | 
|---|
| 169 | 
 | 
|---|
| 170 | /** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
 | 
|---|
| 171 |  * \param *out ofstream for debugging messages
 | 
|---|
| 172 |  * Tries to translate a vector into each adjacent neighbouring cell.
 | 
|---|
| 173 |  */
 | 
|---|
| 174 | void Vector::KeepPeriodic(const double * const matrix)
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   //  int N[NDIM];
 | 
|---|
| 177 |   //  bool flag = false;
 | 
|---|
| 178 |     //vector Shifted, TranslationVector;
 | 
|---|
| 179 |   //  Log() << Verbose(1) << "Begin of KeepPeriodic." << endl;
 | 
|---|
| 180 |   //  Log() << Verbose(2) << "Vector is: ";
 | 
|---|
| 181 |   //  Output(out);
 | 
|---|
| 182 |   //  Log() << Verbose(0) << endl;
 | 
|---|
| 183 |     InverseMatrixMultiplication(matrix);
 | 
|---|
| 184 |     for(int i=NDIM;i--;) { // correct periodically
 | 
|---|
| 185 |       if (at(i) < 0) {  // get every coefficient into the interval [0,1)
 | 
|---|
| 186 |         at(i) += ceil(at(i));
 | 
|---|
| 187 |       } else {
 | 
|---|
| 188 |         at(i) -= floor(at(i));
 | 
|---|
| 189 |       }
 | 
|---|
| 190 |     }
 | 
|---|
| 191 |     MatrixMultiplication(matrix);
 | 
|---|
| 192 |   //  Log() << Verbose(2) << "New corrected vector is: ";
 | 
|---|
| 193 |   //  Output(out);
 | 
|---|
| 194 |   //  Log() << Verbose(0) << endl;
 | 
|---|
| 195 |   //  Log() << Verbose(1) << "End of KeepPeriodic." << endl;
 | 
|---|
| 196 | };
 | 
|---|
| 197 | 
 | 
|---|
| 198 | /** Calculates scalar product between this and another vector.
 | 
|---|
| 199 |  * \param *y array to second vector
 | 
|---|
| 200 |  * \return \f$\langle x, y \rangle\f$
 | 
|---|
| 201 |  */
 | 
|---|
| 202 | double Vector::ScalarProduct(const Vector &y) const
 | 
|---|
| 203 | {
 | 
|---|
| 204 |   double res = 0.;
 | 
|---|
| 205 |   for (int i=NDIM;i--;)
 | 
|---|
| 206 |     res += at(i)*y[i];
 | 
|---|
| 207 |   return (res);
 | 
|---|
| 208 | };
 | 
|---|
| 209 | 
 | 
|---|
| 210 | 
 | 
|---|
| 211 | /** Calculates VectorProduct between this and another vector.
 | 
|---|
| 212 |  *  -# returns the Product in place of vector from which it was initiated
 | 
|---|
| 213 |  *  -# ATTENTION: Only three dim.
 | 
|---|
| 214 |  *  \param *y array to vector with which to calculate crossproduct
 | 
|---|
| 215 |  *  \return \f$ x \times y \f&
 | 
|---|
| 216 |  */
 | 
|---|
| 217 | void Vector::VectorProduct(const Vector &y)
 | 
|---|
| 218 | {
 | 
|---|
| 219 |   Vector tmp;
 | 
|---|
| 220 |   for(int i=NDIM;i--;)
 | 
|---|
| 221 |     tmp[i] = at((i+1)%NDIM)*y[(i+2)%NDIM] - at((i+2)%NDIM)*y[(i+1)%NDIM];
 | 
|---|
| 222 |   (*this) = tmp;
 | 
|---|
| 223 | };
 | 
|---|
| 224 | 
 | 
|---|
| 225 | 
 | 
|---|
| 226 | /** projects this vector onto plane defined by \a *y.
 | 
|---|
| 227 |  * \param *y normal vector of plane
 | 
|---|
| 228 |  * \return \f$\langle x, y \rangle\f$
 | 
|---|
| 229 |  */
 | 
|---|
| 230 | void Vector::ProjectOntoPlane(const Vector &y)
 | 
|---|
| 231 | {
 | 
|---|
| 232 |   Vector tmp;
 | 
|---|
| 233 |   tmp = y;
 | 
|---|
| 234 |   tmp.Normalize();
 | 
|---|
| 235 |   tmp.Scale(ScalarProduct(tmp));
 | 
|---|
| 236 |   *this -= tmp;
 | 
|---|
| 237 | };
 | 
|---|
| 238 | 
 | 
|---|
| 239 | /** Calculates the minimum distance of this vector to the plane.
 | 
|---|
| 240 |  * \sa Vector::GetDistanceVectorToPlane()
 | 
|---|
| 241 |  * \param *out output stream for debugging
 | 
|---|
| 242 |  * \param *PlaneNormal normal of plane
 | 
|---|
| 243 |  * \param *PlaneOffset offset of plane
 | 
|---|
| 244 |  * \return distance to plane
 | 
|---|
| 245 |  */
 | 
|---|
| 246 | double Vector::DistanceToSpace(const Space &space) const
 | 
|---|
| 247 | {
 | 
|---|
| 248 |   return space.distance(*this);
 | 
|---|
| 249 | };
 | 
|---|
| 250 | 
 | 
|---|
| 251 | /** Calculates the projection of a vector onto another \a *y.
 | 
|---|
| 252 |  * \param *y array to second vector
 | 
|---|
| 253 |  */
 | 
|---|
| 254 | void Vector::ProjectIt(const Vector &y)
 | 
|---|
| 255 | {
 | 
|---|
| 256 |   (*this) += (-ScalarProduct(y))*y;
 | 
|---|
| 257 | };
 | 
|---|
| 258 | 
 | 
|---|
| 259 | /** Calculates the projection of a vector onto another \a *y.
 | 
|---|
| 260 |  * \param *y array to second vector
 | 
|---|
| 261 |  * \return Vector
 | 
|---|
| 262 |  */
 | 
|---|
| 263 | Vector Vector::Projection(const Vector &y) const
 | 
|---|
| 264 | {
 | 
|---|
| 265 |   Vector helper = y;
 | 
|---|
| 266 |   helper.Scale((ScalarProduct(y)/y.NormSquared()));
 | 
|---|
| 267 | 
 | 
|---|
| 268 |   return helper;
 | 
|---|
| 269 | };
 | 
|---|
| 270 | 
 | 
|---|
| 271 | /** Calculates norm of this vector.
 | 
|---|
| 272 |  * \return \f$|x|\f$
 | 
|---|
| 273 |  */
 | 
|---|
| 274 | double Vector::Norm() const
 | 
|---|
| 275 | {
 | 
|---|
| 276 |   return (sqrt(NormSquared()));
 | 
|---|
| 277 | };
 | 
|---|
| 278 | 
 | 
|---|
| 279 | /** Calculates squared norm of this vector.
 | 
|---|
| 280 |  * \return \f$|x|^2\f$
 | 
|---|
| 281 |  */
 | 
|---|
| 282 | double Vector::NormSquared() const
 | 
|---|
| 283 | {
 | 
|---|
| 284 |   return (ScalarProduct(*this));
 | 
|---|
| 285 | };
 | 
|---|
| 286 | 
 | 
|---|
| 287 | /** Normalizes this vector.
 | 
|---|
| 288 |  */
 | 
|---|
| 289 | void Vector::Normalize()
 | 
|---|
| 290 | {
 | 
|---|
| 291 |   double factor = Norm();
 | 
|---|
| 292 |   (*this) *= 1/factor;
 | 
|---|
| 293 | };
 | 
|---|
| 294 | 
 | 
|---|
| 295 | /** Zeros all components of this vector.
 | 
|---|
| 296 |  */
 | 
|---|
| 297 | void Vector::Zero()
 | 
|---|
| 298 | {
 | 
|---|
| 299 |   at(0)=at(1)=at(2)=0;
 | 
|---|
| 300 | };
 | 
|---|
| 301 | 
 | 
|---|
| 302 | /** Zeros all components of this vector.
 | 
|---|
| 303 |  */
 | 
|---|
| 304 | void Vector::One(const double one)
 | 
|---|
| 305 | {
 | 
|---|
| 306 |   at(0)=at(1)=at(2)=one;
 | 
|---|
| 307 | };
 | 
|---|
| 308 | 
 | 
|---|
| 309 | /** Checks whether vector has all components zero.
 | 
|---|
| 310 |  * @return true - vector is zero, false - vector is not
 | 
|---|
| 311 |  */
 | 
|---|
| 312 | bool Vector::IsZero() const
 | 
|---|
| 313 | {
 | 
|---|
| 314 |   return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON);
 | 
|---|
| 315 | };
 | 
|---|
| 316 | 
 | 
|---|
| 317 | /** Checks whether vector has length of 1.
 | 
|---|
| 318 |  * @return true - vector is normalized, false - vector is not
 | 
|---|
| 319 |  */
 | 
|---|
| 320 | bool Vector::IsOne() const
 | 
|---|
| 321 | {
 | 
|---|
| 322 |   return (fabs(Norm() - 1.) < MYEPSILON);
 | 
|---|
| 323 | };
 | 
|---|
| 324 | 
 | 
|---|
| 325 | /** Checks whether vector is normal to \a *normal.
 | 
|---|
| 326 |  * @return true - vector is normalized, false - vector is not
 | 
|---|
| 327 |  */
 | 
|---|
| 328 | bool Vector::IsNormalTo(const Vector &normal) const
 | 
|---|
| 329 | {
 | 
|---|
| 330 |   if (ScalarProduct(normal) < MYEPSILON)
 | 
|---|
| 331 |     return true;
 | 
|---|
| 332 |   else
 | 
|---|
| 333 |     return false;
 | 
|---|
| 334 | };
 | 
|---|
| 335 | 
 | 
|---|
| 336 | /** Checks whether vector is normal to \a *normal.
 | 
|---|
| 337 |  * @return true - vector is normalized, false - vector is not
 | 
|---|
| 338 |  */
 | 
|---|
| 339 | bool Vector::IsEqualTo(const Vector &a) const
 | 
|---|
| 340 | {
 | 
|---|
| 341 |   bool status = true;
 | 
|---|
| 342 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| 343 |     if (fabs(at(i) - a[i]) > MYEPSILON)
 | 
|---|
| 344 |       status = false;
 | 
|---|
| 345 |   }
 | 
|---|
| 346 |   return status;
 | 
|---|
| 347 | };
 | 
|---|
| 348 | 
 | 
|---|
| 349 | /** Calculates the angle between this and another vector.
 | 
|---|
| 350 |  * \param *y array to second vector
 | 
|---|
| 351 |  * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
 | 
|---|
| 352 |  */
 | 
|---|
| 353 | double Vector::Angle(const Vector &y) const
 | 
|---|
| 354 | {
 | 
|---|
| 355 |   double norm1 = Norm(), norm2 = y.Norm();
 | 
|---|
| 356 |   double angle = -1;
 | 
|---|
| 357 |   if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
 | 
|---|
| 358 |     angle = this->ScalarProduct(y)/norm1/norm2;
 | 
|---|
| 359 |   // -1-MYEPSILON occured due to numerical imprecision, catch ...
 | 
|---|
| 360 |   //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
 | 
|---|
| 361 |   if (angle < -1)
 | 
|---|
| 362 |     angle = -1;
 | 
|---|
| 363 |   if (angle > 1)
 | 
|---|
| 364 |     angle = 1;
 | 
|---|
| 365 |   return acos(angle);
 | 
|---|
| 366 | };
 | 
|---|
| 367 | 
 | 
|---|
| 368 | 
 | 
|---|
| 369 | double& Vector::operator[](size_t i){
 | 
|---|
| 370 |   ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
 | 
|---|
| 371 |   return *gsl_vector_ptr (content, i);
 | 
|---|
| 372 | }
 | 
|---|
| 373 | 
 | 
|---|
| 374 | const double& Vector::operator[](size_t i) const{
 | 
|---|
| 375 |   ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
 | 
|---|
| 376 |   return *gsl_vector_ptr (content, i);
 | 
|---|
| 377 | }
 | 
|---|
| 378 | 
 | 
|---|
| 379 | double& Vector::at(size_t i){
 | 
|---|
| 380 |   return (*this)[i];
 | 
|---|
| 381 | }
 | 
|---|
| 382 | 
 | 
|---|
| 383 | const double& Vector::at(size_t i) const{
 | 
|---|
| 384 |   return (*this)[i];
 | 
|---|
| 385 | }
 | 
|---|
| 386 | 
 | 
|---|
| 387 | gsl_vector* Vector::get(){
 | 
|---|
| 388 |   return content;
 | 
|---|
| 389 | }
 | 
|---|
| 390 | 
 | 
|---|
| 391 | /** Compares vector \a to vector \a b component-wise.
 | 
|---|
| 392 |  * \param a base vector
 | 
|---|
| 393 |  * \param b vector components to add
 | 
|---|
| 394 |  * \return a == b
 | 
|---|
| 395 |  */
 | 
|---|
| 396 | bool Vector::operator==(const Vector& b) const
 | 
|---|
| 397 | {
 | 
|---|
| 398 |   return IsEqualTo(b);
 | 
|---|
| 399 | };
 | 
|---|
| 400 | 
 | 
|---|
| 401 | bool Vector::operator!=(const Vector& b) const
 | 
|---|
| 402 | {
 | 
|---|
| 403 |   return !IsEqualTo(b);
 | 
|---|
| 404 | }
 | 
|---|
| 405 | 
 | 
|---|
| 406 | /** Sums vector \a to this lhs component-wise.
 | 
|---|
| 407 |  * \param a base vector
 | 
|---|
| 408 |  * \param b vector components to add
 | 
|---|
| 409 |  * \return lhs + a
 | 
|---|
| 410 |  */
 | 
|---|
| 411 | const Vector& Vector::operator+=(const Vector& b)
 | 
|---|
| 412 | {
 | 
|---|
| 413 |   this->AddVector(b);
 | 
|---|
| 414 |   return *this;
 | 
|---|
| 415 | };
 | 
|---|
| 416 | 
 | 
|---|
| 417 | /** Subtracts vector \a from this lhs component-wise.
 | 
|---|
| 418 |  * \param a base vector
 | 
|---|
| 419 |  * \param b vector components to add
 | 
|---|
| 420 |  * \return lhs - a
 | 
|---|
| 421 |  */
 | 
|---|
| 422 | const Vector& Vector::operator-=(const Vector& b)
 | 
|---|
| 423 | {
 | 
|---|
| 424 |   this->SubtractVector(b);
 | 
|---|
| 425 |   return *this;
 | 
|---|
| 426 | };
 | 
|---|
| 427 | 
 | 
|---|
| 428 | /** factor each component of \a a times a double \a m.
 | 
|---|
| 429 |  * \param a base vector
 | 
|---|
| 430 |  * \param m factor
 | 
|---|
| 431 |  * \return lhs.x[i] * m
 | 
|---|
| 432 |  */
 | 
|---|
| 433 | const Vector& operator*=(Vector& a, const double m)
 | 
|---|
| 434 | {
 | 
|---|
| 435 |   a.Scale(m);
 | 
|---|
| 436 |   return a;
 | 
|---|
| 437 | };
 | 
|---|
| 438 | 
 | 
|---|
| 439 | /** Sums two vectors \a  and \b component-wise.
 | 
|---|
| 440 |  * \param a first vector
 | 
|---|
| 441 |  * \param b second vector
 | 
|---|
| 442 |  * \return a + b
 | 
|---|
| 443 |  */
 | 
|---|
| 444 | Vector const Vector::operator+(const Vector& b) const
 | 
|---|
| 445 | {
 | 
|---|
| 446 |   Vector x = *this;
 | 
|---|
| 447 |   x.AddVector(b);
 | 
|---|
| 448 |   return x;
 | 
|---|
| 449 | };
 | 
|---|
| 450 | 
 | 
|---|
| 451 | /** Subtracts vector \a from \b component-wise.
 | 
|---|
| 452 |  * \param a first vector
 | 
|---|
| 453 |  * \param b second vector
 | 
|---|
| 454 |  * \return a - b
 | 
|---|
| 455 |  */
 | 
|---|
| 456 | Vector const Vector::operator-(const Vector& b) const
 | 
|---|
| 457 | {
 | 
|---|
| 458 |   Vector x = *this;
 | 
|---|
| 459 |   x.SubtractVector(b);
 | 
|---|
| 460 |   return x;
 | 
|---|
| 461 | };
 | 
|---|
| 462 | 
 | 
|---|
| 463 | /** Factors given vector \a a times \a m.
 | 
|---|
| 464 |  * \param a vector
 | 
|---|
| 465 |  * \param m factor
 | 
|---|
| 466 |  * \return m * a
 | 
|---|
| 467 |  */
 | 
|---|
| 468 | Vector const operator*(const Vector& a, const double m)
 | 
|---|
| 469 | {
 | 
|---|
| 470 |   Vector x(a);
 | 
|---|
| 471 |   x.Scale(m);
 | 
|---|
| 472 |   return x;
 | 
|---|
| 473 | };
 | 
|---|
| 474 | 
 | 
|---|
| 475 | /** Factors given vector \a a times \a m.
 | 
|---|
| 476 |  * \param m factor
 | 
|---|
| 477 |  * \param a vector
 | 
|---|
| 478 |  * \return m * a
 | 
|---|
| 479 |  */
 | 
|---|
| 480 | Vector const operator*(const double m, const Vector& a )
 | 
|---|
| 481 | {
 | 
|---|
| 482 |   Vector x(a);
 | 
|---|
| 483 |   x.Scale(m);
 | 
|---|
| 484 |   return x;
 | 
|---|
| 485 | };
 | 
|---|
| 486 | 
 | 
|---|
| 487 | ostream& operator<<(ostream& ost, const Vector& m)
 | 
|---|
| 488 | {
 | 
|---|
| 489 |   ost << "(";
 | 
|---|
| 490 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| 491 |     ost << m[i];
 | 
|---|
| 492 |     if (i != 2)
 | 
|---|
| 493 |       ost << ",";
 | 
|---|
| 494 |   }
 | 
|---|
| 495 |   ost << ")";
 | 
|---|
| 496 |   return ost;
 | 
|---|
| 497 | };
 | 
|---|
| 498 | 
 | 
|---|
| 499 | 
 | 
|---|
| 500 | void Vector::ScaleAll(const double *factor)
 | 
|---|
| 501 | {
 | 
|---|
| 502 |   for (int i=NDIM;i--;)
 | 
|---|
| 503 |     at(i) *= factor[i];
 | 
|---|
| 504 | };
 | 
|---|
| 505 | 
 | 
|---|
| 506 | 
 | 
|---|
| 507 | 
 | 
|---|
| 508 | void Vector::Scale(const double factor)
 | 
|---|
| 509 | {
 | 
|---|
| 510 |   for (int i=NDIM;i--;)
 | 
|---|
| 511 |     at(i) *= factor;
 | 
|---|
| 512 | };
 | 
|---|
| 513 | 
 | 
|---|
| 514 | /** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
 | 
|---|
| 515 |  * \param *M matrix of box
 | 
|---|
| 516 |  * \param *Minv inverse matrix
 | 
|---|
| 517 |  */
 | 
|---|
| 518 | void Vector::WrapPeriodically(const double * const M, const double * const Minv)
 | 
|---|
| 519 | {
 | 
|---|
| 520 |   MatrixMultiplication(Minv);
 | 
|---|
| 521 |   // truncate to [0,1] for each axis
 | 
|---|
| 522 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| 523 |     //at(i) += 0.5;  // set to center of box
 | 
|---|
| 524 |     while (at(i) >= 1.)
 | 
|---|
| 525 |       at(i) -= 1.;
 | 
|---|
| 526 |     while (at(i) < 0.)
 | 
|---|
| 527 |       at(i) += 1.;
 | 
|---|
| 528 |   }
 | 
|---|
| 529 |   MatrixMultiplication(M);
 | 
|---|
| 530 | };
 | 
|---|
| 531 | 
 | 
|---|
| 532 | std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
 | 
|---|
| 533 |   double factor = ScalarProduct(rhs)/rhs.NormSquared();
 | 
|---|
| 534 |   Vector res= factor * rhs;
 | 
|---|
| 535 |   return make_pair(res,(*this)-res);
 | 
|---|
| 536 | }
 | 
|---|
| 537 | 
 | 
|---|
| 538 | std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
 | 
|---|
| 539 |   Vector helper = *this;
 | 
|---|
| 540 |   pointset res;
 | 
|---|
| 541 |   for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
 | 
|---|
| 542 |     pair<Vector,Vector> currPart = helper.partition(*iter);
 | 
|---|
| 543 |     res.push_back(currPart.first);
 | 
|---|
| 544 |     helper = currPart.second;
 | 
|---|
| 545 |   }
 | 
|---|
| 546 |   return make_pair(res,helper);
 | 
|---|
| 547 | }
 | 
|---|
| 548 | 
 | 
|---|
| 549 | /** Do a matrix multiplication.
 | 
|---|
| 550 |  * \param *matrix NDIM_NDIM array
 | 
|---|
| 551 |  */
 | 
|---|
| 552 | void Vector::MatrixMultiplication(const double * const M)
 | 
|---|
| 553 | {
 | 
|---|
| 554 |   Vector tmp;
 | 
|---|
| 555 |   // do the matrix multiplication
 | 
|---|
| 556 |   for(int i=NDIM;i--;)
 | 
|---|
| 557 |     tmp[i] = M[i]*at(0)+M[i+3]*at(1)+M[i+6]*at(2);
 | 
|---|
| 558 | 
 | 
|---|
| 559 |   (*this) = tmp;
 | 
|---|
| 560 | };
 | 
|---|
| 561 | 
 | 
|---|
| 562 | /** Do a matrix multiplication with the \a *A' inverse.
 | 
|---|
| 563 |  * \param *matrix NDIM_NDIM array
 | 
|---|
| 564 |  */
 | 
|---|
| 565 | bool Vector::InverseMatrixMultiplication(const double * const A)
 | 
|---|
| 566 | {
 | 
|---|
| 567 |   double B[NDIM*NDIM];
 | 
|---|
| 568 |   double detA = RDET3(A);
 | 
|---|
| 569 |   double detAReci;
 | 
|---|
| 570 | 
 | 
|---|
| 571 |   // calculate the inverse B
 | 
|---|
| 572 |   if (fabs(detA) > MYEPSILON) {;  // RDET3(A) yields precisely zero if A irregular
 | 
|---|
| 573 |     detAReci = 1./detA;
 | 
|---|
| 574 |     B[0] =  detAReci*RDET2(A[4],A[5],A[7],A[8]);    // A_11
 | 
|---|
| 575 |     B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]);    // A_12
 | 
|---|
| 576 |     B[2] =  detAReci*RDET2(A[1],A[2],A[4],A[5]);    // A_13
 | 
|---|
| 577 |     B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]);    // A_21
 | 
|---|
| 578 |     B[4] =  detAReci*RDET2(A[0],A[2],A[6],A[8]);    // A_22
 | 
|---|
| 579 |     B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]);    // A_23
 | 
|---|
| 580 |     B[6] =  detAReci*RDET2(A[3],A[4],A[6],A[7]);    // A_31
 | 
|---|
| 581 |     B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]);    // A_32
 | 
|---|
| 582 |     B[8] =  detAReci*RDET2(A[0],A[1],A[3],A[4]);    // A_33
 | 
|---|
| 583 | 
 | 
|---|
| 584 |     MatrixMultiplication(B);
 | 
|---|
| 585 | 
 | 
|---|
| 586 |     return true;
 | 
|---|
| 587 |   } else {
 | 
|---|
| 588 |     return false;
 | 
|---|
| 589 |   }
 | 
|---|
| 590 | };
 | 
|---|
| 591 | 
 | 
|---|
| 592 | 
 | 
|---|
| 593 | /** Creates this vector as the b y *factors' components scaled linear combination of the given three.
 | 
|---|
| 594 |  * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
 | 
|---|
| 595 |  * \param *x1 first vector
 | 
|---|
| 596 |  * \param *x2 second vector
 | 
|---|
| 597 |  * \param *x3 third vector
 | 
|---|
| 598 |  * \param *factors three-component vector with the factor for each given vector
 | 
|---|
| 599 |  */
 | 
|---|
| 600 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
 | 
|---|
| 601 | {
 | 
|---|
| 602 |   (*this) = (factors[0]*x1) +
 | 
|---|
| 603 |             (factors[1]*x2) +
 | 
|---|
| 604 |             (factors[2]*x3);
 | 
|---|
| 605 | };
 | 
|---|
| 606 | 
 | 
|---|
| 607 | /** Calculates orthonormal vector to one given vectors.
 | 
|---|
| 608 |  * Just subtracts the projection onto the given vector from this vector.
 | 
|---|
| 609 |  * The removed part of the vector is Vector::Projection()
 | 
|---|
| 610 |  * \param *x1 vector
 | 
|---|
| 611 |  * \return true - success, false - vector is zero
 | 
|---|
| 612 |  */
 | 
|---|
| 613 | bool Vector::MakeNormalTo(const Vector &y1)
 | 
|---|
| 614 | {
 | 
|---|
| 615 |   bool result = false;
 | 
|---|
| 616 |   double factor = y1.ScalarProduct(*this)/y1.NormSquared();
 | 
|---|
| 617 |   Vector x1 = factor * y1;
 | 
|---|
| 618 |   SubtractVector(x1);
 | 
|---|
| 619 |   for (int i=NDIM;i--;)
 | 
|---|
| 620 |     result = result || (fabs(at(i)) > MYEPSILON);
 | 
|---|
| 621 | 
 | 
|---|
| 622 |   return result;
 | 
|---|
| 623 | };
 | 
|---|
| 624 | 
 | 
|---|
| 625 | /** Creates this vector as one of the possible orthonormal ones to the given one.
 | 
|---|
| 626 |  * Just scan how many components of given *vector are unequal to zero and
 | 
|---|
| 627 |  * try to get the skp of both to be zero accordingly.
 | 
|---|
| 628 |  * \param *vector given vector
 | 
|---|
| 629 |  * \return true - success, false - failure (null vector given)
 | 
|---|
| 630 |  */
 | 
|---|
| 631 | bool Vector::GetOneNormalVector(const Vector &GivenVector)
 | 
|---|
| 632 | {
 | 
|---|
| 633 |   int Components[NDIM]; // contains indices of non-zero components
 | 
|---|
| 634 |   int Last = 0;   // count the number of non-zero entries in vector
 | 
|---|
| 635 |   int j;  // loop variables
 | 
|---|
| 636 |   double norm;
 | 
|---|
| 637 | 
 | 
|---|
| 638 |   for (j=NDIM;j--;)
 | 
|---|
| 639 |     Components[j] = -1;
 | 
|---|
| 640 | 
 | 
|---|
| 641 |   // in two component-systems we need to find the one position that is zero
 | 
|---|
| 642 |   int zeroPos = -1;
 | 
|---|
| 643 |   // find two components != 0
 | 
|---|
| 644 |   for (j=0;j<NDIM;j++){
 | 
|---|
| 645 |     if (fabs(GivenVector[j]) > MYEPSILON)
 | 
|---|
| 646 |       Components[Last++] = j;
 | 
|---|
| 647 |     else
 | 
|---|
| 648 |       // this our zero Position
 | 
|---|
| 649 |       zeroPos = j;
 | 
|---|
| 650 |   }
 | 
|---|
| 651 | 
 | 
|---|
| 652 |   switch(Last) {
 | 
|---|
| 653 |     case 3:  // threecomponent system
 | 
|---|
| 654 |       // the position of the zero is arbitrary in three component systems
 | 
|---|
| 655 |       zeroPos = Components[2];
 | 
|---|
| 656 |     case 2:  // two component system
 | 
|---|
| 657 |       norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
 | 
|---|
| 658 |       at(zeroPos) = 0.;
 | 
|---|
| 659 |       // in skp both remaining parts shall become zero but with opposite sign and third is zero
 | 
|---|
| 660 |       at(Components[1]) = -1./GivenVector[Components[1]] / norm;
 | 
|---|
| 661 |       at(Components[0]) = 1./GivenVector[Components[0]] / norm;
 | 
|---|
| 662 |       return true;
 | 
|---|
| 663 |       break;
 | 
|---|
| 664 |     case 1: // one component system
 | 
|---|
| 665 |       // set sole non-zero component to 0, and one of the other zero component pendants to 1
 | 
|---|
| 666 |       at((Components[0]+2)%NDIM) = 0.;
 | 
|---|
| 667 |       at((Components[0]+1)%NDIM) = 1.;
 | 
|---|
| 668 |       at(Components[0]) = 0.;
 | 
|---|
| 669 |       return true;
 | 
|---|
| 670 |       break;
 | 
|---|
| 671 |     default:
 | 
|---|
| 672 |       return false;
 | 
|---|
| 673 |   }
 | 
|---|
| 674 | };
 | 
|---|
| 675 | 
 | 
|---|
| 676 | /** Adds vector \a *y componentwise.
 | 
|---|
| 677 |  * \param *y vector
 | 
|---|
| 678 |  */
 | 
|---|
| 679 | void Vector::AddVector(const Vector &y)
 | 
|---|
| 680 | {
 | 
|---|
| 681 |   for(int i=NDIM;i--;)
 | 
|---|
| 682 |     at(i) += y[i];
 | 
|---|
| 683 | }
 | 
|---|
| 684 | 
 | 
|---|
| 685 | /** Adds vector \a *y componentwise.
 | 
|---|
| 686 |  * \param *y vector
 | 
|---|
| 687 |  */
 | 
|---|
| 688 | void Vector::SubtractVector(const Vector &y)
 | 
|---|
| 689 | {
 | 
|---|
| 690 |   for(int i=NDIM;i--;)
 | 
|---|
| 691 |     at(i) -= y[i];
 | 
|---|
| 692 | }
 | 
|---|
| 693 | 
 | 
|---|
| 694 | /**
 | 
|---|
| 695 |  * Checks whether this vector is within the parallelepiped defined by the given three vectors and
 | 
|---|
| 696 |  * their offset.
 | 
|---|
| 697 |  *
 | 
|---|
| 698 |  * @param offest for the origin of the parallelepiped
 | 
|---|
| 699 |  * @param three vectors forming the matrix that defines the shape of the parallelpiped
 | 
|---|
| 700 |  */
 | 
|---|
| 701 | bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
 | 
|---|
| 702 | {
 | 
|---|
| 703 |   Vector a = (*this)-offset;
 | 
|---|
| 704 |   a.InverseMatrixMultiplication(parallelepiped);
 | 
|---|
| 705 |   bool isInside = true;
 | 
|---|
| 706 | 
 | 
|---|
| 707 |   for (int i=NDIM;i--;)
 | 
|---|
| 708 |     isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
 | 
|---|
| 709 | 
 | 
|---|
| 710 |   return isInside;
 | 
|---|
| 711 | }
 | 
|---|
| 712 | 
 | 
|---|
| 713 | 
 | 
|---|
| 714 | // some comonly used vectors
 | 
|---|
| 715 | const Vector zeroVec(0,0,0);
 | 
|---|
| 716 | const Vector e1(1,0,0);
 | 
|---|
| 717 | const Vector e2(0,1,0);
 | 
|---|
| 718 | const Vector e3(0,0,1);
 | 
|---|