| 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 |  | 
|---|
| 8 | /** \file vector.cpp | 
|---|
| 9 | * | 
|---|
| 10 | * Function implementations for the class vector. | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | // include config.h | 
|---|
| 15 | #ifdef HAVE_CONFIG_H | 
|---|
| 16 | #include <config.h> | 
|---|
| 17 | #endif | 
|---|
| 18 |  | 
|---|
| 19 | #include "Helpers/MemDebug.hpp" | 
|---|
| 20 |  | 
|---|
| 21 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 22 | #include "VectorContent.hpp" | 
|---|
| 23 | #include "Helpers/Verbose.hpp" | 
|---|
| 24 | #include "World.hpp" | 
|---|
| 25 | #include "Helpers/Assert.hpp" | 
|---|
| 26 | #include "Helpers/fast_functions.hpp" | 
|---|
| 27 | #include "Exceptions/MathException.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | #include <iostream> | 
|---|
| 30 | #include <gsl/gsl_blas.h> | 
|---|
| 31 | #include <gsl/gsl_vector.h> | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | using namespace std; | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | /************************************ Functions for class vector ************************************/ | 
|---|
| 38 |  | 
|---|
| 39 | /** Constructor of class vector. | 
|---|
| 40 | */ | 
|---|
| 41 | Vector::Vector() | 
|---|
| 42 | { | 
|---|
| 43 | content = new VectorContent(); | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 | * Copy constructor | 
|---|
| 48 | */ | 
|---|
| 49 |  | 
|---|
| 50 | Vector::Vector(const Vector& src) | 
|---|
| 51 | { | 
|---|
| 52 | content = new VectorContent(); | 
|---|
| 53 | gsl_vector_memcpy(content->content, src.content->content); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | /** Constructor of class vector. | 
|---|
| 57 | */ | 
|---|
| 58 | Vector::Vector(const double x1, const double x2, const double x3) | 
|---|
| 59 | { | 
|---|
| 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); | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 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 |  | 
|---|
| 76 | Vector::Vector(VectorContent *_content) : | 
|---|
| 77 | content(_content) | 
|---|
| 78 | {} | 
|---|
| 79 |  | 
|---|
| 80 | /** | 
|---|
| 81 | * Assignment operator | 
|---|
| 82 | */ | 
|---|
| 83 | Vector& Vector::operator=(const Vector& src){ | 
|---|
| 84 | // check for self assignment | 
|---|
| 85 | if(&src!=this){ | 
|---|
| 86 | gsl_vector_memcpy(content->content, src.content->content); | 
|---|
| 87 | } | 
|---|
| 88 | return *this; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** Desctructor of class vector. | 
|---|
| 92 | */ | 
|---|
| 93 | Vector::~Vector() { | 
|---|
| 94 | delete content; | 
|---|
| 95 | }; | 
|---|
| 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 | */ | 
|---|
| 101 | double Vector::DistanceSquared(const Vector &y) const | 
|---|
| 102 | { | 
|---|
| 103 | double res = 0.; | 
|---|
| 104 | for (int i=NDIM;i--;) | 
|---|
| 105 | res += (at(i)-y[i])*(at(i)-y[i]); | 
|---|
| 106 | return (res); | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | /** Calculates distance between this and another vector. | 
|---|
| 110 | * \param *y array to second vector | 
|---|
| 111 | * \return \f$| x - y |\f$ | 
|---|
| 112 | */ | 
|---|
| 113 | double Vector::distance(const Vector &y) const | 
|---|
| 114 | { | 
|---|
| 115 | return (sqrt(DistanceSquared(y))); | 
|---|
| 116 | }; | 
|---|
| 117 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 148 | double Vector::ScalarProduct(const Vector &y) const | 
|---|
| 149 | { | 
|---|
| 150 | double res = 0.; | 
|---|
| 151 | gsl_blas_ddot(content->content, y.content->content, &res); | 
|---|
| 152 | return (res); | 
|---|
| 153 | }; | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 | /** Calculates VectorProduct between this and another vector. | 
|---|
| 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& | 
|---|
| 161 | */ | 
|---|
| 162 | void Vector::VectorProduct(const Vector &y) | 
|---|
| 163 | { | 
|---|
| 164 | Vector tmp; | 
|---|
| 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]; | 
|---|
| 167 | (*this) = tmp; | 
|---|
| 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 | */ | 
|---|
| 175 | void Vector::ProjectOntoPlane(const Vector &y) | 
|---|
| 176 | { | 
|---|
| 177 | Vector tmp; | 
|---|
| 178 | tmp = y; | 
|---|
| 179 | tmp.Normalize(); | 
|---|
| 180 | tmp.Scale(ScalarProduct(tmp)); | 
|---|
| 181 | *this -= tmp; | 
|---|
| 182 | }; | 
|---|
| 183 |  | 
|---|
| 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 | */ | 
|---|
| 191 | double Vector::DistanceToSpace(const Space &space) const | 
|---|
| 192 | { | 
|---|
| 193 | return space.distance(*this); | 
|---|
| 194 | }; | 
|---|
| 195 |  | 
|---|
| 196 | /** Calculates the projection of a vector onto another \a *y. | 
|---|
| 197 | * \param *y array to second vector | 
|---|
| 198 | */ | 
|---|
| 199 | void Vector::ProjectIt(const Vector &y) | 
|---|
| 200 | { | 
|---|
| 201 | (*this) += (-ScalarProduct(y))*y; | 
|---|
| 202 | }; | 
|---|
| 203 |  | 
|---|
| 204 | /** Calculates the projection of a vector onto another \a *y. | 
|---|
| 205 | * \param *y array to second vector | 
|---|
| 206 | * \return Vector | 
|---|
| 207 | */ | 
|---|
| 208 | Vector Vector::Projection(const Vector &y) const | 
|---|
| 209 | { | 
|---|
| 210 | Vector helper = y; | 
|---|
| 211 | helper.Scale((ScalarProduct(y)/y.NormSquared())); | 
|---|
| 212 |  | 
|---|
| 213 | return helper; | 
|---|
| 214 | }; | 
|---|
| 215 |  | 
|---|
| 216 | /** Calculates norm of this vector. | 
|---|
| 217 | * \return \f$|x|\f$ | 
|---|
| 218 | */ | 
|---|
| 219 | double Vector::Norm() const | 
|---|
| 220 | { | 
|---|
| 221 | return (sqrt(NormSquared())); | 
|---|
| 222 | }; | 
|---|
| 223 |  | 
|---|
| 224 | /** Calculates squared norm of this vector. | 
|---|
| 225 | * \return \f$|x|^2\f$ | 
|---|
| 226 | */ | 
|---|
| 227 | double Vector::NormSquared() const | 
|---|
| 228 | { | 
|---|
| 229 | return (ScalarProduct(*this)); | 
|---|
| 230 | }; | 
|---|
| 231 |  | 
|---|
| 232 | /** Normalizes this vector. | 
|---|
| 233 | */ | 
|---|
| 234 | void Vector::Normalize() | 
|---|
| 235 | { | 
|---|
| 236 | double factor = Norm(); | 
|---|
| 237 | (*this) *= 1/factor; | 
|---|
| 238 | }; | 
|---|
| 239 |  | 
|---|
| 240 | Vector Vector::getNormalized() const{ | 
|---|
| 241 | Vector res= *this; | 
|---|
| 242 | res.Normalize(); | 
|---|
| 243 | return res; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | /** Zeros all components of this vector. | 
|---|
| 247 | */ | 
|---|
| 248 | void Vector::Zero() | 
|---|
| 249 | { | 
|---|
| 250 | at(0)=at(1)=at(2)=0; | 
|---|
| 251 | }; | 
|---|
| 252 |  | 
|---|
| 253 | /** Zeros all components of this vector. | 
|---|
| 254 | */ | 
|---|
| 255 | void Vector::One(const double one) | 
|---|
| 256 | { | 
|---|
| 257 | at(0)=at(1)=at(2)=one; | 
|---|
| 258 | }; | 
|---|
| 259 |  | 
|---|
| 260 | /** Checks whether vector has all components zero. | 
|---|
| 261 | * @return true - vector is zero, false - vector is not | 
|---|
| 262 | */ | 
|---|
| 263 | bool Vector::IsZero() const | 
|---|
| 264 | { | 
|---|
| 265 | return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON); | 
|---|
| 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); | 
|---|
| 274 | }; | 
|---|
| 275 |  | 
|---|
| 276 | /** Checks whether vector is normal to \a *normal. | 
|---|
| 277 | * @return true - vector is normalized, false - vector is not | 
|---|
| 278 | */ | 
|---|
| 279 | bool Vector::IsNormalTo(const Vector &normal) const | 
|---|
| 280 | { | 
|---|
| 281 | if (ScalarProduct(normal) < MYEPSILON) | 
|---|
| 282 | return true; | 
|---|
| 283 | else | 
|---|
| 284 | return false; | 
|---|
| 285 | }; | 
|---|
| 286 |  | 
|---|
| 287 | /** Checks whether vector is normal to \a *normal. | 
|---|
| 288 | * @return true - vector is normalized, false - vector is not | 
|---|
| 289 | */ | 
|---|
| 290 | bool Vector::IsEqualTo(const Vector &a) const | 
|---|
| 291 | { | 
|---|
| 292 | bool status = true; | 
|---|
| 293 | for (int i=0;i<NDIM;i++) { | 
|---|
| 294 | if (fabs(at(i) - a[i]) > MYEPSILON) | 
|---|
| 295 | status = false; | 
|---|
| 296 | } | 
|---|
| 297 | return status; | 
|---|
| 298 | }; | 
|---|
| 299 |  | 
|---|
| 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 | */ | 
|---|
| 304 | double Vector::Angle(const Vector &y) const | 
|---|
| 305 | { | 
|---|
| 306 | double norm1 = Norm(), norm2 = y.Norm(); | 
|---|
| 307 | double angle = -1; | 
|---|
| 308 | if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON)) | 
|---|
| 309 | angle = this->ScalarProduct(y)/norm1/norm2; | 
|---|
| 310 | // -1-MYEPSILON occured due to numerical imprecision, catch ... | 
|---|
| 311 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl; | 
|---|
| 312 | if (angle < -1) | 
|---|
| 313 | angle = -1; | 
|---|
| 314 | if (angle > 1) | 
|---|
| 315 | angle = 1; | 
|---|
| 316 | return acos(angle); | 
|---|
| 317 | }; | 
|---|
| 318 |  | 
|---|
| 319 |  | 
|---|
| 320 | double& Vector::operator[](size_t i){ | 
|---|
| 321 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range"); | 
|---|
| 322 | return *gsl_vector_ptr (content->content, i); | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | const double& Vector::operator[](size_t i) const{ | 
|---|
| 326 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range"); | 
|---|
| 327 | return *gsl_vector_ptr (content->content, i); | 
|---|
| 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 |  | 
|---|
| 338 | VectorContent* Vector::get(){ | 
|---|
| 339 | return content; | 
|---|
| 340 | } | 
|---|
| 341 |  | 
|---|
| 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 | */ | 
|---|
| 347 | bool Vector::operator==(const Vector& b) const | 
|---|
| 348 | { | 
|---|
| 349 | return IsEqualTo(b); | 
|---|
| 350 | }; | 
|---|
| 351 |  | 
|---|
| 352 | bool Vector::operator!=(const Vector& b) const | 
|---|
| 353 | { | 
|---|
| 354 | return !IsEqualTo(b); | 
|---|
| 355 | } | 
|---|
| 356 |  | 
|---|
| 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 | */ | 
|---|
| 362 | const Vector& Vector::operator+=(const Vector& b) | 
|---|
| 363 | { | 
|---|
| 364 | this->AddVector(b); | 
|---|
| 365 | return *this; | 
|---|
| 366 | }; | 
|---|
| 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 | */ | 
|---|
| 373 | const Vector& Vector::operator-=(const Vector& b) | 
|---|
| 374 | { | 
|---|
| 375 | this->SubtractVector(b); | 
|---|
| 376 | return *this; | 
|---|
| 377 | }; | 
|---|
| 378 |  | 
|---|
| 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 | */ | 
|---|
| 384 | const Vector& operator*=(Vector& a, const double m) | 
|---|
| 385 | { | 
|---|
| 386 | a.Scale(m); | 
|---|
| 387 | return a; | 
|---|
| 388 | }; | 
|---|
| 389 |  | 
|---|
| 390 | /** Sums two vectors \a  and \b component-wise. | 
|---|
| 391 | * \param a first vector | 
|---|
| 392 | * \param b second vector | 
|---|
| 393 | * \return a + b | 
|---|
| 394 | */ | 
|---|
| 395 | Vector const Vector::operator+(const Vector& b) const | 
|---|
| 396 | { | 
|---|
| 397 | Vector x = *this; | 
|---|
| 398 | x.AddVector(b); | 
|---|
| 399 | return x; | 
|---|
| 400 | }; | 
|---|
| 401 |  | 
|---|
| 402 | /** Subtracts vector \a from \b component-wise. | 
|---|
| 403 | * \param a first vector | 
|---|
| 404 | * \param b second vector | 
|---|
| 405 | * \return a - b | 
|---|
| 406 | */ | 
|---|
| 407 | Vector const Vector::operator-(const Vector& b) const | 
|---|
| 408 | { | 
|---|
| 409 | Vector x = *this; | 
|---|
| 410 | x.SubtractVector(b); | 
|---|
| 411 | return x; | 
|---|
| 412 | }; | 
|---|
| 413 |  | 
|---|
| 414 | /** Factors given vector \a a times \a m. | 
|---|
| 415 | * \param a vector | 
|---|
| 416 | * \param m factor | 
|---|
| 417 | * \return m * a | 
|---|
| 418 | */ | 
|---|
| 419 | Vector const operator*(const Vector& a, const double m) | 
|---|
| 420 | { | 
|---|
| 421 | Vector x(a); | 
|---|
| 422 | x.Scale(m); | 
|---|
| 423 | return x; | 
|---|
| 424 | }; | 
|---|
| 425 |  | 
|---|
| 426 | /** Factors given vector \a a times \a m. | 
|---|
| 427 | * \param m factor | 
|---|
| 428 | * \param a vector | 
|---|
| 429 | * \return m * a | 
|---|
| 430 | */ | 
|---|
| 431 | Vector const operator*(const double m, const Vector& a ) | 
|---|
| 432 | { | 
|---|
| 433 | Vector x(a); | 
|---|
| 434 | x.Scale(m); | 
|---|
| 435 | return x; | 
|---|
| 436 | }; | 
|---|
| 437 |  | 
|---|
| 438 | ostream& operator<<(ostream& ost, const Vector& m) | 
|---|
| 439 | { | 
|---|
| 440 | ost << "("; | 
|---|
| 441 | for (int i=0;i<NDIM;i++) { | 
|---|
| 442 | ost << m[i]; | 
|---|
| 443 | if (i != 2) | 
|---|
| 444 | ost << ","; | 
|---|
| 445 | } | 
|---|
| 446 | ost << ")"; | 
|---|
| 447 | return ost; | 
|---|
| 448 | }; | 
|---|
| 449 |  | 
|---|
| 450 |  | 
|---|
| 451 | void Vector::ScaleAll(const double *factor) | 
|---|
| 452 | { | 
|---|
| 453 | for (int i=NDIM;i--;) | 
|---|
| 454 | at(i) *= factor[i]; | 
|---|
| 455 | }; | 
|---|
| 456 |  | 
|---|
| 457 | void Vector::ScaleAll(const Vector &factor){ | 
|---|
| 458 | gsl_vector_mul(content->content, factor.content->content); | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 |  | 
|---|
| 462 | void Vector::Scale(const double factor) | 
|---|
| 463 | { | 
|---|
| 464 | gsl_vector_scale(content->content,factor); | 
|---|
| 465 | }; | 
|---|
| 466 |  | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 491 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors) | 
|---|
| 492 | { | 
|---|
| 493 | (*this) = (factors[0]*x1) + | 
|---|
| 494 | (factors[1]*x2) + | 
|---|
| 495 | (factors[2]*x3); | 
|---|
| 496 | }; | 
|---|
| 497 |  | 
|---|
| 498 | /** Calculates orthonormal vector to one given vectors. | 
|---|
| 499 | * Just subtracts the projection onto the given vector from this vector. | 
|---|
| 500 | * The removed part of the vector is Vector::Projection() | 
|---|
| 501 | * \param *x1 vector | 
|---|
| 502 | * \return true - success, false - vector is zero | 
|---|
| 503 | */ | 
|---|
| 504 | bool Vector::MakeNormalTo(const Vector &y1) | 
|---|
| 505 | { | 
|---|
| 506 | bool result = false; | 
|---|
| 507 | double factor = y1.ScalarProduct(*this)/y1.NormSquared(); | 
|---|
| 508 | Vector x1 = factor * y1; | 
|---|
| 509 | SubtractVector(x1); | 
|---|
| 510 | for (int i=NDIM;i--;) | 
|---|
| 511 | result = result || (fabs(at(i)) > MYEPSILON); | 
|---|
| 512 |  | 
|---|
| 513 | return result; | 
|---|
| 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 | */ | 
|---|
| 522 | bool Vector::GetOneNormalVector(const Vector &GivenVector) | 
|---|
| 523 | { | 
|---|
| 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; | 
|---|
| 531 |  | 
|---|
| 532 | // in two component-systems we need to find the one position that is zero | 
|---|
| 533 | int zeroPos = -1; | 
|---|
| 534 | // find two components != 0 | 
|---|
| 535 | for (j=0;j<NDIM;j++){ | 
|---|
| 536 | if (fabs(GivenVector[j]) > MYEPSILON) | 
|---|
| 537 | Components[Last++] = j; | 
|---|
| 538 | else | 
|---|
| 539 | // this our zero Position | 
|---|
| 540 | zeroPos = j; | 
|---|
| 541 | } | 
|---|
| 542 |  | 
|---|
| 543 | switch(Last) { | 
|---|
| 544 | case 3:  // threecomponent system | 
|---|
| 545 | // the position of the zero is arbitrary in three component systems | 
|---|
| 546 | zeroPos = Components[2]; | 
|---|
| 547 | case 2:  // two component system | 
|---|
| 548 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]])); | 
|---|
| 549 | at(zeroPos) = 0.; | 
|---|
| 550 | // in skp both remaining parts shall become zero but with opposite sign and third is zero | 
|---|
| 551 | at(Components[1]) = -1./GivenVector[Components[1]] / norm; | 
|---|
| 552 | at(Components[0]) = 1./GivenVector[Components[0]] / norm; | 
|---|
| 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 | 
|---|
| 557 | at((Components[0]+2)%NDIM) = 0.; | 
|---|
| 558 | at((Components[0]+1)%NDIM) = 1.; | 
|---|
| 559 | at(Components[0]) = 0.; | 
|---|
| 560 | return true; | 
|---|
| 561 | break; | 
|---|
| 562 | default: | 
|---|
| 563 | return false; | 
|---|
| 564 | } | 
|---|
| 565 | }; | 
|---|
| 566 |  | 
|---|
| 567 | /** Adds vector \a *y componentwise. | 
|---|
| 568 | * \param *y vector | 
|---|
| 569 | */ | 
|---|
| 570 | void Vector::AddVector(const Vector &y) | 
|---|
| 571 | { | 
|---|
| 572 | gsl_vector_add(content->content, y.content->content); | 
|---|
| 573 | } | 
|---|
| 574 |  | 
|---|
| 575 | /** Adds vector \a *y componentwise. | 
|---|
| 576 | * \param *y vector | 
|---|
| 577 | */ | 
|---|
| 578 | void Vector::SubtractVector(const Vector &y) | 
|---|
| 579 | { | 
|---|
| 580 | gsl_vector_sub(content->content, y.content->content); | 
|---|
| 581 | } | 
|---|
| 582 |  | 
|---|
| 583 |  | 
|---|
| 584 | // some comonly used vectors | 
|---|
| 585 | const Vector zeroVec(0,0,0); | 
|---|
| 586 | const Vector unitVec[NDIM]={Vector(1,0,0),Vector(0,1,0),Vector(0,0,1)}; | 
|---|