11#include <autodiff/forward/real.hpp>
22 template <
size_t N,
typename T>
bool isfinite(
const autodiff::Real<N, T> &x)
24 return std::isfinite(autodiff::val(x)) && std::isfinite(autodiff::derivative(x));
36 template <
int n,
typename NumberType>
37 requires requires(NumberType x) {
41 constexpr KOKKOS_INLINE_FUNCTION NumberType
powr(
const NumberType x)
44 return NumberType(1.);
45 else if constexpr (n < 0)
46 return NumberType(1.) / powr<-n, NumberType>(x);
47 else if constexpr (n == 1)
49 else if constexpr (n % 2 == 0)
50 return powr<n / 2>(x) * powr<n / 2>(x);
52 return powr<n / 2>(x) * powr<n / 2>(x) * x;
55 template <
typename NumberType>
56 requires std::is_integral_v<NumberType>
57 constexpr KOKKOS_INLINE_FUNCTION NumberType
factorial(
const NumberType &x)
60 for (NumberType i = 2; i <= x; ++i)
71 template <
typename NT>
constexpr KOKKOS_INLINE_FUNCTION
double V_d(NT d)
75 return pow(M_PI, d / 2.) / tgamma(d / 2. + 1.);
86 template <
typename NT1,
typename NT2>
constexpr KOKKOS_INLINE_FUNCTION
double V_d(NT1 d, NT2 extent)
90 return pow(M_PI, d / 2.) / tgamma(d / 2. + 1.) * pow(extent, d);
99 template <
typename NT>
constexpr KOKKOS_INLINE_FUNCTION
double S_d(NT d)
103 return 2. * pow(M_PI, d / 2.) / tgamma(d / 2.);
117 return static_cast<NT
>(2) *
static_cast<NT
>(M_PI);
119 return static_cast<NT
>(4) *
static_cast<NT
>(M_PI);
121 return static_cast<NT
>(2) * powr<2>(
static_cast<NT
>(M_PI));
123 return static_cast<NT
>(8) * powr<2>(
static_cast<NT
>(M_PI)) /
static_cast<NT
>(3);
125 return powr<3>(
static_cast<NT
>(M_PI));
127 return static_cast<NT
>(16) * powr<3>(
static_cast<NT
>(M_PI)) /
static_cast<NT
>(15);
128 return std::numeric_limits<NT>::quiet_NaN();
134 template <
typename NumberType>
135 requires requires(NumberType x) { x >= 0; }
138 if constexpr (std::is_same_v<NumberType, autodiff::real>)
139 return x >= 0. ? 1. : 0.;
141 return x >=
static_cast<NumberType
>(0) ?
static_cast<NumberType
>(1) :
static_cast<NumberType
>(0);
147 template <
typename NumberType>
148 requires requires(NumberType x) { x >= 0; }
149 constexpr KOKKOS_INLINE_FUNCTION
auto sign(
const NumberType x)
151 if constexpr (std::is_same_v<NumberType, autodiff::real>)
152 return x >= 0. ? 1. : -1.;
154 return x >=
static_cast<NumberType
>(0) ?
static_cast<NumberType
>(1) :
static_cast<NumberType
>(-1);
164 template <
typename T1,
typename T2,
typename T3>
165 requires(std::is_floating_point<T1>::value || std::is_same_v<T1, autodiff::real> || is_complex<T1>::value) &&
166 (std::is_floating_point<T2>::value || std::is_same_v<T2, autodiff::real> || is_complex<T2>::value) &&
167 std::is_floating_point<T3>::value
168 bool KOKKOS_INLINE_FUNCTION
is_close(T1 a, T2 b, T3 eps_)
172 }
else if constexpr (std::is_same_v<T1, autodiff::real> || std::is_same_v<T2, autodiff::real>)
173 return is_close((
double)a, (
double)b, (
double)eps_);
175 T1 diff = std::fabs(a - b);
176 if (diff <= eps_)
return true;
177 if (diff <= std::fmax(std::fabs(a), std::fabs(b)) * eps_)
return true;
188 template <
typename T1,
typename T2>
189 requires(std::is_floating_point<T1>::value || std::is_same_v<T1, autodiff::real> || is_complex<T1>::value) &&
190 (std::is_floating_point<T2>::value || std::is_same_v<T2, autodiff::real> || is_complex<T1>::value)
195 }
else if constexpr (std::is_same_v<T1, autodiff::real> || std::is_same_v<T2, autodiff::real>) {
196 constexpr auto eps_ = std::numeric_limits<double>::epsilon() * 10.;
197 return is_close((
double)a, (
double)b, eps_);
199 constexpr auto eps_ = std::max(std::numeric_limits<T1>::epsilon(), std::numeric_limits<T2>::epsilon());
209 template <u
int n,
typename NT,
typename A1,
typename A2>
210 requires requires(A1 a1, A2 a2) { a1[0] * a2[0]; }
211 NT
dot(
const A1 &a1,
const A2 &a2)
213 NT ret = a1[0] * a2[0];
214 for (
uint i = 1; i < n; ++i)
215 ret += a1[i] * a2[i];
222 using ::Kokkos::atan;
224 using ::Kokkos::cosh;
226 using ::Kokkos::imag;
229 using ::Kokkos::real;
231 using ::Kokkos::sinh;
232 using ::Kokkos::sqrt;
234 using ::Kokkos::tanh;
236 using ::Kokkos::fmax;
237 using ::Kokkos::fmin;
242 using ::Kokkos::fabs;
244 using ::Kokkos::atan2;
247 template <
typename T1,
typename T2,
typename T3>
248 requires(!std::is_arithmetic_v<T1> || !std::is_arithmetic_v<T2> || !std::is_arithmetic_v<T3>)
249 constexpr KOKKOS_FORCEINLINE_FUNCTION
auto fma(
const T1 &a,
const T2 &b,
const T3 &c)
254 template <
size_t N,
typename T>
255 requires std::is_arithmetic_v<T>
256 constexpr KOKKOS_FORCEINLINE_FUNCTION T
conj(
const autodiff::Real<N, T> x)
261 template <
size_t N,
typename T>
262 requires std::is_arithmetic_v<T>
266 autodiff::detail::For<0, N + 1>([&](
auto i)
constexpr { res[i] = Kokkos::conj(x[i]); });
270 template <
typename T>
271 requires std::is_arithmetic_v<T>
272 constexpr KOKKOS_FORCEINLINE_FUNCTION T
conj(
const T x)
277 template <
typename T>
279 constexpr KOKKOS_FORCEINLINE_FUNCTION T
conj(
const T x)
281 return Kokkos::conj(x);
286 template <
typename NT>
constexpr auto cot(
const NT x) {
return NT(1) / tan(x); }
287 template <
typename NT>
constexpr auto coth(
const NT x) {
return NT(1) / tanh(x); }
constexpr auto coth(const NT x)
Definition math.hh:287
constexpr KOKKOS_FORCEINLINE_FUNCTION auto fma(const T1 &a, const T2 &b, const T3 &c)
Definition math.hh:249
constexpr KOKKOS_FORCEINLINE_FUNCTION T conj(const autodiff::Real< N, T > x)
Definition math.hh:256
constexpr auto cot(const NT x)
Definition math.hh:286
Definition complex_math.hh:10
constexpr KOKKOS_INLINE_FUNCTION NumberType factorial(const NumberType &x)
Definition math.hh:57
constexpr KOKKOS_INLINE_FUNCTION double S_d(NT d)
Surface of a d-dimensional sphere.
Definition math.hh:99
constexpr KOKKOS_INLINE_FUNCTION auto heaviside_theta(const NumberType x)
A compile-time evaluatable theta function.
Definition math.hh:136
constexpr KOKKOS_INLINE_FUNCTION auto sign(const NumberType x)
A compile-time evaluatable sign function.
Definition math.hh:149
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:168
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:211
constexpr KOKKOS_INLINE_FUNCTION NumberType powr(const NumberType x)
A compile-time evaluatable power function for whole number exponents.
Definition math.hh:41
consteval NT S_d_prec(uint d)
Surface of a d-dimensional sphere (precompiled)
Definition math.hh:112
constexpr KOKKOS_INLINE_FUNCTION double V_d(NT d)
Volume of a d-dimensional sphere.
Definition math.hh:71
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 isfinite(const autodiff::Real< N, T > &x)
Finite-ness check for autodiff::real.
Definition math.hh:22
Definition complex_math.hh:89