47template <
typename T,
typename... Args>
51 std::forward<Args>(args)...);
57template <ExpressionType T>
58struct BinaryMinusExpression;
60template <ExpressionType T>
61struct BinaryPlusExpression;
63struct ConstExpression;
65template <ExpressionType T>
68template <ExpressionType T>
71template <ExpressionType T>
72struct UnaryMinusExpression;
98 std::array<ExpressionPtr, 2>
args{
nullptr,
nullptr};
118 :
args{
std::move(lhs), nullptr} {}
153 if (lhs->is_constant(0.0)) {
156 }
else if (rhs->is_constant(0.0)) {
159 }
else if (lhs->is_constant(1.0)) {
161 }
else if (rhs->is_constant(1.0)) {
172 if (rhs->type() ==
LINEAR) {
179 }
else if (rhs->type() ==
CONSTANT) {
180 if (lhs->type() ==
LINEAR) {
187 }
else if (lhs->type() ==
LINEAR && rhs->type() ==
LINEAR) {
205 if (lhs->is_constant(0.0)) {
208 }
else if (rhs->is_constant(1.0)) {
219 if (lhs->type() ==
LINEAR) {
242 if (lhs ==
nullptr || lhs->is_constant(0.0)) {
244 }
else if (rhs ==
nullptr || rhs->is_constant(0.0)) {
253 auto type = std::max(lhs->type(), rhs->type());
271 return lhs = lhs + rhs;
285 if (lhs->is_constant(0.0)) {
286 if (rhs->is_constant(0.0)) {
292 }
else if (rhs->is_constant(0.0)) {
301 auto type = std::max(lhs->type(), rhs->type());
320 if (lhs->is_constant(0.0)) {
330 if (lhs->type() ==
LINEAR) {
355 virtual double value([[maybe_unused]]
double lhs,
356 [[maybe_unused]]
double rhs)
const = 0;
374 virtual double grad_l([[maybe_unused]]
double lhs,
375 [[maybe_unused]]
double rhs,
376 [[maybe_unused]]
double parent_adjoint)
const {
388 virtual double grad_r([[maybe_unused]]
double lhs,
389 [[maybe_unused]]
double rhs,
390 [[maybe_unused]]
double parent_adjoint)
const {
405 [[maybe_unused]]
const ExpressionPtr& parent_adjoint)
const {
420 [[maybe_unused]]
const ExpressionPtr& parent_adjoint)
const {
436template <ExpressionType T>
447 double value(
double lhs,
double rhs)
const override {
return lhs - rhs; }
451 double grad_l(
double,
double,
double parent_adjoint)
const override {
452 return parent_adjoint;
455 double grad_r(
double,
double,
double parent_adjoint)
const override {
456 return -parent_adjoint;
462 return parent_adjoint;
468 return -parent_adjoint;
477template <ExpressionType T>
488 double value(
double lhs,
double rhs)
const override {
return lhs + rhs; }
492 double grad_l(
double,
double,
double parent_adjoint)
const override {
493 return parent_adjoint;
496 double grad_r(
double,
double,
double parent_adjoint)
const override {
497 return parent_adjoint;
503 return parent_adjoint;
509 return parent_adjoint;
525 double value(
double x,
double)
const override {
return std::cbrt(x); }
529 double grad_l(
double x,
double,
double parent_adjoint)
const override {
530 double c = std::cbrt(x);
531 return parent_adjoint / (3.0 * c * c);
555 }
else if (x->
val == -1.0 || x->
val == 1.0) {
581 double value(
double,
double)
const override {
return val; }
603 double value(
double,
double)
const override {
return val; }
613template <ExpressionType T>
624 double value(
double lhs,
double rhs)
const override {
return lhs / rhs; }
628 double grad_l(
double,
double rhs,
double parent_adjoint)
const override {
629 return parent_adjoint / rhs;
632 double grad_r(
double lhs,
double rhs,
double parent_adjoint)
const override {
633 return parent_adjoint * -lhs / (rhs * rhs);
639 return parent_adjoint / rhs;
645 return parent_adjoint * -lhs / (rhs * rhs);
654template <ExpressionType T>
665 double value(
double lhs,
double rhs)
const override {
return lhs * rhs; }
669 double grad_l([[maybe_unused]]
double lhs,
double rhs,
670 double parent_adjoint)
const override {
671 return parent_adjoint * rhs;
674 double grad_r(
double lhs, [[maybe_unused]]
double rhs,
675 double parent_adjoint)
const override {
676 return parent_adjoint * lhs;
682 return parent_adjoint * rhs;
688 return parent_adjoint * lhs;
697template <ExpressionType T>
707 double value(
double lhs,
double)
const override {
return -lhs; }
711 double grad_l(
double,
double,
double parent_adjoint)
const override {
712 return -parent_adjoint;
718 return -parent_adjoint;
742 stack.emplace_back(expr);
744 while (!stack.empty()) {
745 auto elem = stack.back();
750 if (--elem->ref_count == 0) {
751 if (elem->adjoint_expr !=
nullptr) {
752 stack.emplace_back(elem->adjoint_expr.get());
754 for (
auto&
arg : elem->args) {
755 if (
arg !=
nullptr) {
756 stack.emplace_back(
arg.get());
764 std::allocator_traits<
decltype(alloc)>::deallocate(alloc, elem,
783 double value(
double x,
double)
const override {
return std::abs(x); }
787 double grad_l(
double x,
double,
double parent_adjoint)
const override {
789 return -parent_adjoint;
790 }
else if (x > 0.0) {
791 return parent_adjoint;
801 return -parent_adjoint;
802 }
else if (x->val > 0.0) {
803 return parent_adjoint;
845 double value(
double x,
double)
const override {
return std::acos(x); }
849 double grad_l(
double x,
double,
double parent_adjoint)
const override {
850 return -parent_adjoint / std::sqrt(1.0 - x * x);
856 return -parent_adjoint /
894 double value(
double x,
double)
const override {
return std::asin(x); }
898 double grad_l(
double x,
double,
double parent_adjoint)
const override {
899 return parent_adjoint / std::sqrt(1.0 - x * x);
905 return parent_adjoint /
944 double value(
double x,
double)
const override {
return std::atan(x); }
948 double grad_l(
double x,
double,
double parent_adjoint)
const override {
949 return parent_adjoint / (1.0 + x * x);
994 double value(
double y,
double x)
const override {
return std::atan2(y, x); }
998 double grad_l(
double y,
double x,
double parent_adjoint)
const override {
999 return parent_adjoint * x / (y * y + x * x);
1002 double grad_r(
double y,
double x,
double parent_adjoint)
const override {
1003 return parent_adjoint * -y / (y * y + x * x);
1009 return parent_adjoint * x / (y * y + x * x);
1015 return parent_adjoint * -y / (y * y + x * x);
1056 double value(
double x,
double)
const override {
return std::cos(x); }
1060 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1061 return -parent_adjoint * std::sin(x);
1104 double value(
double x,
double)
const override {
return std::cosh(x); }
1108 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1109 return parent_adjoint * std::sinh(x);
1152 double value(
double x,
double)
const override {
return std::erf(x); }
1156 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1157 return parent_adjoint * 2.0 * std::numbers::inv_sqrtpi * std::exp(-x * x);
1163 return parent_adjoint *
1165 std::numbers::inv_sqrtpi) *
1204 double value(
double x,
double)
const override {
return std::exp(x); }
1208 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1209 return parent_adjoint * std::exp(x);
1255 double value(
double x,
double y)
const override {
return std::hypot(x, y); }
1259 double grad_l(
double x,
double y,
double parent_adjoint)
const override {
1260 return parent_adjoint * x / std::hypot(x, y);
1263 double grad_r(
double x,
double y,
double parent_adjoint)
const override {
1264 return parent_adjoint * y / std::hypot(x, y);
1316 double value(
double x,
double)
const override {
return std::log(x); }
1320 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1321 return parent_adjoint / x;
1327 return parent_adjoint / x;
1365 double value(
double x,
double)
const override {
return std::log10(x); }
1369 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1370 return parent_adjoint / (std::numbers::ln10 * x);
1376 return parent_adjoint /
1410template <ExpressionType T>
1421 double value(
double base,
double power)
const override {
1422 return std::pow(base, power);
1428 double parent_adjoint)
const override {
1429 return parent_adjoint * std::pow(base, power - 1) * power;
1433 double parent_adjoint)
const override {
1438 return parent_adjoint * std::pow(base, power - 1) * base * std::log(base);
1445 return parent_adjoint *
1455 if (base->val == 0.0) {
1459 return parent_adjoint *
1494 std::pow(base->
val, power->
val));
1520 double value(
double x,
double)
const override {
1523 }
else if (x == 0.0) {
1532 double grad_l(
double,
double,
double)
const override {
return 0.0; }
1553 }
else if (x->
val == 0.0) {
1576 double value(
double x,
double)
const override {
return std::sin(x); }
1580 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1581 return parent_adjoint * std::cos(x);
1625 double value(
double x,
double)
const override {
return std::sinh(x); }
1629 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1630 return parent_adjoint * std::cosh(x);
1674 double value(
double x,
double)
const override {
return std::sqrt(x); }
1678 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1679 return parent_adjoint / (2.0 * std::sqrt(x));
1685 return parent_adjoint /
1700 if (x->
val == 0.0) {
1703 }
else if (x->
val == 1.0) {
1725 double value(
double x,
double)
const override {
return std::tan(x); }
1729 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1730 return parent_adjoint / (std::cos(x) * std::cos(x));
1774 double value(
double x,
double)
const override {
return std::tanh(x); }
1778 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1779 return parent_adjoint / (std::cosh(x) * std::cosh(x));
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Returns a named argument to be used in a formatting function.
Definition base.h:2831
sign
Definition base.h:682
wpi::SmallVector< T > small_vector
Definition small_vector.hpp:10
Definition expression_graph.hpp:11
ExpressionPtr log10(const ExpressionPtr &x)
std::log10() for Expressions.
Definition expression.hpp:1386
constexpr void inc_ref_count(Expression *expr)
Refcount increment for intrusive shared pointer.
Definition expression.hpp:727
ExpressionPtr tanh(const ExpressionPtr &x)
std::tanh() for Expressions.
Definition expression.hpp:1794
ExpressionPtr sqrt(const ExpressionPtr &x)
std::sqrt() for Expressions.
Definition expression.hpp:1695
ExpressionPtr pow(const ExpressionPtr &base, const ExpressionPtr &power)
std::pow() for Expressions.
Definition expression.hpp:1473
ExpressionPtr exp(const ExpressionPtr &x)
std::exp() for Expressions.
Definition expression.hpp:1224
IntrusiveSharedPtr< Expression > ExpressionPtr
Typedef for intrusive shared pointer to Expression.
Definition expression.hpp:38
ExpressionPtr hypot(const ExpressionPtr &x, const ExpressionPtr &y)
std::hypot() for Expressions.
Definition expression.hpp:1286
ExpressionPtr atan(const ExpressionPtr &x)
std::atan() for Expressions.
Definition expression.hpp:964
void dec_ref_count(Expression *expr)
Refcount decrement for intrusive shared pointer.
Definition expression.hpp:736
ExpressionPtr erf(const ExpressionPtr &x)
std::erf() for Expressions.
Definition expression.hpp:1175
ExpressionPtr sinh(const ExpressionPtr &x)
std::sinh() for Expressions.
Definition expression.hpp:1645
ExpressionPtr acos(const ExpressionPtr &x)
std::acos() for Expressions.
Definition expression.hpp:866
constexpr bool USE_POOL_ALLOCATOR
Definition expression.hpp:27
ExpressionPtr cosh(const ExpressionPtr &x)
std::cosh() for Expressions.
Definition expression.hpp:1124
ExpressionPtr atan2(const ExpressionPtr &y, const ExpressionPtr &x)
std::atan2() for Expressions.
Definition expression.hpp:1025
ExpressionPtr cos(const ExpressionPtr &x)
std::cos() for Expressions.
Definition expression.hpp:1076
static ExpressionPtr make_expression_ptr(Args &&... args)
Creates an intrusive shared pointer to an expression from the global pool allocator.
Definition expression.hpp:48
ExpressionPtr cbrt(const ExpressionPtr &x)
std::cbrt() for Expressions.
Definition expression.hpp:547
ExpressionPtr log(const ExpressionPtr &x)
std::log() for Expressions.
Definition expression.hpp:1336
ExpressionPtr sin(const ExpressionPtr &x)
std::sin() for Expressions.
Definition expression.hpp:1596
ExpressionPtr abs(const ExpressionPtr &x)
std::abs() for Expressions.
Definition expression.hpp:816
ExpressionPtr asin(const ExpressionPtr &x)
std::asin() for Expressions.
Definition expression.hpp:915
IntrusiveSharedPtr< T > make_intrusive_shared(Args &&... args)
Constructs an object of type T and wraps it in an intrusive shared pointer using args as the paramete...
Definition intrusive_shared_ptr.hpp:303
ExpressionType
Expression type.
Definition expression_type.hpp:18
@ CONSTANT
The expression is a constant.
@ QUADRATIC
The expression is composed of quadratic and lower-order operators.
@ LINEAR
The expression is composed of linear and lower-order operators.
@ NONLINEAR
The expression is composed of nonlinear and lower-order operators.
PoolAllocator< T > global_pool_allocator()
Returns an allocator for a global pool memory resource.
Definition pool.hpp:193
IntrusiveSharedPtr< T > allocate_intrusive_shared(Alloc alloc, Args &&... args)
Constructs an object of type T and wraps it in an intrusive shared pointer using alloc as the storage...
Definition intrusive_shared_ptr.hpp:319
Definition PointerIntPair.h:280
Derived expression type for std::abs().
Definition expression.hpp:774
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:785
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:787
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:797
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:783
constexpr AbsExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:780
Derived expression type for std::acos().
Definition expression.hpp:836
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:847
constexpr AcosExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:842
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:849
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:845
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:853
Derived expression type for std::asin().
Definition expression.hpp:885
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:894
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:902
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:896
constexpr AsinExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:891
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:898
Derived expression type for std::atan2().
Definition expression.hpp:984
ExpressionPtr grad_expr_r(const ExpressionPtr &y, const ExpressionPtr &x, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:1012
double value(double y, double x) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:994
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:996
constexpr Atan2Expression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:991
double grad_l(double y, double x, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:998
double grad_r(double y, double x, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:1002
ExpressionPtr grad_expr_l(const ExpressionPtr &y, const ExpressionPtr &x, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1006
Derived expression type for std::atan().
Definition expression.hpp:935
constexpr AtanExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:941
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:944
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:946
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:952
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:948
Derived expression type for binary minus operator.
Definition expression.hpp:437
double value(double lhs, double rhs) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:447
constexpr BinaryMinusExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:444
double grad_r(double, double, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:455
double grad_l(double, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:451
ExpressionPtr grad_expr_r(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:465
ExpressionPtr grad_expr_l(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:459
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:449
Derived expression type for binary plus operator.
Definition expression.hpp:478
double grad_r(double, double, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:496
double grad_l(double, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:492
double value(double lhs, double rhs) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:488
ExpressionPtr grad_expr_r(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:506
constexpr BinaryPlusExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:485
ExpressionPtr grad_expr_l(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:500
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:490
Derived expression type for std::cbrt().
Definition expression.hpp:516
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:529
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:534
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:527
constexpr CbrtExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:522
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:525
Derived expression type for constant.
Definition expression.hpp:568
constexpr ConstExpression(double value)
Constructs a nullary expression (an operator with no arguments).
Definition expression.hpp:579
constexpr ConstExpression()=default
Constructs a constant expression with a value of zero.
double value(double, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:581
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:583
Derived expression type for std::cos().
Definition expression.hpp:1047
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1058
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1056
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1064
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1060
constexpr CosExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1053
Derived expression type for std::cosh().
Definition expression.hpp:1095
constexpr CoshExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1101
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1108
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1104
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1112
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1106
Derived expression type for decision variable.
Definition expression.hpp:589
double value(double, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:603
constexpr DecisionVariableExpression()=default
Constructs a decision variable expression with a value of zero.
constexpr DecisionVariableExpression(double value)
Constructs a nullary expression (an operator with no arguments).
Definition expression.hpp:600
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:605
Derived expression type for binary division operator.
Definition expression.hpp:614
double grad_l(double, double rhs, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:628
ExpressionPtr grad_expr_r(const ExpressionPtr &lhs, const ExpressionPtr &rhs, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:642
double value(double lhs, double rhs) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:624
constexpr DivExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:621
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:626
ExpressionPtr grad_expr_l(const ExpressionPtr &, const ExpressionPtr &rhs, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:636
double grad_r(double lhs, double rhs, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:632
Derived expression type for std::erf().
Definition expression.hpp:1143
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1160
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1152
constexpr ErfExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1149
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1154
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1156
Derived expression type for std::exp().
Definition expression.hpp:1195
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1208
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1206
constexpr ExpExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1201
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1212
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1204
An autodiff expression node.
Definition expression.hpp:77
int32_t col
This expression's column in a Jacobian, or -1 otherwise.
Definition expression.hpp:88
virtual double grad_l(double lhs, double rhs, double parent_adjoint) const
Returns double adjoint of the left child expression.
Definition expression.hpp:374
constexpr bool is_constant(double constant) const
Returns true if the expression is the given constant.
Definition expression.hpp:138
constexpr Expression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:126
constexpr Expression(double value)
Constructs a nullary expression (an operator with no arguments).
Definition expression.hpp:110
virtual ExpressionPtr grad_expr_r(const ExpressionPtr &lhs, const ExpressionPtr &rhs, const ExpressionPtr &parent_adjoint) const
Returns Expression adjoint of the right child expression.
Definition expression.hpp:417
virtual ExpressionType type() const =0
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
ExpressionPtr adjoint_expr
The adjoint of the expression node used during gradient expression tree generation.
Definition expression.hpp:92
virtual ~Expression()=default
friend ExpressionPtr operator+(const ExpressionPtr &lhs)
Unary plus operator.
Definition expression.hpp:344
constexpr Expression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:117
double adjoint
The adjoint of the expression node used during autodiff.
Definition expression.hpp:82
friend ExpressionPtr operator*(const ExpressionPtr &lhs, const ExpressionPtr &rhs)
Expression-Expression multiplication operator.
Definition expression.hpp:148
uint32_t incoming_edges
Counts incoming edges for this node.
Definition expression.hpp:85
friend ExpressionPtr operator+=(ExpressionPtr &lhs, const ExpressionPtr &rhs)
Expression-Expression compound addition operator.
Definition expression.hpp:269
friend ExpressionPtr operator-(const ExpressionPtr &lhs)
Unary minus operator.
Definition expression.hpp:316
friend ExpressionPtr operator-(const ExpressionPtr &lhs, const ExpressionPtr &rhs)
Expression-Expression subtraction operator.
Definition expression.hpp:280
friend ExpressionPtr operator/(const ExpressionPtr &lhs, const ExpressionPtr &rhs)
Expression-Expression division operator.
Definition expression.hpp:200
virtual double grad_r(double lhs, double rhs, double parent_adjoint) const
Returns double adjoint of the right child expression.
Definition expression.hpp:388
virtual double value(double lhs, double rhs) const =0
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
virtual ExpressionPtr grad_expr_l(const ExpressionPtr &lhs, const ExpressionPtr &rhs, const ExpressionPtr &parent_adjoint) const
Returns Expression adjoint of the left child expression.
Definition expression.hpp:402
std::array< ExpressionPtr, 2 > args
Expression arguments.
Definition expression.hpp:98
double val
The value of the expression node.
Definition expression.hpp:79
constexpr Expression()=default
Constructs a constant expression with a value of zero.
uint32_t ref_count
Reference count for intrusive shared pointer.
Definition expression.hpp:95
friend ExpressionPtr operator+(const ExpressionPtr &lhs, const ExpressionPtr &rhs)
Expression-Expression addition operator.
Definition expression.hpp:237
Derived expression type for std::hypot().
Definition expression.hpp:1245
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &y, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1267
double grad_r(double x, double y, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:1263
double grad_l(double x, double y, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1259
ExpressionPtr grad_expr_r(const ExpressionPtr &x, const ExpressionPtr &y, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:1273
double value(double x, double y) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1255
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1257
constexpr HypotExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:1252
Derived expression type for std::log10().
Definition expression.hpp:1356
constexpr Log10Expression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1362
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1369
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1365
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1367
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1373
Derived expression type for std::log().
Definition expression.hpp:1307
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1318
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1324
constexpr LogExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1313
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1320
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1316
Derived expression type for binary multiplication operator.
Definition expression.hpp:655
ExpressionPtr grad_expr_l(const ExpressionPtr &lhs, const ExpressionPtr &rhs, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:679
constexpr MultExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:662
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:667
double grad_l(double lhs, double rhs, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:669
double value(double lhs, double rhs) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:665
ExpressionPtr grad_expr_r(const ExpressionPtr &lhs, const ExpressionPtr &rhs, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:685
double grad_r(double lhs, double rhs, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:674
Derived expression type for std::pow().
Definition expression.hpp:1411
ExpressionPtr grad_expr_l(const ExpressionPtr &base, const ExpressionPtr &power, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1442
double value(double base, double power) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1421
double grad_l(double base, double power, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1427
double grad_r(double base, double power, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:1432
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1425
ExpressionPtr grad_expr_r(const ExpressionPtr &base, const ExpressionPtr &power, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the right child expression.
Definition expression.hpp:1451
constexpr PowExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:1418
Derived expression type for sign().
Definition expression.hpp:1511
double grad_l(double, double, double) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1532
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1530
constexpr SignExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1517
ExpressionPtr grad_expr_l(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1534
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1520
Derived expression type for std::sin().
Definition expression.hpp:1567
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1576
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1580
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1584
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1578
constexpr SinExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1573
Derived expression type for std::sinh().
Definition expression.hpp:1616
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1627
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1629
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1633
constexpr SinhExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1622
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1625
Derived expression type for std::sqrt().
Definition expression.hpp:1665
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1676
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1678
constexpr SqrtExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1671
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1682
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1674
Derived expression type for std::tan().
Definition expression.hpp:1716
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1729
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1725
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1733
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1727
constexpr TanExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1722
Derived expression type for std::tanh().
Definition expression.hpp:1765
constexpr TanhExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1771
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1776
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1778
double value(double x, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:1774
ExpressionPtr grad_expr_l(const ExpressionPtr &x, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1782
Derived expression type for unary minus operator.
Definition expression.hpp:698
constexpr UnaryMinusExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:704
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:709
ExpressionPtr grad_expr_l(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &parent_adjoint) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:715
double value(double lhs, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:707
double grad_l(double, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:711