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