source: src/vector.cpp@ f66b67

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since f66b67 was d0f111, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Changed Vector::WrapPeriodically to use matrixes instead of double*

  • Property mode set to 100644
File size: 17.7 KB
RevLine 
[6ac7ee]1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
[112b09]7#include "Helpers/MemDebug.hpp"
[edb93c]8
[54a746]9#include "vector.hpp"
[325390]10#include "Matrix.hpp"
[54a746]11#include "verbose.hpp"
[b34306]12#include "World.hpp"
[0a4f7f]13#include "Helpers/Assert.hpp"
[753f02]14#include "Helpers/fast_functions.hpp"
[325390]15#include "Exceptions/MathException.hpp"
[6ac7ee]16
[1bd79e]17#include <iostream>
[923b6c]18#include <gsl/gsl_blas.h>
19
[1bd79e]20
21using namespace std;
[6ac7ee]22
[97498a]23
[6ac7ee]24/************************************ Functions for class vector ************************************/
25
26/** Constructor of class vector.
27 */
[753f02]28Vector::Vector()
29{
[d690fa]30 content = gsl_vector_calloc (NDIM);
[753f02]31};
[6ac7ee]32
[753f02]33/**
34 * Copy constructor
[821907]35 */
[1bd79e]36
[753f02]37Vector::Vector(const Vector& src)
[821907]38{
[d690fa]39 content = gsl_vector_alloc(NDIM);
[93987b]40 gsl_vector_memcpy(content, src.content);
[1bd79e]41}
[821907]42
43/** Constructor of class vector.
44 */
[753f02]45Vector::Vector(const double x1, const double x2, const double x3)
[821907]46{
[d690fa]47 content = gsl_vector_alloc(NDIM);
48 gsl_vector_set(content,0,x1);
49 gsl_vector_set(content,1,x2);
50 gsl_vector_set(content,2,x3);
[821907]51};
52
[325390]53Vector::Vector(gsl_vector *_content) :
54 content(_content)
55{}
56
[0a4f7f]57/**
58 * Assignment operator
[6ac7ee]59 */
[0a4f7f]60Vector& Vector::operator=(const Vector& src){
61 // check for self assignment
62 if(&src!=this){
[93987b]63 gsl_vector_memcpy(content, src.content);
[0a4f7f]64 }
65 return *this;
66}
[6ac7ee]67
68/** Desctructor of class vector.
69 */
[d466f0]70Vector::~Vector() {
[d690fa]71 gsl_vector_free(content);
[d466f0]72};
[6ac7ee]73
74/** Calculates square of distance between this and another vector.
75 * \param *y array to second vector
76 * \return \f$| x - y |^2\f$
77 */
[273382]78double Vector::DistanceSquared(const Vector &y) const
[6ac7ee]79{
[042f82]80 double res = 0.;
81 for (int i=NDIM;i--;)
[d466f0]82 res += (at(i)-y[i])*(at(i)-y[i]);
[042f82]83 return (res);
[6ac7ee]84};
85
86/** Calculates distance between this and another vector.
87 * \param *y array to second vector
88 * \return \f$| x - y |\f$
89 */
[1513a74]90double Vector::distance(const Vector &y) const
[6ac7ee]91{
[273382]92 return (sqrt(DistanceSquared(y)));
[6ac7ee]93};
94
[1513a74]95Vector Vector::getClosestPoint(const Vector &point) const{
96 // the closest point to a single point space is always the single point itself
97 return *this;
98}
99
[6ac7ee]100/** Calculates distance between this and another vector in a periodic cell.
101 * \param *y array to second vector
102 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
103 * \return \f$| x - y |\f$
104 */
[273382]105double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
[6ac7ee]106{
[c94eeb]107 double res = distance(y), tmp;
108 Matrix matrix;
[753f02]109 Vector Shiftedy, TranslationVector;
110 int N[NDIM];
[c94eeb]111 matrix.at(0,0) = cell_size[0];
112 matrix.at(1,0) = cell_size[1];
113 matrix.at(2,0) = cell_size[3];
114 matrix.at(0,1) = cell_size[1];
115 matrix.at(1,1) = cell_size[2];
116 matrix.at(2,1) = cell_size[4];
117 matrix.at(0,2) = cell_size[3];
118 matrix.at(1,2) = cell_size[4];
119 matrix.at(2,2) = cell_size[5];
[753f02]120 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
121 for (N[0]=-1;N[0]<=1;N[0]++)
122 for (N[1]=-1;N[1]<=1;N[1]++)
123 for (N[2]=-1;N[2]<=1;N[2]++) {
124 // create the translation vector
125 TranslationVector.Zero();
126 for (int i=NDIM;i--;)
127 TranslationVector[i] = (double)N[i];
128 TranslationVector.MatrixMultiplication(matrix);
129 // add onto the original vector to compare with
130 Shiftedy = y + TranslationVector;
131 // get distance and compare with minimum so far
[1513a74]132 tmp = distance(Shiftedy);
[753f02]133 if (tmp < res) res = tmp;
134 }
135 return (res);
[6ac7ee]136};
137
138/** Calculates distance between this and another vector in a periodic cell.
139 * \param *y array to second vector
140 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
141 * \return \f$| x - y |^2\f$
142 */
[273382]143double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
[6ac7ee]144{
[c94eeb]145 double res = DistanceSquared(y), tmp;
146 Matrix matrix;
[753f02]147 Vector Shiftedy, TranslationVector;
148 int N[NDIM];
[c94eeb]149 matrix.at(0,0) = cell_size[0];
150 matrix.at(1,0) = cell_size[1];
151 matrix.at(2,0) = cell_size[3];
152 matrix.at(0,1) = cell_size[1];
153 matrix.at(1,1) = cell_size[2];
154 matrix.at(2,1) = cell_size[4];
155 matrix.at(0,2) = cell_size[3];
156 matrix.at(1,2) = cell_size[4];
157 matrix.at(2,2) = cell_size[5];
[753f02]158 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
159 for (N[0]=-1;N[0]<=1;N[0]++)
160 for (N[1]=-1;N[1]<=1;N[1]++)
161 for (N[2]=-1;N[2]<=1;N[2]++) {
162 // create the translation vector
163 TranslationVector.Zero();
164 for (int i=NDIM;i--;)
165 TranslationVector[i] = (double)N[i];
166 TranslationVector.MatrixMultiplication(matrix);
167 // add onto the original vector to compare with
168 Shiftedy = y + TranslationVector;
169 // get distance and compare with minimum so far
170 tmp = DistanceSquared(Shiftedy);
171 if (tmp < res) res = tmp;
172 }
173 return (res);
[6ac7ee]174};
175
176/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
177 * \param *out ofstream for debugging messages
178 * Tries to translate a vector into each adjacent neighbouring cell.
179 */
[c94eeb]180void Vector::KeepPeriodic(const double * const _matrix)
[6ac7ee]181{
[c94eeb]182 Matrix matrix = Matrix(_matrix);
[753f02]183 // int N[NDIM];
184 // bool flag = false;
185 //vector Shifted, TranslationVector;
186 // Log() << Verbose(1) << "Begin of KeepPeriodic." << endl;
187 // Log() << Verbose(2) << "Vector is: ";
188 // Output(out);
189 // Log() << Verbose(0) << endl;
[c94eeb]190 MatrixMultiplication(matrix.invert());
[753f02]191 for(int i=NDIM;i--;) { // correct periodically
192 if (at(i) < 0) { // get every coefficient into the interval [0,1)
193 at(i) += ceil(at(i));
194 } else {
195 at(i) -= floor(at(i));
196 }
[042f82]197 }
[753f02]198 MatrixMultiplication(matrix);
199 // Log() << Verbose(2) << "New corrected vector is: ";
200 // Output(out);
201 // Log() << Verbose(0) << endl;
202 // Log() << Verbose(1) << "End of KeepPeriodic." << endl;
[6ac7ee]203};
204
205/** Calculates scalar product between this and another vector.
206 * \param *y array to second vector
207 * \return \f$\langle x, y \rangle\f$
208 */
[273382]209double Vector::ScalarProduct(const Vector &y) const
[6ac7ee]210{
[042f82]211 double res = 0.;
[923b6c]212 gsl_blas_ddot(content, y.content, &res);
[042f82]213 return (res);
[6ac7ee]214};
215
216
217/** Calculates VectorProduct between this and another vector.
[042f82]218 * -# returns the Product in place of vector from which it was initiated
219 * -# ATTENTION: Only three dim.
220 * \param *y array to vector with which to calculate crossproduct
221 * \return \f$ x \times y \f&
[6ac7ee]222 */
[273382]223void Vector::VectorProduct(const Vector &y)
[6ac7ee]224{
[042f82]225 Vector tmp;
[d466f0]226 for(int i=NDIM;i--;)
227 tmp[i] = at((i+1)%NDIM)*y[(i+2)%NDIM] - at((i+2)%NDIM)*y[(i+1)%NDIM];
[753f02]228 (*this) = tmp;
[6ac7ee]229};
230
231
232/** projects this vector onto plane defined by \a *y.
233 * \param *y normal vector of plane
234 * \return \f$\langle x, y \rangle\f$
235 */
[273382]236void Vector::ProjectOntoPlane(const Vector &y)
[6ac7ee]237{
[042f82]238 Vector tmp;
[753f02]239 tmp = y;
[042f82]240 tmp.Normalize();
[753f02]241 tmp.Scale(ScalarProduct(tmp));
242 *this -= tmp;
[2319ed]243};
244
[821907]245/** Calculates the minimum distance of this vector to the plane.
246 * \sa Vector::GetDistanceVectorToPlane()
247 * \param *out output stream for debugging
248 * \param *PlaneNormal normal of plane
249 * \param *PlaneOffset offset of plane
250 * \return distance to plane
251 */
[d4c9ae]252double Vector::DistanceToSpace(const Space &space) const
[821907]253{
[d4c9ae]254 return space.distance(*this);
[c4d4df]255};
256
[6ac7ee]257/** Calculates the projection of a vector onto another \a *y.
258 * \param *y array to second vector
259 */
[273382]260void Vector::ProjectIt(const Vector &y)
[6ac7ee]261{
[753f02]262 (*this) += (-ScalarProduct(y))*y;
[ef9df36]263};
264
265/** Calculates the projection of a vector onto another \a *y.
266 * \param *y array to second vector
267 * \return Vector
268 */
[273382]269Vector Vector::Projection(const Vector &y) const
[ef9df36]270{
[753f02]271 Vector helper = y;
272 helper.Scale((ScalarProduct(y)/y.NormSquared()));
[ef9df36]273
274 return helper;
[6ac7ee]275};
276
277/** Calculates norm of this vector.
278 * \return \f$|x|\f$
279 */
280double Vector::Norm() const
281{
[273382]282 return (sqrt(NormSquared()));
[6ac7ee]283};
284
[d4d0dd]285/** Calculates squared norm of this vector.
286 * \return \f$|x|^2\f$
287 */
288double Vector::NormSquared() const
289{
[273382]290 return (ScalarProduct(*this));
[d4d0dd]291};
292
[6ac7ee]293/** Normalizes this vector.
294 */
295void Vector::Normalize()
296{
[1bd79e]297 double factor = Norm();
298 (*this) *= 1/factor;
[6ac7ee]299};
300
301/** Zeros all components of this vector.
302 */
303void Vector::Zero()
304{
[753f02]305 at(0)=at(1)=at(2)=0;
[6ac7ee]306};
307
308/** Zeros all components of this vector.
309 */
[776b64]310void Vector::One(const double one)
[6ac7ee]311{
[753f02]312 at(0)=at(1)=at(2)=one;
[6ac7ee]313};
314
[9c20aa]315/** Checks whether vector has all components zero.
316 * @return true - vector is zero, false - vector is not
317 */
[54a746]318bool Vector::IsZero() const
[9c20aa]319{
[d466f0]320 return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON);
[54a746]321};
322
323/** Checks whether vector has length of 1.
324 * @return true - vector is normalized, false - vector is not
325 */
326bool Vector::IsOne() const
327{
328 return (fabs(Norm() - 1.) < MYEPSILON);
[9c20aa]329};
330
[ef9df36]331/** Checks whether vector is normal to \a *normal.
332 * @return true - vector is normalized, false - vector is not
333 */
[273382]334bool Vector::IsNormalTo(const Vector &normal) const
[ef9df36]335{
336 if (ScalarProduct(normal) < MYEPSILON)
337 return true;
338 else
339 return false;
340};
341
[b998c3]342/** Checks whether vector is normal to \a *normal.
343 * @return true - vector is normalized, false - vector is not
344 */
[273382]345bool Vector::IsEqualTo(const Vector &a) const
[b998c3]346{
347 bool status = true;
348 for (int i=0;i<NDIM;i++) {
[d466f0]349 if (fabs(at(i) - a[i]) > MYEPSILON)
[b998c3]350 status = false;
351 }
352 return status;
353};
354
[6ac7ee]355/** Calculates the angle between this and another vector.
356 * \param *y array to second vector
357 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
358 */
[273382]359double Vector::Angle(const Vector &y) const
[6ac7ee]360{
[753f02]361 double norm1 = Norm(), norm2 = y.Norm();
[ef9df36]362 double angle = -1;
[d4d0dd]363 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
364 angle = this->ScalarProduct(y)/norm1/norm2;
[02da9e]365 // -1-MYEPSILON occured due to numerical imprecision, catch ...
[e138de]366 //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
[02da9e]367 if (angle < -1)
368 angle = -1;
369 if (angle > 1)
370 angle = 1;
[042f82]371 return acos(angle);
[6ac7ee]372};
373
[0a4f7f]374
375double& Vector::operator[](size_t i){
[753f02]376 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
[d690fa]377 return *gsl_vector_ptr (content, i);
[0a4f7f]378}
379
380const double& Vector::operator[](size_t i) const{
[753f02]381 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
[d690fa]382 return *gsl_vector_ptr (content, i);
[0a4f7f]383}
384
385double& Vector::at(size_t i){
386 return (*this)[i];
387}
388
389const double& Vector::at(size_t i) const{
390 return (*this)[i];
391}
392
[0c7ed8]393gsl_vector* Vector::get(){
394 return content;
[0a4f7f]395}
[6ac7ee]396
[ef9df36]397/** Compares vector \a to vector \a b component-wise.
398 * \param a base vector
399 * \param b vector components to add
400 * \return a == b
401 */
[72e7fa]402bool Vector::operator==(const Vector& b) const
[ef9df36]403{
[1bd79e]404 return IsEqualTo(b);
[ef9df36]405};
406
[fa5a6a]407bool Vector::operator!=(const Vector& b) const
408{
409 return !IsEqualTo(b);
410}
411
[6ac7ee]412/** Sums vector \a to this lhs component-wise.
413 * \param a base vector
414 * \param b vector components to add
415 * \return lhs + a
416 */
[72e7fa]417const Vector& Vector::operator+=(const Vector& b)
[6ac7ee]418{
[273382]419 this->AddVector(b);
[72e7fa]420 return *this;
[6ac7ee]421};
[54a746]422
423/** Subtracts vector \a from this lhs component-wise.
424 * \param a base vector
425 * \param b vector components to add
426 * \return lhs - a
427 */
[72e7fa]428const Vector& Vector::operator-=(const Vector& b)
[54a746]429{
[273382]430 this->SubtractVector(b);
[72e7fa]431 return *this;
[54a746]432};
433
[6ac7ee]434/** factor each component of \a a times a double \a m.
435 * \param a base vector
436 * \param m factor
437 * \return lhs.x[i] * m
438 */
[b84d5d]439const Vector& operator*=(Vector& a, const double m)
[6ac7ee]440{
[042f82]441 a.Scale(m);
442 return a;
[6ac7ee]443};
444
[042f82]445/** Sums two vectors \a and \b component-wise.
[6ac7ee]446 * \param a first vector
447 * \param b second vector
448 * \return a + b
449 */
[72e7fa]450Vector const Vector::operator+(const Vector& b) const
[6ac7ee]451{
[72e7fa]452 Vector x = *this;
[273382]453 x.AddVector(b);
[b84d5d]454 return x;
[6ac7ee]455};
456
[54a746]457/** Subtracts vector \a from \b component-wise.
458 * \param a first vector
459 * \param b second vector
460 * \return a - b
461 */
[72e7fa]462Vector const Vector::operator-(const Vector& b) const
[54a746]463{
[72e7fa]464 Vector x = *this;
[273382]465 x.SubtractVector(b);
[b84d5d]466 return x;
[54a746]467};
468
[325390]469Vector &Vector::operator*=(const Matrix &mat){
470 (*this) = mat*(*this);
471 return *this;
472}
473
474Vector operator*(const Matrix &mat,const Vector &vec){
475 gsl_vector *res = gsl_vector_calloc(NDIM);
476 gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content, vec.content, 0.0, res);
477 return Vector(res);
478}
479
480
[6ac7ee]481/** Factors given vector \a a times \a m.
482 * \param a vector
483 * \param m factor
[54a746]484 * \return m * a
[6ac7ee]485 */
[b84d5d]486Vector const operator*(const Vector& a, const double m)
[6ac7ee]487{
[b84d5d]488 Vector x(a);
489 x.Scale(m);
490 return x;
[6ac7ee]491};
492
[54a746]493/** Factors given vector \a a times \a m.
494 * \param m factor
495 * \param a vector
496 * \return m * a
497 */
[b84d5d]498Vector const operator*(const double m, const Vector& a )
[54a746]499{
[b84d5d]500 Vector x(a);
501 x.Scale(m);
502 return x;
[54a746]503};
504
[9c20aa]505ostream& operator<<(ostream& ost, const Vector& m)
[6ac7ee]506{
[042f82]507 ost << "(";
508 for (int i=0;i<NDIM;i++) {
[0a4f7f]509 ost << m[i];
[042f82]510 if (i != 2)
511 ost << ",";
512 }
513 ost << ")";
514 return ost;
[6ac7ee]515};
516
517
[1bd79e]518void Vector::ScaleAll(const double *factor)
[6ac7ee]519{
[042f82]520 for (int i=NDIM;i--;)
[d466f0]521 at(i) *= factor[i];
[6ac7ee]522};
523
524
[1bd79e]525
[776b64]526void Vector::Scale(const double factor)
[6ac7ee]527{
[93987b]528 gsl_vector_scale(content,factor);
[6ac7ee]529};
530
[d09ff7]531/** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
532 * \param *M matrix of box
533 * \param *Minv inverse matrix
534 */
[d0f111]535void Vector::WrapPeriodically(const Matrix &M, const Matrix &Minv)
[d09ff7]536{
537 MatrixMultiplication(Minv);
538 // truncate to [0,1] for each axis
539 for (int i=0;i<NDIM;i++) {
[1dc9ec]540 //at(i) += 0.5; // set to center of box
[d466f0]541 while (at(i) >= 1.)
542 at(i) -= 1.;
543 while (at(i) < 0.)
544 at(i) += 1.;
[d09ff7]545 }
546 MatrixMultiplication(M);
547};
548
[45ef76]549std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
550 double factor = ScalarProduct(rhs)/rhs.NormSquared();
551 Vector res= factor * rhs;
552 return make_pair(res,(*this)-res);
553}
554
555std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
556 Vector helper = *this;
557 pointset res;
558 for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
559 pair<Vector,Vector> currPart = helper.partition(*iter);
560 res.push_back(currPart.first);
561 helper = currPart.second;
562 }
563 return make_pair(res,helper);
564}
565
[6ac7ee]566/** Do a matrix multiplication.
567 * \param *matrix NDIM_NDIM array
568 */
[c94eeb]569void Vector::MatrixMultiplication(const Matrix &M)
[6ac7ee]570{
[c94eeb]571 (*this) *= M;
[6ac7ee]572};
573
574/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
575 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
576 * \param *x1 first vector
577 * \param *x2 second vector
578 * \param *x3 third vector
579 * \param *factors three-component vector with the factor for each given vector
580 */
[273382]581void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
[6ac7ee]582{
[273382]583 (*this) = (factors[0]*x1) +
584 (factors[1]*x2) +
585 (factors[2]*x3);
[6ac7ee]586};
587
588/** Calculates orthonormal vector to one given vectors.
589 * Just subtracts the projection onto the given vector from this vector.
[ef9df36]590 * The removed part of the vector is Vector::Projection()
[6ac7ee]591 * \param *x1 vector
592 * \return true - success, false - vector is zero
593 */
[0a4f7f]594bool Vector::MakeNormalTo(const Vector &y1)
[6ac7ee]595{
[042f82]596 bool result = false;
[753f02]597 double factor = y1.ScalarProduct(*this)/y1.NormSquared();
[45ef76]598 Vector x1 = factor * y1;
[753f02]599 SubtractVector(x1);
[042f82]600 for (int i=NDIM;i--;)
[d466f0]601 result = result || (fabs(at(i)) > MYEPSILON);
[6ac7ee]602
[042f82]603 return result;
[6ac7ee]604};
605
606/** Creates this vector as one of the possible orthonormal ones to the given one.
607 * Just scan how many components of given *vector are unequal to zero and
608 * try to get the skp of both to be zero accordingly.
609 * \param *vector given vector
610 * \return true - success, false - failure (null vector given)
611 */
[273382]612bool Vector::GetOneNormalVector(const Vector &GivenVector)
[6ac7ee]613{
[042f82]614 int Components[NDIM]; // contains indices of non-zero components
615 int Last = 0; // count the number of non-zero entries in vector
616 int j; // loop variables
617 double norm;
618
619 for (j=NDIM;j--;)
620 Components[j] = -1;
[1829c4]621
622 // in two component-systems we need to find the one position that is zero
623 int zeroPos = -1;
[042f82]624 // find two components != 0
[1829c4]625 for (j=0;j<NDIM;j++){
[753f02]626 if (fabs(GivenVector[j]) > MYEPSILON)
[042f82]627 Components[Last++] = j;
[1829c4]628 else
629 // this our zero Position
630 zeroPos = j;
631 }
[042f82]632
633 switch(Last) {
634 case 3: // threecomponent system
[1829c4]635 // the position of the zero is arbitrary in three component systems
636 zeroPos = Components[2];
[042f82]637 case 2: // two component system
[753f02]638 norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
[1829c4]639 at(zeroPos) = 0.;
[042f82]640 // in skp both remaining parts shall become zero but with opposite sign and third is zero
[1829c4]641 at(Components[1]) = -1./GivenVector[Components[1]] / norm;
642 at(Components[0]) = 1./GivenVector[Components[0]] / norm;
[042f82]643 return true;
644 break;
645 case 1: // one component system
646 // set sole non-zero component to 0, and one of the other zero component pendants to 1
[1829c4]647 at((Components[0]+2)%NDIM) = 0.;
648 at((Components[0]+1)%NDIM) = 1.;
649 at(Components[0]) = 0.;
[042f82]650 return true;
651 break;
652 default:
653 return false;
654 }
[6ac7ee]655};
656
657/** Adds vector \a *y componentwise.
658 * \param *y vector
659 */
[273382]660void Vector::AddVector(const Vector &y)
[6ac7ee]661{
[93987b]662 gsl_vector_add(content, y.content);
[6ac7ee]663}
664
665/** Adds vector \a *y componentwise.
666 * \param *y vector
667 */
[273382]668void Vector::SubtractVector(const Vector &y)
[6ac7ee]669{
[93987b]670 gsl_vector_sub(content, y.content);
[ef9df36]671}
672
[89c8b2]673/**
674 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
675 * their offset.
676 *
677 * @param offest for the origin of the parallelepiped
678 * @param three vectors forming the matrix that defines the shape of the parallelpiped
679 */
[2f1a7a]680bool Vector::IsInParallelepiped(const Vector &offset, const double * const _parallelepiped) const
[89c8b2]681{
[753f02]682 Vector a = (*this)-offset;
[2f1a7a]683 Matrix parallelepiped = Matrix(_parallelepiped).invert();
684 a.MatrixMultiplication(parallelepiped);
[89c8b2]685 bool isInside = true;
686
687 for (int i=NDIM;i--;)
[753f02]688 isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
[89c8b2]689
690 return isInside;
691}
[005e18]692
693
694// some comonly used vectors
695const Vector zeroVec(0,0,0);
696const Vector e1(1,0,0);
697const Vector e2(0,1,0);
698const Vector e3(0,0,1);
Note: See TracBrowser for help on using the repository browser.