codac 2.0.0
Loading...
Searching...
No Matches
codac2_Traj_operator.h
Go to the documentation of this file.
1
9
10#pragma once
11
13#include "codac2_ExprType.h"
14#include "codac2_Wrapper.h"
15
16namespace codac2
17{
18 template<typename TR>
19 struct TrajectoryOp
20 {
21 using T = typename TR::Type::Scalar;
22
23 template<typename X1>
24 static std::string str(const X1& x1)
25 {
26 return "traj(" + x1->str() + ")";
27 }
28
29 template<typename X1>
30 static std::pair<Index,Index> output_shape(const X1& s1)
31 {
32 return {s1->size(),1};
33 }
34
35 static typename TR::Type::Domain fwd(const TR& x1, const Interval& x2)
36 {
37 return x1(x2);
38 }
39
40 static typename TR::Type fwd_natural(const TR& x1, const ScalarType& x2)
41 {
42 return {
43 fwd(x1,x2.m),
44 x2.def_domain && x1.tdomain().is_superset(x2.m)
45 };
46 }
47
48 static typename TR::Type fwd_centered(const TR& x1, [[maybe_unused]] const SampledTraj<T>& x1_deriv, const ScalarType& x2)
49 {
50 IntervalMatrix d(x1.size(),x2.da.cols());
51
52 // The diff operation is not relevant in this context,
53 // so the following code has been disabled.
54 //
55 // if constexpr(std::is_same_v<TR,SampledTraj<T>>)
56 // {
57 // IntervalVector dx1 = [&x1_deriv,&x2]() {
58 // if constexpr(std::is_same_v<T,double>)
59 // return IntervalVector({x1_deriv(x2.a)});
60 // else
61 // return x1_deriv(x2.a);
62 // }();
63 //
64 // for(Index i = 0 ; i < x1.size() ; i++)
65 // for(Index j = 0 ; j < x2.da.cols() ; j++)
66 // d(i,j) = dx1[i];
67 // }
68
69 return {
70 fwd(x1,x2.m),
71 fwd(x1,x2.a),
72 d,
73 x2.def_domain && x1.tdomain().is_superset(x2.m)
74 };
75 }
76
77 static void bwd(
78 [[maybe_unused]] const TR& x1,
79 [[maybe_unused]] const typename TR::Type::Domain& y,
80 [[maybe_unused]] Interval& x2)
81 {
82 // todo
83 }
84 };
85
86 // todo: merge this code with SlicedTube_operator?
87 template<typename TR, typename T, typename S>
88 class AnalyticOperationExpr<TrajectoryOp<TR>,T,S>
89 : public AnalyticExpr<typename TR::Type>, public OperationExprBase<AnalyticExpr<ScalarType>>
90 {
91 public:
92
93 AnalyticOperationExpr(const TR& x1, const ScalarExpr& x2)
94 : OperationExprBase<AnalyticExpr<ScalarType>>(x2), _x1(x1),
95 _x1_deriv([&]() {
96 // The following is disabled, see the previous comment in TrajectoryOp::fwd_centered
97 // if constexpr(std::is_same_v<TR,SampledTraj<typename TR::Type::Scalar>>)
98 // return x1.derivative();
99 // else
100 return SampledTraj<typename T::Scalar>();
101 }())
102 { }
103
104 std::shared_ptr<ExprBase> copy() const
105 {
106 return std::make_shared<AnalyticOperationExpr<TrajectoryOp<TR>,T,ScalarType>>(*this);
107 }
108
109 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
110 {
111 return OperationExprBase<AnalyticExpr<ScalarType>>::replace_arg(old_arg_id, new_expr);
112 }
113
114 T fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
115 {
116 if(natural_eval)
117 return AnalyticExpr<T>::init_value(
118 v, TrajectoryOp<TR>::fwd_natural(_x1, std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval)));
119 else
120 return AnalyticExpr<T>::init_value(
121 v, TrajectoryOp<TR>::fwd_centered(_x1, _x1_deriv, std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval)));
122 }
123
124 void bwd_eval(ValuesMap& v) const
125 {
126 TrajectoryOp<TR>::bwd(_x1, AnalyticExpr<T>::value(v).a, std::get<0>(this->_x)->value(v).a);
127 std::get<0>(this->_x)->bwd_eval(v);
128 }
129
130 std::pair<Index,Index> output_shape() const {
131 return _x1.shape();
132 }
133
134 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
135 {
136 return std::get<0>(this->_x)->belongs_to_args_list(args);
137 }
138
139 std::string str(bool in_parentheses = false) const
140 {
141 std::string s = "T"; // user cannot (yet) specify a name for the trajectory
142 return in_parentheses ? "(" + s + ")" : s;
143 }
144
145 virtual bool is_str_leaf() const
146 {
147 return true;
148 }
149
150 protected:
151
152 const TR _x1;
153 const SampledTraj<typename T::Scalar> _x1_deriv; // only available for operators on SampledTraj<T>
154 };
155}
A base class for expressions representing operations with multiple operands.
Definition codac2_ExprBase.h:164
OperationExprBase(std::shared_ptr< X >... x)
Definition codac2_ExprBase.h:176
std::tuple< std::shared_ptr< X >... > _x
Definition codac2_ExprBase.h:258
Definition codac2_OctaSym.h:21
Eigen::Matrix< Interval,-1,-1 > IntervalMatrix
Alias for a dynamic-size matrix of intervals.
Definition codac2_IntervalMatrix.h:25