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