ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
vector3.h
Go to the documentation of this file.
1#ifndef VECTOR3_H
2#define VECTOR3_H
3
4#include <cmath>
5#include <cassert>
6#include <iomanip>
7#include <iostream>
8#include <array>
9
10#ifdef _MCD_CHECK
11#endif
12
13namespace ModuleBase
14{
15 // Small epsilon value for numerical comparisons
16 constexpr double epsilon = 1e-10;
17
23template <class T> class Vector3
24{
25 public:
29
37 Vector3(const T &x1 = 0, const T &y1 = 0, const T &z1 = 0)
38 : x(x1), y(y1), z(z1)
39 {}
41 : x(v.x), y(v.y), z(v.z)
42 {}
43 explicit Vector3(const std::array<T,3> &v)
44 : x(v[0]), y(v[1]), z(v[2])
45 {}
46
47 template <typename U>
48 explicit Vector3(const Vector3<U>& other)
49 : x(static_cast<T>(other.x)), y(static_cast<T>(other.y)), z(static_cast<T>(other.z))
50 {}
51
52 Vector3(Vector3<T> &&v) noexcept
53 : x(v.x), y(v.y), z(v.z)
54 {}
55
63 void set(const T &x1, const T &y1, const T &z1)
64 {
65 x = x1;
66 y = y1;
67 z = z1;
68 }
69
77 {
78 x = u.x;
79 y = u.y;
80 z = u.z;
81 return *this;
82 }
83
85 {
86 x = u;
87 y = u;
88 z = u;
89 return *this;
90 }
91
99 {
100 x = u.x;
101 y = u.y;
102 z = u.z;
103 return *this;
104 }
105
113 {
114 x += u.x;
115 y += u.y;
116 z += u.z;
117 return *this;
118 }
119
127 {
128 x -= u.x;
129 y -= u.y;
130 z -= u.z;
131 return *this;
132 }
133
141 {
142 x *= s;
143 y *= s;
144 z *= s;
145 return *this;
146 }
147
155 {
156 assert(s != 0); // Avoid division by zero
157 x /= s;
158 y /= s;
159 z /= s;
160 return *this;
161 }
162
169 {
170 return Vector3<T>(-x, -y, -z);
171 }
172
179 T operator[](int index) const
180 {
181 assert(index >= 0 && index < 3);
182 //return (&x)[index]; // this is undefind behavior and breaks with icpx
183 T const* ptr[3] = {&x, &y, &z};
184 return *ptr[index];
185 }
186
193 T &operator[](int index)
194 {
195 assert(index >= 0 && index < 3);
196 //return (&x)[index]; // this is undefind behavior and breaks with icpx
197 T* ptr[3] = {&x, &y, &z};
198 return *ptr[index];
199 }
200
206 inline T norm2(void) const
207 {
208 return x * x + y * y + z * z;
209 }
210
216 inline T norm(void) const
217 {
218 return sqrt(norm2());
219 }
220
227 {
228 const T m = norm();
229 if (m > epsilon) // Avoid division by zero
230 {
231 x /= m;
232 y /= m;
233 z /= m;
234 }
235 return *this;
236 } // Peize Lin update return 2019-09-08
237
244 {
245 x = -x;
246 y = -y;
247 z = -z;
248 return *this;
249 } // Peize Lin update return 2019-09-08
250
261 void print(const int precision = 5) const;
262};
263
271template <class T> inline Vector3<T> operator+(const Vector3<T> &u, const Vector3<T> &v)
272{
273 return Vector3<T>(u.x + v.x, u.y + v.y, u.z + v.z);
274}
275
283template <class T> inline Vector3<T> operator-(const Vector3<T> &u, const Vector3<T> &v)
284{
285 return Vector3<T>(u.x - v.x, u.y - v.y, u.z - v.z);
286}
287
296template <class T> inline T operator*(const Vector3<T> &u, const Vector3<T> &v)
297{
298 return (u.x * v.x + u.y * v.y + u.z * v.z);
299}
300
308template <class T> inline Vector3<T> operator*(const T &s, const Vector3<T> &u)
309{
310 return Vector3<T>(u.x * s, u.y * s, u.z * s);
311}
312
320template <class T> inline Vector3<T> operator*(const Vector3<T> &u, const T &s)
321{
322 return Vector3<T>(u.x * s, u.y * s, u.z * s);
323} // mohan add 2009-5-10
324
333template <class T> inline Vector3<T> operator/(const Vector3<T> &u, const T &s)
334{
335 return Vector3<T>(u.x / s, u.y / s, u.z / s);
336}
337
346template <class T> inline Vector3<T> operator/(const T &s, const Vector3<T> &u)
347{
348 return Vector3<T>(s/u.x, s/u.y, s/u.z);
349}
350
359template <class T> inline T dot(const Vector3<T> &u, const Vector3<T> &v)
360{
361 return (u.x * v.x + u.y * v.y + u.z * v.z);
362}
363
376template <class T> inline Vector3<T> operator^(const Vector3<T> &u, const Vector3<T> &v)
377{
378 return Vector3<T>(u.y * v.z - u.z * v.y, -u.x * v.z + u.z * v.x, u.x * v.y - u.y * v.x);
379}
380
393template <class T> inline Vector3<T> cross(const Vector3<T> &u, const Vector3<T> &v)
394{
395 return Vector3<T>(u.y * v.z - u.z * v.y, -u.x * v.z + u.z * v.x, u.x * v.y - u.y * v.x);
396}
397// s = u.(v x w)
398// template <class T> T TripleScalarProduct(Vector3<T> u, Vector3<T> v, Vector3<T> w)
399//{
400// return T((u.x * (v.y * w.z - v.z * w.y)) +
401// (u.y * (-v.x * w.z + v.z * w.x)) +
402// (u.z * (v.x * w.y - v.y * w.x)));
403// }
404
405// Overload the < operator for sorting
406template <class T> bool operator<(const Vector3<T> &u, const Vector3<T> &v)
407{
408 if (u.x < v.x)
409 {
410 return true;
411 }
412 if (u.x > v.x)
413 {
414 return false;
415 }
416 if (u.y < v.y)
417 {
418 return true;
419 }
420 if (u.y > v.y)
421 {
422 return false;
423 }
424 if (u.z < v.z)
425 {
426 return true;
427 }
428 return false;
429}
430
431// whether m1 != m2
432template <class T> inline bool operator!=(const Vector3<T> &u, const Vector3<T> &v)
433{
434 return !(u == v);
435}
436// whether u == v
437template <class T> inline bool operator==(const Vector3<T> &u, const Vector3<T> &v)
438{
439 if (u.x == v.x && u.y == v.y && u.z == v.z)
440 {
441 return true;
442 }
443 return false;
444}
445
451template <class T> void Vector3<T>::print(const int precision) const
452{
453 // Ensure precision is non-negative
454 int valid_precision = precision > 0 ? precision : 5;
455 std::cout.precision(valid_precision);
456 std::cout << "(" << std::setw(10) << x << "," << std::setw(10) << y << "," << std::setw(10) << z << ")"
457 << std::endl;
458 return;
459}
460
470template <class T> static std::ostream &operator<<(std::ostream &os, const Vector3<T> &u)
471{
472 os << "(" << std::setw(10) << u.x << "," << std::setw(10) << u.y << "," << std::setw(10) << u.z << ")";
473 return os;
474}
475
476} // namespace ModuleBase
477
478#endif
3 elements vector
Definition vector3.h:24
Vector3< T > & operator*=(const T &s)
Overload operator "*=" for (Vector3)*scalar.
Definition vector3.h:140
Vector3(const Vector3< T > &v)
Definition vector3.h:40
Vector3< T > & operator=(Vector3< T > &&u) noexcept
Move assignment operator.
Definition vector3.h:98
Vector3(const std::array< T, 3 > &v)
Definition vector3.h:43
T norm2(void) const
Get the square of norm of a Vector3.
Definition vector3.h:206
Vector3< T > & operator=(const T &u)
Definition vector3.h:84
Vector3< T > & operator/=(const T &s)
Overload operator "/=" for Vector3.
Definition vector3.h:154
Vector3< T > & operator+=(const Vector3< T > &u)
Overload operator "+=" for Vector3.
Definition vector3.h:112
T operator[](int index) const
Over load "[]" for accessing elements with pointers.
Definition vector3.h:179
T & operator[](int index)
Overload operator "[]" for accesing elements.
Definition vector3.h:193
Vector3(const Vector3< U > &other)
Definition vector3.h:48
Vector3< T > & operator-=(const Vector3< T > &u)
Overload operator "-=" for Vector3.
Definition vector3.h:126
void set(const T &x1, const T &y1, const T &z1)
set a 3d vector
Definition vector3.h:63
Vector3< T > & operator=(const Vector3< T > &u)
Overload operator "=" for Vector3.
Definition vector3.h:76
T x
Definition vector3.h:26
void print(const int precision=5) const
Print a Vector3 on standard output with formats.
Definition vector3.h:451
Vector3< T > operator-() const
Overload operator "-" to get (-Vector3)
Definition vector3.h:168
Vector3< T > & reverse(void)
Get (-Vector3)
Definition vector3.h:243
Vector3(const T &x1=0, const T &y1=0, const T &z1=0)
Default constructor.
Definition vector3.h:37
T norm(void) const
Get the norm of a Vector3.
Definition vector3.h:216
Vector3< T > & normalize(void)
Normalize a Vector3.
Definition vector3.h:226
T y
Definition vector3.h:27
Vector3(Vector3< T > &&v) noexcept
Definition vector3.h:52
T z
Definition vector3.h:28
#define T
Definition exp.cpp:237
Definition clebsch_gordan_coeff.cpp:8
ComplexMatrix operator-(const ComplexMatrix &m1, const ComplexMatrix &m2)
Definition complexmatrix.cpp:154
std::complex< double > dot(const ComplexArray &cd1, const ComplexArray &cd2)
Take "dot-product" of two ComplexArray: sum of cd1(conjugate)[i] * cd2[i].
Definition complexarray.cpp:352
constexpr double epsilon
Definition vector3.h:16
ComplexArray operator*(const double r, const ComplexArray &cd)
Scale a ComplexArray cd by real r.
Definition complexarray.cpp:173
Vector3< T > operator^(const Vector3< T > &u, const Vector3< T > &v)
Overload "^" for cross product of two Vector3.
Definition vector3.h:376
bool operator!=(const Matrix3 &m1, const Matrix3 &m2)
Overload operator "!=" to assert the inequality between two 3x3 matrices.
Definition matrix3.cpp:189
bool operator<(const Vector3< T > &u, const Vector3< T > &v)
Definition vector3.h:406
Matrix3 operator/(const Matrix3 &m, const double &s)
Overload operator "/" for a (Matrix3)/(scalar) i.e. m/s.
Definition matrix3.cpp:133
Vector3< T > cross(const Vector3< T > &u, const Vector3< T > &v)
Cross product of two Vector3.
Definition vector3.h:393
bool operator==(const Matrix3 &m1, const Matrix3 &m2)
Overload operator "==" to assert the equality between two 3x3 matrices.
Definition matrix3.cpp:171
ComplexMatrix operator+(const ComplexMatrix &m1, const ComplexMatrix &m2)
Definition complexmatrix.cpp:143