|
5 | 5 | #include "../src_parallel/mcd.h" |
6 | 6 | #endif |
7 | 7 |
|
8 | | -#include "vector3.h" |
9 | 8 | #include "matrix.h" |
| 9 | +#include "vector3.h" |
10 | 10 |
|
11 | 11 | namespace ModuleBase |
12 | 12 | { |
13 | 13 |
|
| 14 | +/** |
| 15 | + * @brief 3x3 matrix and related mathamatical operations |
| 16 | + * |
| 17 | + */ |
14 | 18 | class Matrix3 |
15 | 19 | { |
16 | | - /* data */ |
17 | | -public: |
18 | | - // element eij: i_row, j_column |
19 | | - double e11, e12, e13, e21, e22, e23, e31, e32, e33; |
20 | | - |
21 | | - /* Constructors and destructor */ |
22 | | - Matrix3(){ Identity(); } |
23 | | - Matrix3(const double &r11,const double &r12,const double &r13, |
24 | | - const double &r21,const double &r22,const double &r23, |
25 | | - const double &r31,const double &r32,const double &r33); |
26 | | - |
27 | | - void Identity(void); |
28 | | - void Zero(void); |
29 | | - double Det(void) const ; |
30 | | - Matrix3 Transpose(void) const ; |
31 | | - Matrix3 Inverse(void) const ; |
32 | | - |
33 | | - Matrix3& operator=(const Matrix3 &m); |
34 | | - Matrix3& operator+=(const Matrix3 &m); |
35 | | - Matrix3& operator-=(const Matrix3 &m); |
36 | | - Matrix3& operator*=(const double &s); |
37 | | - Matrix3& operator/=(const double &s); |
38 | | - |
39 | | - void print(void)const; |
40 | | - ModuleBase::matrix to_matrix(void)const; |
| 20 | + |
| 21 | + public: |
| 22 | + /** |
| 23 | + * @brief element e_ij: i_row, j_column |
| 24 | + * |
| 25 | + */ |
| 26 | + double e11, e12, e13, e21, e22, e23, e31, e32, e33; |
| 27 | + |
| 28 | + /** |
| 29 | + * @brief Construct a new Matrix 3 object |
| 30 | + * to Identity matrix |
| 31 | + * |
| 32 | + */ |
| 33 | + Matrix3() |
| 34 | + { |
| 35 | + Identity(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Construct a new Matrix 3 object |
| 40 | + * |
| 41 | + * @param r11 element r_ij: i_row, j_column |
| 42 | + * @param r12 |
| 43 | + * @param r13 |
| 44 | + * @param r21 |
| 45 | + * @param r22 |
| 46 | + * @param r23 |
| 47 | + * @param r31 |
| 48 | + * @param r32 |
| 49 | + * @param r33 |
| 50 | + */ |
| 51 | + Matrix3(const double &r11, |
| 52 | + const double &r12, |
| 53 | + const double &r13, |
| 54 | + const double &r21, |
| 55 | + const double &r22, |
| 56 | + const double &r23, |
| 57 | + const double &r31, |
| 58 | + const double &r32, |
| 59 | + const double &r33); |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief Set a 3x3 matrix to identity matrix |
| 63 | + * |
| 64 | + */ |
| 65 | + void Identity(void); |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Set all elements of a 3x3 matrix to zero |
| 69 | + * |
| 70 | + */ |
| 71 | + void Zero(void); |
| 72 | + |
| 73 | + /** |
| 74 | + * @brief Calculate the determinant of a 3x3 matrix |
| 75 | + * |
| 76 | + * @return double |
| 77 | + */ |
| 78 | + double Det(void) const; |
| 79 | + |
| 80 | + /** |
| 81 | + * @brief Transpose a 3x3 matrix |
| 82 | + * |
| 83 | + * @return Matrix3 |
| 84 | + */ |
| 85 | + Matrix3 Transpose(void) const; |
| 86 | + |
| 87 | + /** |
| 88 | + * @brief Inverse a 3x3 matrix |
| 89 | + * |
| 90 | + * @return Matrix3 |
| 91 | + */ |
| 92 | + Matrix3 Inverse(void) const; |
| 93 | + |
| 94 | + /** |
| 95 | + * @brief Overload operator "=" for 3x3 matrices |
| 96 | + * For example, assign mb = ma |
| 97 | + * |
| 98 | + * @param m |
| 99 | + * @return Matrix3& |
| 100 | + */ |
| 101 | + Matrix3 &operator=(const Matrix3 &m); |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Overload operator "+=" for 3x3 matrices |
| 105 | + * For example, mb += ma |
| 106 | + * |
| 107 | + * @param m |
| 108 | + * @return Matrix3& |
| 109 | + */ |
| 110 | + Matrix3 &operator+=(const Matrix3 &m); |
| 111 | + |
| 112 | + /** |
| 113 | + * @brief Overload operator "-=" for 3x3 matrices |
| 114 | + * For example, mb -= ma |
| 115 | + * |
| 116 | + * @param m |
| 117 | + * @return Matrix3& |
| 118 | + */ |
| 119 | + Matrix3 &operator-=(const Matrix3 &m); |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Overload operator "*=" for 3x3 matrix and |
| 123 | + * a scalar |
| 124 | + * For example, mb *= 3.0 |
| 125 | + * |
| 126 | + * @param s The scalar |
| 127 | + * @return Matrix3& |
| 128 | + */ |
| 129 | + Matrix3 &operator*=(const double &s); |
| 130 | + |
| 131 | + /** |
| 132 | + * @brief Overload operator "/=" for 3x3 matrix and |
| 133 | + * a scalar |
| 134 | + * For example, mb /= 3.0 |
| 135 | + * |
| 136 | + * @param s The scalar |
| 137 | + * @return Matrix3& |
| 138 | + */ |
| 139 | + Matrix3 &operator/=(const double &s); |
| 140 | + |
| 141 | + /** |
| 142 | + * @brief Print a 3x3 matrix on screening |
| 143 | + * |
| 144 | + */ |
| 145 | + void print(void) const; |
| 146 | + |
| 147 | + /** |
| 148 | + * @brief Change the form of a 3x3 matrix from that of |
| 149 | + * class Matrix3 to that of class matrix |
| 150 | + * |
| 151 | + * @return ModuleBase::matrix |
| 152 | + */ |
| 153 | + ModuleBase::matrix to_matrix(void) const; |
41 | 154 | }; |
42 | 155 |
|
43 | | -Matrix3 operator +(const Matrix3 &m1, const Matrix3 &m2); //m1+m2 |
44 | | -Matrix3 operator -(const Matrix3 &m1, const Matrix3 &m2); //m1-m2 |
45 | | -Matrix3 operator /(const Matrix3 &m,const double &s); //m/s |
46 | | -Matrix3 operator *(const Matrix3 &m1,const Matrix3 &m2); //m1*m2 |
47 | | -Matrix3 operator *(const Matrix3 &m,const double &s); //m*s |
48 | | -Matrix3 operator *(const double &s, const Matrix3 &m); //s*m |
49 | | -template<typename T> ModuleBase::Vector3<double> operator *(const Matrix3 &m, const ModuleBase::Vector3<T> &u); //m*u // Peize Lin change ModuleBase::Vector3<T> 2017-01-10 |
50 | | -template<typename T> ModuleBase::Vector3<double> operator *(const ModuleBase::Vector3<T> &u, const Matrix3 &m); //u*m // Peize Lin change ModuleBase::Vector3<T> 2017-01-10 |
| 156 | +/** |
| 157 | + * @brief Overload operator "+" for two 3x3 matrices m1 and m2 |
| 158 | + * i.e. m1+m2 |
| 159 | + * |
| 160 | + * @param m1 |
| 161 | + * @param m2 |
| 162 | + * @return Matrix3 |
| 163 | + */ |
| 164 | +Matrix3 operator+(const Matrix3 &m1, const Matrix3 &m2); |
| 165 | + |
| 166 | +/** |
| 167 | + * @brief Overload operator "-" for two 3x3 matrices m1 and m2, |
| 168 | + * i.e. m1-m2 |
| 169 | + * |
| 170 | + * @param m1 |
| 171 | + * @param m2 |
| 172 | + * @return Matrix3 |
| 173 | + */ |
| 174 | +Matrix3 operator-(const Matrix3 &m1, const Matrix3 &m2); |
51 | 175 |
|
52 | | -bool operator ==(const Matrix3 &m1, const Matrix3 &m2); //whether m1 == m2 |
53 | | -bool operator !=(const Matrix3 &m1, const Matrix3 &m2); //whethor m1 != m2 |
| 176 | +/** |
| 177 | + * @brief Overload operator "/" for a (Matrix3)/(scalar) |
| 178 | + * i.e. m/s |
| 179 | + * |
| 180 | + * @param m The 3x3 matrix |
| 181 | + * @param s The scalar |
| 182 | + * @return Matrix3 |
| 183 | + */ |
| 184 | +Matrix3 operator/(const Matrix3 &m, const double &s); |
54 | 185 |
|
| 186 | +/** |
| 187 | + * @brief Overload operator "*" for two 3x3 matrices m1 and m2 |
| 188 | + * i.e. m1*m2 |
| 189 | + * |
| 190 | + * @param m1 |
| 191 | + * @param m2 |
| 192 | + * @return Matrix3 |
| 193 | + */ |
| 194 | +Matrix3 operator*(const Matrix3 &m1, const Matrix3 &m2); |
55 | 195 |
|
| 196 | +/** |
| 197 | + * @brief Overload operator "*" for (Matrix3)*(scalar) |
| 198 | + * i.e. m*s |
| 199 | + * |
| 200 | + * @param m The 3x3 matrix |
| 201 | + * @param s The scalar |
| 202 | + * @return Matrix3 |
| 203 | + */ |
| 204 | +Matrix3 operator*(const Matrix3 &m, const double &s); |
56 | 205 |
|
| 206 | +/** |
| 207 | + * @brief Overload operator "*" for (scalar)*(Matrix3) |
| 208 | + * i.e. s*m |
| 209 | + * |
| 210 | + * @param s The scalar |
| 211 | + * @param m The 3x3 matrix |
| 212 | + * @return Matrix3 |
| 213 | + */ |
| 214 | +Matrix3 operator*(const double &s, const Matrix3 &m); |
57 | 215 |
|
58 | | -//m*u |
59 | | -template<typename T> |
60 | | -ModuleBase::Vector3<double> operator *(const Matrix3 &m, const ModuleBase::Vector3<T> &u) |
| 216 | +/** |
| 217 | + * @brief Overload operator "*" for (Matrix3)*(Vector3) |
| 218 | + * |
| 219 | + * @tparam T |
| 220 | + * @param m The 3x3 matrix |
| 221 | + * @param u The vector with 3 elements |
| 222 | + * @return ModuleBase::Vector3<double> |
| 223 | + * @author Peize Lin |
| 224 | + */ |
| 225 | +template <typename T> ModuleBase::Vector3<double> operator*(const Matrix3 &m, const ModuleBase::Vector3<T> &u); |
| 226 | + |
| 227 | +/** |
| 228 | + * @brief Overload operator "*" for (Vector3)*(Matrix3) |
| 229 | + * |
| 230 | + * @tparam T |
| 231 | + * @param u The vector with 3 elements |
| 232 | + * @param m The 3x3 matrix |
| 233 | + * @return ModuleBase::Vector3<double> |
| 234 | + */ |
| 235 | +template <typename T> ModuleBase::Vector3<double> operator*(const ModuleBase::Vector3<T> &u, const Matrix3 &m); |
| 236 | + |
| 237 | +/** |
| 238 | + * @brief Overload operator "==" to assert |
| 239 | + * the equality between two 3x3 matrices |
| 240 | + * |
| 241 | + * @param m1 |
| 242 | + * @param m2 |
| 243 | + * @return true: if two matrices equal each other |
| 244 | + * @return false: if they do not equal |
| 245 | + */ |
| 246 | +bool operator==(const Matrix3 &m1, const Matrix3 &m2); |
| 247 | + |
| 248 | +/** |
| 249 | + * @brief Overload operator "!=" to assert |
| 250 | + * the inequality between two 3x3 matrices |
| 251 | + * |
| 252 | + * @param m1 |
| 253 | + * @param m2 |
| 254 | + * @return true: if two matrices are inequal |
| 255 | + * @return false: if they equal |
| 256 | + */ |
| 257 | +bool operator!=(const Matrix3 &m1, const Matrix3 &m2); // whethor m1 != m2 |
| 258 | + |
| 259 | +// m*u |
| 260 | +template <typename T> ModuleBase::Vector3<double> operator*(const Matrix3 &m, const ModuleBase::Vector3<T> &u) |
61 | 261 | { |
62 | | - return ModuleBase::Vector3<double>(m.e11*u.x + m.e12*u.y + m.e13*u.z, |
63 | | - m.e21*u.x + m.e22*u.y + m.e23*u.z, |
64 | | - m.e31*u.x + m.e32*u.y + m.e33*u.z); |
| 262 | + return ModuleBase::Vector3<double>(m.e11 * u.x + m.e12 * u.y + m.e13 * u.z, |
| 263 | + m.e21 * u.x + m.e22 * u.y + m.e23 * u.z, |
| 264 | + m.e31 * u.x + m.e32 * u.y + m.e33 * u.z); |
65 | 265 | } |
66 | 266 |
|
67 | | -//u*m |
68 | | -template<typename T> |
69 | | -ModuleBase::Vector3<double> operator *(const ModuleBase::Vector3<T> &u, const Matrix3 &m) |
| 267 | +// u*m |
| 268 | +template <typename T> ModuleBase::Vector3<double> operator*(const ModuleBase::Vector3<T> &u, const Matrix3 &m) |
70 | 269 | { |
71 | | - return ModuleBase::Vector3<double>(u.x*m.e11 + u.y*m.e21 + u.z*m.e31, |
72 | | - u.x*m.e12 + u.y*m.e22 + u.z*m.e32, |
73 | | - u.x*m.e13 + u.y*m.e23 + u.z*m.e33); |
| 270 | + return ModuleBase::Vector3<double>(u.x * m.e11 + u.y * m.e21 + u.z * m.e31, |
| 271 | + u.x * m.e12 + u.y * m.e22 + u.z * m.e32, |
| 272 | + u.x * m.e13 + u.y * m.e23 + u.z * m.e33); |
74 | 273 | } |
75 | 274 |
|
76 | | -} |
| 275 | +} // namespace ModuleBase |
77 | 276 |
|
78 | 277 | #endif // MATRIX3_H |
79 | | - |
|
0 commit comments