codac 2.0.0
Loading...
Searching...
No Matches
codac2_MatrixBase_addons_IntervalMatrixBase.h
Go to the documentation of this file.
1
20
31template<typename U=Scalar>
32 requires IsIntervalDomain<U>
33inline double volume() const
34{
35 if(this->is_empty())
36 return 0.;
37
38 double v = 0.;
39 for(Index i = 0 ; i < this->rows() ; i++)
40 for(Index j = 0 ; j < this->cols() ; j++)
41 {
42 if((*this)(i,j).is_unbounded()) return codac2::oo;
43 if((*this)(i,j).is_degenerated()) return 0.;
44 v += std::log((*this)(i,j).diam());
45 }
46 return std::exp(v);
47}
48
56inline bool is_empty() const
57{
58 for(Index i = 0 ; i < rows() ; i++)
59 for(Index j = 0 ; j < cols() ; j++)
60 if((*this)(i,j).is_empty())
61 return true;
62 return false;
63}
64
69#define degenerate_mat(op) \
70 Matrix<double,RowsAtCompileTime,ColsAtCompileTime> m(this->rows(),this->cols()); \
71 \
72 if(this->is_empty()) \
73 m.init(std::numeric_limits<double>::quiet_NaN()); \
74 \
75 else \
76 { \
77 for(Index i = 0 ; i < this->rows() ; i++) \
78 for(Index j = 0 ; j < this->cols() ; j++) \
79 m(i,j) = (*this)(i,j).op(); \
80 } \
81 \
82 return m; \
83
84
89template<typename U=Scalar>
90 requires IsIntervalDomain<U>
91inline auto lb() const
92{
94}
95
101template<typename U=Scalar>
102 requires IsIntervalDomain<U>
103inline auto ub() const
104{
106}
107
115template<typename U=Scalar>
116 requires IsIntervalDomain<U>
117inline auto mid() const
118{
120}
121
129template<typename U=Scalar>
130 requires IsIntervalDomain<U>
131inline auto mag() const
132{
134}
135
141template<typename U=Scalar>
142 requires IsIntervalDomain<U>
143inline auto mig() const
144{
146}
147
155template<typename U=Scalar>
156 requires IsIntervalDomain<U>
157inline auto smag() const
158{
160}
161
167template<typename U=Scalar>
168 requires IsIntervalDomain<U>
169inline auto smig() const
170{
172}
173
181template<typename U=Scalar>
182 requires IsIntervalDomain<U>
183inline auto rand() const
184{
186}
187
195template<typename U=Scalar>
196 requires IsIntervalDomain<U>
197inline auto rad() const
198{
200}
201
209template<typename U=Scalar>
210 requires IsIntervalDomain<U>
211inline auto diam() const
212{
214}
215
221inline double min_rad() const
222 requires IsIntervalDomain<Scalar>
223{
224 return coeff(this->extr_diam_index(true)).rad();
225}
226
232inline double max_rad() const
233 requires IsIntervalDomain<Scalar>
234{
235 return coeff(this->extr_diam_index(false)).rad();
236}
237
243inline double min_diam() const
244 requires IsIntervalDomain<Scalar>
245{
246 return coeff(this->extr_diam_index(true)).diam();
247}
248
254inline double max_diam() const
255 requires IsIntervalDomain<Scalar>
256{
257 return coeff(this->extr_diam_index(false)).diam();
258}
259
265inline Index min_diam_index() const
266 requires IsIntervalDomain<Scalar>
267{
268 return this->extr_diam_index(true);
269}
270
276inline Index max_diam_index() const
277 requires IsIntervalDomain<Scalar>
278{
279 return this->extr_diam_index(false);
280}
281
298inline Index extr_diam_index(bool min) const
299 requires IsIntervalDomain<Scalar>
300{
301 // This code originates from the ibex-lib
302 // See: ibex_TemplateVector.h
303 // Author: Gilles Chabert
304
305 double d = min ? codac2::oo : -1; // -1 to be sure that even a 0-diameter interval can be selected
306 int selected_index = -1;
307 bool unbounded = false;
308 assert_release(!this->is_empty() && "Diameter of an empty IntervalVector is undefined");
309
310 Index i;
311
312 for(i = 0 ; i < this->size() ; i++)
313 {
314 if(coeff(i).is_unbounded())
315 {
316 unbounded = true;
317 if(!min) break;
318 }
319 else
320 {
321 double w = coeff(i).diam();
322 if(min ? w<d : w>d)
323 {
324 selected_index = i;
325 d = w;
326 }
327 }
328 }
329
330 if(min && selected_index == -1)
331 {
332 assert(unbounded);
333 // the selected interval is the first one.
334 i = 0;
335 }
336
337 // The unbounded intervals are not considered if we look for the minimal diameter
338 // and some bounded intervals have been found (selected_index!=-1)
339 if(unbounded && (!min || selected_index == -1))
340 {
341 double pt = min ? -codac2::oo : codac2::oo; // keep the point less/most distant from +oo (we normalize if the lower bound is -oo)
342 selected_index = i;
343
344 for(; i < this->size() ; i++)
345 {
346 if(coeff(i).lb() == -codac2::oo)
347 {
348 if(coeff(i).ub() == codac2::oo)
349 if(!min)
350 {
351 selected_index = i;
352 break;
353 }
354
355 if((min && (-coeff(i).ub() > pt)) || (!min && (-coeff(i).ub() < pt)))
356 {
357 selected_index = i;
358 pt = -coeff(i).ub();
359 }
360 }
361
362 else if(coeff(i).ub() == codac2::oo)
363 if((min && (coeff(i).lb() > pt)) || (!min && (coeff(i).lb() < pt)))
364 {
365 selected_index = i;
366 pt = coeff(i).lb();
367 }
368 }
369 }
370
371 return selected_index;
372}
373
383{
384 return _contains(x);
385}
386
393template<typename OtherDerived>
394inline bool contains(const MatrixBase<OtherDerived>& x) const
395{
396 return _contains(x);
397}
398
402template<typename T>
403inline bool _contains(const T& x) const
404{
405 assert_release(x.size() == this->size());
406
407 if(this->is_empty())
408 return false;
409
410 for(Index i = 0 ; i < this->rows() ; i++)
411 for(Index j = 0 ; j < this->cols() ; j++)
412 if(!(*this)(i,j).contains(x(i,j)))
413 return false;
414
415 return true;
416}
417
430
437template<typename OtherDerived>
438inline bool interior_contains(const MatrixBase<OtherDerived>& x) const
439{
440 return _interior_contains(x);
441}
442
446template<typename T>
447inline bool _interior_contains(const T& x) const
448{
449 assert_release(x.size() == this->size());
450
451 if(this->is_empty())
452 return false;
453
454 for(Index i = 0 ; i < this->rows() ; i++)
455 for(Index j = 0 ; j < this->cols() ; j++)
456 if(!(*this)(i,j).interior_contains(x(i,j)))
457 return false;
458
459 return true;
460}
461
467inline bool is_unbounded() const
468{
469 if(this->is_empty()) return false;
470 for(Index i = 0 ; i < this->rows() ; i++)
471 for(Index j = 0 ; j < this->cols() ; j++)
472 if((*this)(i,j).is_unbounded())
473 return true;
474 return false;
475}
476
484inline bool is_degenerated() const
485{
486 for(Index i = 0 ; i < this->rows() ; i++)
487 for(Index j = 0 ; j < this->cols() ; j++)
488 if(!(*this)(i,j).is_degenerated())
489 return false;
490 return true;
491}
492
501inline bool is_flat() const
502{
503 if(this->is_empty()) return true;
504 for(Index i = 0 ; i < this->rows() ; i++)
505 for(Index j = 0 ; j < this->cols() ; j++)
506 if((*this)(i,j).is_degenerated()) // don't use diam() because of roundoff
507 return true;
508 return false;
509}
510
523
530template<typename OtherDerived>
531inline bool intersects(const MatrixBase<OtherDerived>& x) const
532{
533 return _intersects(x);
534}
535
539template<typename OtherDerived>
540inline bool _intersects(const MatrixBase<OtherDerived>& x) const
541{
542 assert_release(this->size() == x.size());
543
544 if(this->is_empty())
545 return false;
546
547 for(Index i = 0 ; i < this->rows() ; i++)
548 for(Index j = 0 ; j < this->cols() ; j++)
549 if(!(*this)(i,j).intersects(x(i,j)))
550 return false;
551
552 return true;
553}
554
567
574template<typename OtherDerived>
575inline bool is_disjoint(const MatrixBase<OtherDerived>& x) const
576{
577 return _is_disjoint(x);
578}
579
583template<typename OtherDerived>
584inline bool _is_disjoint(const MatrixBase<OtherDerived>& x) const
585{
586 assert_release(this->size() == x.size());
587
588 if(this->is_empty())
589 return true;
590
591 for(Index i = 0 ; i < this->rows() ; i++)
592 for(Index j = 0 ; j < this->cols() ; j++)
593 if((*this)(i,j).is_disjoint(x(i,j)))
594 return true;
595
596 return false;
597}
598
608{
609 return _overlaps(x);
610}
611
618template<typename OtherDerived>
619inline bool overlaps(const MatrixBase<OtherDerived>& x) const
620{
621 return _overlaps(x);
622}
623
627template<typename OtherDerived>
628inline bool _overlaps(const MatrixBase<OtherDerived>& x) const
629{
630 assert_release(this->size() == x.size());
631
632 if(this->is_empty())
633 return false;
634
635 for(Index i = 0 ; i < this->rows() ; i++)
636 for(Index j = 0 ; j < this->cols() ; j++)
637 if(!(*this)(i,j).overlaps(x(i,j)))
638 return false;
639
640 return true;
641}
642
653{
654 return _is_subset(x);
655}
656
663template<typename OtherDerived>
664inline bool is_subset(const MatrixBase<OtherDerived>& x) const
665{
666 return _is_subset(x);
667}
668
672template<typename T>
673inline bool _is_subset(const T& x) const
674{
675 assert_release(this->size() == x.size());
676
677 if(this->is_empty())
678 return true;
679
680 for(Index i = 0 ; i < this->rows() ; i++)
681 for(Index j = 0 ; j < this->cols() ; j++)
682 if(!(*this)(i,j).is_subset(x(i,j)))
683 return false;
684
685 return true;
686}
687
700
707template<typename OtherDerived>
708inline bool is_strict_subset(const MatrixBase<OtherDerived>& x) const
709{
710 return _is_strict_subset(x);
711}
712
716template<typename T>
717inline bool _is_strict_subset(const T& x) const
718{
719 assert_release(this->size() == x.size());
720
721 if(this->is_empty())
722 return true;
723
724 if(!is_subset(x))
725 return false;
726
727 for(Index i = 0 ; i < this->rows() ; i++)
728 for(Index j = 0 ; j < this->cols() ; j++)
729 if((*this)(i,j).is_strict_subset(x(i,j)))
730 return true;
731
732 return false;
733}
734
747
754template<typename OtherDerived>
755inline bool is_interior_subset(const MatrixBase<OtherDerived>& x) const
756{
757 return _is_interior_subset(x);
758}
759
763template<typename OtherDerived>
764inline bool _is_interior_subset(const MatrixBase<OtherDerived>& x) const
765{
766 assert_release(this->size() == x.size());
767
768 if(this->is_empty())
769 return true;
770
771 for(Index i = 0 ; i < this->rows() ; i++)
772 for(Index j = 0 ; j < this->cols() ; j++)
773 if(!(*this)(i,j).is_interior_subset(x(i,j)))
774 return false;
775
776 return true;
777}
778
792
799template<typename OtherDerived>
800inline bool is_strict_interior_subset(const MatrixBase<OtherDerived>& x) const
801{
803}
804
808template<typename OtherDerived>
809inline bool _is_strict_interior_subset(const MatrixBase<OtherDerived>& x) const
810{
811 assert_release(this->size() == x.size());
812
813 if(this->is_empty())
814 return true;
815
816 for(Index i = 0 ; i < this->rows() ; i++)
817 for(Index j = 0 ; j < this->cols() ; j++)
818 if(!(*this)(i,j).is_strict_interior_subset(x(i,j)))
819 return false;
820
821 return true;
822}
823
836
843template<typename OtherDerived>
844inline bool is_superset(const MatrixBase<OtherDerived>& x) const
845{
846 return _is_superset(x);
847}
848
852template<typename OtherDerived>
853inline bool _is_superset(const MatrixBase<OtherDerived>& x) const
854{
855 assert_release(this->size() == x.size());
856
857 if(this->is_empty())
858 return false;
859
860 for(Index i = 0 ; i < this->rows() ; i++)
861 for(Index j = 0 ; j < this->cols() ; j++)
862 if(!x(i,j).is_subset((*this)(i,j)))
863 return false;
864
865 return true;
866}
867
881
888template<typename OtherDerived>
889inline bool is_strict_superset(const MatrixBase<OtherDerived>& x) const
890{
891 return _is_strict_superset(x);
892}
893
897template<typename OtherDerived>
898inline bool _is_strict_superset(const MatrixBase<OtherDerived>& x) const
899{
900 assert_release(this->size() == x.size());
901
902 if(this->is_empty())
903 return false;
904
905 if(!is_superset(x))
906 return false;
907
908 for(Index i = 0 ; i < this->rows() ; i++)
909 for(Index j = 0 ; j < this->cols() ; j++)
910 if(x(i,j).is_strict_subset((*this)(i,j)))
911 return true;
912
913 return false;
914}
915
923inline bool is_bisectable() const
924{
925 for(Index i = 0 ; i < this->rows() ; i++)
926 for(Index j = 0 ; j < this->cols() ; j++)
927 if((*this)(i,j).is_bisectable())
928 return true;
929 return false;
930}
931
937inline bool has_integer_bounds() const
938{
939 for(Index i = 0 ; i < this->rows() ; i++)
940 for(Index j = 0 ; j < this->cols() ; j++)
941 if(!(*this)(i,j).has_integer_bounds())
942 return false;
943 return true;
944}
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
bool overlaps(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix overlaps with another.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:607
auto lb() const
Returns a matrix containing the lower bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:91
bool is_strict_superset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is a strict superset of another matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:877
auto mid() const
Returns a matrix containing the midpoints of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:117
bool is_degenerated() const
Checks if the interval matrix is degenerated.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:484
bool is_superset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is a superset of another interval matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:832
auto smig() const
Returns a matrix containing the signed mignitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:169
bool is_unbounded() const
Checks if the interval matrix contains any unbounded intervals.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:467
auto mag() const
Returns a matrix containing the magnitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:131
bool _is_subset(const T &x) const
Internal helper for subset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:673
bool is_interior_subset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is an interior subset of another.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:743
bool is_strict_subset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is a strict subset of another matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:696
auto diam() const
Returns a matrix containing the diameters of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:211
bool is_empty() const
Checks whether the interval matrix is empty.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:56
Index max_diam_index() const
Returns the index of the element with the maximum diameter.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:276
bool _is_strict_subset(const T &x) const
Internal helper for strict subset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:717
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:426
double min_diam() const
Returns the minimum diameter among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:243
bool is_subset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is a subset of another interval matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:652
bool _overlaps(const MatrixBase< OtherDerived > &x) const
Internal helper to check overlap.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:628
auto smag() const
Returns a matrix containing the signed magnitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:157
bool is_disjoint(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks if this matrix is disjoint with another matrix of intervals.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:563
auto mig() const
Returns a matrix containing the mignitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:143
bool _is_interior_subset(const MatrixBase< OtherDerived > &x) const
Internal helper for interior subset checking.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:764
double min_rad() const
Returns the minimum radius among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:221
#define degenerate_mat(op)
Helper macro to create a matrix from a specific operation applied to each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:69
bool _interior_contains(const T &x) const
Internal helper function to check interior containment.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:447
bool is_strict_interior_subset(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix is a strict interior subset of another matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:788
bool _is_strict_interior_subset(const MatrixBase< OtherDerived > &x) const
Internal helper for strict interior subset relation.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:809
bool is_flat() const
Checks if the interval matrix is flat.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:501
bool is_bisectable() const
Checks whether at least one interval in the matrix is bisectable.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:923
bool _is_superset(const MatrixBase< OtherDerived > &x) const
Internal helper for superset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:853
auto ub() const
Returns a matrix containing the upper bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:103
auto rand() const
Returns a matrix with random values chosen inside each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:183
bool _contains(const T &x) const
Internal helper function to check containment.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:403
Index extr_diam_index(bool min) const
Returns the index of the element with the minimum or maximum diameter.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:298
bool has_integer_bounds() const
Checks whether all intervals in the matrix have integer lower and upper bounds.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:937
bool contains(const Matrix< double, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks if this interval matrix contains the specified matrix x.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:382
auto rad() const
Returns a matrix containing the radii of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:197
bool _intersects(const MatrixBase< OtherDerived > &x) const
Internal helper that performs intersection checking.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:540
Index min_diam_index() const
Returns the index of the element with the minimum diameter.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:265
double max_diam() const
Returns the maximum diameter among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:254
double volume() const
Computes the volume of the interval matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:33
bool _is_disjoint(const MatrixBase< OtherDerived > &x) const
Internal helper for disjointness checking.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:584
double max_rad() const
Returns the maximum radius among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:232
bool intersects(const Matrix< codac2::Interval, RowsAtCompileTime, ColsAtCompileTime > &x) const
Checks whether this matrix intersects with another matrix of intervals.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:519
bool _is_strict_superset(const MatrixBase< OtherDerived > &x) const
Internal helper for strict superset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:898