codac 2.0.0
Loading...
Searching...
No Matches
codac2_SlicedTube_integral_impl.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <cmath>
13#include <tuple>
14#include <utility>
15#include "codac2_Interval.h"
16
17namespace codac2
18{
19 template<typename T>
21 {
22 auto partial_integ = partial_integral(t);
23
24 if(partial_integ.first.is_empty() || partial_integ.second.is_empty())
25 return this->empty_value();
26
27 else if(partial_integ.first.is_unbounded() || partial_integ.second.is_unbounded())
28 return this->all_reals_value();
29
30 else
31 return T(partial_integ.first.lb()) | partial_integ.second.ub();
32 }
33
34 template<typename T>
35 T SlicedTube<T>::integral(const Interval& t1, const Interval& t2) const
36 {
37 if(t1.is_empty() || t2.is_empty())
38 return this->empty_value();
39 if(!t1.is_subset(this->t0_tf()) || !t2.is_subset(this->t0_tf()))
40 return this->all_reals_value();
41
42 if(t1.is_degenerated() && t2.is_degenerated())
43 {
44 auto p = this->all_reals_value();
45 p.init(Interval(0.));
46 if(t1 == t2)
47 return p;
48
49 // Integrate only the slices between the two dates
50 Interval t = t1 | t2;
51 for(auto it = _tdomain->tslice(t.lb()) ; it != _tdomain->end() && it->lb() < t.ub() ; ++it)
52 {
53 if(it->is_gate())
54 continue;
55 Interval ti = t & it->t0_tf();
56 if(ti.is_empty() || ti.is_degenerated())
57 continue;
58 const auto& x = this->slice(it)->codomain();
59 if(x.is_empty())
60 return this->empty_value();
61 Interval dt = Interval(ti.ub()) - Interval(ti.lb());
62 p += dt * x;
63 }
64
65 return t1.lb() < t2.lb() ? p : T(-p);
66 }
67
68 auto p = partial_integral(t1,t2);
69 if(p.first.is_empty() || p.second.is_empty())
70 return this->empty_value();
71 if(p.first.is_unbounded() || p.second.is_unbounded())
72 return this->all_reals_value();
73
74 if(t1.ub() <= t2.lb())
75 return T(p.first.lb()) | p.second.ub();
76 if(t2.ub() <= t1.lb())
77 return T(p.second.lb()) | p.first.ub();
78
79 return p.first | p.second;
80 }
81
82 template<typename T>
83 std::pair<T,T> SlicedTube<T>::partial_integral(const Interval& t) const
84 {
85 if(t.is_empty())
86 return { this->empty_value(), this->empty_value() };
87 if(!t.is_subset(this->t0_tf()) || t.is_unbounded() || !std::isfinite(this->t0_tf().lb()))
88 return { this->all_reals_value(), this->all_reals_value() };
89
90 auto zero = this->all_reals_value();
91 zero.init(Interval(0.));
92 if(t == Interval(this->t0_tf().lb()))
93 return { zero, zero };
94
95 std::pair<T,T> p { zero, zero };
96 std::pair<T,T> result { this->empty_value(), this->empty_value() };
97
98 for(const auto& s : *this)
99 {
100 if(s.t0_tf().lb() >= t.ub())
101 break;
102 if(s.is_gate())
103 continue;
104
105 const auto& x = s.codomain();
106 if(x.is_empty())
107 return { this->empty_value(), this->empty_value() };
108 if(x.is_unbounded())
109 return { this->all_reals_value(), this->all_reals_value() };
110
111 // Evaluate the affine partial primitives on the queried part of the slice
112 Interval ti = s.t0_tf() & t;
113 if(!ti.is_empty())
114 {
115 Interval dt = ti - Interval(s.t0_tf().lb());
116 result.first |= p.first + dt * T(x.lb());
117 result.second |= p.second + dt * T(x.ub());
118 }
119
120 if(s.t0_tf().ub() >= t.ub())
121 break;
122
123 Interval dt = Interval(s.t0_tf().ub()) - Interval(s.t0_tf().lb());
124 p.first += dt * T(x.lb());
125 p.second += dt * T(x.ub());
126 }
127
128 return result;
129 }
130
131 template<typename T>
132 std::pair<T,T> SlicedTube<T>::partial_integral(const Interval& t1, const Interval& t2) const
133 {
134 auto integ_t1 = partial_integral(t1);
135 auto integ_t2 = partial_integral(t2);
136 return {
137 integ_t2.first - integ_t1.first,
138 integ_t2.second - integ_t1.second
139 };
140 }
141
142 template<typename T>
143 std::pair<SlicedTube<T>,SlicedTube<T>> SlicedTube<T>::partial_primitives() const
144 {
145 const Interval t = this->t0_tf();
146 assert_release(!t.is_empty() && !t.is_unbounded());
147
148 for(const auto& s : *this)
149 if(!s.is_gate())
150 assert_release(!s.codomain().is_empty() && !s.codomain().is_unbounded());
151 auto zero = this->all_reals_value();
152 zero.init(Interval(0.));
153
154 std::pair<SlicedTube<T>,SlicedTube<T>> primitives(
155 std::piecewise_construct,
156 std::forward_as_tuple(this->tdomain(), zero),
157 std::forward_as_tuple(this->tdomain(), zero));
158
159 std::pair<T,T> p { zero, zero };
160
161 for(auto it = _tdomain->begin(); it != _tdomain->end(); ++it)
162 {
163 auto pm = primitives.first.slice(it);
164 auto pp = primitives.second.slice(it);
165
166 if(it->is_gate())
167 {
168 // Gates have zero duration and do not contribute to the integral.
169 pm->set(p.first, false);
170 pp->set(p.second, false);
171 continue;
172 }
173
174 const auto& x = this->slice(it)->codomain();
175 auto q = p;
176
177 // The interval duration encloses the exact difference of the endpoints.
178 const Interval dt = Interval(it->ub()) - Interval(it->lb());
179 q.first += dt * T(x.lb());
180 q.second += dt * T(x.ub());
181
182 // Each partial primitive is affine on a non-gate slice.
183 // Its range is enclosed by the hull of its endpoint enclosures.
184 pm->set(p.first | q.first, false);
185 pp->set(p.second | q.second, false);
186 p = q;
187 }
188
189 return primitives;
190 }
191
192 template<typename T>
194 const std::pair<SlicedTube<T>,SlicedTube<T>>& primitives) const
195 {
196 auto p = partial_integral(t, primitives);
197 if(p.first.is_empty() || p.second.is_empty())
198 return this->empty_value();
199 if(p.first.is_unbounded() || p.second.is_unbounded())
200 return this->all_reals_value();
201 return T(p.first.lb()) | p.second.ub();
202 }
203
204 template<typename T>
206 const std::pair<SlicedTube<T>,SlicedTube<T>>& primitives) const
207 {
208 auto p = partial_integral(t1, t2, primitives);
209 if(p.first.is_empty() || p.second.is_empty())
210 return this->empty_value();
211 if(p.first.is_unbounded() || p.second.is_unbounded())
212 return this->all_reals_value();
213
214 if(t1.ub() <= t2.lb())
215 return T(p.first.lb()) | p.second.ub();
216 if(t2.ub() <= t1.lb())
217 return T(p.second.lb()) | p.first.ub();
218
219 // Both integration directions are possible when the time intervals overlap.
220 return p.first | p.second;
221 }
222
223 template<typename T>
225 const std::pair<SlicedTube<T>,SlicedTube<T>>& primitives) const
226 {
227 if(t.is_empty())
228 return { this->empty_value(), this->empty_value() };
229 if(!t.is_subset(this->t0_tf()))
230 return { this->all_reals_value(), this->all_reals_value() };
231
232 assert_release(primitives.first.tdomain() == this->tdomain() &&
233 primitives.second.tdomain() == this->tdomain());
234
235 auto it = _tdomain->tslice(t.lb());
236 assert(it != _tdomain->end() && it->contains(t.lb()));
237
238 std::pair<T,T> p { this->empty_value(), this->empty_value() };
239
240 for(; it != _tdomain->cend() && it->lb() <= t.ub(); ++it)
241 {
242 const Interval ti = t & it->t0_tf();
243 const auto pm = primitives.first.slice(it);
244 const auto pp = primitives.second.slice(it);
245 std::pair<T,T> q { pm->codomain(), pp->codomain() };
246
247 if(!it->is_gate())
248 {
249 const auto& x = this->slice(it)->codomain();
250
251 if(!q.first.is_unbounded() && !q.second.is_unbounded() && ti != it->t0_tf())
252 {
253 // Reconstruct the affine primitives on the queried part of the slice.
254 // With explicit gates, the endpoint enclosures are the cached sums.
255 const Interval dt = ti - Interval(it->lb());
256 auto pm0 = pm->input_gate();
257 auto pp0 = pp->input_gate();
258 if(it->lb() == this->t0_tf().lb())
259 {
260 pm0.init(Interval(0.));
261 pp0.init(Interval(0.));
262 }
263 q.first &= pm0 + dt * T(x.lb());
264 q.second &= pp0 + dt * T(x.ub());
265
266 // Also use the right endpoint, notably when gates are implicit.
267 const Interval dtf = Interval(it->ub()) - ti;
268 q.first &= pm->output_gate() - dtf * T(x.lb());
269 q.second &= pp->output_gate() - dtf * T(x.ub());
270 }
271 }
272
273 p.first |= q.first;
274 p.second |= q.second;
275
276 if(it->ub() >= t.ub())
277 break;
278 }
279
280 return p;
281 }
282
283 template<typename T>
284 std::pair<T,T> SlicedTube<T>::partial_integral(const Interval& t1, const Interval& t2,
285 const std::pair<SlicedTube<T>,SlicedTube<T>>& primitives) const
286 {
287 auto p1 = partial_integral(t1, primitives);
288 auto p2 = partial_integral(t2, primitives);
289 return { p2.first - p1.first, p2.second - p1.second };
290 }
291}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:50
bool is_unbounded() const
Tests if one of the bounds of this is infinite.
Definition codac2_Interval_impl.h:236
bool is_empty() const
Tests if this is empty.
Definition codac2_Interval_impl.h:221
double ub() const
Returns the upper bound of this.
Definition codac2_Interval_impl.h:131
bool is_degenerated() const
Tests if this is degenerated, that is, in the form of .
Definition codac2_Interval_impl.h:241
bool is_subset(const Interval &x) const
Tests if this is a subset of x.
Definition codac2_Interval_impl.h:271
double lb() const
Returns the lower bound of this.
Definition codac2_Interval_impl.h:126
SlicedTube(const std::shared_ptr< TDomain > &tdomain, const T &codomain)
Creates a sliced tube with constant codomain over all temporal slices.
Definition codac2_SlicedTube.h:47
T all_reals_value() const
Returns the unbounded codomain value associated with T.
Definition codac2_SlicedTube.h:1213
T empty_value() const
Returns the empty codomain value associated with T.
Definition codac2_SlicedTube.h:1225
std::pair< SlicedTube< T >, SlicedTube< T > > partial_primitives() const
Returns the primitives of the lower and upper bounds of this tube .
Definition codac2_SlicedTube_integral_impl.h:143
std::shared_ptr< Slice< T > > slice(const std::list< TSlice >::iterator &it)
Returns the slice attached to a temporal iterator.
Definition codac2_SlicedTube.h:249
const std::shared_ptr< TDomain > _tdomain
Shared temporal domain of this tube.
Definition codac2_TubeBase.h:71
Interval t0_tf() const
Returns the global temporal interval of this tube.
Definition codac2_TubeBase.h:61
const std::shared_ptr< TDomain > & tdomain() const
Returns the temporal domain of this tube.
Definition codac2_TubeBase.h:49
auto lb() const
Returns a matrix containing the lower bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:91
std::pair< T, T > partial_integral(const Interval &t) const
Returns lower and upper enclosures of the integrals of this tube from to .
Definition codac2_SlicedTube_integral_impl.h:83
T integral(const Interval &t) const
Returns an enclosure of the integrals of this tube from to .
Definition codac2_SlicedTube_integral_impl.h:20
Definition codac2_OctaSym.h:21