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