Tatooine
math.h
Go to the documentation of this file.
1#ifndef TATOOINE_MATH_H
2#define TATOOINE_MATH_H
3//==============================================================================
4#include <tatooine/concepts.h>
5#include <tatooine/pow.h>
7
8#include <cmath>
9#include <gcem.hpp>
10//==============================================================================
11namespace tatooine {
12//==============================================================================
13template <typename A, typename B>
14requires same_as<std::decay_t<A>, std::decay_t<B>>
15constexpr auto min(A&& a, B&& b) {
16 return gcem::min(std::forward<A>(a), std::forward<B>(b));
17}
18template <typename A, typename B>
19requires same_as<std::decay_t<A>, std::decay_t<B>>
20constexpr auto max(A&& a, B&& b) {
21 return gcem::max(std::forward<A>(a), std::forward<B>(b));
22}
23constexpr auto log(arithmetic auto const x) { return gcem::log(x); }
24constexpr auto log2(arithmetic auto const x) { return gcem::log2(x); }
25//constexpr auto log10(arithmetic auto const x) { return gcem::log10(x); }
26constexpr auto abs(arithmetic auto const x) { return gcem::abs(x); }
27constexpr auto sin(arithmetic auto const x) { return gcem::sin(x); }
28constexpr auto cos(arithmetic auto const x) { return gcem::cos(x); }
29constexpr auto sqrt(arithmetic auto const x) { return gcem::sqrt(x); }
30constexpr auto pow(arithmetic auto const x) { return gcem::pow(x); }
31//==============================================================================
32template <template <typename> typename Comparator, typename T0, typename T1,
33 typename... TRest>
34requires requires(T0 a, T1 b, Comparator<std::decay_t<T0>> comp) {
35 { comp(a, b) } -> std::convertible_to<bool>;
36 } && same_as<std::decay_t<T0>, std::decay_t<T1>> &&
37 (same_as<std::decay_t<T0>, std::decay_t<TRest>> && ...)
38constexpr auto compare_variadic(T0&& a, T1&& b, TRest&&... rest) {
39 if constexpr (sizeof...(TRest) == 0) {
40 return Comparator<std::decay_t<T0>>{}(a, b) ? std::forward<T0>(a)
41 : std::forward<T1>(b);
42 } else {
43 return tatooine::compare_variadic<Comparator>(
44 std::forward<T0>(a),
45 tatooine::compare_variadic<Comparator>(std::forward<T1>(b),
46 std::forward<TRest>(rest)...));
47 }
48}
49//------------------------------------------------------------------------------
50template <typename T0>
51constexpr auto max(T0&& a) -> decltype(auto) {
52 return std::forward<T0>(a);
53}
54// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55template <typename T0, typename T1, typename T2, typename... TRest>
56requires requires(T0&& a, T1&& b) {
57 { a > b } -> std::convertible_to<bool>;
58 }
59constexpr auto max(T0&& a, T1&& b, T2&& c, TRest&&... rest) -> decltype(auto) {
60 return compare_variadic<std::greater>(
61 std::forward<T0>(a), std::forward<T1>(b), std::forward<T2>(c),
62 std::forward<TRest>(rest)...);
63}
64//------------------------------------------------------------------------------
65template <typename T0, typename T1, typename T2, typename... TRest>
66requires requires(T0&& a, T1&& b) {
67 { a > b } -> std::convertible_to<bool>;
68 }
69constexpr auto min(T0&& a, T1&& b, T2&& c, TRest&&... rest) -> decltype(auto) {
70 return compare_variadic<std::less>(std::forward<T0>(a), std::forward<T1>(b),
71 std::forward<T2>(c),
72 std::forward<TRest>(rest)...);
73}
74//------------------------------------------------------------------------------
75template <typename T, std::size_t N, std::size_t... Is>
76constexpr auto min(std::array<T, N> const& arr,
77 std::index_sequence<Is...> /*seq*/) {
78 return min(arr[Is]...);
79}
80//------------------------------------------------------------------------------
81template <typename T, std::size_t N>
82constexpr auto min(std::array<T, N> const& arr) {
83 return min(arr, std::make_index_sequence<N>{});
84}
85//------------------------------------------------------------------------------
86template <typename T, std::size_t N, std::size_t... Is>
87constexpr auto max(std::array<T, N> const& arr,
88 std::index_sequence<Is...> /*seq*/) {
89 return max(arr[Is]...);
90}
91//------------------------------------------------------------------------------
92template <typename T, std::size_t N>
93constexpr auto max(std::array<T, N> const& arr) {
94 return max(arr, std::make_index_sequence<N>{});
95}
96//------------------------------------------------------------------------------
97constexpr auto ipow(integral auto const base, integral auto const exp) {
98 std::decay_t<decltype(base)> p = 1;
99 for (std::decay_t<decltype(exp)> i = 0; i < exp; ++i) {
100 p *= base;
101 }
102 return p;
103}
104//------------------------------------------------------------------------------
105template <integral Int> // Windows needs this
106constexpr auto factorial(Int const i) -> std::decay_t<Int> {
107 if (i == 0) {
108 return 1;
109 }
110 return factorial(i - 1) * i;
111}
112//==============================================================================
113} // namespace tatooine
114//==============================================================================
115#endif
Definition: concepts.h:33
Definition: concepts.h:21
Definition: algorithm.h:6
constexpr auto abs(arithmetic auto const x)
Definition: math.h:26
constexpr auto cos(arithmetic auto const x)
Definition: math.h:28
constexpr auto log(arithmetic auto const x)
Definition: math.h:23
constexpr auto pow(arithmetic auto const x)
Definition: math.h:30
constexpr auto max(A &&a, B &&b)
Definition: math.h:20
constexpr auto compare_variadic(T0 &&a, T1 &&b, TRest &&... rest)
Definition: math.h:38
constexpr auto ipow(integral auto const base, integral auto const exp)
Definition: math.h:97
constexpr auto min(A &&a, B &&b)
Definition: math.h:15
constexpr auto sin(arithmetic auto const x)
Definition: math.h:27
constexpr auto sqrt(arithmetic auto const x)
Definition: math.h:29
constexpr auto log2(arithmetic auto const x)
Definition: math.h:24
constexpr auto factorial(Int const i) -> std::decay_t< Int >
Definition: math.h:106