31#include <fmt/format.h>
38namespace func_sketch::math {
47template <
typename FunctionType,
58 template <
typename InputFunctionType>
61 function_(std::forward<InputFunctionType>(function)) {}
68 [[nodiscard]] std::string_view
name() const noexcept {
return name_; }
77 constexpr std::size_t num_args =
sizeof...(AcceptableTypesPerArgument);
78 if (args.size() != num_args) {
80 "{} function requires exactly {} arguments. Actual: {}.",
name_,
81 num_args, args.size()));
83 operate_impl(args, result, std::make_index_sequence<num_args>{});
97 template <std::size_t... Indices>
99 std::index_sequence<Indices...> )
const {
101 [
this, &result](
const auto&... actual_args) {
102 constexpr bool is_all_acceptable =
106 if constexpr (is_all_acceptable) {
108 std::decay_t<
decltype(actual_args)>, Indices>(
111 constexpr std::size_t first_unacceptable_index =
115 using FirstUnacceptableType = std::decay_t<
116 decltype(std::get<first_unacceptable_index>(
117 std::forward_as_tuple(actual_args...)))>;
119 "{} function can not accept {} for the {}-th argument.",
121 first_unacceptable_index + 1));
138 template <
typename GivenType, std::
size_t Index>
141 std::tuple_element_t<Index, AcceptableTypesTuples>;
142 return !std::is_same_v<
143 typename AcceptableTypes::template SelectTypeToUse<GivenType>,
156 template <
typename GivenType, std::
size_t Index>
158 const GivenType& value) {
160 std::tuple_element_t<Index, AcceptableTypesTuples>;
162 AcceptableTypes::template SelectTypeToUse<GivenType>
>(value);
174 template <std::same_as<
bool>... T>
176 bool first, T... remaining) {
205template <
typename Type>
223template <
typename Type>
238 typename FunctionType, std::size_t... Indices>
240 FunctionType&& function, std::index_sequence<Indices...> ) {
242 std::tuple_element_t<Indices, AcceptableTypesTuple>...>(
243 std::move(name), std::forward<FunctionType>(function));
258template <details::AcceptableTypesTupleType AcceptableTypesTuple,
259 typename FunctionType>
261 std::string name, FunctionType&& function) {
263 std::move(name), std::forward<FunctionType>(function),
264 std::make_index_sequence<std::tuple_size_v<AcceptableTypesTuple>>{});
Definition of AcceptableTypes struct.
Class of exceptions of invalid expressions.
Class of general implementation of mathematical functions.
GeneralMathFunction(std::string name, InputFunctionType &&function)
Constructor.
void operate_impl(const std::vector< Number > &args, Number &result, std::index_sequence< Indices... >) const
Operate on scalars (internal implementation).
std::tuple< AcceptableTypesPerArgument... > AcceptableTypesTuples
Type of tuples of acceptable types.
static constexpr bool is_acceptable()
Check whether the given type is acceptable for the argument at the given index.
void operator()(const std::vector< Number > &args, Number &result) const
Operate on scalars.
static constexpr std::size_t get_first_false()
Get the first false. (Variadic template base case).
static auto convert_to_acceptable_type(const GivenType &value)
Convert the given value to an acceptable type for the argument at the given index.
static constexpr std::size_t get_first_false(bool first, T... remaining)
Get the index of the first false value.
std::string_view name() const noexcept
Get the name of the function.
std::string name_
Function name.
FunctionType function_
Function object to compute numbers.
Definitions of common types.
std::variant< Integer, Real, Complex > Number
Type of numbers.
Concept to check whether a type is func_sketch::math::AcceptableTypes.
Concept to check whether a type is a tuple of func_sketch::math::AcceptableTypes.
Definition of exception classes.
auto make_general_math_function_impl(std::string name, FunctionType &&function, std::index_sequence< Indices... >)
Make a general mathematical function.
auto make_general_math_function(std::string name, FunctionType &&function)
Make a general mathematical function.
Definition of number_type_name constant.
constexpr std::string_view number_type_name
Name of the number type.
Traits struct to handle acceptable number types.
Struct to indicate that there is no acceptable type.
Check whether a type is a tuple of func_sketch::math::AcceptableTypes.