11#include <autodiff/forward/real.hpp>
31 template <
size_t N,
typename T>
bool isfinite(
const autodiff::Real<N, T> &x)
33 return std::isfinite(autodiff::val(x)) && std::isfinite(autodiff::derivative(x));
45 template <
int n,
typename NumberType>
46 requires requires(NumberType x) {
50 constexpr KOKKOS_INLINE_FUNCTION NumberType
powr(
const NumberType x)
53 return NumberType(1.);
54 else if constexpr (n < 0)
55 return NumberType(1.) / powr<-n, NumberType>(x);
56 else if constexpr (n == 1)
58 else if constexpr (n % 2 == 0)
59 return powr<n / 2>(x) * powr<n / 2>(x);
61 return powr<n / 2>(x) * powr<n / 2>(x) * x;
64 template <
typename NumberType>
65 requires std::is_integral_v<NumberType>
66 constexpr KOKKOS_INLINE_FUNCTION NumberType
factorial(
const NumberType &x)
69 for (NumberType i = 2; i <= x; ++i)
80 template <
typename NT>
constexpr KOKKOS_INLINE_FUNCTION
double V_d(NT d)
84 return pow(M_PI, d / 2.) / tgamma(d / 2. + 1.);
95 template <
typename NT1,
typename NT2>
constexpr KOKKOS_INLINE_FUNCTION
double V_d(NT1 d, NT2 extent)
99 return pow(M_PI, d / 2.) / tgamma(d / 2. + 1.) * pow(extent, d);
108 template <
typename NT>
constexpr KOKKOS_INLINE_FUNCTION
double S_d(NT d)
112 return 2. * pow(M_PI, d / 2.) / tgamma(d / 2.);
126 return static_cast<NT
>(2) *
static_cast<NT
>(M_PI);
128 return static_cast<NT
>(4) *
static_cast<NT
>(M_PI);
130 return static_cast<NT
>(2) * powr<2>(
static_cast<NT
>(M_PI));
132 return static_cast<NT
>(8) * powr<2>(
static_cast<NT
>(M_PI)) /
static_cast<NT
>(3);
134 return powr<3>(
static_cast<NT
>(M_PI));
136 return static_cast<NT
>(16) * powr<3>(
static_cast<NT
>(M_PI)) /
static_cast<NT
>(15);
137 return std::numeric_limits<NT>::quiet_NaN();
143 template <
typename NumberType>
144 requires requires(NumberType x) { x >= 0; }
147 if constexpr (std::is_same_v<NumberType, autodiff::real>)
148 return x >= 0. ? 1. : 0.;
150 return x >=
static_cast<NumberType
>(0) ?
static_cast<NumberType
>(1) :
static_cast<NumberType
>(0);
156 template <
typename NumberType>
157 requires requires(NumberType x) { x >= 0; }
158 constexpr KOKKOS_INLINE_FUNCTION
auto sign(
const NumberType x)
160 if constexpr (std::is_same_v<NumberType, autodiff::real>)
161 return x >= 0. ? 1. : -1.;
163 return x >=
static_cast<NumberType
>(0) ?
static_cast<NumberType
>(1) :
static_cast<NumberType
>(-1);
173 template <
typename T1,
typename T2,
typename T3>
176 std::is_floating_point<T3>::value
177 bool KOKKOS_INLINE_FUNCTION
is_close(T1 a, T2 b, T3 eps_)
182 return is_close((
double)a, (
double)b, (
double)eps_);
184 T1 diff = std::fabs(a - b);
185 if (diff <= eps_)
return true;
186 if (diff <= std::fmax(std::fabs(a), std::fabs(b)) * eps_)
return true;
197 template <
typename T1,
typename T2>
205 constexpr auto eps_ = std::numeric_limits<double>::epsilon() * 10.;
206 return is_close((
double)a, (
double)b, eps_);
208 constexpr auto eps_ = std::max(std::numeric_limits<T1>::epsilon(), std::numeric_limits<T2>::epsilon());
218 template <u
int n,
typename NT,
typename A1,
typename A2>
219 requires requires(A1 a1, A2 a2) { a1[0] * a2[0]; }
220 NT
dot(
const A1 &a1,
const A2 &a2)
222 NT ret = a1[0] * a2[0];
223 for (
uint i = 1; i < n; ++i)
224 ret += a1[i] * a2[i];
231 using ::Kokkos::atan;
233 using ::Kokkos::cosh;
235 using ::Kokkos::imag;
238 using ::Kokkos::real;
240 using ::Kokkos::sinh;
241 using ::Kokkos::sqrt;
243 using ::Kokkos::tanh;
245 using ::Kokkos::fmax;
246 using ::Kokkos::fmin;
251 using ::Kokkos::fabs;
253 using ::Kokkos::atan2;
256 template <
typename T1,
typename T2,
typename T3>
257 requires(!std::is_arithmetic_v<T1> || !std::is_arithmetic_v<T2> || !std::is_arithmetic_v<T3>)
258 constexpr KOKKOS_FORCEINLINE_FUNCTION
auto fma(
const T1 &a,
const T2 &b,
const T3 &c)
263 template <
size_t N,
typename T>
264 requires std::is_arithmetic_v<T>
265 constexpr KOKKOS_FORCEINLINE_FUNCTION T
conj(
const autodiff::Real<N, T> x)
270 template <
size_t N,
typename T>
271 requires std::is_arithmetic_v<T>
275 autodiff::detail::For<0, N + 1>([&](
auto i)
constexpr { res[i] = Kokkos::conj(x[i]); });
279 template <
typename T>
280 requires std::is_arithmetic_v<T>
281 constexpr KOKKOS_FORCEINLINE_FUNCTION T
conj(
const T x)
286 template <
typename T>
288 constexpr KOKKOS_FORCEINLINE_FUNCTION T
conj(
const T x)
290 return Kokkos::conj(x);
295 template <
typename NT>
constexpr auto cot(
const NT x) {
return NT(1) / tan(x); }
296 template <
typename NT>
constexpr auto coth(
const NT x) {
return NT(1) / tanh(x); }
constexpr auto coth(const NT x)
Definition math.hh:296
constexpr KOKKOS_FORCEINLINE_FUNCTION auto fma(const T1 &a, const T2 &b, const T3 &c)
Definition math.hh:258
constexpr KOKKOS_FORCEINLINE_FUNCTION T conj(const autodiff::Real< N, T > x)
Definition math.hh:265
constexpr auto cot(const NT x)
Definition math.hh:295
Definition complex_math.hh:10
constexpr KOKKOS_INLINE_FUNCTION NumberType factorial(const NumberType &x)
Definition math.hh:66
constexpr KOKKOS_INLINE_FUNCTION double S_d(NT d)
Surface of a d-dimensional sphere.
Definition math.hh:108
constexpr KOKKOS_INLINE_FUNCTION auto heaviside_theta(const NumberType x)
A compile-time evaluatable theta function.
Definition math.hh:145
constexpr KOKKOS_INLINE_FUNCTION auto sign(const NumberType x)
A compile-time evaluatable sign function.
Definition math.hh:158
constexpr KOKKOS_FORCEINLINE_FUNCTION auto imag(const autodiff::Real< N, T > &)
Definition complex_math.hh:97
NT dot(const A1 &a1, const A2 &a2)
A dot product which takes the dot product between a1 and a2, assuming each has n entries which can be...
Definition math.hh:220
constexpr bool is_autodiff_real_v
Definition math.hh:23
constexpr KOKKOS_INLINE_FUNCTION NumberType powr(const NumberType x)
A compile-time evaluatable power function for whole number exponents.
Definition math.hh:50
consteval NT S_d_prec(uint d)
Surface of a d-dimensional sphere (precompiled)
Definition math.hh:121
constexpr KOKKOS_INLINE_FUNCTION double V_d(NT d)
Volume of a d-dimensional sphere.
Definition math.hh:80
unsigned int uint
Definition utils.hh:24
KOKKOS_FORCEINLINE_FUNCTION auto real(const autodiff::Real< N, T > &a)
Definition complex_math.hh:96
autodiff::Real< N, complex< T > > cxReal
Definition complex_math.hh:86
bool KOKKOS_INLINE_FUNCTION is_close(T1 a, T2 b, T3 eps_)
Function to evaluate whether two floats are equal to numerical precision. Tests for both relative and...
Definition math.hh:177
bool isfinite(const autodiff::Real< N, T > &x)
Finite-ness check for autodiff::real.
Definition math.hh:31
Definition complex_math.hh:19
Type trait: true iff T is any autodiff::Real<N, U> specialization. Allows generic handling of higher-...
Definition math.hh:21
Definition complex_math.hh:89