codac 1.5.6
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 static IntervalVector fwd(const IntervalVector& x1, Index i, Index j);
23 static VectorType fwd_natural(const VectorType& x1, Index i, Index j);
24 static VectorType fwd_centered(const VectorType& x1, Index i, Index j);
25 static void bwd(const IntervalVector& y, IntervalVector& x1, Index i, Index j);
26 };
27
28 // Analytic operator
29
30 template<>
31 class AnalyticOperationExpr<SubvectorOp,VectorType,VectorType> : public AnalyticExpr<VectorType>, public OperationExprBase<AnalyticExpr<VectorType>>
32 {
33 public:
34
35 AnalyticOperationExpr(const std::shared_ptr<AnalyticExpr<VectorType>>& x1, Index i, Index j)
36 : OperationExprBase<AnalyticExpr<VectorType>>(x1), _i(i), _j(j)
37 { }
38
39 AnalyticOperationExpr(const AnalyticOperationExpr& e)
40 : OperationExprBase<AnalyticExpr<VectorType>>(e), _i(e._i), _j(e._j)
41 { }
42
43 std::shared_ptr<ExprBase> copy() const
44 {
45 return std::make_shared<AnalyticOperationExpr<SubvectorOp,VectorType,VectorType>>(*this);
46 }
47
48 void replace_expr(const ExprID& old_expr_id, const std::shared_ptr<ExprBase>& new_expr)
49 {
50 return OperationExprBase<AnalyticExpr<VectorType>>::replace_expr(old_expr_id, new_expr);
51 }
52
53 VectorType fwd_eval(ValuesMap& v, Index total_input_size, bool natural_eval) const
54 {
55 if(natural_eval)
56 return AnalyticExpr<VectorType>::init_value(
57 v, SubvectorOp::fwd_natural(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
58 else
59 return AnalyticExpr<VectorType>::init_value(
60 v, SubvectorOp::fwd_centered(std::get<0>(this->_x)->fwd_eval(v, total_input_size, natural_eval), _i, _j));
61 }
62
63 void bwd_eval(ValuesMap& v) const
64 {
65 SubvectorOp::bwd(AnalyticExpr<VectorType>::value(v).a, std::get<0>(this->_x)->value(v).a, _i, _j);
66 std::get<0>(this->_x)->bwd_eval(v);
67 }
68
69 virtual bool belongs_to_args_list(const FunctionArgsList& args) const
70 {
71 return std::get<0>(this->_x)->belongs_to_args_list(args);
72 }
73
74 protected:
75
76 const Index _i, _j;
77 };
78
79 // Inline functions
80
81 inline IntervalVector SubvectorOp::fwd(const IntervalVector& x1, Index i, Index j)
82 {
83 assert(i >= 0 && i < x1.size() && j >= i && j < x1.size());
84 return x1.subvector(i,j);
85 }
86
87 inline VectorType SubvectorOp::fwd_natural(const VectorType& x1, Index i, Index j)
88 {
89 assert(i >= 0 && i < x1.a.rows() && j >= i && j < x1.a.rows());
90 return {
91 fwd(x1.a,i,j),
92 x1.def_domain
93 };
94 }
95
96 inline VectorType SubvectorOp::fwd_centered(const VectorType& x1, Index i, Index j)
97 {
98 if(centered_form_not_available_for_args(x1))
99 return fwd_natural(x1,i,j);
100
101 assert(i >= 0 && i < x1.a.rows() && j >= i && j < x1.a.rows());
102 return {
103 fwd(x1.m,i,j),
104 fwd(x1.a,i,j),
105 x1.da.block(i,0,j-i+1,x1.da.cols()),
106 x1.def_domain
107 };
108 }
109
110 inline void SubvectorOp::bwd(const IntervalVector& y, IntervalVector& x1, Index i, Index j)
111 {
112 assert(i >= 0 && i < x1.size() && j >= i && j < x1.size());
113 assert(j-i < y.size());
114 for(Index k = 0 ; k < j-i+1 ; k++)
115 x1[i+k] &= y[k];
116 }
117}