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