codac 2.0.0
Loading...
Searching...
No Matches
codac2_CtcDeriv.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "codac2_Ctc.h"
13#include "codac2_Slice.h"
14#include "codac2_Interval.h"
15
16namespace codac2
17{
18 class CtcDeriv
19 {
20 public:
21
22 CtcDeriv()
23 { }
24
25 inline void contract(Slice<Interval>& x, const Slice<Interval>& v) const
26 {
27 assert_release(x.t0_tf() == v.t0_tf());
28
29 x.set(this->slice_hull({0,x.t0_tf().diam()},
30 x.codomain(),x.input_gate(),x.output_gate(),
31 v.codomain()
32 ), false); // disable propagation to adjacent slices
33 // v remains unchanged
34
35 auto x_next = x.next_slice();
36 if(x_next && x_next->is_gate())
37 {
38 x_next->set(
39 x_next->codomain() &
40 (x.input_gate()+x.t0_tf().diam()*v.codomain()),
41 false); // disable propagation to adjacent slices
42 // v remains unchanged
43 }
44
45 auto x_prev = x.prev_slice();
46 if(x_prev && x_prev->is_gate())
47 {
48 x_prev->set(
49 x_prev->codomain() &
50 (x.output_gate()-x.t0_tf().diam()*v.codomain()),
51 false); // disable propagation to adjacent slices
52 // v remains unchanged
53 }
54 }
55
56 inline void contract(Slice<IntervalVector>& x, const Slice<IntervalVector>& v) const
57 {
58 assert_release(x.size() == v.size());
59 assert_release(x.t0_tf() == v.t0_tf());
60
61 auto x_prev = x.prev_slice();
62 auto x_next = x.next_slice();
63
64 auto hull = x.codomain();
65 auto input_gate = x.input_gate();
66 auto output_gate = x.output_gate();
67
68 for(Index i = 0 ; i < x.size() ; i++)
69 {
70 hull[i] = this->slice_hull({0,x.t0_tf().diam()},
71 x.codomain()[i],x.input_gate()[i],x.output_gate()[i],
72 v.codomain()[i]
73 );
74
75 if(x_next && x_next->is_gate())
76 output_gate[i] &= input_gate[i]+x.t0_tf().diam()*v.codomain()[i];
77
78 if(x_prev && x_prev->is_gate())
79 input_gate[i] &= output_gate[i]-x.t0_tf().diam()*v.codomain()[i];
80 }
81
82 if(x_prev && x_prev->is_gate())
83 x_prev->set(input_gate, false);
84
85 if(x_next && x_next->is_gate())
86 x_next->set(output_gate, false);
87
88 x.set(hull, false); // disable propagation to adjacent slices
89 // v remains unchanged
90 }
91
92 template<typename T>
93 inline void contract(SlicedTube<T>& x, const SlicedTube<T>& v) const
94 {
95 assert_release(x.tdomain()->all_gates_defined() && "not available without gates");
96 assert_release(x.tdomain() == v.tdomain());
97
98 auto xi = x.begin(); auto vi = v.begin();
99 while(xi != x.end())
100 {
101 assert(vi != v.end());
102 if(!xi->is_gate())
103 this->contract(*xi,*vi);
104 xi++; vi++;
105 }
106 assert(vi == v.end());
107
108 auto rxi = x.rbegin(); auto rvi = v.rbegin();
109 while(rxi != x.rend())
110 {
111 assert(rvi != v.rend());
112 if(!rxi->is_gate())
113 this->contract(*rxi,*rvi);
114 rxi++; rvi++;
115 }
116 assert(rvi == v.rend());
117 }
118
119 protected:
120
121 Interval slice_hull(const Interval& dt,
122 const Interval& x, const Interval& input_x, const Interval& output_x,
123 const Interval& v) const
124 {
125 Interval hull = x;
126 hull &= input_x + dt*v;
127 hull &= output_x - dt*v;
128 return hull;
129 }
130 };
131}