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