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