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