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