source: src/vector.cpp@ 325390

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

Started work on basic matrix class to be used with Vector class

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