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 {
430template <ExpressionType T>
441 double value(
double lhs,
double rhs)
const override {
return lhs - rhs; }
445 double grad_l(
double,
double,
double parent_adjoint)
const override {
446 return parent_adjoint;
449 double grad_r(
double,
double,
double parent_adjoint)
const override {
450 return -parent_adjoint;
456 return parent_adjoint;
462 return -parent_adjoint;
471template <ExpressionType T>
482 double value(
double lhs,
double rhs)
const override {
return lhs + rhs; }
486 double grad_l(
double,
double,
double parent_adjoint)
const override {
487 return parent_adjoint;
490 double grad_r(
double,
double,
double parent_adjoint)
const override {
491 return parent_adjoint;
497 return parent_adjoint;
503 return parent_adjoint;
523 double value(
double,
double)
const override {
return val; }
545 double value(
double,
double)
const override {
return val; }
555template <ExpressionType T>
566 double value(
double lhs,
double rhs)
const override {
return lhs / rhs; }
570 double grad_l(
double,
double rhs,
double parent_adjoint)
const override {
571 return parent_adjoint / rhs;
574 double grad_r(
double lhs,
double rhs,
double parent_adjoint)
const override {
575 return parent_adjoint * -lhs / (rhs * rhs);
581 return parent_adjoint / rhs;
587 return parent_adjoint * -lhs / (rhs * rhs);
596template <ExpressionType T>
607 double value(
double lhs,
double rhs)
const override {
return lhs * rhs; }
611 double grad_l([[maybe_unused]]
double lhs,
double rhs,
612 double parent_adjoint)
const override {
613 return parent_adjoint * rhs;
616 double grad_r(
double lhs, [[maybe_unused]]
double rhs,
617 double parent_adjoint)
const override {
618 return parent_adjoint * lhs;
624 return parent_adjoint * rhs;
630 return parent_adjoint * lhs;
639template <ExpressionType T>
649 double value(
double lhs,
double)
const override {
return -lhs; }
653 double grad_l(
double,
double,
double parent_adjoint)
const override {
654 return -parent_adjoint;
660 return -parent_adjoint;
691 while (!stack.
empty()) {
692 auto elem = stack.
back();
697 if (--elem->ref_count == 0) {
698 if (elem->adjoint_expr !=
nullptr) {
701 for (
auto&
arg : elem->args) {
702 if (
arg !=
nullptr) {
711 std::allocator_traits<
decltype(alloc)>::deallocate(alloc, elem,
730 double value(
double x,
double)
const override {
return std::abs(x); }
734 double grad_l(
double x,
double,
double parent_adjoint)
const override {
736 return -parent_adjoint;
737 }
else if (x > 0.0) {
738 return parent_adjoint;
748 return -parent_adjoint;
749 }
else if (x->val > 0.0) {
750 return parent_adjoint;
792 double value(
double x,
double)
const override {
return std::acos(x); }
796 double grad_l(
double x,
double,
double parent_adjoint)
const override {
797 return -parent_adjoint / std::sqrt(1.0 - x * x);
803 return -parent_adjoint /
841 double value(
double x,
double)
const override {
return std::asin(x); }
845 double grad_l(
double x,
double,
double parent_adjoint)
const override {
846 return parent_adjoint / std::sqrt(1.0 - x * x);
852 return parent_adjoint /
891 double value(
double x,
double)
const override {
return std::atan(x); }
895 double grad_l(
double x,
double,
double parent_adjoint)
const override {
896 return parent_adjoint / (1.0 + x * x);
941 double value(
double y,
double x)
const override {
return std::atan2(y, x); }
945 double grad_l(
double y,
double x,
double parent_adjoint)
const override {
946 return parent_adjoint * x / (y * y + x * x);
949 double grad_r(
double y,
double x,
double parent_adjoint)
const override {
950 return parent_adjoint * -y / (y * y + x * x);
956 return parent_adjoint * x / (y * y + x * x);
962 return parent_adjoint * -y / (y * y + x * x);
1003 double value(
double x,
double)
const override {
return std::cos(x); }
1007 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1008 return -parent_adjoint * std::sin(x);
1051 double value(
double x,
double)
const override {
return std::cosh(x); }
1055 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1056 return parent_adjoint * std::sinh(x);
1099 double value(
double x,
double)
const override {
return std::erf(x); }
1103 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1104 return parent_adjoint * 2.0 * std::numbers::inv_sqrtpi * std::exp(-x * x);
1110 return parent_adjoint *
1112 std::numbers::inv_sqrtpi) *
1151 double value(
double x,
double)
const override {
return std::exp(x); }
1155 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1156 return parent_adjoint * std::exp(x);
1202 double value(
double x,
double y)
const override {
return std::hypot(x, y); }
1206 double grad_l(
double x,
double y,
double parent_adjoint)
const override {
1207 return parent_adjoint * x / std::hypot(x, y);
1210 double grad_r(
double x,
double y,
double parent_adjoint)
const override {
1211 return parent_adjoint * y / std::hypot(x, y);
1263 double value(
double x,
double)
const override {
return std::log(x); }
1267 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1268 return parent_adjoint / x;
1274 return parent_adjoint / x;
1312 double value(
double x,
double)
const override {
return std::log10(x); }
1316 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1317 return parent_adjoint / (std::numbers::ln10 * x);
1323 return parent_adjoint /
1357template <ExpressionType T>
1368 double value(
double base,
double power)
const override {
1369 return std::pow(base, power);
1375 double parent_adjoint)
const override {
1376 return parent_adjoint * std::pow(base, power - 1) * power;
1380 double parent_adjoint)
const override {
1385 return parent_adjoint * std::pow(base, power - 1) * base * std::log(base);
1392 return parent_adjoint *
1402 if (base->val == 0.0) {
1406 return parent_adjoint *
1441 std::pow(base->
val, power->
val));
1467 double value(
double x,
double)
const override {
1470 }
else if (x == 0.0) {
1479 double grad_l(
double,
double,
double)
const override {
return 0.0; }
1500 }
else if (x->
val == 0.0) {
1523 double value(
double x,
double)
const override {
return std::sin(x); }
1527 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1528 return parent_adjoint * std::cos(x);
1572 double value(
double x,
double)
const override {
return std::sinh(x); }
1576 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1577 return parent_adjoint * std::cosh(x);
1621 double value(
double x,
double)
const override {
return std::sqrt(x); }
1625 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1626 return parent_adjoint / (2.0 * std::sqrt(x));
1632 return parent_adjoint /
1647 if (x->
val == 0.0) {
1650 }
else if (x->
val == 1.0) {
1672 double value(
double x,
double)
const override {
return std::tan(x); }
1676 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1677 return parent_adjoint / (std::cos(x) * std::cos(x));
1721 double value(
double x,
double)
const override {
return std::tanh(x); }
1725 double grad_l(
double x,
double,
double parent_adjoint)
const override {
1726 return parent_adjoint / (std::cosh(x) * std::cosh(x));
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1198
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:939
void pop_back()
Definition SmallVector.h:427
bool empty() const
Definition SmallVector.h:88
reference back()
Definition SmallVector.h:310
Definition expression_graph.hpp:11
ExpressionPtr log10(const ExpressionPtr &x)
std::log10() for Expressions.
Definition expression.hpp:1333
constexpr void inc_ref_count(Expression *expr)
Refcount increment for intrusive shared pointer.
Definition expression.hpp:674
ExpressionPtr tanh(const ExpressionPtr &x)
std::tanh() for Expressions.
Definition expression.hpp:1741
ExpressionPtr sqrt(const ExpressionPtr &x)
std::sqrt() for Expressions.
Definition expression.hpp:1642
ExpressionPtr pow(const ExpressionPtr &base, const ExpressionPtr &power)
std::pow() for Expressions.
Definition expression.hpp:1420
ExpressionPtr exp(const ExpressionPtr &x)
std::exp() for Expressions.
Definition expression.hpp:1171
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:1233
ExpressionPtr atan(const ExpressionPtr &x)
std::atan() for Expressions.
Definition expression.hpp:911
void dec_ref_count(Expression *expr)
Refcount decrement for intrusive shared pointer.
Definition expression.hpp:683
ExpressionPtr erf(const ExpressionPtr &x)
std::erf() for Expressions.
Definition expression.hpp:1122
ExpressionPtr sinh(const ExpressionPtr &x)
std::sinh() for Expressions.
Definition expression.hpp:1592
ExpressionPtr acos(const ExpressionPtr &x)
std::acos() for Expressions.
Definition expression.hpp:813
constexpr bool USE_POOL_ALLOCATOR
Definition expression.hpp:27
ExpressionPtr cosh(const ExpressionPtr &x)
std::cosh() for Expressions.
Definition expression.hpp:1071
ExpressionPtr atan2(const ExpressionPtr &y, const ExpressionPtr &x)
std::atan2() for Expressions.
Definition expression.hpp:972
ExpressionPtr cos(const ExpressionPtr &x)
std::cos() for Expressions.
Definition expression.hpp:1023
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 log(const ExpressionPtr &x)
std::log() for Expressions.
Definition expression.hpp:1283
ExpressionPtr sin(const ExpressionPtr &x)
std::sin() for Expressions.
Definition expression.hpp:1543
ExpressionPtr abs(const ExpressionPtr &x)
std::abs() for Expressions.
Definition expression.hpp:763
ExpressionPtr asin(const ExpressionPtr &x)
std::asin() for Expressions.
Definition expression.hpp:862
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:721
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:732
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:734
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:744
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:730
constexpr AbsExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:727
Derived expression type for std::acos().
Definition expression.hpp:783
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:794
constexpr AcosExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:789
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:796
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:792
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:800
Derived expression type for std::asin().
Definition expression.hpp:832
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:841
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:849
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:843
constexpr AsinExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:838
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:845
Derived expression type for std::atan2().
Definition expression.hpp:931
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:959
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:941
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:943
constexpr Atan2Expression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:938
double grad_l(double y, double x, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:945
double grad_r(double y, double x, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:949
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:953
Derived expression type for std::atan().
Definition expression.hpp:882
constexpr AtanExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:888
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:891
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:893
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:899
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:895
Derived expression type for binary minus operator.
Definition expression.hpp:431
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:441
constexpr BinaryMinusExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:438
double grad_r(double, double, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:449
double grad_l(double, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:445
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:459
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:453
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:443
Derived expression type for binary plus operator.
Definition expression.hpp:472
double grad_r(double, double, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:490
double grad_l(double, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:486
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:482
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:500
constexpr BinaryPlusExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:479
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:494
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:484
Derived expression type for constant.
Definition expression.hpp:510
constexpr ConstExpression(double value)
Constructs a nullary expression (an operator with no arguments).
Definition expression.hpp:521
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:523
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:525
Derived expression type for std::cos().
Definition expression.hpp:994
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1005
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:1003
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:1011
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1007
constexpr CosExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1000
Derived expression type for std::cosh().
Definition expression.hpp:1042
constexpr CoshExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1048
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1055
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:1051
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:1059
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1053
Derived expression type for decision variable.
Definition expression.hpp:531
double value(double, double) const override
Either nullary operator with no arguments, unary operator with one argument, or binary operator with ...
Definition expression.hpp:545
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:542
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:547
Derived expression type for binary division operator.
Definition expression.hpp:556
double grad_l(double, double rhs, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:570
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:584
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:566
constexpr DivExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:563
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:568
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:578
double grad_r(double lhs, double rhs, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:574
Derived expression type for std::erf().
Definition expression.hpp:1090
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:1107
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:1099
constexpr ErfExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1096
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
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:1103
Derived expression type for std::exp().
Definition expression.hpp:1142
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1155
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1153
constexpr ExpExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1148
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:1159
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:1151
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:1192
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:1214
double grad_r(double x, double y, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:1210
double grad_l(double x, double y, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1206
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:1220
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:1202
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1204
constexpr HypotExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:1199
Derived expression type for std::log10().
Definition expression.hpp:1303
constexpr Log10Expression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1309
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1316
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:1312
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1314
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:1320
Derived expression type for std::log().
Definition expression.hpp:1254
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1265
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:1271
constexpr LogExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1260
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1267
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:1263
Derived expression type for binary multiplication operator.
Definition expression.hpp:597
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:621
constexpr MultExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:604
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:609
double grad_l(double lhs, double rhs, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:611
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:607
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:627
double grad_r(double lhs, double rhs, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:616
Derived expression type for std::pow().
Definition expression.hpp:1358
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:1389
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:1368
double grad_l(double base, double power, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1374
double grad_r(double base, double power, double parent_adjoint) const override
Returns double adjoint of the right child expression.
Definition expression.hpp:1379
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1372
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:1398
constexpr PowExpression(ExpressionPtr lhs, ExpressionPtr rhs)
Constructs a binary expression (an operator with two arguments).
Definition expression.hpp:1365
Derived expression type for sign().
Definition expression.hpp:1458
double grad_l(double, double, double) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1479
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1477
constexpr SignExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1464
ExpressionPtr grad_expr_l(const ExpressionPtr &, const ExpressionPtr &, const ExpressionPtr &) const override
Returns Expression adjoint of the left child expression.
Definition expression.hpp:1481
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:1467
Derived expression type for std::sin().
Definition expression.hpp:1514
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:1523
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1527
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:1531
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1525
constexpr SinExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1520
Derived expression type for std::sinh().
Definition expression.hpp:1563
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1574
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1576
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:1580
constexpr SinhExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1569
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:1572
Derived expression type for std::sqrt().
Definition expression.hpp:1612
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1623
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1625
constexpr SqrtExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1618
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:1629
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:1621
Derived expression type for std::tan().
Definition expression.hpp:1663
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1676
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:1672
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:1680
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1674
constexpr TanExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1669
Derived expression type for std::tanh().
Definition expression.hpp:1712
constexpr TanhExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:1718
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:1723
double grad_l(double x, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:1725
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:1721
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:1729
Derived expression type for unary minus operator.
Definition expression.hpp:640
constexpr UnaryMinusExpression(ExpressionPtr lhs)
Constructs an unary expression (an operator with one argument).
Definition expression.hpp:646
ExpressionType type() const override
Returns the type of this expression (constant, linear, quadratic, or nonlinear).
Definition expression.hpp:651
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:657
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:649
double grad_l(double, double, double parent_adjoint) const override
Returns double adjoint of the left child expression.
Definition expression.hpp:653
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:2844
sign
Definition base.h:698