codac 2.0.0
Loading...
Searching...
No Matches
codac2_Matrix_addons_IntervalMatrixBase.h
Go to the documentation of this file.
1
20
21// does not work.. template<typename OtherDerived1,typename OtherDerived2,typename U=Scalar,int R=RowsAtCompileTime,int C=ColsAtCompileTime>
22// does not work.. requires (
23// does not work.. IsIntervalDomain<U>
24// does not work.. && (!IsIntervalDomain<typename OtherDerived1::Scalar>)
25// does not work.. && (!IsIntervalDomain<typename OtherDerived2::Scalar>)
26// does not work.. && OtherDerived1::RowsAtCompileTime == R
27// does not work.. && OtherDerived2::RowsAtCompileTime == R
28// does not work.. && OtherDerived1::ColsAtCompileTime == C
29// does not work.. && OtherDerived2::ColsAtCompileTime == C)
30// does not work.. Matrix(MatrixBase<OtherDerived1> lb, MatrixBase<OtherDerived2> ub)
31// does not work.. : Matrix<U,R,C>(lb)
32
48template<int R=RowsAtCompileTime,int C=ColsAtCompileTime>
49 requires IsIntervalDomain<Scalar>
51 : Matrix<Scalar,R,C>(lb)
52{
53 assert_release(lb.size() == ub.size());
54
55 for(Index i = 0 ; i < this->size() ; i++)
56 {
57 auto& lbi = *(this->data()+i);
58 const auto& ubi = *(ub.data()+i);
59
60 if(lbi.lb() > ubi)
61 {
62 set_empty();
63 break;
64 }
65
66 else
67 lbi |= ubi;
68 }
69}
70
89template<int R=RowsAtCompileTime,int C=ColsAtCompileTime>
90 requires IsIntervalDomain<Scalar>
91Matrix(int r, int c, const double bounds[][2])
92 : Matrix<Scalar,R,C>(r,c)
93{
94 assert_release(r > 0 && c > 0);
95
96 Index k = 0;
97 for(Index i = 0 ; i < this->rows() ; i++)
98 for(Index j = 0 ; j < this->cols() ; j++)
99 {
100 (*this)(i,j) = codac2::Interval(bounds[k][0],bounds[k][1]);
101 k++;
102 }
103 assert_release(k == this->size() && "incorrect array size");
104}
105
113inline auto& init()
114{
115 for(Index i = 0 ; i < this->size() ; i++)
116 (this->data()+i)->init();
117 return *this;
118}
119
129template<int R=RowsAtCompileTime,int C=ColsAtCompileTime,typename OtherDerived>
130 requires IsIntervalDomain<Scalar>
131inline bool operator==(const MatrixBase<OtherDerived>& x) const
132{
133 return operator==(x.eval().template cast<codac2::Interval>());
134}
135
141inline double min_rad() const
142 requires IsIntervalDomain<Scalar>
143{
144 return (this->data()+extr_diam_index(true))->rad();
145}
146
152inline double max_rad() const
153 requires IsIntervalDomain<Scalar>
154{
155 return (this->data()+extr_diam_index(false))->rad();
156}
157
163inline double min_diam() const
164 requires IsIntervalDomain<Scalar>
165{
166 return (this->data()+extr_diam_index(true))->diam();
167}
168
174inline double max_diam() const
175 requires IsIntervalDomain<Scalar>
176{
177 return (this->data()+extr_diam_index(false))->diam();
178}
179
185inline Index min_diam_index() const
186 requires IsIntervalDomain<Scalar>
187{
188 return extr_diam_index(true);
189}
190
196inline Index max_diam_index() const
197 requires IsIntervalDomain<Scalar>
198{
199 return extr_diam_index(false);
200}
201
218inline Index extr_diam_index(bool min) const
219 requires IsIntervalDomain<Scalar>
220{
221 // This code originates from the ibex-lib
222 // See: ibex_TemplateVector.h
223 // Author: Gilles Chabert
224
225 double d = min ? codac2::oo : -1; // -1 to be sure that even a 0-diameter interval can be selected
226 int selected_index = -1;
227 bool unbounded = false;
228 assert_release(!this->is_empty() && "Diameter of an empty IntervalVector is undefined");
229
230 Index i;
231
232 for(i = 0 ; i < this->size() ; i++)
233 {
234 if((this->data()+i)->is_unbounded())
235 {
236 unbounded = true;
237 if(!min) break;
238 }
239 else
240 {
241 double w = (this->data()+i)->diam();
242 if(min ? w<d : w>d)
243 {
244 selected_index = i;
245 d = w;
246 }
247 }
248 }
249
250 if(min && selected_index == -1)
251 {
252 assert(unbounded);
253 // the selected interval is the first one.
254 i = 0;
255 }
256
257 // The unbounded intervals are not considered if we look for the minimal diameter
258 // and some bounded intervals have been found (selected_index!=-1)
259 if(unbounded && (!min || selected_index == -1))
260 {
261 double pt = min ? -codac2::oo : codac2::oo; // keep the point less/most distant from +oo (we normalize if the lower bound is -oo)
262 selected_index = i;
263
264 for(; i < this->size() ; i++)
265 {
266 if((this->data()+i)->lb() == -codac2::oo)
267 {
268 if((this->data()+i)->ub() == codac2::oo)
269 if(!min)
270 {
271 selected_index = i;
272 break;
273 }
274
275 if((min && (-(this->data()+i)->ub() > pt)) || (!min && (-(this->data()+i)->ub() < pt)))
276 {
277 selected_index = i;
278 pt = -(this->data()+i)->ub();
279 }
280 }
281
282 else if((this->data()+i)->ub() == codac2::oo)
283 if((min && ((this->data()+i)->lb() > pt)) || (!min && ((this->data()+i)->lb() < pt)))
284 {
285 selected_index = i;
286 pt = (this->data()+i)->lb();
287 }
288 }
289 }
290
291 return selected_index;
292}
293
299inline void set_empty()
300 requires IsIntervalDomain<Scalar>
301{
303}
304
315inline auto& inflate(double r)
316 requires IsIntervalDomain<Scalar>
317{
318 assert_release(r >= 0.);
319
320 for(Index i = 0 ; i < this->size() ; i++)
321 (this->data()+i)->inflate(r);
322 return *this;
323}
324
336template<typename OtherDerived>
337 requires IsIntervalDomain<Scalar>
338inline auto& inflate(const MatrixBase<OtherDerived>& r)
339{
340 assert_release(this->size() == r.size());
341 assert_release(r.min_coeff() >= 0.);
342
343 for(Index i = 0 ; i < this->rows() ; i++)
344 for(Index j = 0 ; j < this->cols() ; j++)
345 (*this)(i,j).inflate(r(i,j));
346 return *this;
347}
348
362template<typename OtherDerived>
363inline auto& operator&=(const MatrixBase<OtherDerived>& x)
364{
365 assert_release(this->size() == x.size());
366
367 if constexpr(std::is_same_v<typename MatrixBase<OtherDerived>::Scalar,codac2::Interval>)
368 {
369 if(x.is_empty())
370 {
371 set_empty();
372 return *this;
373 }
374 }
375
376 for(Index i = 0 ; i < this->rows() ; i++)
377 for(Index j = 0 ; j < this->cols() ; j++)
378 (*this)(i,j) &= x(i,j);
379 return *this;
380}
381
395template<typename OtherDerived>
396inline auto& operator|=(const MatrixBase<OtherDerived>& x)
397{
398 assert_release(this->size() == x.size());
399
400 if constexpr(std::is_same_v<typename MatrixBase<OtherDerived>::Scalar,codac2::Interval>)
401 {
402 if(x.is_empty())
403 return *this;
404 }
405
406 for(Index i = 0 ; i < this->rows() ; i++)
407 for(Index j = 0 ; j < this->cols() ; j++)
408 (*this)(i,j) |= x(i,j);
409 return *this;
410}
411
421template<typename OtherDerived>
422inline auto operator&(const MatrixBase<OtherDerived>& x) const
423{
424 auto y = *this;
425 return y &= x;
426}
427
437template<typename OtherDerived>
438inline auto operator|(const MatrixBase<OtherDerived>& x) const
439{
440 auto y = *this;
441 return y |= x.template cast<codac2::Interval>();
442}
443
451template<int R=RowsAtCompileTime,int C=ColsAtCompileTime>
452 requires IsIntervalDomain<Scalar>
453inline static auto empty(Index r, Index c)
454{
455 assert_release(r >= 0 && c >= 0);
457}
458
471template<int R=RowsAtCompileTime,int C=ColsAtCompileTime>
472 requires IsIntervalDomain<Scalar>
473inline auto bisect(Index i, float ratio = 0.49) const
474{
475 assert_release(i >= 0 && i < this->size());
476 assert_release((this->data()+i)->is_bisectable());
477 assert_release(codac2::Interval(0,1).interior_contains(ratio));
478
479 auto p = std::make_pair(*this,*this);
480 auto pi = (this->data()+i)->bisect(ratio);
481 *(p.first.data()+i) = pi.first;
482 *(p.second.data()+i) = pi.second;
483 return p;
484}
485
495template<int R=RowsAtCompileTime,int C=ColsAtCompileTime>
496 requires IsIntervalDomain<Scalar>
497inline auto bisect_largest(float ratio = 0.49) const
498{
499 return bisect(max_diam_index(), ratio);
500}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:49
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:551
auto & operator&=(const MatrixBase< OtherDerived > &x)
Performs element-wise intersection assignment with another matrix.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:363
auto & inflate(double r)
Inflates all intervals in the matrix by a fixed radius.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:315
bool operator==(const MatrixBase< OtherDerived > &x) const
Compares this interval matrix with another matrix for equality.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:131
Index max_diam_index() const
Returns the index of the element with the maximum diameter.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:196
double min_diam() const
Returns the minimum diameter among the interval elements.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:163
double min_rad() const
Returns the minimum radius among the interval elements.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:141
Index extr_diam_index(bool min) const
Returns the index of the element with the minimum or maximum diameter.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:218
Index min_diam_index() const
Returns the index of the element with the minimum diameter.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:185
double max_diam() const
Returns the maximum diameter among the interval elements.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:174
auto bisect(Index i, float ratio=0.49) const
Bisects the interval at the given index into two sub-interval matrices.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:473
void set_empty()
Marks the interval matrix as empty.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:299
auto bisect_largest(float ratio=0.49) const
Bisects the interval with the largest diameter in the matrix.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:497
double max_rad() const
Returns the maximum radius among the interval elements.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:152
Matrix(const Matrix< double, R, C > &lb, const Matrix< double, R, C > &ub)
Constructs an interval matrix from lower and upper bound matrices.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:50
auto & init()
Initializes all elements of the matrix with default intervals.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:113
auto & operator|=(const MatrixBase< OtherDerived > &x)
Performs element-wise union assignment with another matrix.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:396
auto operator&(const MatrixBase< OtherDerived > &x) const
Returns the element-wise intersection of this matrix with another.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:422
auto operator|(const MatrixBase< OtherDerived > &x) const
Returns the element-wise union of this matrix with another.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:438
Matrix()=delete
Deleted default constructor to prevent default instantiation when either the number of rows or column...
auto lb() const
Returns a matrix containing the lower bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:91
bool is_unbounded() const
Checks if the interval matrix contains any unbounded intervals.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:283
auto diam() const
Returns a matrix containing the diameters of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:185
bool is_empty() const
Checks whether the interval matrix is empty.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:56
bool interior_contains(const Matrix< double, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks if the interior of this interval matrix contains the specified matrix x.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:242
bool is_bisectable() const
Checks whether at least one interval in the matrix is bisectable.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:739
auto ub() const
Returns a matrix containing the upper bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:103