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