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
10namespace ModuleBase
11{
12 // Small epsilon value for numerical comparisons
13 constexpr double epsilon = 1e-10;
14
20template <class T> class Vector3
21{
22 public:
26
34 Vector3(const T &x1 = 0, const T &y1 = 0, const T &z1 = 0)
35 : x(x1), y(y1), z(z1)
36 {}
38 : x(v.x), y(v.y), z(v.z)
39 {}
40 explicit Vector3(const std::array<T,3> &v)
41 : x(v[0]), y(v[1]), z(v[2])
42 {}
43
44 template <typename U>
45 explicit Vector3(const Vector3<U>& other)
46 : x(static_cast<T>(other.x)), y(static_cast<T>(other.y)), z(static_cast<T>(other.z))
47 {}
48
49 Vector3(Vector3<T> &&v) noexcept
50 : x(v.x), y(v.y), z(v.z)
51 {}
52
60 void set(const T &x1, const T &y1, const T &z1)
61 {
62 x = x1;
63 y = y1;
64 z = z1;
65 }
66
74 {
75 x = u.x;
76 y = u.y;
77 z = u.z;
78 return *this;
79 }
80
82 {
83 x = u;
84 y = u;
85 z = u;
86 return *this;
87 }
88
96 {
97 x = u.x;
98 y = u.y;
99 z = u.z;
100 return *this;
101 }
102
110 {
111 x += u.x;
112 y += u.y;
113 z += u.z;
114 return *this;
115 }
116
124 {
125 x -= u.x;
126 y -= u.y;
127 z -= u.z;
128 return *this;
129 }
130
138 {
139 x *= s;
140 y *= s;
141 z *= s;
142 return *this;
143 }
144
152 {
153 assert(s != 0); // Avoid division by zero
154 x /= s;
155 y /= s;
156 z /= s;
157 return *this;
158 }
159
166 {
167 return Vector3<T>(-x, -y, -z);
168 }
169
176 T operator[](int index) const
177 {
178 assert(index >= 0 && index < 3);
179 //return (&x)[index]; // this is undefind behavior and breaks with icpx
180 T const* ptr[3] = {&x, &y, &z};
181 return *ptr[index];
182 }
183
190 T &operator[](int index)
191 {
192 assert(index >= 0 && index < 3);
193 //return (&x)[index]; // this is undefind behavior and breaks with icpx
194 T* ptr[3] = {&x, &y, &z};
195 return *ptr[index];
196 }
197
203 inline T norm2(void) const
204 {
205 return x * x + y * y + z * z;
206 }
207
213 inline T norm(void) const
214 {
215 return sqrt(norm2());
216 }
217
224 {
225 const T m = norm();
226 if (m > epsilon) // Avoid division by zero
227 {
228 x /= m;
229 y /= m;
230 z /= m;
231 }
232 return *this;
233 } // Peize Lin update return 2019-09-08
234
241 {
242 x = -x;
243 y = -y;
244 z = -z;
245 return *this;
246 } // Peize Lin update return 2019-09-08
247
258 void print(const int precision = 5) const;
259};
260
268template <class T> inline Vector3<T> operator+(const Vector3<T> &u, const Vector3<T> &v)
269{
270 return Vector3<T>(u.x + v.x, u.y + v.y, u.z + v.z);
271}
272
280template <class T> inline Vector3<T> operator-(const Vector3<T> &u, const Vector3<T> &v)
281{
282 return Vector3<T>(u.x - v.x, u.y - v.y, u.z - v.z);
283}
284
293template <class T> inline T operator*(const Vector3<T> &u, const Vector3<T> &v)
294{
295 return (u.x * v.x + u.y * v.y + u.z * v.z);
296}
297
305template <class T> inline Vector3<T> operator*(const T &s, const Vector3<T> &u)
306{
307 return Vector3<T>(u.x * s, u.y * s, u.z * s);
308}
309
317template <class T> inline Vector3<T> operator*(const Vector3<T> &u, const T &s)
318{
319 return Vector3<T>(u.x * s, u.y * s, u.z * s);
320} // mohan add 2009-5-10
321
330template <class T> inline Vector3<T> operator/(const Vector3<T> &u, const T &s)
331{
332 return Vector3<T>(u.x / s, u.y / s, u.z / s);
333}
334
343template <class T> inline Vector3<T> operator/(const T &s, const Vector3<T> &u)
344{
345 return Vector3<T>(s/u.x, s/u.y, s/u.z);
346}
347
356template <class T> inline T dot(const Vector3<T> &u, const Vector3<T> &v)
357{
358 return (u.x * v.x + u.y * v.y + u.z * v.z);
359}
360
373template <class T> inline Vector3<T> operator^(const Vector3<T> &u, const Vector3<T> &v)
374{
375 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);
376}
377
390template <class T> inline Vector3<T> cross(const Vector3<T> &u, const Vector3<T> &v)
391{
392 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);
393}
394// s = u.(v x w)
395// template <class T> T TripleScalarProduct(Vector3<T> u, Vector3<T> v, Vector3<T> w)
396//{
397// return T((u.x * (v.y * w.z - v.z * w.y)) +
398// (u.y * (-v.x * w.z + v.z * w.x)) +
399// (u.z * (v.x * w.y - v.y * w.x)));
400// }
401
402// Overload the < operator for sorting
403template <class T> bool operator<(const Vector3<T> &u, const Vector3<T> &v)
404{
405 if (u.x < v.x)
406 {
407 return true;
408 }
409 if (u.x > v.x)
410 {
411 return false;
412 }
413 if (u.y < v.y)
414 {
415 return true;
416 }
417 if (u.y > v.y)
418 {
419 return false;
420 }
421 if (u.z < v.z)
422 {
423 return true;
424 }
425 return false;
426}
427
428// whether m1 != m2
429template <class T> inline bool operator!=(const Vector3<T> &u, const Vector3<T> &v)
430{
431 return !(u == v);
432}
433// whether u == v
434template <class T> inline bool operator==(const Vector3<T> &u, const Vector3<T> &v)
435{
436 if (u.x == v.x && u.y == v.y && u.z == v.z)
437 {
438 return true;
439 }
440 return false;
441}
442
448template <class T> void Vector3<T>::print(const int precision) const
449{
450 // Ensure precision is non-negative
451 int valid_precision = precision > 0 ? precision : 5;
452 std::cout.precision(valid_precision);
453 std::cout << "(" << std::setw(10) << x << "," << std::setw(10) << y << "," << std::setw(10) << z << ")"
454 << std::endl;
455 return;
456}
457
467template <class T> static std::ostream &operator<<(std::ostream &os, const Vector3<T> &u)
468{
469 os << "(" << std::setw(10) << u.x << "," << std::setw(10) << u.y << "," << std::setw(10) << u.z << ")";
470 return os;
471}
472
473} // namespace ModuleBase
474
475#endif
3 elements vector
Definition vector3.h:21
Vector3< T > & operator*=(const T &s)
Overload operator "*=" for (Vector3)*scalar.
Definition vector3.h:137
Vector3(const Vector3< T > &v)
Definition vector3.h:37
Vector3< T > & operator=(Vector3< T > &&u) noexcept
Move assignment operator.
Definition vector3.h:95
Vector3(const std::array< T, 3 > &v)
Definition vector3.h:40
T norm2(void) const
Get the square of norm of a Vector3.
Definition vector3.h:203
Vector3< T > & operator=(const T &u)
Definition vector3.h:81
Vector3< T > & operator/=(const T &s)
Overload operator "/=" for Vector3.
Definition vector3.h:151
Vector3< T > & operator+=(const Vector3< T > &u)
Overload operator "+=" for Vector3.
Definition vector3.h:109
T operator[](int index) const
Over load "[]" for accessing elements with pointers.
Definition vector3.h:176
T & operator[](int index)
Overload operator "[]" for accesing elements.
Definition vector3.h:190
Vector3(const Vector3< U > &other)
Definition vector3.h:45
Vector3< T > & operator-=(const Vector3< T > &u)
Overload operator "-=" for Vector3.
Definition vector3.h:123
void set(const T &x1, const T &y1, const T &z1)
set a 3d vector
Definition vector3.h:60
Vector3< T > & operator=(const Vector3< T > &u)
Overload operator "=" for Vector3.
Definition vector3.h:73
double x
Definition vector3.h:23
void print(const int precision=5) const
Print a Vector3 on standard output with formats.
Definition vector3.h:448
Vector3< T > operator-() const
Overload operator "-" to get (-Vector3).
Definition vector3.h:165
Vector3< T > & reverse(void)
Get (-Vector3).
Definition vector3.h:240
Vector3(const T &x1=0, const T &y1=0, const T &z1=0)
Default constructor.
Definition vector3.h:34
T norm(void) const
Get the norm of a Vector3.
Definition vector3.h:213
Vector3< T > & normalize(void)
Normalize a Vector3.
Definition vector3.h:223
double y
Definition vector3.h:24
Vector3(Vector3< T > &&v) noexcept
Definition vector3.h:49
double z
Definition vector3.h:25
#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:13
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:373
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:403
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:390
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