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 <numeric>
13#include "codac2_Ctc.h"
14#include "codac2_TimePropag.h"
16#include "codac2_TDomain.h"
17
18namespace codac2
19{
20 template<class T>
21 class Slice;
22
23 template<typename T>
24 class SlicedTube;
25
26 class CtcDeriv
27 {
28 public:
29
30 CtcDeriv(const TimePropag& time_propag = TimePropag::FWD_BWD, bool fast_mode = true);
31
32 void restrict_tdomain(const Interval& tdomain)
33 {
34 _tdomain = tdomain;
35 }
36
37 template<typename T>
38 inline void contract(Slice<T>& x, const Slice<T>& v, const std::vector<Index>& ctc_indices = {}) const
39 requires std::is_same_v<T,Interval> || std::is_same_v<T,IntervalVector>
40 {
41 assert(x.size() == v.size());
42 assert(x.t0_tf() == v.t0_tf());
43
44 T input = x.input_gate();
45 T output = x.output_gate();
46 T envelope = x.codomain();
47
48 if constexpr(std::is_same_v<T,Interval>)
49 contract(x.t0_tf(), envelope, input, output, v.codomain(), _time_propag, _fast_mode);
50
51 else if constexpr(std::is_same_v<T,IntervalVector>)
52 {
53 std::vector<Index> ctc_indices_(ctc_indices);
54 if(ctc_indices_.empty())
55 {
56 // Then all the dimensions will be contracted
57 ctc_indices_ = std::vector<Index>(x.size());
58 std::iota(ctc_indices_.begin(), ctc_indices_.end(), 0);
59 }
60
61 for(auto i : ctc_indices_)
62 contract(x.t0_tf(), envelope[i], input[i], output[i], v.codomain()[i], _time_propag, _fast_mode);
63 }
64
65 auto x_next = x.next_slice();
66 if(x_next && x_next->is_gate())
67 x_next->set(output, false);
68
69 auto x_prev = x.prev_slice();
70 if(x_prev && x_prev->is_gate())
71 x_prev->set(input, false);
72
73 x.set(envelope, false);
74 }
75
76 template<typename T>
77 inline void contract(SlicedTube<T>& x, const SlicedTube<T>& v, const std::vector<Index>& ctc_indices = {}) const
78 requires std::is_same_v<T,Interval> || std::is_same_v<T,IntervalVector>
79 {
80 assert_release(TDomain::are_same(x.tdomain(), v.tdomain()));
81 Interval t = x.tdomain()->t0_tf() & _tdomain;
82 auto it_beg = x.tdomain()->sample(t.lb(),true);
83 auto it_end = x.tdomain()->sample(t.ub(),true);
84
85 if((_time_propag & TimePropag::FWD) == TimePropag::FWD)
86 {
87 for(auto it = it_beg ; it != std::next(it_end) ; it++)
88 {
89 auto sx = x.slice(it);
90 if(!sx->is_gate())
91 this->contract(*sx, *v.slice(it), ctc_indices);
92 }
93 }
94
95 if((_time_propag & TimePropag::BWD) == TimePropag::BWD)
96 {
97 for(auto it = it_end ; it != std::prev(it_beg) ; it--)
98 {
99 auto sx = x.slice(it);
100 if(!sx->is_gate())
101 this->contract(*sx, *v.slice(it), ctc_indices);
102 }
103 }
104 }
105
106 static ConvexPolygon polygon_slice(
107 const Interval& t, const Interval& envelope,
108 const Interval& input, const Interval& proj_input,
109 const Interval& output, const Interval& proj_output,
110 const Interval& v);
111
112 protected:
113
114 static void contract(
115 const Interval& t, Interval& envelope,
116 Interval& input, Interval& output,
117 const Interval& v, const TimePropag& time_propag, bool fast_mode);
118
119 const TimePropag _time_propag;
120 const bool _fast_mode;
121 Interval _tdomain;
122 };
123}
Definition codac2_OctaSym.h:21
TimePropag
Enumeration specifying the temporal propagation way (forward or backward in time).
Definition codac2_TimePropag.h:23