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