| 1 | /** \file vector.cpp | 
|---|
| 2 | * | 
|---|
| 3 | * Function implementations for the class vector. | 
|---|
| 4 | * | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #include "Helpers/MemDebug.hpp" | 
|---|
| 8 |  | 
|---|
| 9 | #include "vector.hpp" | 
|---|
| 10 | #include "Matrix.hpp" | 
|---|
| 11 | #include "verbose.hpp" | 
|---|
| 12 | #include "World.hpp" | 
|---|
| 13 | #include "Helpers/Assert.hpp" | 
|---|
| 14 | #include "Helpers/fast_functions.hpp" | 
|---|
| 15 | #include "Exceptions/MathException.hpp" | 
|---|
| 16 |  | 
|---|
| 17 | #include <iostream> | 
|---|
| 18 | #include <gsl/gsl_blas.h> | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | using namespace std; | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | /************************************ Functions for class vector ************************************/ | 
|---|
| 25 |  | 
|---|
| 26 | /** Constructor of class vector. | 
|---|
| 27 | */ | 
|---|
| 28 | Vector::Vector() | 
|---|
| 29 | { | 
|---|
| 30 | content = gsl_vector_calloc (NDIM); | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 | * Copy constructor | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | Vector::Vector(const Vector& src) | 
|---|
| 38 | { | 
|---|
| 39 | content = gsl_vector_alloc(NDIM); | 
|---|
| 40 | gsl_vector_memcpy(content, src.content); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | /** Constructor of class vector. | 
|---|
| 44 | */ | 
|---|
| 45 | Vector::Vector(const double x1, const double x2, const double x3) | 
|---|
| 46 | { | 
|---|
| 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); | 
|---|
| 51 | }; | 
|---|
| 52 |  | 
|---|
| 53 | Vector::Vector(gsl_vector *_content) : | 
|---|
| 54 | content(_content) | 
|---|
| 55 | {} | 
|---|
| 56 |  | 
|---|
| 57 | /** | 
|---|
| 58 | * Assignment operator | 
|---|
| 59 | */ | 
|---|
| 60 | Vector& Vector::operator=(const Vector& src){ | 
|---|
| 61 | // check for self assignment | 
|---|
| 62 | if(&src!=this){ | 
|---|
| 63 | gsl_vector_memcpy(content, src.content); | 
|---|
| 64 | } | 
|---|
| 65 | return *this; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | /** Desctructor of class vector. | 
|---|
| 69 | */ | 
|---|
| 70 | Vector::~Vector() { | 
|---|
| 71 | gsl_vector_free(content); | 
|---|
| 72 | }; | 
|---|
| 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 | */ | 
|---|
| 78 | double Vector::DistanceSquared(const Vector &y) const | 
|---|
| 79 | { | 
|---|
| 80 | double res = 0.; | 
|---|
| 81 | for (int i=NDIM;i--;) | 
|---|
| 82 | res += (at(i)-y[i])*(at(i)-y[i]); | 
|---|
| 83 | return (res); | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | /** Calculates distance between this and another vector. | 
|---|
| 87 | * \param *y array to second vector | 
|---|
| 88 | * \return \f$| x - y |\f$ | 
|---|
| 89 | */ | 
|---|
| 90 | double Vector::distance(const Vector &y) const | 
|---|
| 91 | { | 
|---|
| 92 | return (sqrt(DistanceSquared(y))); | 
|---|
| 93 | }; | 
|---|
| 94 |  | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 105 | double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const | 
|---|
| 106 | { | 
|---|
| 107 | return sqrt(PeriodicDistanceSquared(y,cell_size)); | 
|---|
| 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 | */ | 
|---|
| 115 | double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const | 
|---|
| 116 | { | 
|---|
| 117 | double res = DistanceSquared(y), tmp; | 
|---|
| 118 | Matrix matrix = ReturnFullMatrixforSymmetric(cell_size); | 
|---|
| 119 | Vector Shiftedy, TranslationVector; | 
|---|
| 120 | int N[NDIM]; | 
|---|
| 121 |  | 
|---|
| 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); | 
|---|
| 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 | */ | 
|---|
| 144 | double Vector::ScalarProduct(const Vector &y) const | 
|---|
| 145 | { | 
|---|
| 146 | double res = 0.; | 
|---|
| 147 | gsl_blas_ddot(content, y.content, &res); | 
|---|
| 148 | return (res); | 
|---|
| 149 | }; | 
|---|
| 150 |  | 
|---|
| 151 |  | 
|---|
| 152 | /** Calculates VectorProduct between this and another vector. | 
|---|
| 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& | 
|---|
| 157 | */ | 
|---|
| 158 | void Vector::VectorProduct(const Vector &y) | 
|---|
| 159 | { | 
|---|
| 160 | Vector tmp; | 
|---|
| 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]; | 
|---|
| 163 | (*this) = tmp; | 
|---|
| 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 | */ | 
|---|
| 171 | void Vector::ProjectOntoPlane(const Vector &y) | 
|---|
| 172 | { | 
|---|
| 173 | Vector tmp; | 
|---|
| 174 | tmp = y; | 
|---|
| 175 | tmp.Normalize(); | 
|---|
| 176 | tmp.Scale(ScalarProduct(tmp)); | 
|---|
| 177 | *this -= tmp; | 
|---|
| 178 | }; | 
|---|
| 179 |  | 
|---|
| 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 | */ | 
|---|
| 187 | double Vector::DistanceToSpace(const Space &space) const | 
|---|
| 188 | { | 
|---|
| 189 | return space.distance(*this); | 
|---|
| 190 | }; | 
|---|
| 191 |  | 
|---|
| 192 | /** Calculates the projection of a vector onto another \a *y. | 
|---|
| 193 | * \param *y array to second vector | 
|---|
| 194 | */ | 
|---|
| 195 | void Vector::ProjectIt(const Vector &y) | 
|---|
| 196 | { | 
|---|
| 197 | (*this) += (-ScalarProduct(y))*y; | 
|---|
| 198 | }; | 
|---|
| 199 |  | 
|---|
| 200 | /** Calculates the projection of a vector onto another \a *y. | 
|---|
| 201 | * \param *y array to second vector | 
|---|
| 202 | * \return Vector | 
|---|
| 203 | */ | 
|---|
| 204 | Vector Vector::Projection(const Vector &y) const | 
|---|
| 205 | { | 
|---|
| 206 | Vector helper = y; | 
|---|
| 207 | helper.Scale((ScalarProduct(y)/y.NormSquared())); | 
|---|
| 208 |  | 
|---|
| 209 | return helper; | 
|---|
| 210 | }; | 
|---|
| 211 |  | 
|---|
| 212 | /** Calculates norm of this vector. | 
|---|
| 213 | * \return \f$|x|\f$ | 
|---|
| 214 | */ | 
|---|
| 215 | double Vector::Norm() const | 
|---|
| 216 | { | 
|---|
| 217 | return (sqrt(NormSquared())); | 
|---|
| 218 | }; | 
|---|
| 219 |  | 
|---|
| 220 | /** Calculates squared norm of this vector. | 
|---|
| 221 | * \return \f$|x|^2\f$ | 
|---|
| 222 | */ | 
|---|
| 223 | double Vector::NormSquared() const | 
|---|
| 224 | { | 
|---|
| 225 | return (ScalarProduct(*this)); | 
|---|
| 226 | }; | 
|---|
| 227 |  | 
|---|
| 228 | /** Normalizes this vector. | 
|---|
| 229 | */ | 
|---|
| 230 | void Vector::Normalize() | 
|---|
| 231 | { | 
|---|
| 232 | double factor = Norm(); | 
|---|
| 233 | (*this) *= 1/factor; | 
|---|
| 234 | }; | 
|---|
| 235 |  | 
|---|
| 236 | /** Zeros all components of this vector. | 
|---|
| 237 | */ | 
|---|
| 238 | void Vector::Zero() | 
|---|
| 239 | { | 
|---|
| 240 | at(0)=at(1)=at(2)=0; | 
|---|
| 241 | }; | 
|---|
| 242 |  | 
|---|
| 243 | /** Zeros all components of this vector. | 
|---|
| 244 | */ | 
|---|
| 245 | void Vector::One(const double one) | 
|---|
| 246 | { | 
|---|
| 247 | at(0)=at(1)=at(2)=one; | 
|---|
| 248 | }; | 
|---|
| 249 |  | 
|---|
| 250 | /** Checks whether vector has all components zero. | 
|---|
| 251 | * @return true - vector is zero, false - vector is not | 
|---|
| 252 | */ | 
|---|
| 253 | bool Vector::IsZero() const | 
|---|
| 254 | { | 
|---|
| 255 | return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON); | 
|---|
| 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); | 
|---|
| 264 | }; | 
|---|
| 265 |  | 
|---|
| 266 | /** Checks whether vector is normal to \a *normal. | 
|---|
| 267 | * @return true - vector is normalized, false - vector is not | 
|---|
| 268 | */ | 
|---|
| 269 | bool Vector::IsNormalTo(const Vector &normal) const | 
|---|
| 270 | { | 
|---|
| 271 | if (ScalarProduct(normal) < MYEPSILON) | 
|---|
| 272 | return true; | 
|---|
| 273 | else | 
|---|
| 274 | return false; | 
|---|
| 275 | }; | 
|---|
| 276 |  | 
|---|
| 277 | /** Checks whether vector is normal to \a *normal. | 
|---|
| 278 | * @return true - vector is normalized, false - vector is not | 
|---|
| 279 | */ | 
|---|
| 280 | bool Vector::IsEqualTo(const Vector &a) const | 
|---|
| 281 | { | 
|---|
| 282 | bool status = true; | 
|---|
| 283 | for (int i=0;i<NDIM;i++) { | 
|---|
| 284 | if (fabs(at(i) - a[i]) > MYEPSILON) | 
|---|
| 285 | status = false; | 
|---|
| 286 | } | 
|---|
| 287 | return status; | 
|---|
| 288 | }; | 
|---|
| 289 |  | 
|---|
| 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 | */ | 
|---|
| 294 | double Vector::Angle(const Vector &y) const | 
|---|
| 295 | { | 
|---|
| 296 | double norm1 = Norm(), norm2 = y.Norm(); | 
|---|
| 297 | double angle = -1; | 
|---|
| 298 | if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON)) | 
|---|
| 299 | angle = this->ScalarProduct(y)/norm1/norm2; | 
|---|
| 300 | // -1-MYEPSILON occured due to numerical imprecision, catch ... | 
|---|
| 301 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl; | 
|---|
| 302 | if (angle < -1) | 
|---|
| 303 | angle = -1; | 
|---|
| 304 | if (angle > 1) | 
|---|
| 305 | angle = 1; | 
|---|
| 306 | return acos(angle); | 
|---|
| 307 | }; | 
|---|
| 308 |  | 
|---|
| 309 |  | 
|---|
| 310 | double& Vector::operator[](size_t i){ | 
|---|
| 311 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range"); | 
|---|
| 312 | return *gsl_vector_ptr (content, i); | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | const double& Vector::operator[](size_t i) const{ | 
|---|
| 316 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range"); | 
|---|
| 317 | return *gsl_vector_ptr (content, i); | 
|---|
| 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 |  | 
|---|
| 328 | gsl_vector* Vector::get(){ | 
|---|
| 329 | return content; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 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 | */ | 
|---|
| 337 | bool Vector::operator==(const Vector& b) const | 
|---|
| 338 | { | 
|---|
| 339 | return IsEqualTo(b); | 
|---|
| 340 | }; | 
|---|
| 341 |  | 
|---|
| 342 | bool Vector::operator!=(const Vector& b) const | 
|---|
| 343 | { | 
|---|
| 344 | return !IsEqualTo(b); | 
|---|
| 345 | } | 
|---|
| 346 |  | 
|---|
| 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 | */ | 
|---|
| 352 | const Vector& Vector::operator+=(const Vector& b) | 
|---|
| 353 | { | 
|---|
| 354 | this->AddVector(b); | 
|---|
| 355 | return *this; | 
|---|
| 356 | }; | 
|---|
| 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 | */ | 
|---|
| 363 | const Vector& Vector::operator-=(const Vector& b) | 
|---|
| 364 | { | 
|---|
| 365 | this->SubtractVector(b); | 
|---|
| 366 | return *this; | 
|---|
| 367 | }; | 
|---|
| 368 |  | 
|---|
| 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 | */ | 
|---|
| 374 | const Vector& operator*=(Vector& a, const double m) | 
|---|
| 375 | { | 
|---|
| 376 | a.Scale(m); | 
|---|
| 377 | return a; | 
|---|
| 378 | }; | 
|---|
| 379 |  | 
|---|
| 380 | /** Sums two vectors \a  and \b component-wise. | 
|---|
| 381 | * \param a first vector | 
|---|
| 382 | * \param b second vector | 
|---|
| 383 | * \return a + b | 
|---|
| 384 | */ | 
|---|
| 385 | Vector const Vector::operator+(const Vector& b) const | 
|---|
| 386 | { | 
|---|
| 387 | Vector x = *this; | 
|---|
| 388 | x.AddVector(b); | 
|---|
| 389 | return x; | 
|---|
| 390 | }; | 
|---|
| 391 |  | 
|---|
| 392 | /** Subtracts vector \a from \b component-wise. | 
|---|
| 393 | * \param a first vector | 
|---|
| 394 | * \param b second vector | 
|---|
| 395 | * \return a - b | 
|---|
| 396 | */ | 
|---|
| 397 | Vector const Vector::operator-(const Vector& b) const | 
|---|
| 398 | { | 
|---|
| 399 | Vector x = *this; | 
|---|
| 400 | x.SubtractVector(b); | 
|---|
| 401 | return x; | 
|---|
| 402 | }; | 
|---|
| 403 |  | 
|---|
| 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 |  | 
|---|
| 416 | /** Factors given vector \a a times \a m. | 
|---|
| 417 | * \param a vector | 
|---|
| 418 | * \param m factor | 
|---|
| 419 | * \return m * a | 
|---|
| 420 | */ | 
|---|
| 421 | Vector const operator*(const Vector& a, const double m) | 
|---|
| 422 | { | 
|---|
| 423 | Vector x(a); | 
|---|
| 424 | x.Scale(m); | 
|---|
| 425 | return x; | 
|---|
| 426 | }; | 
|---|
| 427 |  | 
|---|
| 428 | /** Factors given vector \a a times \a m. | 
|---|
| 429 | * \param m factor | 
|---|
| 430 | * \param a vector | 
|---|
| 431 | * \return m * a | 
|---|
| 432 | */ | 
|---|
| 433 | Vector const operator*(const double m, const Vector& a ) | 
|---|
| 434 | { | 
|---|
| 435 | Vector x(a); | 
|---|
| 436 | x.Scale(m); | 
|---|
| 437 | return x; | 
|---|
| 438 | }; | 
|---|
| 439 |  | 
|---|
| 440 | ostream& operator<<(ostream& ost, const Vector& m) | 
|---|
| 441 | { | 
|---|
| 442 | ost << "("; | 
|---|
| 443 | for (int i=0;i<NDIM;i++) { | 
|---|
| 444 | ost << m[i]; | 
|---|
| 445 | if (i != 2) | 
|---|
| 446 | ost << ","; | 
|---|
| 447 | } | 
|---|
| 448 | ost << ")"; | 
|---|
| 449 | return ost; | 
|---|
| 450 | }; | 
|---|
| 451 |  | 
|---|
| 452 |  | 
|---|
| 453 | void Vector::ScaleAll(const double *factor) | 
|---|
| 454 | { | 
|---|
| 455 | for (int i=NDIM;i--;) | 
|---|
| 456 | at(i) *= factor[i]; | 
|---|
| 457 | }; | 
|---|
| 458 |  | 
|---|
| 459 |  | 
|---|
| 460 |  | 
|---|
| 461 | void Vector::Scale(const double factor) | 
|---|
| 462 | { | 
|---|
| 463 | gsl_vector_scale(content,factor); | 
|---|
| 464 | }; | 
|---|
| 465 |  | 
|---|
| 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 | */ | 
|---|
| 470 | void Vector::WrapPeriodically(const Matrix &M, const Matrix &Minv) | 
|---|
| 471 | { | 
|---|
| 472 | MatrixMultiplication(Minv); | 
|---|
| 473 | // truncate to [0,1] for each axis | 
|---|
| 474 | for (int i=0;i<NDIM;i++) { | 
|---|
| 475 | //at(i) += 0.5;  // set to center of box | 
|---|
| 476 | while (at(i) >= 1.) | 
|---|
| 477 | at(i) -= 1.; | 
|---|
| 478 | while (at(i) < 0.) | 
|---|
| 479 | at(i) += 1.; | 
|---|
| 480 | } | 
|---|
| 481 | MatrixMultiplication(M); | 
|---|
| 482 | }; | 
|---|
| 483 |  | 
|---|
| 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 |  | 
|---|
| 501 | /** Do a matrix multiplication. | 
|---|
| 502 | * \param *matrix NDIM_NDIM array | 
|---|
| 503 | */ | 
|---|
| 504 | void Vector::MatrixMultiplication(const Matrix &M) | 
|---|
| 505 | { | 
|---|
| 506 | (*this) *= M; | 
|---|
| 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 | */ | 
|---|
| 516 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors) | 
|---|
| 517 | { | 
|---|
| 518 | (*this) = (factors[0]*x1) + | 
|---|
| 519 | (factors[1]*x2) + | 
|---|
| 520 | (factors[2]*x3); | 
|---|
| 521 | }; | 
|---|
| 522 |  | 
|---|
| 523 | /** Calculates orthonormal vector to one given vectors. | 
|---|
| 524 | * Just subtracts the projection onto the given vector from this vector. | 
|---|
| 525 | * The removed part of the vector is Vector::Projection() | 
|---|
| 526 | * \param *x1 vector | 
|---|
| 527 | * \return true - success, false - vector is zero | 
|---|
| 528 | */ | 
|---|
| 529 | bool Vector::MakeNormalTo(const Vector &y1) | 
|---|
| 530 | { | 
|---|
| 531 | bool result = false; | 
|---|
| 532 | double factor = y1.ScalarProduct(*this)/y1.NormSquared(); | 
|---|
| 533 | Vector x1 = factor * y1; | 
|---|
| 534 | SubtractVector(x1); | 
|---|
| 535 | for (int i=NDIM;i--;) | 
|---|
| 536 | result = result || (fabs(at(i)) > MYEPSILON); | 
|---|
| 537 |  | 
|---|
| 538 | return result; | 
|---|
| 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 | */ | 
|---|
| 547 | bool Vector::GetOneNormalVector(const Vector &GivenVector) | 
|---|
| 548 | { | 
|---|
| 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; | 
|---|
| 556 |  | 
|---|
| 557 | // in two component-systems we need to find the one position that is zero | 
|---|
| 558 | int zeroPos = -1; | 
|---|
| 559 | // find two components != 0 | 
|---|
| 560 | for (j=0;j<NDIM;j++){ | 
|---|
| 561 | if (fabs(GivenVector[j]) > MYEPSILON) | 
|---|
| 562 | Components[Last++] = j; | 
|---|
| 563 | else | 
|---|
| 564 | // this our zero Position | 
|---|
| 565 | zeroPos = j; | 
|---|
| 566 | } | 
|---|
| 567 |  | 
|---|
| 568 | switch(Last) { | 
|---|
| 569 | case 3:  // threecomponent system | 
|---|
| 570 | // the position of the zero is arbitrary in three component systems | 
|---|
| 571 | zeroPos = Components[2]; | 
|---|
| 572 | case 2:  // two component system | 
|---|
| 573 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]])); | 
|---|
| 574 | at(zeroPos) = 0.; | 
|---|
| 575 | // in skp both remaining parts shall become zero but with opposite sign and third is zero | 
|---|
| 576 | at(Components[1]) = -1./GivenVector[Components[1]] / norm; | 
|---|
| 577 | at(Components[0]) = 1./GivenVector[Components[0]] / norm; | 
|---|
| 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 | 
|---|
| 582 | at((Components[0]+2)%NDIM) = 0.; | 
|---|
| 583 | at((Components[0]+1)%NDIM) = 1.; | 
|---|
| 584 | at(Components[0]) = 0.; | 
|---|
| 585 | return true; | 
|---|
| 586 | break; | 
|---|
| 587 | default: | 
|---|
| 588 | return false; | 
|---|
| 589 | } | 
|---|
| 590 | }; | 
|---|
| 591 |  | 
|---|
| 592 | /** Adds vector \a *y componentwise. | 
|---|
| 593 | * \param *y vector | 
|---|
| 594 | */ | 
|---|
| 595 | void Vector::AddVector(const Vector &y) | 
|---|
| 596 | { | 
|---|
| 597 | gsl_vector_add(content, y.content); | 
|---|
| 598 | } | 
|---|
| 599 |  | 
|---|
| 600 | /** Adds vector \a *y componentwise. | 
|---|
| 601 | * \param *y vector | 
|---|
| 602 | */ | 
|---|
| 603 | void Vector::SubtractVector(const Vector &y) | 
|---|
| 604 | { | 
|---|
| 605 | gsl_vector_sub(content, y.content); | 
|---|
| 606 | } | 
|---|
| 607 |  | 
|---|
| 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 | */ | 
|---|
| 615 | bool Vector::IsInParallelepiped(const Vector &offset, const double * const _parallelepiped) const | 
|---|
| 616 | { | 
|---|
| 617 | Vector a = (*this)-offset; | 
|---|
| 618 | Matrix parallelepiped = Matrix(_parallelepiped).invert(); | 
|---|
| 619 | a.MatrixMultiplication(parallelepiped); | 
|---|
| 620 | bool isInside = true; | 
|---|
| 621 |  | 
|---|
| 622 | for (int i=NDIM;i--;) | 
|---|
| 623 | isInside = isInside && ((a[i] <= 1) && (a[i] >= 0)); | 
|---|
| 624 |  | 
|---|
| 625 | return isInside; | 
|---|
| 626 | } | 
|---|
| 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); | 
|---|