[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 |
|
---|
[ad011c] | 19 | #include "CodePatterns/MemDebug.hpp"
|
---|
[edb93c] | 20 |
|
---|
[ad011c] | 21 | #include "CodePatterns/Assert.hpp"
|
---|
[9b410d] | 22 | #include "CodePatterns/Verbose.hpp"
|
---|
| 23 | #include "Exceptions/MathException.hpp"
|
---|
| 24 | #include "LinearAlgebra/defs.hpp"
|
---|
[06aedc] | 25 | #include "LinearAlgebra/fast_functions.hpp"
|
---|
[9b410d] | 26 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 27 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
[6ac7ee] | 28 |
|
---|
[1be8a5] | 29 | #include <cmath>
|
---|
[1bd79e] | 30 | #include <iostream>
|
---|
[06aedc] | 31 | #include <cmath>
|
---|
[923b6c] | 32 | #include <gsl/gsl_blas.h>
|
---|
[a439e5] | 33 | #include <gsl/gsl_vector.h>
|
---|
[923b6c] | 34 |
|
---|
[1bd79e] | 35 |
|
---|
| 36 | using namespace std;
|
---|
[6ac7ee] | 37 |
|
---|
[97498a] | 38 |
|
---|
[6ac7ee] | 39 | /************************************ Functions for class vector ************************************/
|
---|
| 40 |
|
---|
| 41 | /** Constructor of class vector.
|
---|
| 42 | */
|
---|
[753f02] | 43 | Vector::Vector()
|
---|
| 44 | {
|
---|
[bbf1bd] | 45 | content = new VectorContent((size_t) NDIM);
|
---|
[753f02] | 46 | };
|
---|
[6ac7ee] | 47 |
|
---|
[bbf1bd] | 48 | /** Copy constructor.
|
---|
| 49 | * \param &src source Vector reference
|
---|
[821907] | 50 | */
|
---|
[753f02] | 51 | Vector::Vector(const Vector& src)
|
---|
[821907] | 52 | {
|
---|
[bbf1bd] | 53 | content = new VectorContent(*(src.content));
|
---|
[1bd79e] | 54 | }
|
---|
[821907] | 55 |
|
---|
| 56 | /** Constructor of class vector.
|
---|
[bbf1bd] | 57 | * \param x1 first component
|
---|
| 58 | * \param x2 second component
|
---|
| 59 | * \param x3 third component
|
---|
[821907] | 60 | */
|
---|
[753f02] | 61 | Vector::Vector(const double x1, const double x2, const double x3)
|
---|
[821907] | 62 | {
|
---|
[bbf1bd] | 63 | content = new VectorContent((size_t) NDIM);
|
---|
| 64 | content->at(0) = x1;
|
---|
| 65 | content->at(1) = x2;
|
---|
| 66 | content->at(2) = x3;
|
---|
[821907] | 67 | };
|
---|
| 68 |
|
---|
[d74077] | 69 | /** Constructor of class vector.
|
---|
[bbf1bd] | 70 | * \param x[3] three values to initialize Vector with
|
---|
[d74077] | 71 | */
|
---|
| 72 | Vector::Vector(const double x[3])
|
---|
| 73 | {
|
---|
[bbf1bd] | 74 | content = new VectorContent((size_t) NDIM);
|
---|
| 75 | for (size_t i = NDIM; i--; )
|
---|
| 76 | content->at(i) = x[i];
|
---|
[d74077] | 77 | };
|
---|
| 78 |
|
---|
[bbf1bd] | 79 | /** Copy constructor of class vector from VectorContent.
|
---|
| 80 | * \note This is destructive, i.e. we take over _content.
|
---|
| 81 | */
|
---|
| 82 | Vector::Vector(VectorContent *&_content) :
|
---|
| 83 | content(_content)
|
---|
| 84 | {
|
---|
| 85 | _content = NULL;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /** Copy constructor of class vector from VectorContent.
|
---|
| 89 | * \note This is non-destructive, i.e. _content is copied.
|
---|
| 90 | */
|
---|
| 91 | Vector::Vector(VectorContent &_content)
|
---|
| 92 | {
|
---|
| 93 | content = new VectorContent(_content);
|
---|
| 94 | }
|
---|
[325390] | 95 |
|
---|
[bbf1bd] | 96 | /** Assignment operator.
|
---|
| 97 | * \param &src source vector to assign \a *this to
|
---|
| 98 | * \return reference to \a *this
|
---|
[6ac7ee] | 99 | */
|
---|
[0a4f7f] | 100 | Vector& Vector::operator=(const Vector& src){
|
---|
| 101 | // check for self assignment
|
---|
| 102 | if(&src!=this){
|
---|
[bbf1bd] | 103 | *content = *(src.content);
|
---|
[0a4f7f] | 104 | }
|
---|
| 105 | return *this;
|
---|
| 106 | }
|
---|
[6ac7ee] | 107 |
|
---|
| 108 | /** Desctructor of class vector.
|
---|
[bbf1bd] | 109 | * Vector::content is deleted.
|
---|
[6ac7ee] | 110 | */
|
---|
[d466f0] | 111 | Vector::~Vector() {
|
---|
[ce3d2b] | 112 | delete content;
|
---|
[d466f0] | 113 | };
|
---|
[6ac7ee] | 114 |
|
---|
| 115 | /** Calculates square of distance between this and another vector.
|
---|
| 116 | * \param *y array to second vector
|
---|
| 117 | * \return \f$| x - y |^2\f$
|
---|
| 118 | */
|
---|
[273382] | 119 | double Vector::DistanceSquared(const Vector &y) const
|
---|
[6ac7ee] | 120 | {
|
---|
[042f82] | 121 | double res = 0.;
|
---|
| 122 | for (int i=NDIM;i--;)
|
---|
[d466f0] | 123 | res += (at(i)-y[i])*(at(i)-y[i]);
|
---|
[042f82] | 124 | return (res);
|
---|
[6ac7ee] | 125 | };
|
---|
| 126 |
|
---|
| 127 | /** Calculates distance between this and another vector.
|
---|
| 128 | * \param *y array to second vector
|
---|
| 129 | * \return \f$| x - y |\f$
|
---|
| 130 | */
|
---|
[1513a74] | 131 | double Vector::distance(const Vector &y) const
|
---|
[6ac7ee] | 132 | {
|
---|
[273382] | 133 | return (sqrt(DistanceSquared(y)));
|
---|
[6ac7ee] | 134 | };
|
---|
| 135 |
|
---|
[a439e5] | 136 | size_t Vector::GreatestComponent() const
|
---|
| 137 | {
|
---|
| 138 | int greatest = 0;
|
---|
| 139 | for (int i=1;i<NDIM;i++) {
|
---|
| 140 | if (at(i) > at(greatest))
|
---|
| 141 | greatest = i;
|
---|
| 142 | }
|
---|
| 143 | return greatest;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | size_t Vector::SmallestComponent() const
|
---|
| 147 | {
|
---|
| 148 | int smallest = 0;
|
---|
| 149 | for (int i=1;i<NDIM;i++) {
|
---|
| 150 | if (at(i) < at(smallest))
|
---|
| 151 | smallest = i;
|
---|
| 152 | }
|
---|
| 153 | return smallest;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
[1513a74] | 157 | Vector Vector::getClosestPoint(const Vector &point) const{
|
---|
| 158 | // the closest point to a single point space is always the single point itself
|
---|
| 159 | return *this;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[6ac7ee] | 162 | /** Calculates scalar product between this and another vector.
|
---|
| 163 | * \param *y array to second vector
|
---|
| 164 | * \return \f$\langle x, y \rangle\f$
|
---|
| 165 | */
|
---|
[273382] | 166 | double Vector::ScalarProduct(const Vector &y) const
|
---|
[6ac7ee] | 167 | {
|
---|
[042f82] | 168 | double res = 0.;
|
---|
[ce3d2b] | 169 | gsl_blas_ddot(content->content, y.content->content, &res);
|
---|
[042f82] | 170 | return (res);
|
---|
[6ac7ee] | 171 | };
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | /** Calculates VectorProduct between this and another vector.
|
---|
[042f82] | 175 | * -# returns the Product in place of vector from which it was initiated
|
---|
| 176 | * -# ATTENTION: Only three dim.
|
---|
| 177 | * \param *y array to vector with which to calculate crossproduct
|
---|
| 178 | * \return \f$ x \times y \f&
|
---|
[6ac7ee] | 179 | */
|
---|
[273382] | 180 | void Vector::VectorProduct(const Vector &y)
|
---|
[6ac7ee] | 181 | {
|
---|
[042f82] | 182 | Vector tmp;
|
---|
[d466f0] | 183 | for(int i=NDIM;i--;)
|
---|
| 184 | tmp[i] = at((i+1)%NDIM)*y[(i+2)%NDIM] - at((i+2)%NDIM)*y[(i+1)%NDIM];
|
---|
[753f02] | 185 | (*this) = tmp;
|
---|
[6ac7ee] | 186 | };
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | /** projects this vector onto plane defined by \a *y.
|
---|
| 190 | * \param *y normal vector of plane
|
---|
| 191 | * \return \f$\langle x, y \rangle\f$
|
---|
| 192 | */
|
---|
[273382] | 193 | void Vector::ProjectOntoPlane(const Vector &y)
|
---|
[6ac7ee] | 194 | {
|
---|
[042f82] | 195 | Vector tmp;
|
---|
[753f02] | 196 | tmp = y;
|
---|
[042f82] | 197 | tmp.Normalize();
|
---|
[753f02] | 198 | tmp.Scale(ScalarProduct(tmp));
|
---|
| 199 | *this -= tmp;
|
---|
[2319ed] | 200 | };
|
---|
| 201 |
|
---|
[821907] | 202 | /** Calculates the minimum distance of this vector to the plane.
|
---|
| 203 | * \sa Vector::GetDistanceVectorToPlane()
|
---|
| 204 | * \param *out output stream for debugging
|
---|
| 205 | * \param *PlaneNormal normal of plane
|
---|
| 206 | * \param *PlaneOffset offset of plane
|
---|
| 207 | * \return distance to plane
|
---|
| 208 | */
|
---|
[d4c9ae] | 209 | double Vector::DistanceToSpace(const Space &space) const
|
---|
[821907] | 210 | {
|
---|
[d4c9ae] | 211 | return space.distance(*this);
|
---|
[c4d4df] | 212 | };
|
---|
| 213 |
|
---|
[6ac7ee] | 214 | /** Calculates the projection of a vector onto another \a *y.
|
---|
| 215 | * \param *y array to second vector
|
---|
| 216 | */
|
---|
[273382] | 217 | void Vector::ProjectIt(const Vector &y)
|
---|
[6ac7ee] | 218 | {
|
---|
[753f02] | 219 | (*this) += (-ScalarProduct(y))*y;
|
---|
[ef9df36] | 220 | };
|
---|
| 221 |
|
---|
| 222 | /** Calculates the projection of a vector onto another \a *y.
|
---|
| 223 | * \param *y array to second vector
|
---|
| 224 | * \return Vector
|
---|
| 225 | */
|
---|
[273382] | 226 | Vector Vector::Projection(const Vector &y) const
|
---|
[ef9df36] | 227 | {
|
---|
[753f02] | 228 | Vector helper = y;
|
---|
| 229 | helper.Scale((ScalarProduct(y)/y.NormSquared()));
|
---|
[ef9df36] | 230 |
|
---|
| 231 | return helper;
|
---|
[6ac7ee] | 232 | };
|
---|
| 233 |
|
---|
| 234 | /** Calculates norm of this vector.
|
---|
| 235 | * \return \f$|x|\f$
|
---|
| 236 | */
|
---|
| 237 | double Vector::Norm() const
|
---|
| 238 | {
|
---|
[694eae] | 239 | return (content->Norm());
|
---|
[6ac7ee] | 240 | };
|
---|
| 241 |
|
---|
[d4d0dd] | 242 | /** Calculates squared norm of this vector.
|
---|
| 243 | * \return \f$|x|^2\f$
|
---|
| 244 | */
|
---|
| 245 | double Vector::NormSquared() const
|
---|
| 246 | {
|
---|
[694eae] | 247 | return (content->NormSquared());
|
---|
[d4d0dd] | 248 | };
|
---|
| 249 |
|
---|
[6ac7ee] | 250 | /** Normalizes this vector.
|
---|
| 251 | */
|
---|
| 252 | void Vector::Normalize()
|
---|
| 253 | {
|
---|
[694eae] | 254 | content->Normalize();
|
---|
[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 |
|
---|
[694eae] | 397 | /** factor each component of \a *this times \a m.
|
---|
[6ac7ee] | 398 | * \param m factor
|
---|
[694eae] | 399 | * \return \f$(\text{*this} \cdot m\f$
|
---|
[6ac7ee] | 400 | */
|
---|
[694eae] | 401 | const Vector& Vector::operator*=(const double m)
|
---|
[6ac7ee] | 402 | {
|
---|
[694eae] | 403 | Scale(m);
|
---|
| 404 | return *this;
|
---|
[6ac7ee] | 405 | };
|
---|
| 406 |
|
---|
[042f82] | 407 | /** Sums two vectors \a and \b component-wise.
|
---|
[6ac7ee] | 408 | * \param a first vector
|
---|
| 409 | * \param b second vector
|
---|
| 410 | * \return a + b
|
---|
| 411 | */
|
---|
[72e7fa] | 412 | Vector const Vector::operator+(const Vector& b) const
|
---|
[6ac7ee] | 413 | {
|
---|
[72e7fa] | 414 | Vector x = *this;
|
---|
[273382] | 415 | x.AddVector(b);
|
---|
[b84d5d] | 416 | return x;
|
---|
[6ac7ee] | 417 | };
|
---|
| 418 |
|
---|
[54a746] | 419 | /** Subtracts vector \a from \b component-wise.
|
---|
| 420 | * \param a first vector
|
---|
| 421 | * \param b second vector
|
---|
| 422 | * \return a - b
|
---|
| 423 | */
|
---|
[72e7fa] | 424 | Vector const Vector::operator-(const Vector& b) const
|
---|
[54a746] | 425 | {
|
---|
[72e7fa] | 426 | Vector x = *this;
|
---|
[273382] | 427 | x.SubtractVector(b);
|
---|
[b84d5d] | 428 | return x;
|
---|
[54a746] | 429 | };
|
---|
| 430 |
|
---|
[694eae] | 431 | /** Factors given vector \a *this times \a m.
|
---|
[6ac7ee] | 432 | * \param m factor
|
---|
[694eae] | 433 | * \return \f$(\text{*this} \cdot m)\f$
|
---|
[6ac7ee] | 434 | */
|
---|
[694eae] | 435 | const Vector Vector::operator*(const double m) const
|
---|
[6ac7ee] | 436 | {
|
---|
[694eae] | 437 | Vector x(*this);
|
---|
[b84d5d] | 438 | x.Scale(m);
|
---|
| 439 | return x;
|
---|
[6ac7ee] | 440 | };
|
---|
| 441 |
|
---|
[54a746] | 442 | /** Factors given vector \a a times \a m.
|
---|
| 443 | * \param m factor
|
---|
| 444 | * \param a vector
|
---|
| 445 | * \return m * a
|
---|
| 446 | */
|
---|
[b84d5d] | 447 | Vector const operator*(const double m, const Vector& a )
|
---|
[54a746] | 448 | {
|
---|
[b84d5d] | 449 | Vector x(a);
|
---|
| 450 | x.Scale(m);
|
---|
| 451 | return x;
|
---|
[54a746] | 452 | };
|
---|
| 453 |
|
---|
[9c20aa] | 454 | ostream& operator<<(ostream& ost, const Vector& m)
|
---|
[6ac7ee] | 455 | {
|
---|
[042f82] | 456 | ost << "(";
|
---|
| 457 | for (int i=0;i<NDIM;i++) {
|
---|
[0a4f7f] | 458 | ost << m[i];
|
---|
[042f82] | 459 | if (i != 2)
|
---|
| 460 | ost << ",";
|
---|
| 461 | }
|
---|
| 462 | ost << ")";
|
---|
| 463 | return ost;
|
---|
[6ac7ee] | 464 | };
|
---|
| 465 |
|
---|
| 466 |
|
---|
[1bd79e] | 467 | void Vector::ScaleAll(const double *factor)
|
---|
[6ac7ee] | 468 | {
|
---|
[042f82] | 469 | for (int i=NDIM;i--;)
|
---|
[d466f0] | 470 | at(i) *= factor[i];
|
---|
[6ac7ee] | 471 | };
|
---|
| 472 |
|
---|
[b5bf84] | 473 | void Vector::ScaleAll(const Vector &factor){
|
---|
[ce3d2b] | 474 | gsl_vector_mul(content->content, factor.content->content);
|
---|
[b5bf84] | 475 | }
|
---|
[6ac7ee] | 476 |
|
---|
[1bd79e] | 477 |
|
---|
[776b64] | 478 | void Vector::Scale(const double factor)
|
---|
[6ac7ee] | 479 | {
|
---|
[ce3d2b] | 480 | gsl_vector_scale(content->content,factor);
|
---|
[6ac7ee] | 481 | };
|
---|
| 482 |
|
---|
[45ef76] | 483 | std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
|
---|
| 484 | double factor = ScalarProduct(rhs)/rhs.NormSquared();
|
---|
| 485 | Vector res= factor * rhs;
|
---|
| 486 | return make_pair(res,(*this)-res);
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
|
---|
| 490 | Vector helper = *this;
|
---|
| 491 | pointset res;
|
---|
| 492 | for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
|
---|
| 493 | pair<Vector,Vector> currPart = helper.partition(*iter);
|
---|
| 494 | res.push_back(currPart.first);
|
---|
| 495 | helper = currPart.second;
|
---|
| 496 | }
|
---|
| 497 | return make_pair(res,helper);
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[6ac7ee] | 500 | /** Creates this vector as the b y *factors' components scaled linear combination of the given three.
|
---|
| 501 | * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
|
---|
| 502 | * \param *x1 first vector
|
---|
| 503 | * \param *x2 second vector
|
---|
| 504 | * \param *x3 third vector
|
---|
| 505 | * \param *factors three-component vector with the factor for each given vector
|
---|
| 506 | */
|
---|
[273382] | 507 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
|
---|
[6ac7ee] | 508 | {
|
---|
[273382] | 509 | (*this) = (factors[0]*x1) +
|
---|
| 510 | (factors[1]*x2) +
|
---|
| 511 | (factors[2]*x3);
|
---|
[6ac7ee] | 512 | };
|
---|
| 513 |
|
---|
| 514 | /** Calculates orthonormal vector to one given vectors.
|
---|
| 515 | * Just subtracts the projection onto the given vector from this vector.
|
---|
[ef9df36] | 516 | * The removed part of the vector is Vector::Projection()
|
---|
[6ac7ee] | 517 | * \param *x1 vector
|
---|
| 518 | * \return true - success, false - vector is zero
|
---|
| 519 | */
|
---|
[0a4f7f] | 520 | bool Vector::MakeNormalTo(const Vector &y1)
|
---|
[6ac7ee] | 521 | {
|
---|
[042f82] | 522 | bool result = false;
|
---|
[753f02] | 523 | double factor = y1.ScalarProduct(*this)/y1.NormSquared();
|
---|
[45ef76] | 524 | Vector x1 = factor * y1;
|
---|
[753f02] | 525 | SubtractVector(x1);
|
---|
[042f82] | 526 | for (int i=NDIM;i--;)
|
---|
[d466f0] | 527 | result = result || (fabs(at(i)) > MYEPSILON);
|
---|
[6ac7ee] | 528 |
|
---|
[042f82] | 529 | return result;
|
---|
[6ac7ee] | 530 | };
|
---|
| 531 |
|
---|
| 532 | /** Creates this vector as one of the possible orthonormal ones to the given one.
|
---|
| 533 | * Just scan how many components of given *vector are unequal to zero and
|
---|
| 534 | * try to get the skp of both to be zero accordingly.
|
---|
| 535 | * \param *vector given vector
|
---|
| 536 | * \return true - success, false - failure (null vector given)
|
---|
| 537 | */
|
---|
[273382] | 538 | bool Vector::GetOneNormalVector(const Vector &GivenVector)
|
---|
[6ac7ee] | 539 | {
|
---|
[042f82] | 540 | int Components[NDIM]; // contains indices of non-zero components
|
---|
| 541 | int Last = 0; // count the number of non-zero entries in vector
|
---|
| 542 | int j; // loop variables
|
---|
| 543 | double norm;
|
---|
| 544 |
|
---|
| 545 | for (j=NDIM;j--;)
|
---|
| 546 | Components[j] = -1;
|
---|
[1829c4] | 547 |
|
---|
| 548 | // in two component-systems we need to find the one position that is zero
|
---|
| 549 | int zeroPos = -1;
|
---|
[042f82] | 550 | // find two components != 0
|
---|
[1829c4] | 551 | for (j=0;j<NDIM;j++){
|
---|
[753f02] | 552 | if (fabs(GivenVector[j]) > MYEPSILON)
|
---|
[042f82] | 553 | Components[Last++] = j;
|
---|
[1829c4] | 554 | else
|
---|
| 555 | // this our zero Position
|
---|
| 556 | zeroPos = j;
|
---|
| 557 | }
|
---|
[042f82] | 558 |
|
---|
| 559 | switch(Last) {
|
---|
| 560 | case 3: // threecomponent system
|
---|
[1829c4] | 561 | // the position of the zero is arbitrary in three component systems
|
---|
| 562 | zeroPos = Components[2];
|
---|
[042f82] | 563 | case 2: // two component system
|
---|
[753f02] | 564 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
|
---|
[1829c4] | 565 | at(zeroPos) = 0.;
|
---|
[042f82] | 566 | // in skp both remaining parts shall become zero but with opposite sign and third is zero
|
---|
[1829c4] | 567 | at(Components[1]) = -1./GivenVector[Components[1]] / norm;
|
---|
| 568 | at(Components[0]) = 1./GivenVector[Components[0]] / norm;
|
---|
[042f82] | 569 | return true;
|
---|
| 570 | break;
|
---|
| 571 | case 1: // one component system
|
---|
| 572 | // set sole non-zero component to 0, and one of the other zero component pendants to 1
|
---|
[1829c4] | 573 | at((Components[0]+2)%NDIM) = 0.;
|
---|
| 574 | at((Components[0]+1)%NDIM) = 1.;
|
---|
| 575 | at(Components[0]) = 0.;
|
---|
[042f82] | 576 | return true;
|
---|
| 577 | break;
|
---|
| 578 | default:
|
---|
| 579 | return false;
|
---|
| 580 | }
|
---|
[6ac7ee] | 581 | };
|
---|
| 582 |
|
---|
| 583 | /** Adds vector \a *y componentwise.
|
---|
| 584 | * \param *y vector
|
---|
| 585 | */
|
---|
[273382] | 586 | void Vector::AddVector(const Vector &y)
|
---|
[6ac7ee] | 587 | {
|
---|
[ce3d2b] | 588 | gsl_vector_add(content->content, y.content->content);
|
---|
[6ac7ee] | 589 | }
|
---|
| 590 |
|
---|
| 591 | /** Adds vector \a *y componentwise.
|
---|
| 592 | * \param *y vector
|
---|
| 593 | */
|
---|
[273382] | 594 | void Vector::SubtractVector(const Vector &y)
|
---|
[6ac7ee] | 595 | {
|
---|
[ce3d2b] | 596 | gsl_vector_sub(content->content, y.content->content);
|
---|
[ef9df36] | 597 | }
|
---|
| 598 |
|
---|
[005e18] | 599 |
|
---|
| 600 | // some comonly used vectors
|
---|
| 601 | const Vector zeroVec(0,0,0);
|
---|
[407782] | 602 | const Vector unitVec[NDIM]={Vector(1,0,0),Vector(0,1,0),Vector(0,0,1)};
|
---|