limes 3.1.0
Composable Calculus Expressions for C++20
Loading...
Searching...
No Matches
derivative_builder.hpp
Go to the documentation of this file.
1#pragma once
2
37#include <tuple>
38#include <utility>
39#include "nodes/binary.hpp"
40
46namespace limes::expr {
47
48// =============================================================================
49// DerivativeBuilder: Fluent builder for derivatives
50// =============================================================================
51
78template<typename E>
80 using value_type = typename E::value_type;
81 using expr_type = E;
82
84
85 constexpr DerivativeBuilder(E e) noexcept : expr{e} {}
86
87 // =========================================================================
88 // wrt<Dim>(): Compute partial derivative with respect to dimension Dim
89 // =========================================================================
90
92 template<std::size_t Dim>
93 [[nodiscard]] constexpr auto wrt() const {
94 auto result = expr.template derivative<Dim>();
95 return DerivativeBuilder<decltype(result)>{result};
96 }
97
99 template<std::size_t D1, std::size_t D2>
100 [[nodiscard]] constexpr auto wrt() const {
101 return wrt<D1>().template wrt<D2>();
102 }
103
105 template<std::size_t D1, std::size_t D2, std::size_t D3>
106 [[nodiscard]] constexpr auto wrt() const {
107 return wrt<D1>().template wrt<D2>().template wrt<D3>();
108 }
109
110 // =========================================================================
111 // gradient(): Compute all partial derivatives as a tuple
112 // =========================================================================
113
115 [[nodiscard]] constexpr auto gradient() const {
116 return gradient_impl(std::make_index_sequence<E::arity_v>{});
117 }
118
119 // =========================================================================
120 // Implicit conversion to underlying expression
121 // =========================================================================
122
124 [[nodiscard]] constexpr E get() const noexcept { return expr; }
125
127 [[nodiscard]] constexpr value_type eval(std::span<value_type const> args) const {
128 return expr.eval(args);
129 }
130
132 [[nodiscard]] [[deprecated("use eval() instead")]]
133 constexpr value_type evaluate(std::span<value_type const> args) const {
134 return eval(args);
135 }
136
138 [[nodiscard]] std::string to_string() const {
139 return expr.to_string();
140 }
141
143 static constexpr std::size_t arity_v = E::arity_v;
144
146 template<std::size_t Dim>
147 [[nodiscard]] constexpr auto derivative() const {
148 return expr.template derivative<Dim>();
149 }
150
151private:
152 template<std::size_t... Is>
153 [[nodiscard]] constexpr auto gradient_impl(std::index_sequence<Is...>) const {
154 return std::make_tuple(expr.template derivative<Is>()...);
155 }
156};
157
158// Type trait for DerivativeBuilder detection
159template<typename T>
160struct is_derivative_builder : std::false_type {};
161
162template<typename E>
163struct is_derivative_builder<DerivativeBuilder<E>> : std::true_type {};
164
165template<typename T>
167
168// =============================================================================
169// Builder entry point (new API, overloads existing derivative)
170// =============================================================================
171
193template<typename E>
194 requires (is_expr_node_v<E> && !is_derivative_builder_v<E>)
195[[nodiscard]] constexpr auto derivative(E expr) {
196 return DerivativeBuilder<E>{expr};
197}
198
199// Note: The existing derivative<Dim>(expr) free function in derivative.hpp
200// is still available for direct use without the builder pattern.
201
// end of expr_ops group
203
204} // namespace limes::expr
constexpr bool is_derivative_builder_v
Expression layer for composable calculus.
Definition analysis.hpp:7
constexpr auto derivative(E expr)
Fluent builder for computing symbolic derivatives.
constexpr auto wrt() const
Compute ∂³/∂x_D1 ∂x_D2 ∂x_D3 (convenience for third-order mixed partials)
constexpr value_type eval(std::span< value_type const > args) const
Evaluate the derivative expression.
constexpr auto gradient() const
Returns tuple<∂f/∂x₀, ∂f/∂x₁, ..., ∂f/∂x_{n-1}>
std::string to_string() const
String representation.
constexpr auto wrt() const
Compute ∂²/∂x_D1 ∂x_D2 (convenience for second-order mixed partials)
constexpr E get() const noexcept
Allow implicit conversion to the underlying derivative expression.
constexpr auto wrt() const
Compute ∂/∂x_Dim and return a new builder for chaining.
static constexpr std::size_t arity_v
Arity of the derivative expression.
constexpr value_type evaluate(std::span< value_type const > args) const
Deprecated: use eval() instead.
constexpr DerivativeBuilder(E e) noexcept
constexpr auto derivative() const
Further differentiation through the member function.