Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vmath.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_UTILS_VMATH_HPP_
25#define VULPES_UTILS_VMATH_HPP_
26
27#include <gmpxx.h>
28
29#include <cstddef>
30#include <cstdint>
31#include <limits>
32#include <type_traits>
33
34#include <Eigen/Dense>
35
36namespace vulpes {
37namespace vmath {
38
49template <typename T>
51 : std::integral_constant<
52 bool,
53 std::is_same_v<T, mpf_class> || std::is_same_v<T, mpq_class> ||
54 std::is_same_v<T, mpz_class>> {};
55
66template <typename T>
67struct is_pseudoaritmetic : std::integral_constant<
68 bool,
69 is_gnump<T>::value || std::is_arithmetic_v<T>> {
70};
71
82template <typename T>
84 : std::integral_constant<
85 bool,
86 std::is_same_v<T, mpf_class> || std::is_floating_point_v<T>> {};
87
103template <
104 typename U,
105 typename V,
106 typename = std::enable_if_t<std::is_unsigned_v<V> && std::is_integral_v<V>>>
107inline constexpr U upow(U base, V exp, U result = U(1)) {
108 while (exp > 0) {
109 if (exp & 1) {
110 result *= base;
111 }
112 exp >>= 1;
113 if (exp > 0) {
114 base *= base;
115 }
116 }
117 return result;
118}
119
137template <
138 typename U,
139 typename V,
140 typename = std::enable_if_t<std::is_signed_v<V> && std::is_integral_v<V>>>
141inline constexpr U ipow(U base, V exp, U result = U(1)) {
142 if (exp < 0) {
143 exp = -exp;
144 base = result / base;
145 }
146 static_assert(sizeof(V) <= sizeof(std::size_t));
147 return upow(base, static_cast<std::size_t>(exp));
148}
149
167template <
168 typename U,
169 typename V,
170 typename = std::enable_if_t<std::is_integral_v<V>>>
171inline constexpr U pow(U base, V exp, U result = U(1)) {
172 if constexpr (std::is_signed_v<V>) {
173 return ipow(base, exp, result);
174 } else {
175 return upow(base, exp, result);
176 }
177}
178
191template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
192inline T abs(const T& value) {
193 if constexpr (std::is_arithmetic_v<T>) {
194 return std::abs(value);
195 } else {
196 return ::abs(value);
197 }
198}
199
200template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
201inline T sqrt(const T& value) {
202 if constexpr (std::is_arithmetic_v<T>) {
203 return std::sqrt(value);
204 } else {
205 return ::sqrt(value);
206 }
207}
208
221template <typename T, typename = std::enable_if_t<is_pseudofloating<T>::value>>
222constexpr T pi() {
223 if constexpr (std::is_floating_point_v<T>) {
224#ifdef __cpp_lib_math_constants
225 return std::numbers::pi_v<T>;
226#else
227 return M_PI;
228#endif
229 } else {
230 // these are 1024 digits of pi, should be enough for precitions up to
231 // ~3000bits
232 return mpf_class(
233 "3."
234 "14159265358979323846264338327950288419716939937510582097494459230781"
235 "64062862089986280348253421170679821480865132823066470938446095505822"
236 "31725359408128481117450284102701938521105559644622948954930381964428"
237 "81097566593344612847564823378678316527120190914564856692346034861045"
238 "43266482133936072602491412737245870066063155881748815209209628292540"
239 "91715364367892590360011330530548820466521384146951941511609433057270"
240 "36575959195309218611738193261179310511854807446237996274956735188575"
241 "27248912279381830119491298336733624406566430860213949463952247371907"
242 "02179860943702770539217176293176752384674818467669405132000568127145"
243 "26356082778577134275778960917363717872146844090122495343014654958537"
244 "10507922796892589235420199561121290219608640344181598136297747713099"
245 "60518707211349999998372978049951059731732816096318595024459455346908"
246 "30264252230825334468503526193118817101000313783875288658753320838142"
247 "06171776691473035982534904287554687311595628638823537875937519577818"
248 "57780532171226806613001927876611195909216420198938095257201065485863");
249 }
250}
251
269template <typename T, typename = std::enable_if_t<is_pseudofloating<T>::value>>
270constexpr T epsilon(const T& instance = T(0)) {
271 if constexpr (std::is_floating_point_v<T>) {
272 return std::numeric_limits<T>::epsilon();
273 } else {
274 // very rough approximation, see https://stackoverflow.com/q/36334104
275 return upow(mpf_class("1.0e-1"), instance.get_prec() / 4);
276 }
277}
278
292template <typename T, typename = std::enable_if_t<is_pseudofloating<T>::value>>
293inline std::size_t precision(const T& instance) {
294 if constexpr (std::is_floating_point_v<T>) {
295 constexpr std::size_t BITS_IN_BYTE = 8UL;
296 return sizeof(T) * BITS_IN_BYTE;
297 } else {
298 return instance.get_prec();
299 }
300}
301
316template <
317 typename V,
318 typename U,
319 typename = std::enable_if_t<
320 is_pseudoaritmetic<U>::value && is_pseudoaritmetic<V>::value>>
321inline V to(const U& src) {
322 if constexpr (std::is_same_v<U, V>) {
323 return src;
324 } else if constexpr (std::is_arithmetic_v<U> && std::is_arithmetic_v<V>) {
325 return static_cast<V>(src);
326 } else if constexpr (is_gnump<V>::value) {
327 return V(src);
328 } else if constexpr (
329 is_gnump<U>::value && std::is_floating_point_v<V> &&
330 sizeof(V) <= sizeof(double)) {
331 return static_cast<V>(src.get_d());
332 } else {
333 // dependence on typename is needed to have a well-formed expression
334 static_assert(!sizeof(U), "Not implemented 'to' for this type");
335 }
336}
337
347template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
348inline constexpr T factorial(T n) {
349 T result = T(1);
350 while (n > 1) {
351 result *= n;
352 n -= T(1);
353 }
354 return result;
355}
356
357template <typename T, typename = std::enable_if_t<is_pseudoaritmetic<T>::value>>
358inline T gamma(const T& value) {
359 if constexpr (std::is_integral_v<T>) {
360 return factorial(value - 1);
361 } else if constexpr (std::is_arithmetic_v<T>) {
362 return std::tgamma(value);
363 } else {
364 return ::gamma(value);
365 }
366}
367
380template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
381inline constexpr int64_t pow1(T exp) {
382 return 1LL - ((exp & 1ULL) << 1ULL);
383}
384
385template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
386constexpr inline T binomial(T n, T k) {
387 if (k > n - k) { // Use symmetry property (minimize number of loops)
388 k = n - k;
389 }
390
391 T result = 1;
392 for (T i = 0; i < k; ++i) {
393 result *= (n - i);
394 // at 1st iter divide by 1. at 2nd iter for sure there is a factor 2
395 // and so on
396 result /= (i + 1);
397 }
398 return result;
399}
400
410template <typename T, int Options>
411inline T sarrus_det(const Eigen::Matrix<T, 3, 3, Options>& mat) {
412 return mat(0, 0) * mat(1, 1) * mat(2, 2) + mat(1, 0) * mat(2, 1) * mat(0, 2) +
413 mat(2, 0) * mat(0, 1) * mat(1, 2) - mat(2, 0) * mat(1, 1) * mat(0, 2) -
414 mat(1, 0) * mat(0, 1) * mat(2, 2) - mat(0, 0) * mat(2, 1) * mat(1, 2);
415}
416
427template <typename T, int Dim, int Options>
428inline T determinant(const Eigen::Matrix<T, Dim, Dim, Options>& mat) {
429 if constexpr (Dim == 2) {
430 return mat(0, 0) * mat(1, 1) - mat(0, 1) * mat(1, 0);
431 } else if constexpr (Dim == 3) {
432 return sarrus_det(mat);
433 } else {
434 static_assert(Dim * 0, "Inline determinant only for Dim 2 or 3");
435 }
436}
437
438template <typename T, int Dim, int Options>
439inline T area(const Eigen::Matrix<T, Dim, Dim - 1, Options>& mat) {
440 if constexpr (Dim == 2) {
441 return mat.norm();
442 } else if constexpr (Dim == 3) {
443 return mat.col(0).cross(mat.col(1)).norm() / 2.0;
444 } else {
445 static_assert(Dim * 0, "Inline determinant only for Dim 2 or 3");
446 }
447}
448
476template <typename FloatType, typename IndexType>
477inline void newellNormal(
478 const FloatType* vertices_data,
479 const IndexType* face_indices_data,
480 IndexType num_face_vertices,
481 FloatType* out_normal) {
482 for (IndexType i = 0; i < num_face_vertices; ++i) {
483 // Get vertex indices for the current edge.
484 const IndexType idx_current = face_indices_data[i];
485 const IndexType idx_next = face_indices_data[(i + 1) % num_face_vertices];
486
487 // Create pointers to the start of each vertex's data (x, y, z).
488 const FloatType* p_current = vertices_data + idx_current * 3;
489 const FloatType* p_next = vertices_data + idx_next * 3;
490
491 // Apply Newell's formula using array indexing from the pointers.
492 out_normal[0] += (p_current[1] - p_next[1]) * (p_current[2] + p_next[2]);
493 out_normal[1] += (p_current[2] - p_next[2]) * (p_current[0] + p_next[0]);
494 out_normal[2] += (p_current[0] - p_next[0]) * (p_current[1] + p_next[1]);
495 }
496
497 const FloatType norm = std::sqrt(
498 out_normal[0] * out_normal[0] + out_normal[1] * out_normal[1] +
499 out_normal[2] * out_normal[2]);
500
501 assert(norm > vmath::epsilon<FloatType>());
502 out_normal[0] /= norm;
503 out_normal[1] /= norm;
504 out_normal[2] /= norm;
505}
506
507} // namespace vmath
508} // namespace vulpes
509
510#endif // VULPES_UTILS_VMATH_HPP_
Definition vmath.hpp:37
constexpr int64_t pow1(T exp)
Computes (-1)^exp.
Definition vmath.hpp:381
T area(const Eigen::Matrix< T, Dim, Dim - 1, Options > &mat)
Definition vmath.hpp:439
T gamma(const T &value)
Definition vmath.hpp:358
constexpr T factorial(T n)
Computes the factorial of a pseudo-arithmetic number iteratively.
Definition vmath.hpp:348
constexpr U ipow(U base, V exp, U result=U(1))
Computes the signed power of a number.
Definition vmath.hpp:141
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
constexpr T epsilon(const T &instance=T(0))
Returns the machine epsilon for floating-point types or a rough approximation for GMP types.
Definition vmath.hpp:270
constexpr U upow(U base, V exp, U result=U(1))
Computes the unsigned power of a number.
Definition vmath.hpp:107
void newellNormal(const FloatType *vertices_data, const IndexType *face_indices_data, IndexType num_face_vertices, FloatType *out_normal)
Accumulates the unnormalized normal of a polygon using Newell's method.
Definition vmath.hpp:477
constexpr T binomial(T n, T k)
Definition vmath.hpp:386
T abs(const T &value)
Computes the absolute value of a number.
Definition vmath.hpp:192
T sqrt(const T &value)
Definition vmath.hpp:201
T sarrus_det(const Eigen::Matrix< T, 3, 3, Options > &mat)
Computes the determinant of a 3x3 matrix using Sarrus' rule.
Definition vmath.hpp:411
T determinant(const Eigen::Matrix< T, Dim, Dim, Options > &mat)
Computes the determinant of a 2x2 or 3x3 matrix.
Definition vmath.hpp:428
std::size_t precision(const T &instance)
Returns the precision of the input value in bits.
Definition vmath.hpp:293
constexpr U pow(U base, V exp, U result=U(1))
Computes the power of a number.
Definition vmath.hpp:171
constexpr T pi()
Returns the value of pi.
Definition vmath.hpp:222
Definition cell_manager.hpp:38
Type trait to check if a type is one of the GNU Multiple Precision (GMP) classes (mpf_class,...
Definition vmath.hpp:54
Type trait to check if a type is either a GMP class or a built-in arithmetic type.
Definition vmath.hpp:69
Type trait to check if a type is either the GMP floating-point class (mpf_class) or a built-in floati...
Definition vmath.hpp:86