source: src/vector.cpp@ 0c7ed8

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 0c7ed8 was 0c7ed8, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Passed the internal gsl_vector structure from Vector to linearSystemOfEquations.

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