1 #ifndef ABLATELIBRARY_MATHUTILITIES_HPP
2 #define ABLATELIBRARY_MATHUTILITIES_HPP
5 namespace ablate::utilities {
8 template <
class I,
class T>
9 static inline void ScaleVector(I dim, T* vec, T alpha) {
10 for (I d = 0; d < dim; d++) {
15 template <
class I,
class T>
16 static inline void NormVector(I dim, T* vec) {
17 auto mag = MagVector(dim, vec);
18 for (I d = 0; d < dim; d++) {
19 vec[d] = vec[d] / mag;
23 template <
class I,
class T>
24 static inline bool VectorEquals(I dim,
const T* test,
const T* equal, T tolerance = 1.0E-8) {
25 for (I d = 0; d < dim; d++) {
26 if (test[d] < equal[d] - tolerance || test[d] > equal[d] + tolerance) {
33 template <
class R,
class T>
34 static inline bool Equals(R test, T equal, T tolerance = 1.0E-8) {
35 return test > (equal - tolerance) && test < (equal + tolerance);
38 template <
class I,
class T>
39 static inline void NormVector(I dim,
const T* in, T* out) {
41 for (I d = 0; d < dim; d++) {
44 mag = PetscSqrtReal(mag);
45 for (I d = 0; d < dim; d++) {
50 template <
class I,
class T>
51 static inline T MagVector(I dim,
const T* in) {
53 for (I d = 0; d < dim; d++) {
56 return PetscSqrtReal(mag);
59 template <
class I,
class T>
60 static inline T DotVector(I dim,
const T* a,
const T* b) {
62 for (I d = 0; d < dim; d++) {
68 template <
int dim,
class T>
69 static inline T DotVector(
const T* a,
const T* b) {
70 if constexpr (dim == 3) {
71 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
73 if constexpr (dim == 2) {
74 return a[0] * b[0] + a[1] * b[1];
76 if constexpr (dim == 1) {
79 static_assert(dim == 1 || dim == 2 || dim == 3,
"ablate::utilities::MathUtilities::DotVector only support dimensions of 1, 2, or 3");
93 template <
class I,
class T>
94 static inline T
DiffDotVector(I dim,
const T* aE,
const T* aS,
const T* b) {
97 return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]) + ((aE[2] - aS[2]) * b[2]));
100 return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]));
103 return (((aE[0] - aS[0]) * b[0]));
119 template <
int dim,
class T>
121 if constexpr (dim == 3) {
122 return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]) + ((aE[2] - aS[2]) * b[2]));
124 if constexpr (dim == 2) {
125 return (((aE[0] - aS[0]) * b[0]) + ((aE[1] - aS[1]) * b[1]));
127 if constexpr (dim == 1) {
128 return (((aE[0] - aS[0]) * b[0]));
130 static_assert(dim == 1 || dim == 2 || dim == 3,
"ablate::utilities::MathUtilities::DiffDotVector only support dimensions of 1, 2, or 3");
142 template <
class I,
class T>
143 static inline void CrossVector(I dim,
const T* a,
const T* b, T* c) {
146 c[0] = (a[1] * b[2] - b[1] * a[2]);
147 c[1] = (b[0] * a[2] - a[0] * b[2]);
148 c[2] = (a[0] * b[1] - b[0] * a[1]);
152 c[0] = (a[0] * b[1] - b[0] * a[1]);
171 template <
int dim,
class T>
172 static inline void CrossVector([[maybe_unused]]
const T* a, [[maybe_unused]]
const T* b, T* c) {
173 if constexpr (dim == 3) {
174 c[0] = (a[1] * b[2] - b[1] * a[2]);
175 c[1] = (b[0] * a[2] - a[0] * b[2]);
176 c[2] = (a[0] * b[1] - b[0] * a[1]);
178 if constexpr (dim == 2) {
179 c[0] = (a[0] * b[1] - b[0] * a[1]);
181 if constexpr (dim == 1) {
184 static_assert(dim == 1 || dim == 2 || dim == 3,
"ablate::utilities::MathUtilities::CrossVector only support dimensions of 1, 2, or 3");
195 template <
class I,
class T>
196 static inline void Subtract(I dim,
const T* a,
const T* b, T* c) {
197 for (I i = 0; i < dim; i++) {
210 template <
class I,
class T>
211 static inline void Plus(I dim,
const T* a, T* b) {
212 for (I i = 0; i < dim; i++) {
224 template <
class I,
class T>
225 static inline void Multiply(I dim,
const T a[3][3],
const T* in, T* out) {
226 for (I i = 0; i < dim; i++) {
228 for (I j = 0; j < dim; j++) {
229 out[i] += a[i][j] * in[j];
241 template <
class I,
class T>
243 for (I i = 0; i < dim; i++) {
245 for (I j = 0; j < dim; j++) {
246 out[i] += a[j][i] * in[j];
267 static PetscReal
ComputeDeterminant(PetscInt dim, PetscScalar transformationMatrix[3][3]);
272 enum class Norm { L1, L1_NORM, L2, LINF, L2_NORM };
282 static PetscErrorCode
ComputeNorm(
Norm normType, Vec x, Vec y, PetscReal norm[]);
Definition: mathUtilities.hpp:6
static void Subtract(I dim, const T *a, const T *b, T *c)
Definition: mathUtilities.hpp:196
Norm
Definition: mathUtilities.hpp:272
static PetscReal ComputeDeterminant(PetscInt dim, PetscScalar transformationMatrix[3][3])
Definition: mathUtilities.cpp:35
static void CrossVector([[maybe_unused]] const T *a, [[maybe_unused]] const T *b, T *c)
Definition: mathUtilities.hpp:172
static void Plus(I dim, const T *a, T *b)
Definition: mathUtilities.hpp:211
static void ComputeTransformationMatrix(PetscInt dim, const PetscScalar normal[3], PetscScalar transformationMatrix[3][3])
Definition: mathUtilities.cpp:4
static PetscErrorCode ComputeNorm(Norm normType, Vec x, Vec y, PetscReal norm[])
Definition: mathUtilities.cpp:48
static T DiffDotVector(I dim, const T *aE, const T *aS, const T *b)
Definition: mathUtilities.hpp:94
static T DiffDotVector(const T *aE, const T *aS, const T *b)
Definition: mathUtilities.hpp:120
static void MultiplyTranspose(I dim, const T a[3][3], const T *in, T *out)
Definition: mathUtilities.hpp:242
static void Multiply(I dim, const T a[3][3], const T *in, T *out)
Definition: mathUtilities.hpp:225
static void CrossVector(I dim, const T *a, const T *b, T *c)
Definition: mathUtilities.hpp:143