NetDEM v1.0
Loading...
Searching...
No Matches
utils_math.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "utils_macros.hpp"
5#include "utils_operators.hpp"
6#include <cmath>
7#include <ctime>
8#include <iostream>
9#include <tuple>
10
11namespace netdem {
12
13class Math {
14public:
15 constexpr static double PI = 3.1415926535897932384626433832795028841971;
16 constexpr static double Infinity = 1.0e15;
17
18 template <typename T> static int Sign(T val);
19
20 static double NormL2(Vec2d const &val);
21
22 static double NormL2(Vec3d const &val);
23
24 static double NormL2(double val_0, double val_1);
25
26 static double NormL2(double val_0, double val_1, double val_2);
27
28 static double NormL2(double val_0, double val_1, double val_2, double val_3);
29
30 template <size_t N> static double NormL2(VecNd<N> const &val);
31
32 static double NormL2(VecXd const &val);
33
34 static double Determinant(Mat2d const &mat);
35
36 static double Determinant(Mat3d const &mat);
37
38 static Mat2d Inverse(Mat2d const &m_val);
39
40 static Mat3d Inverse(Mat3d const &m_val);
41
42 template <size_t r, size_t cr, size_t c>
43 static MatNd<r, c> Dot(MatNd<r, cr> const &m_1, MatNd<cr, c> const &m_2);
44
45 template <size_t r, size_t cr, size_t c>
47 MatNd<cr, c> const &m_2);
48
49 template <size_t r, size_t cr, size_t c>
51 MatNd<c, cr> const &m_2);
52
53 static Vec3d Cross(Vec3d const &val_1, Vec3d const &val_2);
54
55 static double Dot(Vec3d const &val_1, Vec3d const &val_2);
56
57 static double Dot(VecXT<double> const &val_1, VecXT<double> const &val_2);
58
59 static void Normalize(Vec3d *const val);
60
61 static Vec3d Rotate(Vec3d const &val_old, double rot_angle_cos,
62 double rot_angle_sin, Vec3d const &rot_axis);
63
64 static Vec3d Rotate(Vec3d const &val_old, double rot_angle,
65 Vec3d const &rot_axis);
66
67 static Vec3d Rotate(Vec3d const &val_old, Vec4d const &quat);
68
69 static Vec3d Rotate(Vec3d const &val_old, Mat3d const &rot_mat);
70
71 static Vec3d CartesianToSpherical(Vec3d const &vert_cart);
72
73 static Vec3d SphericalToCartesian(Vec3d const &vert_sph);
74
75 class Quaternion {
76 public:
77 static Vec4d FromRodrigues(double rot_angle, Vec3d const &rot_axis);
78
79 static std::tuple<double, Vec3d> ToRodrigues(Vec4d const &quat);
80
81 static Vec4d FromMatrix(Mat3d const &rot_mat);
82
83 static Mat3d ToMatrix(Vec4d const &quat);
84
85 static Vec4d Multiply(Vec4d const &p, Vec4d const &q);
86
87 static Vec4d Add(Vec4d const &p, Vec4d const &q);
88
89 static Vec4d Conjugate(Vec4d const &p);
90
91 static void Normalize(Vec4d *const q);
92 };
93};
94
95template <typename T> int Math::Sign(T val) {
96 return (T(0) < val) - (val < T(0));
97}
98
99template <size_t N> double Math::NormL2(VecNd<N> const &val) {
100 double res{0.0};
101 for (size_t i = 0; i < N; i++) {
102 res += val[i] * val[i];
103 }
104 return std::sqrt(res);
105}
106
107template <size_t r, size_t cr, size_t c>
109 MatNd<r, c> m_res;
110
111 for (size_t i = 0; i < r; i++) {
112 for (size_t j = 0; j < c; j++) {
113 m_res[i][j] = 0;
114 }
115 }
116
117 for (size_t i = 0; i < r; i++) {
118 for (size_t k = 0; k < cr; k++) {
119 for (size_t j = 0; j < c; j++) {
120 m_res[i][j] += m_1[i][k] * m_2[k][j];
121 }
122 }
123 }
124
125 return m_res;
126}
127
128template <size_t r, size_t cr, size_t c>
130 MatNd<cr, c> const &m_2) {
131 MatNd<r, c> m_res;
132
133 for (size_t i = 0; i < r; i++) {
134 for (size_t j = 0; j < c; j++) {
135 m_res[i][j] = 0;
136 }
137 }
138
139 for (size_t i = 0; i < r; i++) {
140 for (size_t k = 0; k < cr; k++) {
141 for (size_t j = 0; j < c; j++) {
142 m_res[i][j] += m_1[k][i] * m_2[k][j];
143 }
144 }
145 }
146
147 return m_res;
148}
149
150template <size_t r, size_t cr, size_t c>
152 MatNd<c, cr> const &m_2) {
153 MatNd<r, c> m_res;
154
155 for (size_t i = 0; i < r; i++) {
156 for (size_t j = 0; j < c; j++) {
157 m_res[i][j] = 0;
158 }
159 }
160
161 for (size_t i = 0; i < r; i++) {
162 for (size_t j = 0; j < c; j++) {
163 for (size_t k = 0; k < cr; k++) {
164 m_res[i][j] += m_1[i][k] * m_2[j][k];
165 }
166 }
167 }
168
169 return m_res;
170}
171
172} // namespace netdem
Definition utils_math.hpp:75
static Vec4d FromMatrix(Mat3d const &rot_mat)
Definition utils_math.cpp:249
static Vec4d Multiply(Vec4d const &p, Vec4d const &q)
Definition utils_math.cpp:304
static Vec4d FromRodrigues(double rot_angle, Vec3d const &rot_axis)
Definition utils_math.cpp:214
static std::tuple< double, Vec3d > ToRodrigues(Vec4d const &quat)
Definition utils_math.cpp:228
static Mat3d ToMatrix(Vec4d const &quat)
Definition utils_math.cpp:286
static Vec4d Add(Vec4d const &p, Vec4d const &q)
Definition utils_math.cpp:319
static Vec4d Conjugate(Vec4d const &p)
Definition utils_math.cpp:330
static void Normalize(Vec4d *const q)
Definition utils_math.cpp:341
Definition utils_math.hpp:13
static Vec3d Cross(Vec3d const &val_1, Vec3d const &val_2)
Definition utils_math.cpp:94
static void Normalize(Vec3d *const val)
Definition utils_math.cpp:119
static Mat2d Inverse(Mat2d const &m_val)
Definition utils_math.cpp:52
static MatNd< r, c > DotTransportLHS(MatNd< cr, r > const &m_1, MatNd< cr, c > const &m_2)
Definition utils_math.hpp:129
static MatNd< r, c > DotTransportRHS(MatNd< r, cr > const &m_1, MatNd< c, cr > const &m_2)
Definition utils_math.hpp:151
static double NormL2(Vec2d const &val)
Definition utils_math.cpp:5
static double Determinant(Mat2d const &mat)
Definition utils_math.cpp:42
static Vec3d Rotate(Vec3d const &val_old, double rot_angle_cos, double rot_angle_sin, Vec3d const &rot_axis)
Definition utils_math.cpp:134
static constexpr double Infinity
Definition utils_math.hpp:16
static constexpr double PI
Definition utils_math.hpp:15
static int Sign(T val)
Definition utils_math.hpp:95
static Vec3d SphericalToCartesian(Vec3d const &vert_sph)
Definition utils_math.cpp:206
static MatNd< r, c > Dot(MatNd< r, cr > const &m_1, MatNd< cr, c > const &m_2)
Definition utils_math.hpp:108
static Vec3d CartesianToSpherical(Vec3d const &vert_cart)
Definition utils_math.cpp:198
Definition bond_entry.hpp:7
std::array< std::array< double, 3 >, 3 > Mat3d
Definition utils_macros.hpp:22
std::array< double, 2 > Vec2d
Definition utils_macros.hpp:17
std::vector< T > VecXT
Definition utils_macros.hpp:31
std::array< double, 3 > Vec3d
Definition utils_macros.hpp:18
std::array< std::array< double, Nc >, Nr > MatNd
Definition utils_macros.hpp:27
std::array< double, N > VecNd
Definition utils_macros.hpp:25
std::vector< double > VecXd
Definition utils_macros.hpp:29
std::array< double, 4 > Vec4d
Definition utils_macros.hpp:19
std::array< std::array< double, 2 >, 2 > Mat2d
Definition utils_macros.hpp:21