15#include "sleipnir/autodiff/expression.hpp"
16#include "sleipnir/autodiff/expression_type.hpp"
18namespace slp::detail {
23template <
typename Scalar, ExpressionType Type>
25 using AbsExpression::NonLeafExpression::NonLeafExpression;
28 static constexpr std::string_view
NAME =
"abs";
35 template <
typename Value>
36 static void eval_grad(std::array<Value, 1>& g,
38 if constexpr (std::same_as<Value, Scalar>) {
41 }
else if (x >
Scalar(0)) {
56template <
typename Scalar>
57ExpressionPtr<Scalar> abs(
const ExpressionPtr<Scalar>& x) {
58 return fold_or_make<AbsExpression, Scalar>(x);
65template <
typename Scalar, ExpressionType Type>
68 using AcosExpression::NonLeafExpression::NonLeafExpression;
71 static constexpr std::string_view
NAME =
"acos";
78 template <
typename Value>
79 static void eval_grad(std::array<Value, 1>& g,
90template <
typename Scalar>
91ExpressionPtr<Scalar> acos(
const ExpressionPtr<Scalar>& x) {
92 return fold_or_make<AcosExpression, Scalar>(x);
99template <
typename Scalar, ExpressionType Type>
102 using AsinExpression::NonLeafExpression::NonLeafExpression;
105 static constexpr std::string_view
NAME =
"asin";
112 template <
typename Value>
113 static void eval_grad(std::array<Value, 1>& g,
124template <
typename Scalar>
125ExpressionPtr<Scalar> asin(
const ExpressionPtr<Scalar>& x) {
126 return fold_or_make<AsinExpression, Scalar>(x);
133template <
typename Scalar, ExpressionType Type>
136 using AtanExpression::NonLeafExpression::NonLeafExpression;
139 static constexpr std::string_view
NAME =
"atan";
146 template <
typename Value>
147 static void eval_grad(std::array<Value, 1>& g,
157template <
typename Scalar>
158ExpressionPtr<Scalar> atan(
const ExpressionPtr<Scalar>& x) {
159 return fold_or_make<AtanExpression, Scalar>(x);
166template <
typename Scalar, ExpressionType Type>
169 using Atan2Expression::NonLeafExpression::NonLeafExpression;
172 static constexpr std::string_view
NAME =
"atan2";
179 template <
typename Value>
180 static void eval_grad(std::array<Value, 2>& g,
194template <
typename Scalar>
195ExpressionPtr<Scalar> atan2(
const ExpressionPtr<Scalar>& y,
196 const ExpressionPtr<Scalar>& x) {
197 return fold_or_make<Atan2Expression, Scalar>(y, x);
204template <
typename Scalar, ExpressionType Type>
207 using CbrtExpression::NonLeafExpression::NonLeafExpression;
210 static constexpr std::string_view
NAME =
"cbrt";
217 template <
typename Value>
218 static void eval_grad(std::array<Value, 1>& g,
const Value&
val,
228template <
typename Scalar>
229ExpressionPtr<Scalar> cbrt(
const ExpressionPtr<Scalar>& x) {
230 return fold_or_make<CbrtExpression, Scalar>(x);
237template <
typename Scalar, ExpressionType Type>
239 using CosExpression::NonLeafExpression::NonLeafExpression;
242 static constexpr std::string_view
NAME =
"cos";
249 template <
typename Value>
250 static void eval_grad(std::array<Value, 1>& g,
261template <
typename Scalar>
262ExpressionPtr<Scalar> cos(
const ExpressionPtr<Scalar>& x) {
263 return fold_or_make<CosExpression, Scalar>(x);
270template <
typename Scalar, ExpressionType Type>
273 using CoshExpression::NonLeafExpression::NonLeafExpression;
276 static constexpr std::string_view
NAME =
"cosh";
283 template <
typename Value>
284 static void eval_grad(std::array<Value, 1>& g,
295template <
typename Scalar>
296ExpressionPtr<Scalar> cosh(
const ExpressionPtr<Scalar>& x) {
297 return fold_or_make<CoshExpression, Scalar>(x);
304template <
typename Scalar, ExpressionType Type>
306 using ErfExpression::NonLeafExpression::NonLeafExpression;
309 static constexpr std::string_view
NAME =
"erf";
316 template <
typename Value>
317 static void eval_grad(std::array<Value, 1>& g,
328template <
typename Scalar>
329ExpressionPtr<Scalar> erf(
const ExpressionPtr<Scalar>& x) {
330 return fold_or_make<ErfExpression, Scalar>(x);
337template <
typename Scalar, ExpressionType Type>
339 using ExpExpression::NonLeafExpression::NonLeafExpression;
342 static constexpr std::string_view
NAME =
"exp";
349 template <
typename Value>
350 static void eval_grad(std::array<Value, 1>& g,
const Value&
val,
360template <
typename Scalar>
361ExpressionPtr<Scalar> exp(
const ExpressionPtr<Scalar>& x) {
362 return fold_or_make<ExpExpression, Scalar>(x);
365template <
typename Scalar>
366ExpressionPtr<Scalar> hypot(
const ExpressionPtr<Scalar>& x,
367 const ExpressionPtr<Scalar>& y);
373template <
typename Scalar, ExpressionType Type>
376 using Hypot2Expression::NonLeafExpression::NonLeafExpression;
379 static constexpr std::string_view
NAME =
"hypot";
386 template <
typename Value>
387 static void eval_grad(std::array<Value, 2>& g,
const Value&
val,
399template <
typename Scalar>
400ExpressionPtr<Scalar> hypot(
const ExpressionPtr<Scalar>& x,
401 const ExpressionPtr<Scalar>& y) {
403 if (x->is_constant(Scalar(0))) {
405 }
else if (y->is_constant(Scalar(0))) {
409 return fold_or_make<Hypot2Expression, Scalar>(x, y);
412template <
typename Scalar>
413ExpressionPtr<Scalar> hypot(
const ExpressionPtr<Scalar>& x,
414 const ExpressionPtr<Scalar>& y,
415 const ExpressionPtr<Scalar>& z);
421template <
typename Scalar, ExpressionType Type>
424 using Hypot3Expression::NonLeafExpression::NonLeafExpression;
427 static constexpr std::string_view
NAME =
"hypot";
431 return hypot(x, y, z);
434 template <
typename Value>
435 static void eval_grad(std::array<Value, 3>& g,
const Value&
val,
449template <
typename Scalar>
450ExpressionPtr<Scalar> hypot(
const ExpressionPtr<Scalar>& x,
451 const ExpressionPtr<Scalar>& y,
452 const ExpressionPtr<Scalar>& z) {
454 if (x->is_constant(Scalar(0))) {
456 }
else if (y->is_constant(Scalar(0))) {
458 }
else if (z->is_constant(Scalar(0))) {
462 return fold_or_make<Hypot3Expression, Scalar>(x, y, z);
466enum class IfElseCondition : uint8_t {
481template <
typename Scalar, ExpressionType Type>
485 static constexpr std::string_view
NAME =
"if-else";
508 using enum IfElseCondition;
526 template <
typename Value>
531 if constexpr (std::same_as<Value, Scalar>) {
536 auto zero = constant_ptr(
Scalar(0));
537 auto one = constant_ptr(
Scalar(1));
538 g[1] = if_else(
cond, c, one, zero);
539 g[2] = if_else(
cond, c, zero, one);
551template <
typename Scalar>
552ExpressionPtr<Scalar> if_else(IfElseCondition cond,
553 const ExpressionPtr<Scalar>& c,
554 const ExpressionPtr<Scalar>& t,
555 const ExpressionPtr<Scalar>& f) {
556 using enum ExpressionType;
562 }
else if (t->type() == CONSTANT && f->type() == CONSTANT &&
569 if (c->type() == CONSTANT) {
573 if (c->type() <= PARAMETER && t->type() <= PARAMETER &&
574 f->type() <= PARAMETER) {
575 return make_expression_ptr<IfElseExpression<Scalar, PARAMETER>>(cond, c, t,
578 return make_expression_ptr<IfElseExpression<Scalar, NONLINEAR>>(cond, c, t,
587template <
typename Scalar, ExpressionType Type>
589 using LogExpression::NonLeafExpression::NonLeafExpression;
592 static constexpr std::string_view
NAME =
"log";
599 template <
typename Value>
600 static void eval_grad(std::array<Value, 1>& g,
610template <
typename Scalar>
611ExpressionPtr<Scalar> log(
const ExpressionPtr<Scalar>& x) {
612 return fold_or_make<LogExpression, Scalar>(x);
619template <
typename Scalar, ExpressionType Type>
622 using Log10Expression::NonLeafExpression::NonLeafExpression;
625 static constexpr std::string_view
NAME =
"log10";
632 template <
typename Value>
633 static void eval_grad(std::array<Value, 1>& g,
643template <
typename Scalar>
644ExpressionPtr<Scalar> log10(
const ExpressionPtr<Scalar>& x) {
645 return fold_or_make<Log10Expression, Scalar>(x);
654template <
typename Scalar, ExpressionType Type>
656 using MaxExpression::NonLeafExpression::NonLeafExpression;
659 static constexpr std::string_view
NAME =
"max";
666 template <
typename Value>
667 static void eval_grad(std::array<Value, 2>& g,
670 if constexpr (std::same_as<Value, Scalar>) {
675 auto zero = constant_ptr(
Scalar(0));
676 auto one = constant_ptr(
Scalar(1));
678 g[0] = if_else(IfElseCondition::NONNEGATIVE,
c, one, zero);
679 g[1] = if_else(IfElseCondition::NONNEGATIVE,
c, zero, one);
689template <
typename Scalar>
690ExpressionPtr<Scalar> max(
const ExpressionPtr<Scalar>& a,
691 const ExpressionPtr<Scalar>& b) {
692 return fold_or_make<MaxExpression, Scalar>(a, b);
701template <
typename Scalar, ExpressionType Type>
703 using MinExpression::NonLeafExpression::NonLeafExpression;
706 static constexpr std::string_view
NAME =
"min";
713 template <
typename Value>
714 static void eval_grad(std::array<Value, 2>& g,
717 if constexpr (std::same_as<Value, Scalar>) {
722 auto zero = constant_ptr(
Scalar(0));
723 auto one = constant_ptr(
Scalar(1));
725 g[0] = if_else(IfElseCondition::NONNEGATIVE,
c, one, zero);
726 g[1] = if_else(IfElseCondition::NONNEGATIVE,
c, zero, one);
736template <
typename Scalar>
737ExpressionPtr<Scalar> min(
const ExpressionPtr<Scalar>& a,
738 const ExpressionPtr<Scalar>& b) {
739 return fold_or_make<MinExpression, Scalar>(a, b);
742template <
typename Scalar>
743ExpressionPtr<Scalar> pow(
const ExpressionPtr<Scalar>& base,
744 const ExpressionPtr<Scalar>& power);
746template <
typename Scalar>
747ExpressionPtr<Scalar> sqrt(
const ExpressionPtr<Scalar>& x);
753template <
typename Scalar, ExpressionType Type>
755 using PowExpression::NonLeafExpression::NonLeafExpression;
758 static constexpr std::string_view
NAME =
"pow";
765 template <
typename Value>
766 static void eval_grad(std::array<Value, 2>& g,
const Value&
val,
781template <
typename Scalar>
782ExpressionPtr<Scalar> pow(
const ExpressionPtr<Scalar>& base,
783 const ExpressionPtr<Scalar>& power) {
784 using enum ExpressionType;
788 if (base->is_constant(Scalar(0))) {
791 }
else if (base->is_constant(Scalar(1))) {
795 if (power->is_constant(Scalar(0))) {
796 return constant_ptr(Scalar(1));
797 }
else if (power->is_constant(Scalar(1))) {
803 if (base->type() == CONSTANT && power->type() == CONSTANT) {
804 return constant_ptr(pow(base->val, power->val));
807 if (power->is_constant(Scalar(2))) {
808 auto type = base->type() == PARAMETER ? PARAMETER
809 : base->type() == LINEAR ? QUADRATIC
811 return make_typed_expression_ptr<MultExpression, Scalar>(type, base, base);
812 }
else if (power->is_constant(Scalar(0.5))) {
816 if (base->type() <= PARAMETER && power->type() <= PARAMETER) {
817 return make_expression_ptr<PowExpression<Scalar, PARAMETER>>(base, power);
819 return make_expression_ptr<PowExpression<Scalar, NONLINEAR>>(base, power);
827template <
typename Scalar, ExpressionType Type>
830 using SignExpression::NonLeafExpression::NonLeafExpression;
833 static constexpr std::string_view
NAME =
"sign";
838 }
else if (x ==
Scalar(0)) {
850template <
typename Scalar>
859template <
typename Scalar, ExpressionType Type>
861 using SinExpression::NonLeafExpression::NonLeafExpression;
864 static constexpr std::string_view
NAME =
"sin";
871 template <
typename Value>
872 static void eval_grad(std::array<Value, 1>& g,
883template <
typename Scalar>
884ExpressionPtr<Scalar> sin(
const ExpressionPtr<Scalar>& x) {
885 return fold_or_make<SinExpression, Scalar>(x);
892template <
typename Scalar, ExpressionType Type>
895 using SinhExpression::NonLeafExpression::NonLeafExpression;
898 static constexpr std::string_view
NAME =
"sinh";
905 template <
typename Value>
906 static void eval_grad(std::array<Value, 1>& g,
917template <
typename Scalar>
918ExpressionPtr<Scalar> sinh(
const ExpressionPtr<Scalar>& x) {
919 return fold_or_make<SinhExpression, Scalar>(x);
926template <
typename Scalar, ExpressionType Type>
929 using SqrtExpression::NonLeafExpression::NonLeafExpression;
932 static constexpr std::string_view
NAME =
"sqrt";
939 template <
typename Value>
940 static void eval_grad(std::array<Value, 1>& g,
const Value&
val,
950template <
typename Scalar>
951ExpressionPtr<Scalar> sqrt(
const ExpressionPtr<Scalar>& x) {
952 return fold_or_make<SqrtExpression, Scalar>(x);
959template <
typename Scalar, ExpressionType Type>
961 using TanExpression::NonLeafExpression::NonLeafExpression;
964 static constexpr std::string_view
NAME =
"tan";
971 template <
typename Value>
972 static void eval_grad(std::array<Value, 1>& g,
985template <
typename Scalar>
986ExpressionPtr<Scalar> tan(
const ExpressionPtr<Scalar>& x) {
987 return fold_or_make<TanExpression, Scalar>(x);
994template <
typename Scalar, ExpressionType Type>
997 using TanhExpression::NonLeafExpression::NonLeafExpression;
1000 static constexpr std::string_view
NAME =
"tanh";
1007 template <
typename Value>
1008 static void eval_grad(std::array<Value, 1>& g,
1021template <
typename Scalar>
1022ExpressionPtr<Scalar> tanh(
const ExpressionPtr<Scalar>& x) {
1023 return fold_or_make<TanhExpression, Scalar>(x);
Definition intrusive_shared_ptr.hpp:27
Definition expression_functions.hpp:24
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:28
Definition expression_functions.hpp:67
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:71
Definition expression_functions.hpp:101
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:105
Definition expression_functions.hpp:168
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:172
Definition expression_functions.hpp:135
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:139
Definition expression_functions.hpp:206
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:210
Definition expression_functions.hpp:238
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:242
Definition expression_functions.hpp:272
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:276
Definition expression_functions.hpp:305
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:309
Definition expression_functions.hpp:338
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:342
Scalar val
The value of the expression node.
Definition expression.hpp:132
Scalar Scalar
Scalar type alias.
Definition expression.hpp:129
Definition expression_functions.hpp:375
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:379
Definition expression_functions.hpp:423
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:427
Definition expression_functions.hpp:483
static bool satisfied(IfElseCondition cond, Scalar c)
Definition expression_functions.hpp:507
constexpr IfElseExpression(IfElseCondition cond, ExpressionPtr< Scalar > c, ExpressionPtr< Scalar > t, ExpressionPtr< Scalar > f)
Definition expression_functions.hpp:496
IfElseCondition cond
The condition checked on c.
Definition expression_functions.hpp:488
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:485
Definition expression_functions.hpp:621
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:625
Definition expression_functions.hpp:588
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:592
Definition expression_functions.hpp:655
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:659
Definition expression_functions.hpp:702
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:706
Definition expression.hpp:407
Definition expression_functions.hpp:754
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:758
Definition expression_functions.hpp:829
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:833
Definition expression_functions.hpp:860
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:864
Definition expression_functions.hpp:894
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:898
Definition expression_functions.hpp:928
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:932
Definition expression_functions.hpp:960
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:964
Definition expression_functions.hpp:996
static constexpr std::string_view NAME
The expression's name.
Definition expression_functions.hpp:1000