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