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
222inline double min_rad(const std::vector<Index>& among_indices = {}) const
223 requires IsIntervalDomain<Scalar>
224{
225 return coeff(this->extr_diam_index(true,among_indices)).rad();
226}
227
234inline double max_rad(const std::vector<Index>& among_indices = {}) const
235 requires IsIntervalDomain<Scalar>
236{
237 return coeff(this->extr_diam_index(false,among_indices)).rad();
238}
239
246inline double min_diam(const std::vector<Index>& among_indices = {}) const
247 requires IsIntervalDomain<Scalar>
248{
249 return coeff(this->extr_diam_index(true,among_indices)).diam();
250}
251
258inline double max_diam(const std::vector<Index>& among_indices = {}) const
259 requires IsIntervalDomain<Scalar>
260{
261 return coeff(this->extr_diam_index(false,among_indices)).diam();
262}
263
270inline Index min_diam_index(const std::vector<Index>& among_indices = {}) const
271 requires IsIntervalDomain<Scalar>
272{
273 return this->extr_diam_index(true,among_indices);
274}
275
282inline Index max_diam_index(const std::vector<Index>& among_indices = {}) const
283 requires IsIntervalDomain<Scalar>
284{
285 return this->extr_diam_index(false,among_indices);
286}
287
305inline Index extr_diam_index(bool min, const std::vector<Index>& among_indices = {}) const
306 requires IsIntervalDomain<Scalar>
307{
308 assert_release(
309 among_indices.empty() ||
310 std::ranges::all_of(among_indices, [&](Index k){ return 0 <= k && k < this->size(); })
311 );
312
313 // This code originates from the ibex-lib
314 // See: ibex_TemplateVector.h
315 // Author: Gilles Chabert
316
317 double d = min ? codac2::oo : -1; // -1 to be sure that even a 0-diameter interval can be selected
318 int selected_index = -1;
319 bool unbounded = false;
320 assert_release(!this->is_empty() && "Diameter of an empty IntervalVector is undefined");
321
322 Index i;
323
324 for(i = 0 ; i < this->size() ; i++)
325 {
326 if(!among_indices.empty()
327 && std::find(among_indices.begin(),among_indices.end(),i) == among_indices.end())
328 continue; // avoiding this index
329
330 if(coeff(i).is_unbounded())
331 {
332 unbounded = true;
333 if(!min) break;
334 }
335 else
336 {
337 double w = coeff(i).diam();
338 if(min ? w<d : w>d)
339 {
340 selected_index = i;
341 d = w;
342 }
343 }
344 }
345
346 if(min && selected_index == -1)
347 {
348 assert(unbounded);
349
350 if(among_indices.empty())
351 {
352 // the selected interval is the first one.
353 i = 0;
354 }
355 else
356 i = among_indices[0];
357 }
358
359 // The unbounded intervals are not considered if we look for the minimal diameter
360 // and some bounded intervals have been found (selected_index!=-1)
361 if(unbounded && (!min || selected_index == -1))
362 {
363 double pt = min ? -codac2::oo : codac2::oo; // keep the point less/most distant from +oo (we normalize if the lower bound is -oo)
364 selected_index = i;
365
366 for(; i < this->size() ; i++)
367 {
368 if(!among_indices.empty()
369 && std::find(among_indices.begin(),among_indices.end(),i) == among_indices.end())
370 continue; // avoiding this index
371
372 if(coeff(i).lb() == -codac2::oo)
373 {
374 if(coeff(i).ub() == codac2::oo)
375 if(!min)
376 {
377 selected_index = i;
378 break;
379 }
380
381 if((min && (-coeff(i).ub() > pt)) || (!min && (-coeff(i).ub() < pt)))
382 {
383 selected_index = i;
384 pt = -coeff(i).ub();
385 }
386 }
387
388 else if(coeff(i).ub() == codac2::oo)
389 if((min && (coeff(i).lb() > pt)) || (!min && (coeff(i).lb() < pt)))
390 {
391 selected_index = i;
392 pt = coeff(i).lb();
393 }
394 }
395 }
396
397 return selected_index;
398}
399
409{
410 return _contains(x);
411}
412
419template<typename OtherDerived>
420inline bool contains(const MatrixBase<OtherDerived>& x) const
421{
422 return _contains(x);
423}
424
428template<typename T>
429inline bool _contains(const T& x) const
430{
431 assert_release(x.size() == this->size());
432
433 if(this->is_empty())
434 return false;
435
436 for(Index i = 0 ; i < this->rows() ; i++)
437 for(Index j = 0 ; j < this->cols() ; j++)
438 if(!(*this)(i,j).contains(x(i,j)))
439 return false;
440
441 return true;
442}
443
456
463template<typename OtherDerived>
464inline bool interior_contains(const MatrixBase<OtherDerived>& x) const
465{
466 return _interior_contains(x);
467}
468
472template<typename T>
473inline bool _interior_contains(const T& x) const
474{
475 assert_release(x.size() == this->size());
476
477 if(this->is_empty())
478 return false;
479
480 for(Index i = 0 ; i < this->rows() ; i++)
481 for(Index j = 0 ; j < this->cols() ; j++)
482 if(!(*this)(i,j).interior_contains(x(i,j)))
483 return false;
484
485 return true;
486}
487
493inline bool is_unbounded() const
494{
495 if(this->is_empty()) return false;
496 for(Index i = 0 ; i < this->rows() ; i++)
497 for(Index j = 0 ; j < this->cols() ; j++)
498 if((*this)(i,j).is_unbounded())
499 return true;
500 return false;
501}
502
510inline bool is_degenerated() const
511{
512 for(Index i = 0 ; i < this->rows() ; i++)
513 for(Index j = 0 ; j < this->cols() ; j++)
514 if(!(*this)(i,j).is_degenerated())
515 return false;
516 return true;
517}
518
527inline bool is_flat() const
528{
529 if(this->is_empty()) return true;
530 for(Index i = 0 ; i < this->rows() ; i++)
531 for(Index j = 0 ; j < this->cols() ; j++)
532 if((*this)(i,j).is_degenerated()) // don't use diam() because of roundoff
533 return true;
534 return false;
535}
536
549
556template<typename OtherDerived>
557inline bool intersects(const MatrixBase<OtherDerived>& x) const
558{
559 return _intersects(x);
560}
561
565template<typename OtherDerived>
566inline bool _intersects(const MatrixBase<OtherDerived>& x) const
567{
568 assert_release(this->size() == x.size());
569
570 if(this->is_empty())
571 return false;
572
573 for(Index i = 0 ; i < this->rows() ; i++)
574 for(Index j = 0 ; j < this->cols() ; j++)
575 if(!(*this)(i,j).intersects(x(i,j)))
576 return false;
577
578 return true;
579}
580
593
600template<typename OtherDerived>
601inline bool is_disjoint(const MatrixBase<OtherDerived>& x) const
602{
603 return _is_disjoint(x);
604}
605
609template<typename OtherDerived>
610inline bool _is_disjoint(const MatrixBase<OtherDerived>& x) const
611{
612 assert_release(this->size() == x.size());
613
614 if(this->is_empty())
615 return true;
616
617 for(Index i = 0 ; i < this->rows() ; i++)
618 for(Index j = 0 ; j < this->cols() ; j++)
619 if((*this)(i,j).is_disjoint(x(i,j)))
620 return true;
621
622 return false;
623}
624
634{
635 return _overlaps(x);
636}
637
644template<typename OtherDerived>
645inline bool overlaps(const MatrixBase<OtherDerived>& x) const
646{
647 return _overlaps(x);
648}
649
653template<typename OtherDerived>
654inline bool _overlaps(const MatrixBase<OtherDerived>& x) const
655{
656 assert_release(this->size() == x.size());
657
658 if(this->is_empty())
659 return false;
660
661 for(Index i = 0 ; i < this->rows() ; i++)
662 for(Index j = 0 ; j < this->cols() ; j++)
663 if(!(*this)(i,j).overlaps(x(i,j)))
664 return false;
665
666 return true;
667}
668
679{
680 return _is_subset(x);
681}
682
689template<typename OtherDerived>
690inline bool is_subset(const MatrixBase<OtherDerived>& x) const
691{
692 return _is_subset(x);
693}
694
698template<typename T>
699inline bool _is_subset(const T& x) const
700{
701 assert_release(this->size() == x.size());
702
703 if(this->is_empty())
704 return true;
705
706 for(Index i = 0 ; i < this->rows() ; i++)
707 for(Index j = 0 ; j < this->cols() ; j++)
708 if(!(*this)(i,j).is_subset(x(i,j)))
709 return false;
710
711 return true;
712}
713
726
733template<typename OtherDerived>
734inline bool is_strict_subset(const MatrixBase<OtherDerived>& x) const
735{
736 return _is_strict_subset(x);
737}
738
742template<typename T>
743inline bool _is_strict_subset(const T& x) const
744{
745 assert_release(this->size() == x.size());
746
747 if(this->is_empty())
748 return true;
749
750 if(!is_subset(x))
751 return false;
752
753 for(Index i = 0 ; i < this->rows() ; i++)
754 for(Index j = 0 ; j < this->cols() ; j++)
755 if((*this)(i,j).is_strict_subset(x(i,j)))
756 return true;
757
758 return false;
759}
760
773
780template<typename OtherDerived>
781inline bool is_interior_subset(const MatrixBase<OtherDerived>& x) const
782{
783 return _is_interior_subset(x);
784}
785
789template<typename OtherDerived>
790inline bool _is_interior_subset(const MatrixBase<OtherDerived>& x) const
791{
792 assert_release(this->size() == x.size());
793
794 if(this->is_empty())
795 return true;
796
797 for(Index i = 0 ; i < this->rows() ; i++)
798 for(Index j = 0 ; j < this->cols() ; j++)
799 if(!(*this)(i,j).is_interior_subset(x(i,j)))
800 return false;
801
802 return true;
803}
804
818
825template<typename OtherDerived>
826inline bool is_strict_interior_subset(const MatrixBase<OtherDerived>& x) const
827{
829}
830
834template<typename OtherDerived>
835inline bool _is_strict_interior_subset(const MatrixBase<OtherDerived>& x) const
836{
837 assert_release(this->size() == x.size());
838
839 if(this->is_empty())
840 return true;
841
842 for(Index i = 0 ; i < this->rows() ; i++)
843 for(Index j = 0 ; j < this->cols() ; j++)
844 if(!(*this)(i,j).is_strict_interior_subset(x(i,j)))
845 return false;
846
847 return true;
848}
849
862
869template<typename OtherDerived>
870inline bool is_superset(const MatrixBase<OtherDerived>& x) const
871{
872 return _is_superset(x);
873}
874
878template<typename OtherDerived>
879inline bool _is_superset(const MatrixBase<OtherDerived>& x) const
880{
881 assert_release(this->size() == x.size());
882
883 if(this->is_empty())
884 return false;
885
886 for(Index i = 0 ; i < this->rows() ; i++)
887 for(Index j = 0 ; j < this->cols() ; j++)
888 if(!x(i,j).is_subset((*this)(i,j)))
889 return false;
890
891 return true;
892}
893
907
914template<typename OtherDerived>
915inline bool is_strict_superset(const MatrixBase<OtherDerived>& x) const
916{
917 return _is_strict_superset(x);
918}
919
923template<typename OtherDerived>
924inline bool _is_strict_superset(const MatrixBase<OtherDerived>& x) const
925{
926 assert_release(this->size() == x.size());
927
928 if(this->is_empty())
929 return false;
930
931 if(!is_superset(x))
932 return false;
933
934 for(Index i = 0 ; i < this->rows() ; i++)
935 for(Index j = 0 ; j < this->cols() ; j++)
936 if(x(i,j).is_strict_subset((*this)(i,j)))
937 return true;
938
939 return false;
940}
941
949inline bool is_bisectable() const
950{
951 for(Index i = 0 ; i < this->rows() ; i++)
952 for(Index j = 0 ; j < this->cols() ; j++)
953 if((*this)(i,j).is_bisectable())
954 return true;
955 return false;
956}
957
963inline bool has_integer_bounds() const
964{
965 for(Index i = 0 ; i < this->rows() ; i++)
966 for(Index j = 0 ; j < this->cols() ; j++)
967 if(!(*this)(i,j).has_integer_bounds())
968 return false;
969 return true;
970}
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:633
double volume() const
Computes the volume of the interval matrix.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:33
double min_diam(const std::vector< Index > &among_indices={}) const
Returns the minimum diameter among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:246
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:903
bool is_degenerated() const
Checks if the interval matrix is degenerated.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:510
auto diam() const
Returns a matrix containing the diameters of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:211
double max_rad(const std::vector< Index > &among_indices={}) const
Returns the maximum radius among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:234
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:858
Index extr_diam_index(bool min, const std::vector< Index > &among_indices={}) const
Returns the index of the element with the minimum or maximum diameter.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:305
bool is_unbounded() const
Checks if the interval matrix contains any unbounded intervals.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:493
bool _is_subset(const T &x) const
Internal helper for subset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:699
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:769
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:722
double max_diam(const std::vector< Index > &among_indices={}) const
Returns the maximum diameter among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:258
bool is_empty() const
Checks whether the interval matrix is empty.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:56
bool _is_strict_subset(const T &x) const
Internal helper for strict subset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:743
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:452
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:678
bool _overlaps(const MatrixBase< OtherDerived > &x) const
Internal helper to check overlap.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:654
auto mig() const
Returns a matrix containing the mignitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:143
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:589
auto mid() const
Returns a matrix containing the midpoints of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:117
Index min_diam_index(const std::vector< Index > &among_indices={}) const
Returns the index of the element with the minimum diameter.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:270
bool _is_interior_subset(const MatrixBase< OtherDerived > &x) const
Internal helper for interior subset checking.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:790
#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:473
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:814
bool _is_strict_interior_subset(const MatrixBase< OtherDerived > &x) const
Internal helper for strict interior subset relation.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:835
bool is_flat() const
Checks if the interval matrix is flat.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:527
bool is_bisectable() const
Checks whether at least one interval in the matrix is bisectable.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:949
bool _is_superset(const MatrixBase< OtherDerived > &x) const
Internal helper for superset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:879
auto rand() const
Returns a matrix with random values chosen inside each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:183
auto mag() const
Returns a matrix containing the magnitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:131
bool _contains(const T &x) const
Internal helper function to check containment.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:429
Index max_diam_index(const std::vector< Index > &among_indices={}) const
Returns the index of the element with the maximum diameter.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:282
bool has_integer_bounds() const
Checks whether all intervals in the matrix have integer lower and upper bounds.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:963
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:408
auto ub() const
Returns a matrix containing the upper bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:103
bool _intersects(const MatrixBase< OtherDerived > &x) const
Internal helper that performs intersection checking.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:566
auto lb() const
Returns a matrix containing the lower bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:91
auto smag() const
Returns a matrix containing the signed magnitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:157
double min_rad(const std::vector< Index > &among_indices={}) const
Returns the minimum radius among the interval elements.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:222
bool _is_disjoint(const MatrixBase< OtherDerived > &x) const
Internal helper for disjointness checking.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:610
auto rad() const
Returns a matrix containing the radii of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:197
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:545
auto smig() const
Returns a matrix containing the signed mignitudes of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:169
bool _is_strict_superset(const MatrixBase< OtherDerived > &x) const
Internal helper for strict superset check.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:924
Interval min(const Interval &x, const Interval &y)
Returns .
Definition codac2_Interval_operations_impl.h:269