[6ac7ee] | 1 | /** \file vector.cpp
|
---|
| 2 | *
|
---|
| 3 | * Function implementations for the class vector.
|
---|
| 4 | *
|
---|
| 5 | */
|
---|
| 6 |
|
---|
[edb93c] | 7 |
|
---|
[54a746] | 8 | #include "vector.hpp"
|
---|
| 9 | #include "verbose.hpp"
|
---|
[b34306] | 10 | #include "World.hpp"
|
---|
[0a4f7f] | 11 | #include "Helpers/Assert.hpp"
|
---|
[753f02] | 12 | #include "Helpers/fast_functions.hpp"
|
---|
[6ac7ee] | 13 |
|
---|
[1bd79e] | 14 | #include <iostream>
|
---|
| 15 |
|
---|
| 16 | using namespace std;
|
---|
[6ac7ee] | 17 |
|
---|
[97498a] | 18 |
|
---|
[6ac7ee] | 19 | /************************************ Functions for class vector ************************************/
|
---|
| 20 |
|
---|
| 21 | /** Constructor of class vector.
|
---|
| 22 | */
|
---|
[753f02] | 23 | Vector::Vector()
|
---|
| 24 | {
|
---|
| 25 | x[0] = x[1] = x[2] = 0.;
|
---|
| 26 | };
|
---|
[6ac7ee] | 27 |
|
---|
[753f02] | 28 | /**
|
---|
| 29 | * Copy constructor
|
---|
[821907] | 30 | */
|
---|
[1bd79e] | 31 |
|
---|
[753f02] | 32 | Vector::Vector(const Vector& src)
|
---|
[821907] | 33 | {
|
---|
[753f02] | 34 | x[0] = src[0];
|
---|
| 35 | x[1] = src[1];
|
---|
| 36 | x[2] = src[2];
|
---|
[1bd79e] | 37 | }
|
---|
[821907] | 38 |
|
---|
| 39 | /** Constructor of class vector.
|
---|
| 40 | */
|
---|
[753f02] | 41 | Vector::Vector(const double x1, const double x2, const double x3)
|
---|
[821907] | 42 | {
|
---|
[753f02] | 43 | x[0] = x1;
|
---|
| 44 | x[1] = x2;
|
---|
| 45 | x[2] = x3;
|
---|
[821907] | 46 | };
|
---|
| 47 |
|
---|
[0a4f7f] | 48 | /**
|
---|
| 49 | * Assignment operator
|
---|
[6ac7ee] | 50 | */
|
---|
[0a4f7f] | 51 | Vector& Vector::operator=(const Vector& src){
|
---|
| 52 | // check for self assignment
|
---|
| 53 | if(&src!=this){
|
---|
[753f02] | 54 | x[0] = src[0];
|
---|
| 55 | x[1] = src[1];
|
---|
| 56 | x[2] = src[2];
|
---|
[0a4f7f] | 57 | }
|
---|
| 58 | return *this;
|
---|
| 59 | }
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 69 | double Vector::DistanceSquared(const Vector &y) const
|
---|
[6ac7ee] | 70 | {
|
---|
[042f82] | 71 | double res = 0.;
|
---|
| 72 | for (int i=NDIM;i--;)
|
---|
[753f02] | 73 | res += (x[i]-y[i])*(x[i]-y[i]);
|
---|
[042f82] | 74 | return (res);
|
---|
[6ac7ee] | 75 | };
|
---|
| 76 |
|
---|
| 77 | /** Calculates distance between this and another vector.
|
---|
| 78 | * \param *y array to second vector
|
---|
| 79 | * \return \f$| x - y |\f$
|
---|
| 80 | */
|
---|
[1513a74] | 81 | double Vector::distance(const Vector &y) const
|
---|
[6ac7ee] | 82 | {
|
---|
[273382] | 83 | return (sqrt(DistanceSquared(y)));
|
---|
[6ac7ee] | 84 | };
|
---|
| 85 |
|
---|
[1513a74] | 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 |
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 96 | double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
|
---|
[6ac7ee] | 97 | {
|
---|
[1513a74] | 98 | double res = distance(y), tmp, matrix[NDIM*NDIM];
|
---|
[753f02] | 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
|
---|
[1513a74] | 122 | tmp = distance(Shiftedy);
|
---|
[753f02] | 123 | if (tmp < res) res = tmp;
|
---|
| 124 | }
|
---|
| 125 | return (res);
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 133 | double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
|
---|
[6ac7ee] | 134 | {
|
---|
[042f82] | 135 | double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
|
---|
[753f02] | 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);
|
---|
[6ac7ee] | 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 | */
|
---|
[e138de] | 169 | void Vector::KeepPeriodic(const double * const matrix)
|
---|
[6ac7ee] | 170 | {
|
---|
[753f02] | 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 | }
|
---|
[042f82] | 185 | }
|
---|
[753f02] | 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;
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 197 | double Vector::ScalarProduct(const Vector &y) const
|
---|
[6ac7ee] | 198 | {
|
---|
[042f82] | 199 | double res = 0.;
|
---|
| 200 | for (int i=NDIM;i--;)
|
---|
[753f02] | 201 | res += x[i]*y[i];
|
---|
[042f82] | 202 | return (res);
|
---|
[6ac7ee] | 203 | };
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | /** Calculates VectorProduct between this and another vector.
|
---|
[042f82] | 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&
|
---|
[6ac7ee] | 211 | */
|
---|
[273382] | 212 | void Vector::VectorProduct(const Vector &y)
|
---|
[6ac7ee] | 213 | {
|
---|
[042f82] | 214 | Vector tmp;
|
---|
[42a101] | 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];
|
---|
[753f02] | 218 | (*this) = tmp;
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 226 | void Vector::ProjectOntoPlane(const Vector &y)
|
---|
[6ac7ee] | 227 | {
|
---|
[042f82] | 228 | Vector tmp;
|
---|
[753f02] | 229 | tmp = y;
|
---|
[042f82] | 230 | tmp.Normalize();
|
---|
[753f02] | 231 | tmp.Scale(ScalarProduct(tmp));
|
---|
| 232 | *this -= tmp;
|
---|
[2319ed] | 233 | };
|
---|
| 234 |
|
---|
[821907] | 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 | */
|
---|
[d4c9ae] | 242 | double Vector::DistanceToSpace(const Space &space) const
|
---|
[821907] | 243 | {
|
---|
[d4c9ae] | 244 | return space.distance(*this);
|
---|
[c4d4df] | 245 | };
|
---|
| 246 |
|
---|
[6ac7ee] | 247 | /** Calculates the projection of a vector onto another \a *y.
|
---|
| 248 | * \param *y array to second vector
|
---|
| 249 | */
|
---|
[273382] | 250 | void Vector::ProjectIt(const Vector &y)
|
---|
[6ac7ee] | 251 | {
|
---|
[753f02] | 252 | (*this) += (-ScalarProduct(y))*y;
|
---|
[ef9df36] | 253 | };
|
---|
| 254 |
|
---|
| 255 | /** Calculates the projection of a vector onto another \a *y.
|
---|
| 256 | * \param *y array to second vector
|
---|
| 257 | * \return Vector
|
---|
| 258 | */
|
---|
[273382] | 259 | Vector Vector::Projection(const Vector &y) const
|
---|
[ef9df36] | 260 | {
|
---|
[753f02] | 261 | Vector helper = y;
|
---|
| 262 | helper.Scale((ScalarProduct(y)/y.NormSquared()));
|
---|
[ef9df36] | 263 |
|
---|
| 264 | return helper;
|
---|
[6ac7ee] | 265 | };
|
---|
| 266 |
|
---|
| 267 | /** Calculates norm of this vector.
|
---|
| 268 | * \return \f$|x|\f$
|
---|
| 269 | */
|
---|
| 270 | double Vector::Norm() const
|
---|
| 271 | {
|
---|
[273382] | 272 | return (sqrt(NormSquared()));
|
---|
[6ac7ee] | 273 | };
|
---|
| 274 |
|
---|
[d4d0dd] | 275 | /** Calculates squared norm of this vector.
|
---|
| 276 | * \return \f$|x|^2\f$
|
---|
| 277 | */
|
---|
| 278 | double Vector::NormSquared() const
|
---|
| 279 | {
|
---|
[273382] | 280 | return (ScalarProduct(*this));
|
---|
[d4d0dd] | 281 | };
|
---|
| 282 |
|
---|
[6ac7ee] | 283 | /** Normalizes this vector.
|
---|
| 284 | */
|
---|
| 285 | void Vector::Normalize()
|
---|
| 286 | {
|
---|
[1bd79e] | 287 | double factor = Norm();
|
---|
| 288 | (*this) *= 1/factor;
|
---|
[6ac7ee] | 289 | };
|
---|
| 290 |
|
---|
| 291 | /** Zeros all components of this vector.
|
---|
| 292 | */
|
---|
| 293 | void Vector::Zero()
|
---|
| 294 | {
|
---|
[753f02] | 295 | at(0)=at(1)=at(2)=0;
|
---|
[6ac7ee] | 296 | };
|
---|
| 297 |
|
---|
| 298 | /** Zeros all components of this vector.
|
---|
| 299 | */
|
---|
[776b64] | 300 | void Vector::One(const double one)
|
---|
[6ac7ee] | 301 | {
|
---|
[753f02] | 302 | at(0)=at(1)=at(2)=one;
|
---|
[6ac7ee] | 303 | };
|
---|
| 304 |
|
---|
[9c20aa] | 305 | /** Checks whether vector has all components zero.
|
---|
| 306 | * @return true - vector is zero, false - vector is not
|
---|
| 307 | */
|
---|
[54a746] | 308 | bool Vector::IsZero() const
|
---|
[9c20aa] | 309 | {
|
---|
[54a746] | 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);
|
---|
[9c20aa] | 319 | };
|
---|
| 320 |
|
---|
[ef9df36] | 321 | /** Checks whether vector is normal to \a *normal.
|
---|
| 322 | * @return true - vector is normalized, false - vector is not
|
---|
| 323 | */
|
---|
[273382] | 324 | bool Vector::IsNormalTo(const Vector &normal) const
|
---|
[ef9df36] | 325 | {
|
---|
| 326 | if (ScalarProduct(normal) < MYEPSILON)
|
---|
| 327 | return true;
|
---|
| 328 | else
|
---|
| 329 | return false;
|
---|
| 330 | };
|
---|
| 331 |
|
---|
[b998c3] | 332 | /** Checks whether vector is normal to \a *normal.
|
---|
| 333 | * @return true - vector is normalized, false - vector is not
|
---|
| 334 | */
|
---|
[273382] | 335 | bool Vector::IsEqualTo(const Vector &a) const
|
---|
[b998c3] | 336 | {
|
---|
| 337 | bool status = true;
|
---|
| 338 | for (int i=0;i<NDIM;i++) {
|
---|
[753f02] | 339 | if (fabs(x[i] - a[i]) > MYEPSILON)
|
---|
[b998c3] | 340 | status = false;
|
---|
| 341 | }
|
---|
| 342 | return status;
|
---|
| 343 | };
|
---|
| 344 |
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 349 | double Vector::Angle(const Vector &y) const
|
---|
[6ac7ee] | 350 | {
|
---|
[753f02] | 351 | double norm1 = Norm(), norm2 = y.Norm();
|
---|
[ef9df36] | 352 | double angle = -1;
|
---|
[d4d0dd] | 353 | if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
|
---|
| 354 | angle = this->ScalarProduct(y)/norm1/norm2;
|
---|
[02da9e] | 355 | // -1-MYEPSILON occured due to numerical imprecision, catch ...
|
---|
[e138de] | 356 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
|
---|
[02da9e] | 357 | if (angle < -1)
|
---|
| 358 | angle = -1;
|
---|
| 359 | if (angle > 1)
|
---|
| 360 | angle = 1;
|
---|
[042f82] | 361 | return acos(angle);
|
---|
[6ac7ee] | 362 | };
|
---|
| 363 |
|
---|
[0a4f7f] | 364 |
|
---|
| 365 | double& Vector::operator[](size_t i){
|
---|
[753f02] | 366 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
|
---|
| 367 | return x[i];
|
---|
[0a4f7f] | 368 | }
|
---|
| 369 |
|
---|
| 370 | const double& Vector::operator[](size_t i) const{
|
---|
[753f02] | 371 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
|
---|
| 372 | return x[i];
|
---|
[0a4f7f] | 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(){
|
---|
[753f02] | 384 | return x;
|
---|
[0a4f7f] | 385 | }
|
---|
[6ac7ee] | 386 |
|
---|
[ef9df36] | 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 | */
|
---|
[72e7fa] | 392 | bool Vector::operator==(const Vector& b) const
|
---|
[ef9df36] | 393 | {
|
---|
[1bd79e] | 394 | return IsEqualTo(b);
|
---|
[ef9df36] | 395 | };
|
---|
| 396 |
|
---|
[fa5a6a] | 397 | bool Vector::operator!=(const Vector& b) const
|
---|
| 398 | {
|
---|
| 399 | return !IsEqualTo(b);
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[6ac7ee] | 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 | */
|
---|
[72e7fa] | 407 | const Vector& Vector::operator+=(const Vector& b)
|
---|
[6ac7ee] | 408 | {
|
---|
[273382] | 409 | this->AddVector(b);
|
---|
[72e7fa] | 410 | return *this;
|
---|
[6ac7ee] | 411 | };
|
---|
[54a746] | 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 | */
|
---|
[72e7fa] | 418 | const Vector& Vector::operator-=(const Vector& b)
|
---|
[54a746] | 419 | {
|
---|
[273382] | 420 | this->SubtractVector(b);
|
---|
[72e7fa] | 421 | return *this;
|
---|
[54a746] | 422 | };
|
---|
| 423 |
|
---|
[6ac7ee] | 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 | */
|
---|
[b84d5d] | 429 | const Vector& operator*=(Vector& a, const double m)
|
---|
[6ac7ee] | 430 | {
|
---|
[042f82] | 431 | a.Scale(m);
|
---|
| 432 | return a;
|
---|
[6ac7ee] | 433 | };
|
---|
| 434 |
|
---|
[042f82] | 435 | /** Sums two vectors \a and \b component-wise.
|
---|
[6ac7ee] | 436 | * \param a first vector
|
---|
| 437 | * \param b second vector
|
---|
| 438 | * \return a + b
|
---|
| 439 | */
|
---|
[72e7fa] | 440 | Vector const Vector::operator+(const Vector& b) const
|
---|
[6ac7ee] | 441 | {
|
---|
[72e7fa] | 442 | Vector x = *this;
|
---|
[273382] | 443 | x.AddVector(b);
|
---|
[b84d5d] | 444 | return x;
|
---|
[6ac7ee] | 445 | };
|
---|
| 446 |
|
---|
[54a746] | 447 | /** Subtracts vector \a from \b component-wise.
|
---|
| 448 | * \param a first vector
|
---|
| 449 | * \param b second vector
|
---|
| 450 | * \return a - b
|
---|
| 451 | */
|
---|
[72e7fa] | 452 | Vector const Vector::operator-(const Vector& b) const
|
---|
[54a746] | 453 | {
|
---|
[72e7fa] | 454 | Vector x = *this;
|
---|
[273382] | 455 | x.SubtractVector(b);
|
---|
[b84d5d] | 456 | return x;
|
---|
[54a746] | 457 | };
|
---|
| 458 |
|
---|
[6ac7ee] | 459 | /** Factors given vector \a a times \a m.
|
---|
| 460 | * \param a vector
|
---|
| 461 | * \param m factor
|
---|
[54a746] | 462 | * \return m * a
|
---|
[6ac7ee] | 463 | */
|
---|
[b84d5d] | 464 | Vector const operator*(const Vector& a, const double m)
|
---|
[6ac7ee] | 465 | {
|
---|
[b84d5d] | 466 | Vector x(a);
|
---|
| 467 | x.Scale(m);
|
---|
| 468 | return x;
|
---|
[6ac7ee] | 469 | };
|
---|
| 470 |
|
---|
[54a746] | 471 | /** Factors given vector \a a times \a m.
|
---|
| 472 | * \param m factor
|
---|
| 473 | * \param a vector
|
---|
| 474 | * \return m * a
|
---|
| 475 | */
|
---|
[b84d5d] | 476 | Vector const operator*(const double m, const Vector& a )
|
---|
[54a746] | 477 | {
|
---|
[b84d5d] | 478 | Vector x(a);
|
---|
| 479 | x.Scale(m);
|
---|
| 480 | return x;
|
---|
[54a746] | 481 | };
|
---|
| 482 |
|
---|
[9c20aa] | 483 | ostream& operator<<(ostream& ost, const Vector& m)
|
---|
[6ac7ee] | 484 | {
|
---|
[042f82] | 485 | ost << "(";
|
---|
| 486 | for (int i=0;i<NDIM;i++) {
|
---|
[0a4f7f] | 487 | ost << m[i];
|
---|
[042f82] | 488 | if (i != 2)
|
---|
| 489 | ost << ",";
|
---|
| 490 | }
|
---|
| 491 | ost << ")";
|
---|
| 492 | return ost;
|
---|
[6ac7ee] | 493 | };
|
---|
| 494 |
|
---|
| 495 |
|
---|
[1bd79e] | 496 | void Vector::ScaleAll(const double *factor)
|
---|
[6ac7ee] | 497 | {
|
---|
[042f82] | 498 | for (int i=NDIM;i--;)
|
---|
[753f02] | 499 | x[i] *= factor[i];
|
---|
[6ac7ee] | 500 | };
|
---|
| 501 |
|
---|
| 502 |
|
---|
[1bd79e] | 503 |
|
---|
[776b64] | 504 | void Vector::Scale(const double factor)
|
---|
[6ac7ee] | 505 | {
|
---|
[042f82] | 506 | for (int i=NDIM;i--;)
|
---|
| 507 | x[i] *= factor;
|
---|
[6ac7ee] | 508 | };
|
---|
| 509 |
|
---|
[d09ff7] | 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 | */
|
---|
[776b64] | 514 | void Vector::WrapPeriodically(const double * const M, const double * const Minv)
|
---|
[d09ff7] | 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 |
|
---|
[45ef76] | 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 |
|
---|
[6ac7ee] | 545 | /** Do a matrix multiplication.
|
---|
| 546 | * \param *matrix NDIM_NDIM array
|
---|
| 547 | */
|
---|
[776b64] | 548 | void Vector::MatrixMultiplication(const double * const M)
|
---|
[6ac7ee] | 549 | {
|
---|
[042f82] | 550 | // do the matrix multiplication
|
---|
[753f02] | 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];
|
---|
[6ac7ee] | 554 | };
|
---|
| 555 |
|
---|
[2319ed] | 556 | /** Do a matrix multiplication with the \a *A' inverse.
|
---|
[6ac7ee] | 557 | * \param *matrix NDIM_NDIM array
|
---|
| 558 | */
|
---|
[0a4f7f] | 559 | bool Vector::InverseMatrixMultiplication(const double * const A)
|
---|
[6ac7ee] | 560 | {
|
---|
[042f82] | 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
|
---|
[753f02] | 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;
|
---|
[042f82] | 584 | } else {
|
---|
[753f02] | 585 | return false;
|
---|
[042f82] | 586 | }
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 597 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
|
---|
[6ac7ee] | 598 | {
|
---|
[273382] | 599 | (*this) = (factors[0]*x1) +
|
---|
| 600 | (factors[1]*x2) +
|
---|
| 601 | (factors[2]*x3);
|
---|
[6ac7ee] | 602 | };
|
---|
| 603 |
|
---|
| 604 | /** Calculates orthonormal vector to one given vectors.
|
---|
| 605 | * Just subtracts the projection onto the given vector from this vector.
|
---|
[ef9df36] | 606 | * The removed part of the vector is Vector::Projection()
|
---|
[6ac7ee] | 607 | * \param *x1 vector
|
---|
| 608 | * \return true - success, false - vector is zero
|
---|
| 609 | */
|
---|
[0a4f7f] | 610 | bool Vector::MakeNormalTo(const Vector &y1)
|
---|
[6ac7ee] | 611 | {
|
---|
[042f82] | 612 | bool result = false;
|
---|
[753f02] | 613 | double factor = y1.ScalarProduct(*this)/y1.NormSquared();
|
---|
[45ef76] | 614 | Vector x1 = factor * y1;
|
---|
[753f02] | 615 | SubtractVector(x1);
|
---|
[042f82] | 616 | for (int i=NDIM;i--;)
|
---|
| 617 | result = result || (fabs(x[i]) > MYEPSILON);
|
---|
[6ac7ee] | 618 |
|
---|
[042f82] | 619 | return result;
|
---|
[6ac7ee] | 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 | */
|
---|
[273382] | 628 | bool Vector::GetOneNormalVector(const Vector &GivenVector)
|
---|
[6ac7ee] | 629 | {
|
---|
[042f82] | 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;
|
---|
[1829c4] | 637 |
|
---|
| 638 | // in two component-systems we need to find the one position that is zero
|
---|
| 639 | int zeroPos = -1;
|
---|
[042f82] | 640 | // find two components != 0
|
---|
[1829c4] | 641 | for (j=0;j<NDIM;j++){
|
---|
[753f02] | 642 | if (fabs(GivenVector[j]) > MYEPSILON)
|
---|
[042f82] | 643 | Components[Last++] = j;
|
---|
[1829c4] | 644 | else
|
---|
| 645 | // this our zero Position
|
---|
| 646 | zeroPos = j;
|
---|
| 647 | }
|
---|
[042f82] | 648 |
|
---|
| 649 | switch(Last) {
|
---|
| 650 | case 3: // threecomponent system
|
---|
[1829c4] | 651 | // the position of the zero is arbitrary in three component systems
|
---|
| 652 | zeroPos = Components[2];
|
---|
[042f82] | 653 | case 2: // two component system
|
---|
[753f02] | 654 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
|
---|
[1829c4] | 655 | at(zeroPos) = 0.;
|
---|
[042f82] | 656 | // in skp both remaining parts shall become zero but with opposite sign and third is zero
|
---|
[1829c4] | 657 | at(Components[1]) = -1./GivenVector[Components[1]] / norm;
|
---|
| 658 | at(Components[0]) = 1./GivenVector[Components[0]] / norm;
|
---|
[042f82] | 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
|
---|
[1829c4] | 663 | at((Components[0]+2)%NDIM) = 0.;
|
---|
| 664 | at((Components[0]+1)%NDIM) = 1.;
|
---|
| 665 | at(Components[0]) = 0.;
|
---|
[042f82] | 666 | return true;
|
---|
| 667 | break;
|
---|
| 668 | default:
|
---|
| 669 | return false;
|
---|
| 670 | }
|
---|
[6ac7ee] | 671 | };
|
---|
| 672 |
|
---|
| 673 | /** Adds vector \a *y componentwise.
|
---|
| 674 | * \param *y vector
|
---|
| 675 | */
|
---|
[273382] | 676 | void Vector::AddVector(const Vector &y)
|
---|
[6ac7ee] | 677 | {
|
---|
[753f02] | 678 | for(int i=NDIM;i--;)
|
---|
| 679 | x[i] += y[i];
|
---|
[6ac7ee] | 680 | }
|
---|
| 681 |
|
---|
| 682 | /** Adds vector \a *y componentwise.
|
---|
| 683 | * \param *y vector
|
---|
| 684 | */
|
---|
[273382] | 685 | void Vector::SubtractVector(const Vector &y)
|
---|
[6ac7ee] | 686 | {
|
---|
[753f02] | 687 | for(int i=NDIM;i--;)
|
---|
| 688 | x[i] -= y[i];
|
---|
[ef9df36] | 689 | }
|
---|
| 690 |
|
---|
[89c8b2] | 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 | */
|
---|
[776b64] | 698 | bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
|
---|
[89c8b2] | 699 | {
|
---|
[753f02] | 700 | Vector a = (*this)-offset;
|
---|
[89c8b2] | 701 | a.InverseMatrixMultiplication(parallelepiped);
|
---|
| 702 | bool isInside = true;
|
---|
| 703 |
|
---|
| 704 | for (int i=NDIM;i--;)
|
---|
[753f02] | 705 | isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
|
---|
[89c8b2] | 706 |
|
---|
| 707 | return isInside;
|
---|
| 708 | }
|
---|
[005e18] | 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);
|
---|