17 constexpr bool operator()(
const T &lhs,
const T &rhs)
const {
18 static_assert(std::is_arithmetic_v<T>,
19 "In less_abs<T>(), T must be arithmetic");
20 return std::abs(lhs) < std::abs(rhs);
27template <
typename T,
typename... Args>
28T
max(T first, Args... rest) {
29 if constexpr (
sizeof...(rest) == 0) {
32 const auto max_rest =
max(rest...);
33 if (first >= max_rest)
40template <
typename T,
typename... Args>
41T
min(T first, Args... rest) {
42 if constexpr (
sizeof...(rest) == 0) {
45 const auto min_rest =
min(rest...);
46 if (first <= min_rest)
53template <
typename T,
typename... Args>
55 static_assert(std::is_arithmetic_v<T>,
56 "In max_abs<T>(), T must be arithmetic");
57 if constexpr (
sizeof...(rest) == 0) {
60 const auto max_rest =
max_abs(rest...);
61 if (std::abs(first) >= std::abs(max_rest))
68template <
typename T,
typename... Args>
70 static_assert(std::is_arithmetic_v<T>,
71 "In min_abs<T>(), T must be arithmetic");
72 if constexpr (
sizeof...(rest) == 0) {
75 const auto min_rest =
min_abs(rest...);
76 if (std::abs(first) <= std::abs(min_rest))
83template <
typename T,
typename... Args>
85 static_assert(std::is_arithmetic_v<T>,
86 "In max_difference<T>(), T must be arithmetic");
87 return max(first, rest...) -
min(first, rest...);
97template <
int n,
typename T>
98constexpr auto pow(T x) {
101 if constexpr (n < 0) {
102 return double(1.0) /
pow<-n>(x);
103 }
else if constexpr (n == 0) {
105 return static_cast<T
>(1);
106 }
else if constexpr (n == 1) {
109 return x *
pow<n - 1>(x);
115constexpr T
pow(T x,
int n) {
116 static_assert(std::is_floating_point_v<T>,
117 "In pow(T x, int n), T must be foating point");
119 return static_cast<T
>(1) / pow<T>(x, -n);
123 for (
int i = 0; i < n; ++i) {
134 static_assert(std::is_integral_v<T>,
135 "In factorial(T), T must be an integral type");
136 return (x <= 1) ? 1.0 : double(x) * factorial<T>(x - 1);
142 static_assert(std::is_integral_v<T>,
143 "double_factorial(T): T must be an integral type");
145 return (x <= 1) ? 1.0 : double(x) * double_factorial<T>(x - 2);
153 static_assert(std::is_arithmetic_v<T>,
154 "In sign(T value), T must be arithmetic");
155 return (T(0) < value) - (value < T(0));
161 static_assert(std::is_arithmetic_v<T>,
162 "In clamp_abs(T value, T max_abs), T must be arithmetic");
173 static_assert(std::is_arithmetic_v<T>,
174 "In cjop(T value, T min_abs), T must be arithmetic");
176 return static_cast<T
>(0);
Operator overloads for std::vector.
Definition Vector.hpp:503
General-purpose utility library.
Definition Array.hpp:23
T max_difference(T first, Args... rest)
Returns max - min over any number of arguments (variadic).
Definition Maths.hpp:84
T max(T first, Args... rest)
Returns the maximum of any number of parameters (variadic).
Definition Maths.hpp:28
constexpr double double_factorial(T x)
Double factorial x!! - takes integer, returns double.
Definition Maths.hpp:141
constexpr int sign(T value)
Returns the sign of value. Note: sign(0) == 0.
Definition Maths.hpp:152
constexpr T clamp_abs(T value, T max_abs)
Clamps value to [-max_abs, max_abs] - i.e., using abs.
Definition Maths.hpp:160
T max_abs(T first, Args... rest)
Returns the value with maximum absolute value of any number of parameters (variadic).
Definition Maths.hpp:54
constexpr double factorial(T x)
Factorial x! - takes integer, returns double.
Definition Maths.hpp:133
T min(T first, Args... rest)
Returns the minimum of any number of parameters (variadic).
Definition Maths.hpp:41
constexpr T chop(T value, T min_abs)
Sets value to zero if |value| < min_abs, otherwise returns value unchanged.
Definition Maths.hpp:172
T min_abs(T first, Args... rest)
Returns the value with minimum absolute value of any number of parameters (variadic).
Definition Maths.hpp:69
constexpr auto pow(T x)
x^n for compile-time integer n, x any arithmetic type.
Definition Maths.hpp:98
Function object for comparisons by absolute value.
Definition Maths.hpp:15