codac 2.0.0
Loading...
Searching...
No Matches
codac2_subvector.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Interval.h"
14#include "codac2_AnalyticType.h"
15#include "codac2_AnalyticExpr.h"
17
18namespace codac2
19{
20 struct SubvectorOp
21 {
22 template<typename X1>
23 static std::string str(const X1& x1, Index i, Index j)
24 {
25 return x1->str(!x1->is_str_leaf()) + "[" + std::to_string(i) + ":" + std::to_string(j) + "]";
26 }
27
28 template<typename X1>
29 static std::pair<Index,Index> output_shape(const X1& s1, Index i, Index j)
30 {
31 auto shape1=s1->output_shape();
32 assert_release(shape1.second==1 && i<=j && j<shape1.first);
33 return { 1, j-i+1 };
34 }
35
36 static IntervalVector fwd(const IntervalVector& x1, Index i, Index j);
37 static VectorType fwd_natural(const VectorType& x1, Index i, Index j);
38 static VectorType fwd_centered(const VectorType& x1, Index i, Index j);
39 static void bwd(const IntervalVector& y, IntervalVector& x1, Index i, Index j);
40 };
41
42 // Analytic operator
43
44 template<>
45 class AnalyticOperationExpr<SubvectorOp,VectorType,VectorType> : public AnalyticExpr<VectorType>, public OperationExprBase<AnalyticExpr<VectorType>>
46 {
47 public:
48
49 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<VectorType>>& x1, Index i, Index j)
50 : OperationExprBase<AnalyticExpr<VectorType>>(x1), _i(i), _j(j)
51 { }
52
53 AnalyticOperationExpr(const AnalyticOperationExpr& e)
54 : OperationExprBase<AnalyticExpr<VectorType>>(e), _i(e._i), _j(e._j)
55 { }
56
57 std::shared_ptr<ExprBase> copy() const
58 {
59 return std::make_shared<AnalyticOperationExpr<SubvectorOp,VectorType,VectorType>>(*this);
60 }
61
62 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
63 {
64 return OperationExprBase<AnalyticExpr<VectorType>>::replace_arg(old_arg_id, new_expr);
65 }
66
67 VectorType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
68 {
69 if(natural_eval)
70 return AnalyticExpr<VectorType>::init_value(
71 v, SubvectorOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
72 else
73 return AnalyticExpr<VectorType>::init_value(
74 v, SubvectorOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
75 }
76
77 void bwd_eval(ValuesMap& v) const
78 {
79 SubvectorOp::bwd(AnalyticExpr<VectorType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i, _j);
80 std::get<0>(this->_x)->bwd_eval(v);
81 }
82
83 std::pair<Index,Index> output_shape() const {
84 return SubvectorOp::output_shape(std::get<0>(this->_x),_i,_j);
85 }
86
87 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
88 {
89 return std::get<0>(this->_x)->belongs_to_args_list(args);
90 }
91
92 std::string str(bool in_parentheses = false) const
93 {
94 // todo: improve the following:
95 std::string s = SubvectorOp::str(std::get<0>(this->_x), _i, _j);
96 return in_parentheses ? "(" + s + ")" : s;
97 }
98
99 virtual bool is_str_leaf() const
100 {
101 return false;
102 }
103
104 protected:
105
106 const Index _i, _j;
107 };
108
109 // Inline functions
110
111 inline IntervalVector SubvectorOp::fwd(const IntervalVector& x1, Index i, Index j)
112 {
113 assert(i >= 0 && i < x1.size() && j >= i && j < x1.size());
114 return x1.subvector(i,j);
115 }
116
117 inline VectorType SubvectorOp::fwd_natural(const VectorType& x1, Index i, Index j)
118 {
119 assert(i >= 0 && i < x1.a.rows() && j >= i && j < x1.a.rows());
120 return {
121 fwd(x1.a,i,j),
122 x1.def_domain
123 };
124 }
125
126 inline VectorType SubvectorOp::fwd_centered(const VectorType& x1, Index i, Index j)
127 {
128 if(centered_form_not_available_for_args(x1))
129 return fwd_natural(x1,i,j);
130
131 assert(i >= 0 && i < x1.a.rows() && j >= i && j < x1.a.rows());
132 return {
133 fwd(x1.m,i,j),
134 fwd(x1.a,i,j),
135 x1.da.block(i,0,j-i+1,x1.da.cols()),
136 x1.def_domain
137 };
138 }
139
140 inline void SubvectorOp::bwd(const IntervalVector& y, IntervalVector& x1, Index i, Index j)
141 {
142 assert(i >= 0 && i < x1.size() && j >= i && j < x1.size());
143 assert(j-i < y.size());
144 for(Index k = 0 ; k < j-i+1 ; k++)
145 x1[i+k] &= y[k];
146 }
147}
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