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