17 std::vector<T> ext_args(args.begin(), args.end());
18 if (ext_args.size() <= index_dim) {
19 ext_args.resize(index_dim + 1);
21 ext_args.insert(ext_args.begin() +
static_cast<std::ptrdiff_t
>(index_dim), T(0));
29template<
typename Expr, std::
size_t IndexDim>
35 (Expr::arity_v > IndexDim) ? (Expr::arity_v - 1) : Expr::arity_v;
43 [[nodiscard]]
constexpr value_type eval(std::span<value_type const> args)
const {
47 for (
int i =
lo; i <=
hi; ++i) {
48 ext_args[IndexDim] =
static_cast<value_type>(i);
49 sum +=
body.eval(std::span<value_type const>(ext_args));
54 [[nodiscard]] [[deprecated(
"use eval() instead")]]
60 template<std::
size_t Dim>
62 constexpr std::size_t AdjustedDim = (Dim >= IndexDim) ? (Dim + 1) : Dim;
63 auto dbody =
body.template derivative<AdjustedDim>();
68 std::ostringstream oss;
69 oss <<
"(sum[i" << IndexDim <<
"=" <<
lo <<
".." <<
hi <<
"] " <<
body.to_string() <<
")";
75template<
typename Expr, std::
size_t IndexDim>
81 (Expr::arity_v > IndexDim) ? (Expr::arity_v - 1) : Expr::arity_v;
89 [[nodiscard]]
constexpr value_type eval(std::span<value_type const> args)
const {
93 for (
int i =
lo; i <=
hi; ++i) {
94 ext_args[IndexDim] =
static_cast<value_type>(i);
95 prod *=
body.eval(std::span<value_type const>(ext_args));
100 [[nodiscard]] [[deprecated(
"use eval() instead")]]
106 template<std::
size_t Dim>
108 constexpr std::size_t AdjustedDim = (Dim >= IndexDim) ? (Dim + 1) : Dim;
109 auto dbody =
body.template derivative<AdjustedDim>();
110 auto term = dbody /
body;
111 auto sum_of_terms =
FiniteSum<
decltype(term), IndexDim>{term,
lo,
hi};
112 return (*
this) * sum_of_terms;
116 std::ostringstream oss;
117 oss <<
"(prod[i" << IndexDim <<
"=" <<
lo <<
".." <<
hi <<
"] " <<
body.to_string() <<
")";
127template<
typename E, std::
size_t I>
136template<
typename E, std::
size_t I>
144template<std::
size_t IndexDim,
typename E>
145 requires is_expr_node_v<E>
146[[nodiscard]]
constexpr auto sum(E body,
int lo,
int hi) {
150template<std::
size_t IndexDim,
typename E>
151 requires is_expr_node_v<E>
152[[nodiscard]]
constexpr auto product(E body,
int lo,
int hi) {
std::vector< T > make_extended_args(std::span< T const > args, std::size_t index_dim)
Expression layer for composable calculus.
constexpr auto sum(E body, int lo, int hi)
constexpr bool is_finite_sum_v
constexpr auto product(E body, int lo, int hi)
constexpr bool is_finite_product_v
typename Expr::value_type value_type
constexpr auto derivative() const
constexpr value_type eval(std::span< value_type const > args) const
constexpr value_type evaluate(std::span< value_type const > args) const
static constexpr std::size_t arity_v
std::string to_string() const
constexpr FiniteProduct(Expr b, int l, int h) noexcept
std::string to_string() const
constexpr value_type eval(std::span< value_type const > args) const
typename Expr::value_type value_type
constexpr auto derivative() const
constexpr value_type evaluate(std::span< value_type const > args) const
static constexpr std::size_t arity_v
constexpr FiniteSum(Expr b, int l, int h) noexcept