source: src/vector.cpp@ cadbc1

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 Candidate_v1.7.0 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 cadbc1 was 63d2b8, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Removed unused Vector::KeepPeriodic() method.

  • Property mode set to 100644
File size: 16.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/** Calculates scalar product between this and another vector.
177 * \param *y array to second vector
178 * \return \f$\langle x, y \rangle\f$
179 */
[273382]180double Vector::ScalarProduct(const Vector &y) const
[6ac7ee]181{
[042f82]182 double res = 0.;
[923b6c]183 gsl_blas_ddot(content, y.content, &res);
[042f82]184 return (res);
[6ac7ee]185};
186
187
188/** Calculates VectorProduct between this and another vector.
[042f82]189 * -# returns the Product in place of vector from which it was initiated
190 * -# ATTENTION: Only three dim.
191 * \param *y array to vector with which to calculate crossproduct
192 * \return \f$ x \times y \f&
[6ac7ee]193 */
[273382]194void Vector::VectorProduct(const Vector &y)
[6ac7ee]195{
[042f82]196 Vector tmp;
[d466f0]197 for(int i=NDIM;i--;)
198 tmp[i] = at((i+1)%NDIM)*y[(i+2)%NDIM] - at((i+2)%NDIM)*y[(i+1)%NDIM];
[753f02]199 (*this) = tmp;
[6ac7ee]200};
201
202
203/** projects this vector onto plane defined by \a *y.
204 * \param *y normal vector of plane
205 * \return \f$\langle x, y \rangle\f$
206 */
[273382]207void Vector::ProjectOntoPlane(const Vector &y)
[6ac7ee]208{
[042f82]209 Vector tmp;
[753f02]210 tmp = y;
[042f82]211 tmp.Normalize();
[753f02]212 tmp.Scale(ScalarProduct(tmp));
213 *this -= tmp;
[2319ed]214};
215
[821907]216/** Calculates the minimum distance of this vector to the plane.
217 * \sa Vector::GetDistanceVectorToPlane()
218 * \param *out output stream for debugging
219 * \param *PlaneNormal normal of plane
220 * \param *PlaneOffset offset of plane
221 * \return distance to plane
222 */
[d4c9ae]223double Vector::DistanceToSpace(const Space &space) const
[821907]224{
[d4c9ae]225 return space.distance(*this);
[c4d4df]226};
227
[6ac7ee]228/** Calculates the projection of a vector onto another \a *y.
229 * \param *y array to second vector
230 */
[273382]231void Vector::ProjectIt(const Vector &y)
[6ac7ee]232{
[753f02]233 (*this) += (-ScalarProduct(y))*y;
[ef9df36]234};
235
236/** Calculates the projection of a vector onto another \a *y.
237 * \param *y array to second vector
238 * \return Vector
239 */
[273382]240Vector Vector::Projection(const Vector &y) const
[ef9df36]241{
[753f02]242 Vector helper = y;
243 helper.Scale((ScalarProduct(y)/y.NormSquared()));
[ef9df36]244
245 return helper;
[6ac7ee]246};
247
248/** Calculates norm of this vector.
249 * \return \f$|x|\f$
250 */
251double Vector::Norm() const
252{
[273382]253 return (sqrt(NormSquared()));
[6ac7ee]254};
255
[d4d0dd]256/** Calculates squared norm of this vector.
257 * \return \f$|x|^2\f$
258 */
259double Vector::NormSquared() const
260{
[273382]261 return (ScalarProduct(*this));
[d4d0dd]262};
263
[6ac7ee]264/** Normalizes this vector.
265 */
266void Vector::Normalize()
267{
[1bd79e]268 double factor = Norm();
269 (*this) *= 1/factor;
[6ac7ee]270};
271
272/** Zeros all components of this vector.
273 */
274void Vector::Zero()
275{
[753f02]276 at(0)=at(1)=at(2)=0;
[6ac7ee]277};
278
279/** Zeros all components of this vector.
280 */
[776b64]281void Vector::One(const double one)
[6ac7ee]282{
[753f02]283 at(0)=at(1)=at(2)=one;
[6ac7ee]284};
285
[9c20aa]286/** Checks whether vector has all components zero.
287 * @return true - vector is zero, false - vector is not
288 */
[54a746]289bool Vector::IsZero() const
[9c20aa]290{
[d466f0]291 return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON);
[54a746]292};
293
294/** Checks whether vector has length of 1.
295 * @return true - vector is normalized, false - vector is not
296 */
297bool Vector::IsOne() const
298{
299 return (fabs(Norm() - 1.) < MYEPSILON);
[9c20aa]300};
301
[ef9df36]302/** Checks whether vector is normal to \a *normal.
303 * @return true - vector is normalized, false - vector is not
304 */
[273382]305bool Vector::IsNormalTo(const Vector &normal) const
[ef9df36]306{
307 if (ScalarProduct(normal) < MYEPSILON)
308 return true;
309 else
310 return false;
311};
312
[b998c3]313/** Checks whether vector is normal to \a *normal.
314 * @return true - vector is normalized, false - vector is not
315 */
[273382]316bool Vector::IsEqualTo(const Vector &a) const
[b998c3]317{
318 bool status = true;
319 for (int i=0;i<NDIM;i++) {
[d466f0]320 if (fabs(at(i) - a[i]) > MYEPSILON)
[b998c3]321 status = false;
322 }
323 return status;
324};
325
[6ac7ee]326/** Calculates the angle between this and another vector.
327 * \param *y array to second vector
328 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
329 */
[273382]330double Vector::Angle(const Vector &y) const
[6ac7ee]331{
[753f02]332 double norm1 = Norm(), norm2 = y.Norm();
[ef9df36]333 double angle = -1;
[d4d0dd]334 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
335 angle = this->ScalarProduct(y)/norm1/norm2;
[02da9e]336 // -1-MYEPSILON occured due to numerical imprecision, catch ...
[e138de]337 //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
[02da9e]338 if (angle < -1)
339 angle = -1;
340 if (angle > 1)
341 angle = 1;
[042f82]342 return acos(angle);
[6ac7ee]343};
344
[0a4f7f]345
346double& Vector::operator[](size_t i){
[753f02]347 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
[d690fa]348 return *gsl_vector_ptr (content, i);
[0a4f7f]349}
350
351const double& Vector::operator[](size_t i) const{
[753f02]352 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
[d690fa]353 return *gsl_vector_ptr (content, i);
[0a4f7f]354}
355
356double& Vector::at(size_t i){
357 return (*this)[i];
358}
359
360const double& Vector::at(size_t i) const{
361 return (*this)[i];
362}
363
[0c7ed8]364gsl_vector* Vector::get(){
365 return content;
[0a4f7f]366}
[6ac7ee]367
[ef9df36]368/** Compares vector \a to vector \a b component-wise.
369 * \param a base vector
370 * \param b vector components to add
371 * \return a == b
372 */
[72e7fa]373bool Vector::operator==(const Vector& b) const
[ef9df36]374{
[1bd79e]375 return IsEqualTo(b);
[ef9df36]376};
377
[fa5a6a]378bool Vector::operator!=(const Vector& b) const
379{
380 return !IsEqualTo(b);
381}
382
[6ac7ee]383/** Sums vector \a to this lhs component-wise.
384 * \param a base vector
385 * \param b vector components to add
386 * \return lhs + a
387 */
[72e7fa]388const Vector& Vector::operator+=(const Vector& b)
[6ac7ee]389{
[273382]390 this->AddVector(b);
[72e7fa]391 return *this;
[6ac7ee]392};
[54a746]393
394/** Subtracts vector \a from this lhs component-wise.
395 * \param a base vector
396 * \param b vector components to add
397 * \return lhs - a
398 */
[72e7fa]399const Vector& Vector::operator-=(const Vector& b)
[54a746]400{
[273382]401 this->SubtractVector(b);
[72e7fa]402 return *this;
[54a746]403};
404
[6ac7ee]405/** factor each component of \a a times a double \a m.
406 * \param a base vector
407 * \param m factor
408 * \return lhs.x[i] * m
409 */
[b84d5d]410const Vector& operator*=(Vector& a, const double m)
[6ac7ee]411{
[042f82]412 a.Scale(m);
413 return a;
[6ac7ee]414};
415
[042f82]416/** Sums two vectors \a and \b component-wise.
[6ac7ee]417 * \param a first vector
418 * \param b second vector
419 * \return a + b
420 */
[72e7fa]421Vector const Vector::operator+(const Vector& b) const
[6ac7ee]422{
[72e7fa]423 Vector x = *this;
[273382]424 x.AddVector(b);
[b84d5d]425 return x;
[6ac7ee]426};
427
[54a746]428/** Subtracts vector \a from \b component-wise.
429 * \param a first vector
430 * \param b second vector
431 * \return a - b
432 */
[72e7fa]433Vector const Vector::operator-(const Vector& b) const
[54a746]434{
[72e7fa]435 Vector x = *this;
[273382]436 x.SubtractVector(b);
[b84d5d]437 return x;
[54a746]438};
439
[325390]440Vector &Vector::operator*=(const Matrix &mat){
441 (*this) = mat*(*this);
442 return *this;
443}
444
445Vector operator*(const Matrix &mat,const Vector &vec){
446 gsl_vector *res = gsl_vector_calloc(NDIM);
447 gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content, vec.content, 0.0, res);
448 return Vector(res);
449}
450
451
[6ac7ee]452/** Factors given vector \a a times \a m.
453 * \param a vector
454 * \param m factor
[54a746]455 * \return m * a
[6ac7ee]456 */
[b84d5d]457Vector const operator*(const Vector& a, const double m)
[6ac7ee]458{
[b84d5d]459 Vector x(a);
460 x.Scale(m);
461 return x;
[6ac7ee]462};
463
[54a746]464/** Factors given vector \a a times \a m.
465 * \param m factor
466 * \param a vector
467 * \return m * a
468 */
[b84d5d]469Vector const operator*(const double m, const Vector& a )
[54a746]470{
[b84d5d]471 Vector x(a);
472 x.Scale(m);
473 return x;
[54a746]474};
475
[9c20aa]476ostream& operator<<(ostream& ost, const Vector& m)
[6ac7ee]477{
[042f82]478 ost << "(";
479 for (int i=0;i<NDIM;i++) {
[0a4f7f]480 ost << m[i];
[042f82]481 if (i != 2)
482 ost << ",";
483 }
484 ost << ")";
485 return ost;
[6ac7ee]486};
487
488
[1bd79e]489void Vector::ScaleAll(const double *factor)
[6ac7ee]490{
[042f82]491 for (int i=NDIM;i--;)
[d466f0]492 at(i) *= factor[i];
[6ac7ee]493};
494
495
[1bd79e]496
[776b64]497void Vector::Scale(const double factor)
[6ac7ee]498{
[93987b]499 gsl_vector_scale(content,factor);
[6ac7ee]500};
501
[d09ff7]502/** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
503 * \param *M matrix of box
504 * \param *Minv inverse matrix
505 */
[d0f111]506void Vector::WrapPeriodically(const Matrix &M, const Matrix &Minv)
[d09ff7]507{
508 MatrixMultiplication(Minv);
509 // truncate to [0,1] for each axis
510 for (int i=0;i<NDIM;i++) {
[1dc9ec]511 //at(i) += 0.5; // set to center of box
[d466f0]512 while (at(i) >= 1.)
513 at(i) -= 1.;
514 while (at(i) < 0.)
515 at(i) += 1.;
[d09ff7]516 }
517 MatrixMultiplication(M);
518};
519
[45ef76]520std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
521 double factor = ScalarProduct(rhs)/rhs.NormSquared();
522 Vector res= factor * rhs;
523 return make_pair(res,(*this)-res);
524}
525
526std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
527 Vector helper = *this;
528 pointset res;
529 for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
530 pair<Vector,Vector> currPart = helper.partition(*iter);
531 res.push_back(currPart.first);
532 helper = currPart.second;
533 }
534 return make_pair(res,helper);
535}
536
[6ac7ee]537/** Do a matrix multiplication.
538 * \param *matrix NDIM_NDIM array
539 */
[c94eeb]540void Vector::MatrixMultiplication(const Matrix &M)
[6ac7ee]541{
[c94eeb]542 (*this) *= M;
[6ac7ee]543};
544
545/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
546 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
547 * \param *x1 first vector
548 * \param *x2 second vector
549 * \param *x3 third vector
550 * \param *factors three-component vector with the factor for each given vector
551 */
[273382]552void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
[6ac7ee]553{
[273382]554 (*this) = (factors[0]*x1) +
555 (factors[1]*x2) +
556 (factors[2]*x3);
[6ac7ee]557};
558
559/** Calculates orthonormal vector to one given vectors.
560 * Just subtracts the projection onto the given vector from this vector.
[ef9df36]561 * The removed part of the vector is Vector::Projection()
[6ac7ee]562 * \param *x1 vector
563 * \return true - success, false - vector is zero
564 */
[0a4f7f]565bool Vector::MakeNormalTo(const Vector &y1)
[6ac7ee]566{
[042f82]567 bool result = false;
[753f02]568 double factor = y1.ScalarProduct(*this)/y1.NormSquared();
[45ef76]569 Vector x1 = factor * y1;
[753f02]570 SubtractVector(x1);
[042f82]571 for (int i=NDIM;i--;)
[d466f0]572 result = result || (fabs(at(i)) > MYEPSILON);
[6ac7ee]573
[042f82]574 return result;
[6ac7ee]575};
576
577/** Creates this vector as one of the possible orthonormal ones to the given one.
578 * Just scan how many components of given *vector are unequal to zero and
579 * try to get the skp of both to be zero accordingly.
580 * \param *vector given vector
581 * \return true - success, false - failure (null vector given)
582 */
[273382]583bool Vector::GetOneNormalVector(const Vector &GivenVector)
[6ac7ee]584{
[042f82]585 int Components[NDIM]; // contains indices of non-zero components
586 int Last = 0; // count the number of non-zero entries in vector
587 int j; // loop variables
588 double norm;
589
590 for (j=NDIM;j--;)
591 Components[j] = -1;
[1829c4]592
593 // in two component-systems we need to find the one position that is zero
594 int zeroPos = -1;
[042f82]595 // find two components != 0
[1829c4]596 for (j=0;j<NDIM;j++){
[753f02]597 if (fabs(GivenVector[j]) > MYEPSILON)
[042f82]598 Components[Last++] = j;
[1829c4]599 else
600 // this our zero Position
601 zeroPos = j;
602 }
[042f82]603
604 switch(Last) {
605 case 3: // threecomponent system
[1829c4]606 // the position of the zero is arbitrary in three component systems
607 zeroPos = Components[2];
[042f82]608 case 2: // two component system
[753f02]609 norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
[1829c4]610 at(zeroPos) = 0.;
[042f82]611 // in skp both remaining parts shall become zero but with opposite sign and third is zero
[1829c4]612 at(Components[1]) = -1./GivenVector[Components[1]] / norm;
613 at(Components[0]) = 1./GivenVector[Components[0]] / norm;
[042f82]614 return true;
615 break;
616 case 1: // one component system
617 // set sole non-zero component to 0, and one of the other zero component pendants to 1
[1829c4]618 at((Components[0]+2)%NDIM) = 0.;
619 at((Components[0]+1)%NDIM) = 1.;
620 at(Components[0]) = 0.;
[042f82]621 return true;
622 break;
623 default:
624 return false;
625 }
[6ac7ee]626};
627
628/** Adds vector \a *y componentwise.
629 * \param *y vector
630 */
[273382]631void Vector::AddVector(const Vector &y)
[6ac7ee]632{
[93987b]633 gsl_vector_add(content, y.content);
[6ac7ee]634}
635
636/** Adds vector \a *y componentwise.
637 * \param *y vector
638 */
[273382]639void Vector::SubtractVector(const Vector &y)
[6ac7ee]640{
[93987b]641 gsl_vector_sub(content, y.content);
[ef9df36]642}
643
[89c8b2]644/**
645 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
646 * their offset.
647 *
648 * @param offest for the origin of the parallelepiped
649 * @param three vectors forming the matrix that defines the shape of the parallelpiped
650 */
[2f1a7a]651bool Vector::IsInParallelepiped(const Vector &offset, const double * const _parallelepiped) const
[89c8b2]652{
[753f02]653 Vector a = (*this)-offset;
[2f1a7a]654 Matrix parallelepiped = Matrix(_parallelepiped).invert();
655 a.MatrixMultiplication(parallelepiped);
[89c8b2]656 bool isInside = true;
657
658 for (int i=NDIM;i--;)
[753f02]659 isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
[89c8b2]660
661 return isInside;
662}
[005e18]663
664
665// some comonly used vectors
666const Vector zeroVec(0,0,0);
667const Vector e1(1,0,0);
668const Vector e2(0,1,0);
669const Vector e3(0,0,1);
Note: See TracBrowser for help on using the repository browser.