Sleipnir C++ API
Loading...
Searching...
No Matches
expression_functions.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <stdint.h>
6
7#include <algorithm>
8#include <array>
9#include <cmath>
10#include <concepts>
11#include <numbers>
12#include <string_view>
13#include <utility>
14
15#include "sleipnir/autodiff/expression.hpp"
16#include "sleipnir/autodiff/expression_type.hpp"
17
18namespace slp::detail {
23template <typename Scalar, ExpressionType Type>
24struct AbsExpression final : NonLeafExpression<AbsExpression, Scalar, Type, 1> {
25 using AbsExpression::NonLeafExpression::NonLeafExpression;
26
28 static constexpr std::string_view NAME = "abs";
29
30 static Scalar eval(Scalar x) {
31 using std::abs;
32 return abs(x);
33 }
34
35 template <typename Value>
36 static void eval_grad(std::array<Value, 1>& g,
37 [[maybe_unused]] const Value& val, const Value& x) {
38 if constexpr (std::same_as<Value, Scalar>) {
39 if (x < Scalar(0)) {
40 g[0] = Scalar(-1);
41 } else if (x > Scalar(0)) {
42 g[0] = Scalar(1);
43 } else {
44 g[0] = Scalar(0);
45 }
46 } else {
47 g[0] = sign(x);
48 }
49 }
50};
51
56template <typename Scalar>
57ExpressionPtr<Scalar> abs(const ExpressionPtr<Scalar>& x) {
58 return fold_or_make<AbsExpression, Scalar>(x);
59}
60
65template <typename Scalar, ExpressionType Type>
67 : NonLeafExpression<AcosExpression, Scalar, Type, 1> {
68 using AcosExpression::NonLeafExpression::NonLeafExpression;
69
71 static constexpr std::string_view NAME = "acos";
72
73 static Scalar eval(Scalar x) {
74 using std::acos;
75 return acos(x);
76 }
77
78 template <typename Value>
79 static void eval_grad(std::array<Value, 1>& g,
80 [[maybe_unused]] const Value& val, const Value& x) {
81 using std::sqrt;
82 g[0] = lit<Value>(Scalar(-1)) / sqrt(lit<Value>(Scalar(1)) - x * x);
83 }
84};
85
90template <typename Scalar>
91ExpressionPtr<Scalar> acos(const ExpressionPtr<Scalar>& x) {
92 return fold_or_make<AcosExpression, Scalar>(x);
93}
94
99template <typename Scalar, ExpressionType Type>
101 : NonLeafExpression<AsinExpression, Scalar, Type, 1> {
102 using AsinExpression::NonLeafExpression::NonLeafExpression;
103
105 static constexpr std::string_view NAME = "asin";
106
107 static Scalar eval(Scalar x) {
108 using std::asin;
109 return asin(x);
110 }
111
112 template <typename Value>
113 static void eval_grad(std::array<Value, 1>& g,
114 [[maybe_unused]] const Value& val, const Value& x) {
115 using std::sqrt;
116 g[0] = lit<Value>(Scalar(1)) / sqrt(lit<Value>(Scalar(1)) - x * x);
117 }
118};
119
124template <typename Scalar>
125ExpressionPtr<Scalar> asin(const ExpressionPtr<Scalar>& x) {
126 return fold_or_make<AsinExpression, Scalar>(x);
127}
128
133template <typename Scalar, ExpressionType Type>
135 : NonLeafExpression<AtanExpression, Scalar, Type, 1> {
136 using AtanExpression::NonLeafExpression::NonLeafExpression;
137
139 static constexpr std::string_view NAME = "atan";
140
141 static Scalar eval(Scalar x) {
142 using std::atan;
143 return atan(x);
144 }
145
146 template <typename Value>
147 static void eval_grad(std::array<Value, 1>& g,
148 [[maybe_unused]] const Value& val, const Value& x) {
149 g[0] = lit<Value>(Scalar(1)) / (lit<Value>(Scalar(1)) + x * x);
150 }
151};
152
157template <typename Scalar>
158ExpressionPtr<Scalar> atan(const ExpressionPtr<Scalar>& x) {
159 return fold_or_make<AtanExpression, Scalar>(x);
160}
161
166template <typename Scalar, ExpressionType Type>
168 : NonLeafExpression<Atan2Expression, Scalar, Type, 2> {
169 using Atan2Expression::NonLeafExpression::NonLeafExpression;
170
172 static constexpr std::string_view NAME = "atan2";
173
174 static Scalar eval(Scalar y, Scalar x) {
175 using std::atan2;
176 return atan2(y, x);
177 }
178
179 template <typename Value>
180 static void eval_grad(std::array<Value, 2>& g,
181 [[maybe_unused]] const Value& val, const Value& y,
182 const Value& x) {
183 Value d = y * y + x * x;
184 g[0] = x / d;
185 g[1] = -y / d;
186 }
187};
188
194template <typename Scalar>
195ExpressionPtr<Scalar> atan2(const ExpressionPtr<Scalar>& y,
196 const ExpressionPtr<Scalar>& x) {
197 return fold_or_make<Atan2Expression, Scalar>(y, x);
198}
199
204template <typename Scalar, ExpressionType Type>
206 : NonLeafExpression<CbrtExpression, Scalar, Type, 1> {
207 using CbrtExpression::NonLeafExpression::NonLeafExpression;
208
210 static constexpr std::string_view NAME = "cbrt";
211
212 static Scalar eval(Scalar x) {
213 using std::cbrt;
214 return cbrt(x);
215 }
216
217 template <typename Value>
218 static void eval_grad(std::array<Value, 1>& g, const Value& val,
219 [[maybe_unused]] const Value& x) {
220 g[0] = lit<Value>(Scalar(1)) / (lit<Value>(Scalar(3)) * val * val);
221 }
222};
223
228template <typename Scalar>
229ExpressionPtr<Scalar> cbrt(const ExpressionPtr<Scalar>& x) {
230 return fold_or_make<CbrtExpression, Scalar>(x);
231}
232
237template <typename Scalar, ExpressionType Type>
238struct CosExpression final : NonLeafExpression<CosExpression, Scalar, Type, 1> {
239 using CosExpression::NonLeafExpression::NonLeafExpression;
240
242 static constexpr std::string_view NAME = "cos";
243
244 static Scalar eval(Scalar x) {
245 using std::cos;
246 return cos(x);
247 }
248
249 template <typename Value>
250 static void eval_grad(std::array<Value, 1>& g,
251 [[maybe_unused]] const Value& val, const Value& x) {
252 using std::sin;
253 g[0] = -sin(x);
254 }
255};
256
261template <typename Scalar>
262ExpressionPtr<Scalar> cos(const ExpressionPtr<Scalar>& x) {
263 return fold_or_make<CosExpression, Scalar>(x);
264}
265
270template <typename Scalar, ExpressionType Type>
272 : NonLeafExpression<CoshExpression, Scalar, Type, 1> {
273 using CoshExpression::NonLeafExpression::NonLeafExpression;
274
276 static constexpr std::string_view NAME = "cosh";
277
278 static Scalar eval(Scalar x) {
279 using std::cosh;
280 return cosh(x);
281 }
282
283 template <typename Value>
284 static void eval_grad(std::array<Value, 1>& g,
285 [[maybe_unused]] const Value& val, const Value& x) {
286 using std::sinh;
287 g[0] = sinh(x);
288 }
289};
290
295template <typename Scalar>
296ExpressionPtr<Scalar> cosh(const ExpressionPtr<Scalar>& x) {
297 return fold_or_make<CoshExpression, Scalar>(x);
298}
299
304template <typename Scalar, ExpressionType Type>
305struct ErfExpression final : NonLeafExpression<ErfExpression, Scalar, Type, 1> {
306 using ErfExpression::NonLeafExpression::NonLeafExpression;
307
309 static constexpr std::string_view NAME = "erf";
310
311 static Scalar eval(Scalar x) {
312 using std::erf;
313 return erf(x);
314 }
315
316 template <typename Value>
317 static void eval_grad(std::array<Value, 1>& g,
318 [[maybe_unused]] const Value& val, const Value& x) {
319 using std::exp;
320 g[0] = lit<Value>(Scalar(2.0 * std::numbers::inv_sqrtpi)) * exp(-x * x);
321 }
322};
323
328template <typename Scalar>
329ExpressionPtr<Scalar> erf(const ExpressionPtr<Scalar>& x) {
330 return fold_or_make<ErfExpression, Scalar>(x);
331}
332
337template <typename Scalar, ExpressionType Type>
338struct ExpExpression final : NonLeafExpression<ExpExpression, Scalar, Type, 1> {
339 using ExpExpression::NonLeafExpression::NonLeafExpression;
340
342 static constexpr std::string_view NAME = "exp";
343
344 static Scalar eval(Scalar x) {
345 using std::exp;
346 return exp(x);
347 }
348
349 template <typename Value>
350 static void eval_grad(std::array<Value, 1>& g, const Value& val,
351 [[maybe_unused]] const Value& x) {
352 g[0] = val;
353 }
354};
355
360template <typename Scalar>
361ExpressionPtr<Scalar> exp(const ExpressionPtr<Scalar>& x) {
362 return fold_or_make<ExpExpression, Scalar>(x);
363}
364
365template <typename Scalar>
366ExpressionPtr<Scalar> hypot(const ExpressionPtr<Scalar>& x,
367 const ExpressionPtr<Scalar>& y);
368
373template <typename Scalar, ExpressionType Type>
375 : NonLeafExpression<Hypot2Expression, Scalar, Type, 2> {
376 using Hypot2Expression::NonLeafExpression::NonLeafExpression;
377
379 static constexpr std::string_view NAME = "hypot";
380
381 static Scalar eval(Scalar x, Scalar y) {
382 using std::hypot;
383 return hypot(x, y);
384 }
385
386 template <typename Value>
387 static void eval_grad(std::array<Value, 2>& g, const Value& val,
388 const Value& x, const Value& y) {
389 g[0] = x / val;
390 g[1] = y / val;
391 }
392};
393
399template <typename Scalar>
400ExpressionPtr<Scalar> hypot(const ExpressionPtr<Scalar>& x,
401 const ExpressionPtr<Scalar>& y) {
402 // Prune expression
403 if (x->is_constant(Scalar(0))) {
404 return abs(y);
405 } else if (y->is_constant(Scalar(0))) {
406 return abs(x);
407 }
408
409 return fold_or_make<Hypot2Expression, Scalar>(x, y);
410}
411
412template <typename Scalar>
413ExpressionPtr<Scalar> hypot(const ExpressionPtr<Scalar>& x,
414 const ExpressionPtr<Scalar>& y,
415 const ExpressionPtr<Scalar>& z);
416
421template <typename Scalar, ExpressionType Type>
423 : NonLeafExpression<Hypot3Expression, Scalar, Type, 3> {
424 using Hypot3Expression::NonLeafExpression::NonLeafExpression;
425
427 static constexpr std::string_view NAME = "hypot";
428
429 static Scalar eval(Scalar x, Scalar y, Scalar z) {
430 using std::hypot;
431 return hypot(x, y, z);
432 }
433
434 template <typename Value>
435 static void eval_grad(std::array<Value, 3>& g, const Value& val,
436 const Value& x, const Value& y, const Value& z) {
437 g[0] = x / val;
438 g[1] = y / val;
439 g[2] = z / val;
440 }
441};
442
449template <typename Scalar>
450ExpressionPtr<Scalar> hypot(const ExpressionPtr<Scalar>& x,
451 const ExpressionPtr<Scalar>& y,
452 const ExpressionPtr<Scalar>& z) {
453 // Prune expression
454 if (x->is_constant(Scalar(0))) {
455 return hypot(y, z);
456 } else if (y->is_constant(Scalar(0))) {
457 return hypot(x, z);
458 } else if (z->is_constant(Scalar(0))) {
459 return hypot(x, y);
460 }
461
462 return fold_or_make<Hypot3Expression, Scalar>(x, y, z);
463}
464
466enum class IfElseCondition : uint8_t {
468 ZERO,
470 NONNEGATIVE,
472 POSITIVE
473};
474
481template <typename Scalar, ExpressionType Type>
483 : NonLeafExpression<IfElseExpression, Scalar, Type, 3> {
485 static constexpr std::string_view NAME = "if-else";
486
488 IfElseCondition cond;
489
496 constexpr IfElseExpression(IfElseCondition cond, ExpressionPtr<Scalar> c,
499 std::move(f)},
500 cond{cond} {}
501
507 static bool satisfied(IfElseCondition cond, Scalar c) {
508 using enum IfElseCondition;
509
510 switch (cond) {
511 case ZERO:
512 return c == Scalar(0);
513 case NONNEGATIVE:
514 return c >= Scalar(0);
515 case POSITIVE:
516 return c > Scalar(0);
517 default:
518 std::unreachable();
519 }
520 }
521
522 Scalar eval(Scalar c, Scalar t, Scalar f) const {
523 return satisfied(cond, c) ? t : f;
524 }
525
526 template <typename Value>
527 void eval_grad(std::array<Value, 3>& g, [[maybe_unused]] const Value& val,
528 const Value& c, [[maybe_unused]] const Value& t,
529 [[maybe_unused]] const Value& f) const {
530 // c has zero gradient
531 if constexpr (std::same_as<Value, Scalar>) {
532 bool s = satisfied(cond, c);
533 g[1] = s ? Scalar(1) : Scalar(0);
534 g[2] = s ? Scalar(0) : Scalar(1);
535 } else {
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);
540 }
541 }
542};
543
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;
557
558 // Prune expression
559 if (t == f) {
560 // Return t, which both branches currently are
561 return t;
562 } else if (t->type() == CONSTANT && f->type() == CONSTANT &&
563 t->val == f->val) {
564 // Return t, which both branches currently equal
565 return t;
566 }
567
568 // Evaluate constant condition
569 if (c->type() == CONSTANT) {
570 return IfElseExpression<Scalar, NONLINEAR>::satisfied(cond, c->val) ? t : f;
571 }
572
573 if (c->type() <= PARAMETER && t->type() <= PARAMETER &&
574 f->type() <= PARAMETER) {
575 return make_expression_ptr<IfElseExpression<Scalar, PARAMETER>>(cond, c, t,
576 f);
577 } else {
578 return make_expression_ptr<IfElseExpression<Scalar, NONLINEAR>>(cond, c, t,
579 f);
580 }
581}
582
587template <typename Scalar, ExpressionType Type>
588struct LogExpression final : NonLeafExpression<LogExpression, Scalar, Type, 1> {
589 using LogExpression::NonLeafExpression::NonLeafExpression;
590
592 static constexpr std::string_view NAME = "log";
593
594 static Scalar eval(Scalar x) {
595 using std::log;
596 return log(x);
597 }
598
599 template <typename Value>
600 static void eval_grad(std::array<Value, 1>& g,
601 [[maybe_unused]] const Value& val, const Value& x) {
602 g[0] = lit<Value>(Scalar(1)) / x;
603 }
604};
605
610template <typename Scalar>
611ExpressionPtr<Scalar> log(const ExpressionPtr<Scalar>& x) {
612 return fold_or_make<LogExpression, Scalar>(x);
613}
614
619template <typename Scalar, ExpressionType Type>
621 : NonLeafExpression<Log10Expression, Scalar, Type, 1> {
622 using Log10Expression::NonLeafExpression::NonLeafExpression;
623
625 static constexpr std::string_view NAME = "log10";
626
627 static Scalar eval(Scalar x) {
628 using std::log10;
629 return log10(x);
630 }
631
632 template <typename Value>
633 static void eval_grad(std::array<Value, 1>& g,
634 [[maybe_unused]] const Value& val, const Value& x) {
635 g[0] = lit<Value>(Scalar(1)) / (lit<Value>(Scalar(std::numbers::ln10)) * x);
636 }
637};
638
643template <typename Scalar>
644ExpressionPtr<Scalar> log10(const ExpressionPtr<Scalar>& x) {
645 return fold_or_make<Log10Expression, Scalar>(x);
646}
647
654template <typename Scalar, ExpressionType Type>
655struct MaxExpression final : NonLeafExpression<MaxExpression, Scalar, Type, 2> {
656 using MaxExpression::NonLeafExpression::NonLeafExpression;
657
659 static constexpr std::string_view NAME = "max";
660
661 static Scalar eval(Scalar a, Scalar b) {
662 using std::max;
663 return max(a, b);
664 }
665
666 template <typename Value>
667 static void eval_grad(std::array<Value, 2>& g,
668 [[maybe_unused]] const Value& val, const Value& a,
669 const Value& b) {
670 if constexpr (std::same_as<Value, Scalar>) {
671 bool c = a >= b;
672 g[0] = c ? Scalar(1) : Scalar(0);
673 g[1] = c ? Scalar(0) : Scalar(1);
674 } else {
675 auto zero = constant_ptr(Scalar(0));
676 auto one = constant_ptr(Scalar(1));
677 auto c = a - b;
678 g[0] = if_else(IfElseCondition::NONNEGATIVE, c, one, zero);
679 g[1] = if_else(IfElseCondition::NONNEGATIVE, c, zero, one);
680 }
681 }
682};
683
689template <typename Scalar>
690ExpressionPtr<Scalar> max(const ExpressionPtr<Scalar>& a,
691 const ExpressionPtr<Scalar>& b) {
692 return fold_or_make<MaxExpression, Scalar>(a, b);
693}
694
701template <typename Scalar, ExpressionType Type>
702struct MinExpression final : NonLeafExpression<MinExpression, Scalar, Type, 2> {
703 using MinExpression::NonLeafExpression::NonLeafExpression;
704
706 static constexpr std::string_view NAME = "min";
707
708 static Scalar eval(Scalar a, Scalar b) {
709 using std::min;
710 return min(a, b);
711 }
712
713 template <typename Value>
714 static void eval_grad(std::array<Value, 2>& g,
715 [[maybe_unused]] const Value& val, const Value& a,
716 const Value& b) {
717 if constexpr (std::same_as<Value, Scalar>) {
718 bool c = a <= b;
719 g[0] = c ? Scalar(1) : Scalar(0);
720 g[1] = c ? Scalar(0) : Scalar(1);
721 } else {
722 auto zero = constant_ptr(Scalar(0));
723 auto one = constant_ptr(Scalar(1));
724 auto c = b - a;
725 g[0] = if_else(IfElseCondition::NONNEGATIVE, c, one, zero);
726 g[1] = if_else(IfElseCondition::NONNEGATIVE, c, zero, one);
727 }
728 }
729};
730
736template <typename Scalar>
737ExpressionPtr<Scalar> min(const ExpressionPtr<Scalar>& a,
738 const ExpressionPtr<Scalar>& b) {
739 return fold_or_make<MinExpression, Scalar>(a, b);
740}
741
742template <typename Scalar>
743ExpressionPtr<Scalar> pow(const ExpressionPtr<Scalar>& base,
744 const ExpressionPtr<Scalar>& power);
745
746template <typename Scalar>
747ExpressionPtr<Scalar> sqrt(const ExpressionPtr<Scalar>& x);
748
753template <typename Scalar, ExpressionType Type>
754struct PowExpression final : NonLeafExpression<PowExpression, Scalar, Type, 2> {
755 using PowExpression::NonLeafExpression::NonLeafExpression;
756
758 static constexpr std::string_view NAME = "pow";
759
760 static Scalar eval(Scalar base, Scalar power) {
761 using std::pow;
762 return pow(base, power);
763 }
764
765 template <typename Value>
766 static void eval_grad(std::array<Value, 2>& g, const Value& val,
767 const Value& base, const Value& power) {
768 using std::log;
769 using std::pow;
770
771 g[0] = pow(base, power - lit<Value>(Scalar(1))) * power;
772 g[1] = val * log(base);
773 }
774};
775
781template <typename Scalar>
782ExpressionPtr<Scalar> pow(const ExpressionPtr<Scalar>& base,
783 const ExpressionPtr<Scalar>& power) {
784 using enum ExpressionType;
785 using std::pow;
786
787 // Prune expression
788 if (base->is_constant(Scalar(0))) {
789 // Return zero, which base currently is
790 return base;
791 } else if (base->is_constant(Scalar(1))) {
792 // Return one, which base currently is
793 return base;
794 }
795 if (power->is_constant(Scalar(0))) {
796 return constant_ptr(Scalar(1));
797 } else if (power->is_constant(Scalar(1))) {
798 // Return base unmodified
799 return base;
800 }
801
802 // Evaluate constant
803 if (base->type() == CONSTANT && power->type() == CONSTANT) {
804 return constant_ptr(pow(base->val, power->val));
805 }
806
807 if (power->is_constant(Scalar(2))) {
808 auto type = base->type() == PARAMETER ? PARAMETER
809 : base->type() == LINEAR ? QUADRATIC
810 : NONLINEAR;
811 return make_typed_expression_ptr<MultExpression, Scalar>(type, base, base);
812 } else if (power->is_constant(Scalar(0.5))) {
813 return sqrt(base);
814 }
815
816 if (base->type() <= PARAMETER && power->type() <= PARAMETER) {
817 return make_expression_ptr<PowExpression<Scalar, PARAMETER>>(base, power);
818 } else {
819 return make_expression_ptr<PowExpression<Scalar, NONLINEAR>>(base, power);
820 }
821}
822
827template <typename Scalar, ExpressionType Type>
829 : NonLeafExpression<SignExpression, Scalar, Type, 1> {
830 using SignExpression::NonLeafExpression::NonLeafExpression;
831
833 static constexpr std::string_view NAME = "sign";
834
835 static Scalar eval(Scalar x) {
836 if (x < Scalar(0)) {
837 return Scalar(-1);
838 } else if (x == Scalar(0)) {
839 return Scalar(0);
840 } else {
841 return Scalar(1);
842 }
843 }
844};
845
850template <typename Scalar>
853}
854
859template <typename Scalar, ExpressionType Type>
860struct SinExpression final : NonLeafExpression<SinExpression, Scalar, Type, 1> {
861 using SinExpression::NonLeafExpression::NonLeafExpression;
862
864 static constexpr std::string_view NAME = "sin";
865
866 static Scalar eval(Scalar x) {
867 using std::sin;
868 return sin(x);
869 }
870
871 template <typename Value>
872 static void eval_grad(std::array<Value, 1>& g,
873 [[maybe_unused]] const Value& val, const Value& x) {
874 using std::cos;
875 g[0] = cos(x);
876 }
877};
878
883template <typename Scalar>
884ExpressionPtr<Scalar> sin(const ExpressionPtr<Scalar>& x) {
885 return fold_or_make<SinExpression, Scalar>(x);
886}
887
892template <typename Scalar, ExpressionType Type>
894 : NonLeafExpression<SinhExpression, Scalar, Type, 1> {
895 using SinhExpression::NonLeafExpression::NonLeafExpression;
896
898 static constexpr std::string_view NAME = "sinh";
899
900 static Scalar eval(Scalar x) {
901 using std::sinh;
902 return sinh(x);
903 }
904
905 template <typename Value>
906 static void eval_grad(std::array<Value, 1>& g,
907 [[maybe_unused]] const Value& val, const Value& x) {
908 using std::cosh;
909 g[0] = cosh(x);
910 }
911};
912
917template <typename Scalar>
918ExpressionPtr<Scalar> sinh(const ExpressionPtr<Scalar>& x) {
919 return fold_or_make<SinhExpression, Scalar>(x);
920}
921
926template <typename Scalar, ExpressionType Type>
928 : NonLeafExpression<SqrtExpression, Scalar, Type, 1> {
929 using SqrtExpression::NonLeafExpression::NonLeafExpression;
930
932 static constexpr std::string_view NAME = "sqrt";
933
934 static Scalar eval(Scalar x) {
935 using std::sqrt;
936 return sqrt(x);
937 }
938
939 template <typename Value>
940 static void eval_grad(std::array<Value, 1>& g, const Value& val,
941 [[maybe_unused]] const Value& x) {
942 g[0] = lit<Value>(Scalar(1)) / (lit<Value>(Scalar(2)) * val);
943 }
944};
945
950template <typename Scalar>
951ExpressionPtr<Scalar> sqrt(const ExpressionPtr<Scalar>& x) {
952 return fold_or_make<SqrtExpression, Scalar>(x);
953}
954
959template <typename Scalar, ExpressionType Type>
960struct TanExpression final : NonLeafExpression<TanExpression, Scalar, Type, 1> {
961 using TanExpression::NonLeafExpression::NonLeafExpression;
962
964 static constexpr std::string_view NAME = "tan";
965
966 static Scalar eval(Scalar x) {
967 using std::tan;
968 return tan(x);
969 }
970
971 template <typename Value>
972 static void eval_grad(std::array<Value, 1>& g,
973 [[maybe_unused]] const Value& val, const Value& x) {
974 using std::cos;
975
976 Value c = cos(x);
977 g[0] = lit<Value>(Scalar(1)) / (c * c);
978 }
979};
980
985template <typename Scalar>
986ExpressionPtr<Scalar> tan(const ExpressionPtr<Scalar>& x) {
987 return fold_or_make<TanExpression, Scalar>(x);
988}
989
994template <typename Scalar, ExpressionType Type>
996 : NonLeafExpression<TanhExpression, Scalar, Type, 1> {
997 using TanhExpression::NonLeafExpression::NonLeafExpression;
998
1000 static constexpr std::string_view NAME = "tanh";
1001
1002 static Scalar eval(Scalar x) {
1003 using std::tanh;
1004 return tanh(x);
1005 }
1006
1007 template <typename Value>
1008 static void eval_grad(std::array<Value, 1>& g,
1009 [[maybe_unused]] const Value& val, const Value& x) {
1010 using std::cosh;
1011
1012 Value c = cosh(x);
1013 g[0] = lit<Value>(Scalar(1)) / (c * c);
1014 }
1015};
1016
1021template <typename Scalar>
1022ExpressionPtr<Scalar> tanh(const ExpressionPtr<Scalar>& x) {
1023 return fold_or_make<TanhExpression, Scalar>(x);
1024}
1025
1026} // namespace slp::detail
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