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 <iomanip>
6#include <iostream>
7#include <array>
8
9#ifdef _MCD_CHECK
10#include "mcd.h"
11#endif
12
13namespace ModuleBase
14{
15
21template <class T> class Vector3
22{
23 public:
27
35 Vector3(const T &x1 = 0, const T &y1 = 0, const T &z1 = 0) : x(x1), y(y1), z(z1){};
36 Vector3(const Vector3<T> &v) : x(v.x), y(v.y), z(v.z){}; // Peize Lin add 2018-07-16
37 explicit Vector3(const std::array<T,3> &v) :x(v[0]), y(v[1]), z(v[2]){}
38
39 template <typename U>
40 explicit Vector3(const Vector3<U>& other) : x(static_cast<T>(other.x)), y(static_cast<T>(other.y)), z(static_cast<T>(other.z)) {}
41
42 Vector3(Vector3<T> &&v) noexcept : x(v.x), y(v.y), z(v.z) {}
43
51 void set(const T &x1, const T &y1, const T &z1)
52 {
53 x = x1;
54 y = y1;
55 z = z1;
56 }
57
65 {
66 x = u.x;
67 y = u.y;
68 z = u.z;
69 return *this;
70 }
71
73 {
74 x = u;
75 y = u;
76 z = u;
77 return *this;
78 }
79
87 {
88 x += u.x;
89 y += u.y;
90 z += u.z;
91 return *this;
92 }
93
101 {
102 x -= u.x;
103 y -= u.y;
104 z -= u.z;
105 return *this;
106 }
107
115 {
116 x *= s;
117 y *= s;
118 z *= s;
119 return *this;
120 }
121
129 {
130 x /= s;
131 y /= s;
132 z /= s;
133 return *this;
134 }
135
142 {
143 return Vector3<T>(-x, -y, -z);
144 } // Peize Lin add 2017-01-10
145
152 T operator[](int index) const
153 {
154 //return (&x)[index]; // this is undefind behavior and breaks with icpx
155 T const* ptr[3] = {&x, &y, &z};
156 return *ptr[index];
157 }
158
165 T &operator[](int index)
166 {
167 //return (&x)[index]; // this is undefind behavior and breaks with icpx
168 T* ptr[3] = {&x, &y, &z};
169 return *ptr[index];
170 }
171
177 T norm2(void) const
178 {
179 return x * x + y * y + z * z;
180 }
181
187 T norm(void) const
188 {
189 return sqrt(norm2());
190 }
191
198 {
199 const T m = norm();
200 x /= m;
201 y /= m;
202 z /= m;
203 return *this;
204 } // Peize Lin update return 2019-09-08
205
212 {
213 x = -x;
214 y = -y;
215 z = -z;
216 return *this;
217 } // Peize Lin update return 2019-09-08
218
224 void print(void) const; // mohan add 2009-11-29
225};
226
234template <class T> inline Vector3<T> operator+(const Vector3<T> &u, const Vector3<T> &v)
235{
236 return Vector3<T>(u.x + v.x, u.y + v.y, u.z + v.z);
237}
238
246template <class T> inline Vector3<T> operator-(const Vector3<T> &u, const Vector3<T> &v)
247{
248 return Vector3<T>(u.x - v.x, u.y - v.y, u.z - v.z);
249}
250
259template <class T> inline T operator*(const Vector3<T> &u, const Vector3<T> &v)
260{
261 return (u.x * v.x + u.y * v.y + u.z * v.z);
262}
263
271template <class T> inline Vector3<T> operator*(const T &s, const Vector3<T> &u)
272{
273 return Vector3<T>(u.x * s, u.y * s, u.z * s);
274}
275
283template <class T> inline Vector3<T> operator*(const Vector3<T> &u, const T &s)
284{
285 return Vector3<T>(u.x * s, u.y * s, u.z * s);
286} // mohan add 2009-5-10
287
296template <class T> inline Vector3<T> operator/(const Vector3<T> &u, const T &s)
297{
298 return Vector3<T>(u.x / s, u.y / s, u.z / s);
299}
300
309template <class T> inline Vector3<T> operator/(const T &s, const Vector3<T> &u)
310{
311 return Vector3<T>(s/u.x, s/u.y, s/u.z);
312}
313
322template <class T> inline T dot(const Vector3<T> &u, const Vector3<T> &v)
323{
324 return (u.x * v.x + u.y * v.y + u.z * v.z);
325}
326
339template <class T> inline Vector3<T> operator^(const Vector3<T> &u, const Vector3<T> &v)
340{
341 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);
342}
343
356template <class T> inline Vector3<T> cross(const Vector3<T> &u, const Vector3<T> &v)
357{
358 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);
359}
360// s = u.(v x w)
361// template <class T> T TripleScalarProduct(Vector3<T> u, Vector3<T> v, Vector3<T> w)
362//{
363// return T((u.x * (v.y * w.z - v.z * w.y)) +
364// (u.y * (-v.x * w.z + v.z * w.x)) +
365// (u.z * (v.x * w.y - v.y * w.x)));
366// }
367
368// Overload the < operator for sorting
369template <class T> bool operator<(const Vector3<T> &u, const Vector3<T> &v)
370{
371 if (u.x < v.x)
372 return true;
373 if (u.x > v.x)
374 return false;
375 if (u.y < v.y)
376 return true;
377 if (u.y > v.y)
378 return false;
379 if (u.z < v.z)
380 return true;
381 return false;
382}
383
384// whether m1 != m2
385template <class T> inline bool operator!=(const Vector3<T> &u, const Vector3<T> &v)
386{
387 return !(u == v);
388}
389// whether u == v
390template <class T> inline bool operator==(const Vector3<T> &u, const Vector3<T> &v)
391{
392 if (u.x == v.x && u.y == v.y && u.z == v.z)
393 return true;
394 return false;
395}
396
401template <class T> void Vector3<T>::print(void) const
402{
403 std::cout.precision(5);
404 std::cout << "(" << std::setw(10) << x << "," << std::setw(10) << y << "," << std::setw(10) << z << ")"
405 << std::endl;
406 return;
407}
408
418template <class T> static std::ostream &operator<<(std::ostream &os, const Vector3<T> &u)
419{
420 os << "(" << std::setw(10) << u.x << "," << std::setw(10) << u.y << "," << std::setw(10) << u.z << ")";
421 return os;
422}
423
424} // namespace ModuleBase
425
426#endif
3 elements vector
Definition vector3.h:22
Vector3< T > & operator*=(const T &s)
Overload operator "*=" for (Vector3)*scalar.
Definition vector3.h:114
Vector3(const Vector3< T > &v)
Definition vector3.h:36
Vector3(const std::array< T, 3 > &v)
Definition vector3.h:37
T norm2(void) const
Get the square of nomr of a Vector3.
Definition vector3.h:177
Vector3< T > & operator=(const T &u)
Definition vector3.h:72
Vector3< T > & operator/=(const T &s)
Overload operator "/=" for (Vector3)/scalar.
Definition vector3.h:128
Vector3< T > & operator+=(const Vector3< T > &u)
Overload operator "+=" for Vector3.
Definition vector3.h:86
T operator[](int index) const
Over load "[]" for accessing elements with pointers.
Definition vector3.h:152
T & operator[](int index)
Overload operator "[]" for accesing elements.
Definition vector3.h:165
Vector3(const Vector3< U > &other)
Definition vector3.h:40
Vector3< T > & operator-=(const Vector3< T > &u)
Overload operator "-=" for Vector3.
Definition vector3.h:100
void set(const T &x1, const T &y1, const T &z1)
set a 3d vector
Definition vector3.h:51
Vector3< T > & operator=(const Vector3< T > &u)
Overload operator "=" for Vector3.
Definition vector3.h:64
T x
Definition vector3.h:24
Vector3< T > operator-() const
Overload operator "-" to get (-Vector3)
Definition vector3.h:141
Vector3< T > & reverse(void)
Get (-Vector3)
Definition vector3.h:211
Vector3(const T &x1=0, const T &y1=0, const T &z1=0)
Construct a new Vector 3 object.
Definition vector3.h:35
T norm(void) const
Get the norm of a Vector3.
Definition vector3.h:187
void print(void) const
Print a Vector3 on standard output with formats.
Definition vector3.h:401
Vector3< T > & normalize(void)
Normalize a Vector3.
Definition vector3.h:197
T y
Definition vector3.h:25
Vector3(Vector3< T > &&v) noexcept
Definition vector3.h:42
T z
Definition vector3.h:26
#define T
Definition exp.cpp:237
Definition array_pool.h:6
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:218
ComplexArray operator*(const double r, const ComplexArray &cd)
Scale a ComplexArray cd by real r.
Definition complexarray.cpp:120
Vector3< T > operator^(const Vector3< T > &u, const Vector3< T > &v)
Overload "^" for cross product of two Vector3.
Definition vector3.h:339
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:369
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:356
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