22#define MAX_STEP_EVAL 256
25#if defined(__cpp_lib_constexpr_cmath) && __cpp_lib_constexpr_cmath >= 202306L
26#define LINALG_HAS_CXX26_CONSTEXPR_CMATH 1
28#define LINALG_HAS_CXX26_CONSTEXPR_CMATH 0
32#if defined(__cpp_lib_constexpr_complex) && \
33 __cpp_lib_constexpr_complex >= 202306L
34#define LINALG_HAS_CXX26_CONSTEXPR_COMPLEX_MATH 1
36#define LINALG_HAS_CXX26_CONSTEXPR_COMPLEX_MATH 0
46template <
typename T, std::
size_t N>
49 std::index_sequence<
Is...>) {
50 return std::make_tuple(
arr[
Is]...);
52 return to_tuple(x, std::make_index_sequence<N>{});
67 return z.imag() <= tol;
80 if (!(
den != T(0)))
return std::nullopt;
107#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
108 return std::pow(x,
power);
131 if (!(x >= T(0)))
return std::nullopt;
132 if (x == T(0))
return T(0);
134#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
139 if (!(
g > T(0)))
g = T(1);
144 if (!(
g > T(0)))
g = T(1);
145 if (
abs(
g *
g - x) <= tol)
return g;
182#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
185 return x < T(0) ? T(-1) * x : x;
191#if LINALG_HAS_CXX26_CONSTEXPR_COMPLEX_MATH
214 if (
root == 0)
return std::nullopt;
215 if (x == T(0))
return T(0);
219 if (
root % 2 == 0)
return std::nullopt;
224#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
228 if (
root == 2)
return sign * std::sqrt(x);
229 if (
root == 3)
return sign * std::cbrt(x);
230 return sign * std::pow(x, T(1) / T(
root));
232 if (!(
g > T(0)))
g = T(1);
242 if (!(
gkm1 > T(0)))
return std::nullopt;
264 T
g = 1
e-10, T tol = 1
e-10) {
285#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
291 term *= (-(x * x)) / (T(2 *
k) * T(2 *
k + 1));
309#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
315 term *= (-(x * x)) / (T(2 *
k - 1) * T(2 *
k));
335#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
337 return std::atanh(x);
373#if LINALG_HAS_CXX26_CONSTEXPR_COMPLEX_MATH
375 return std::exp(std::complex<T>(T(0),
theta));
394#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
416#if LINALG_HAS_CXX26_CONSTEXPR_COMPLEX_MATH
418 return std::exp(lambda);
453 if (!(
x_in > T(0)))
return std::nullopt;
455#if LINALG_HAS_CXX26_CONSTEXPR_CMATH
457 return std::log(
x_in);
466 for (
int i = 0;
i < 64 && x < T(0.5); ++
i) {
473 if (!
a)
return std::nullopt;
497 if (
l.has_value())
return *
l;
511template <std::
size_t Start, std::
size_t End,
typename Func>
515 f(std::integral_constant<std::size_t, Start>{});
538template <VectorContainer Vec>
543 std::array<std::size_t, N>
p;
544 for (std::size_t
i = 0;
i < N; ++
i)
p[
i] =
i;
545 for (std::size_t
i = 0;
i + 1 < N; ++
i) {
547 for (std::size_t
j =
i + 1;
j < N; ++
j)
#define MAX_STEP_EVAL
Maximum Newton/refinement iterations used by constexpr utilities.
constexpr std::optional< T > constexpr_ln(T x_in, int terms=16)
natural log
constexpr T constexpr_nrt_safe(T x, std::size_t root, T floor=T(0), T g=1e-10, T tol=1e-10)
Return a scalar nth root with a sanitized fallback radicand.
constexpr auto constexpr_sin(T x, int terms=16)
Approximate sine with a truncated Taylor recurrence.
typename scalar_traits_t< T >::real_type real_type_t
Associated real component type registered for T.
constexpr auto constexpr_expi(T theta, int terms=16)
Approximate exp(i * theta) on the complex unit circle.
constexpr std::optional< T > constexpr_sqrt(T x, T g=1e-10, T tol=1e-10)
Approximate a nonnegative square root with Newton iteration.
constexpr std::optional< T > constexpr_nrt(T x, std::size_t root, T g=1e-10, T tol=1e-10)
Approximate a real nth root with Newton iteration.
constexpr T constexpr_atanh_safe(T x, T fallback=T(0), int terms=16)
constexpr-safe atanh with fallback
constexpr auto constexpr_exp(T x, int terms=16)
Approximate the real exponential with a truncated power series.
constexpr std::optional< T > constexpr_div(T num, T den)
Divide two real scalars with an explicit zero-denominator result.
constexpr void constexpr_for(Func &&f)
constexpr for loop which recursively unrolls a dynamic for loop into compile time recursive loop
constexpr auto array_to_tuple(const std::array< T, N > &x)
Copy a fixed-size array into a tuple of the same length.
constexpr T constexpr_div_safe(T num, T den, T fallback=T(0))
Divide two real scalars, returning a fallback for a zero denominator.
constexpr T constexpr_abs(T x)
Compute real absolute value or complex magnitude.
constexpr T constexpr_ln_safe(T x, T floor=std::numeric_limits< T >::min(), int terms=16)
natural log with domain sanitization and fallback
constexpr auto constexpr_cos(T x, int terms=16)
Approximate cosine with a truncated Taylor recurrence.
constexpr T constexpr_pow(T x, std::size_t power)
Raise a real scalar to a nonnegative integral power.
constexpr T constexpr_sqrt_safe(T x, T floor=T(0), T g=1e-10, T tol=1e-10)
Return a scalar square root with a sanitized fallback radicand.
constexpr bool is_same(const T &a, const T &b, real_type_t< T > tol=1e-10)
Compare complex scalars by magnitude of their difference.
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
constexpr bool is_real(const std::complex< T > &z, real_type_t< T > tol=1e-10)
Check whether a complex value's imaginary component is below a bound.
constexpr std::optional< T > constexpr_atanh(T x, int terms=16)
atanh (inverse hyperbolic tangent function)
constexpr auto argsort_desc(const Vec &x)
Return coordinate indices sorted by descending real value.
constexpr auto constexpr_exp_lambda(const T &lambda, int terms=16)
Approximate the exponential of a complex scalar.