codac 2.0.0
Loading...
Searching...
No Matches
codac2_Matrix_addons_IntervalMatrixBase.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
 Matrix (const Matrix< double, R, C > &lb, const Matrix< double, R, C > &ub)
 Constructs an interval matrix from lower and upper bound matrices.
 
template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
 Matrix (int r, int c, const double bounds[][2])
 Constructs an interval matrix from a 2D array of bounds.
 
auto & init ()
 Initializes all elements of the matrix with default intervals.
 
template<int R = RowsAtCompileTime, int C = ColsAtCompileTime, typename OtherDerived>
requires IsIntervalDomain<Scalar>
bool operator== (const MatrixBase< OtherDerived > &x) const
 Compares this interval matrix with another matrix for equality.
 
double min_rad () const
 Returns the minimum radius among the interval elements.
 
double max_rad () const
 Returns the maximum radius among the interval elements.
 
double min_diam () const
 Returns the minimum diameter among the interval elements.
 
double max_diam () const
 Returns the maximum diameter among the interval elements.
 
Index min_diam_index () const
 Returns the index of the element with the minimum diameter.
 
Index max_diam_index () const
 Returns the index of the element with the maximum diameter.
 
Index extr_diam_index (bool min) const
 Returns the index of the element with the minimum or maximum diameter.
 
void set_empty ()
 Marks the interval matrix as empty.
 
auto & inflate (double r)
 Inflates all intervals in the matrix by a fixed radius.
 
template<typename OtherDerived>
requires IsIntervalDomain<Scalar>
auto & inflate (const MatrixBase< OtherDerived > &r)
 Inflates each interval in the matrix by corresponding values from another matrix.
 
template<typename OtherDerived>
auto & operator&= (const MatrixBase< OtherDerived > &x)
 Performs element-wise intersection assignment with another matrix.
 
template<typename OtherDerived>
auto & operator|= (const MatrixBase< OtherDerived > &x)
 Performs element-wise union assignment with another matrix.
 
template<typename OtherDerived>
auto operator& (const MatrixBase< OtherDerived > &x) const
 Returns the element-wise intersection of this matrix with another.
 
template<typename OtherDerived>
auto operator| (const MatrixBase< OtherDerived > &x) const
 Returns the element-wise union of this matrix with another.
 
template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
auto bisect (Index i, float ratio=0.49) const
 Bisects the interval at the given index into two sub-interval matrices.
 
template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
auto bisect_largest (float ratio=0.49) const
 Bisects the interval with the largest diameter in the matrix.
 

Detailed Description

This class reuses some of the functions developed for ibex::IntervalMatrixBase. The original IBEX code is revised in modern C++ and adapted to the template structure proposed in Codac, based on the Eigen library. See ibex::IntervalMatrixBase (IBEX lib, author: Gilles Chabert)

This file is included in the declaration of Eigen::MatrixBase, thanks to the preprocessor token EIGEN_MATRIX_PLUGIN. See: https://eigen.tuxfamily.org/dox/TopicCustomizing_Plugins.html and the file codac2_matrices.h


Date
2024
Author
Simon Rohou, Gilles Chabert
License: GNU Lesser General Public License (LGPL)

Function Documentation

◆ Matrix() [1/2]

template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
Matrix ( const Matrix< double, R, C > & lb,
const Matrix< double, R, C > & ub )

Constructs an interval matrix from lower and upper bound matrices.

Initializes the interval matrix with the given lower bound matrix lb and upper bound matrix ub.

Each element of the resulting interval matrix is constructed from corresponding elements in lb and ub. If any lower bound element is greater than its corresponding upper bound element, the matrix is set to empty.

Parameters
lbMatrix of lower bounds.
ubMatrix of upper bounds.
Precondition
lb and ub must have the same size.
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}
void set_empty()
Marks the interval matrix as empty.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:299
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
auto ub() const
Returns a matrix containing the upper bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:103

◆ Matrix() [2/2]

template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
Matrix ( int r,
int c,
const double bounds[][2] )

Constructs an interval matrix from a 2D array of bounds.

Initializes the matrix with dimensions (r,c) where each element is set to an interval constructed from the corresponding pair of lower and upper bounds in the bounds array.

Parameters
rNumber of rows in the matrix.
cNumber of columns in the matrix.
bounds2D array of size [r*c][2], where each sub-array contains the lower and upper bound for one element.
Precondition
r and c must be positive.
The bounds array size must exactly match the total number of elements.
Postcondition
Each matrix element (i,j) is initialized to codac2::Interval(bounds[k][0], bounds[k][1]) with k mapping row-major order index.
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}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:49

◆ init()

auto & init ( )
inline

Initializes all elements of the matrix with default intervals.

Sets every element in the matrix to a default-constructed codac2::Interval.

Returns
Reference to the current matrix object (enables method chaining).
114{
115 for(Index i = 0 ; i < this->size() ; i++)
116 (this->data()+i)->init();
117 return *this;
118}

◆ operator==()

template<int R = RowsAtCompileTime, int C = ColsAtCompileTime, typename OtherDerived>
requires IsIntervalDomain<Scalar>
bool operator== ( const MatrixBase< OtherDerived > & x) const
inline

Compares this interval matrix with another matrix for equality.

Evaluates and casts the other matrix to an interval matrix, then performs an element-wise equality comparison.

Parameters
xThe other matrix (of arbitrary derived type) to compare.
Returns
true if the matrices are equal after casting; false otherwise.
132{
133 return operator==(x.eval().template cast<codac2::Interval>());
134}
bool operator==(const MatrixBase< OtherDerived > &x) const
Compares this interval matrix with another matrix for equality.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:131

◆ min_rad()

double min_rad ( ) const
inline

Returns the minimum radius among the interval elements.

Returns
The smallest radius value in the matrix.
143{
144 return (this->data()+extr_diam_index(true))->rad();
145}
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

◆ max_rad()

double max_rad ( ) const
inline

Returns the maximum radius among the interval elements.

Returns
The largest radius value in the matrix.
154{
155 return (this->data()+extr_diam_index(false))->rad();
156}

◆ min_diam()

double min_diam ( ) const
inline

Returns the minimum diameter among the interval elements.

Returns
The smallest diameter value in the matrix.
165{
166 return (this->data()+extr_diam_index(true))->diam();
167}

◆ max_diam()

double max_diam ( ) const
inline

Returns the maximum diameter among the interval elements.

Returns
The largest diameter value in the matrix.
176{
177 return (this->data()+extr_diam_index(false))->diam();
178}

◆ min_diam_index()

Index min_diam_index ( ) const
inline

Returns the index of the element with the minimum diameter.

Returns
The index of the matrix element with the smallest diameter.
187{
188 return extr_diam_index(true);
189}

◆ max_diam_index()

Index max_diam_index ( ) const
inline

Returns the index of the element with the maximum diameter.

Returns
The index of the matrix element with the largest diameter.
198{
199 return extr_diam_index(false);
200}

◆ extr_diam_index()

Index extr_diam_index ( bool min) const
inline

Returns the index of the element with the minimum or maximum diameter.

Searches the matrix elements for the interval with either the smallest or largest diameter, depending on the min flag.

This method handles unbounded intervals specially:

  • If looking for minimum diameter, unbounded intervals are ignored unless no bounded intervals exist.
  • If looking for maximum diameter, unbounded intervals are considered.
Parameters
minIf true, returns the index of the element with minimum diameter; otherwise maximum diameter.
Returns
Index of the element with the minimum or maximum diameter.
Precondition
The matrix must not be empty (diameter undefined otherwise).
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}
Interval min(const Interval &x, const Interval &y)
Returns .
Definition codac2_Interval_operations_impl.h:269
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

◆ set_empty()

void set_empty ( )
inline

Marks the interval matrix as empty.

Sets all elements to an empty interval.

301{
303}
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:551
auto & init()
Initializes all elements of the matrix with default intervals.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:113

◆ inflate() [1/2]

auto & inflate ( double r)
inline

Inflates all intervals in the matrix by a fixed radius.

Adds radius r to each interval element, expanding their bounds.

Parameters
rThe non-negative radius by which to inflate each interval.
Returns
Reference to the current matrix object (for chaining).
Precondition
r must be greater than or equal to zero.
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}

◆ inflate() [2/2]

template<typename OtherDerived>
requires IsIntervalDomain<Scalar>
auto & inflate ( const MatrixBase< OtherDerived > & r)
inline

Inflates each interval in the matrix by corresponding values from another matrix.

Inflates each element by the radius specified at the same position in matrix r.

Parameters
rMatrix containing non-negative inflation radii.
Returns
Reference to the current matrix object (for chaining).
Precondition
The size of this matrix and r must be the same.
All elements in r must be greater than or equal to zero.
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}
auto & inflate(double r)
Inflates all intervals in the matrix by a fixed radius.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:315

◆ operator&=()

template<typename OtherDerived>
auto & operator&= ( const MatrixBase< OtherDerived > & x)
inline

Performs element-wise intersection assignment with another matrix.

Updates each element of this matrix by intersecting it with the corresponding element of matrix x.

If x is empty, this matrix is set empty.

Parameters
xThe matrix to intersect with.
Returns
Reference to this matrix after intersection.
Precondition
The size of this matrix and x must be the same.
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}

◆ operator|=()

template<typename OtherDerived>
auto & operator|= ( const MatrixBase< OtherDerived > & x)
inline

Performs element-wise union assignment with another matrix.

Updates each element of this matrix by taking the union with the corresponding element of matrix x.

If x is empty, this matrix remains unchanged.

Parameters
xThe matrix to union with.
Returns
Reference to this matrix after union.
Precondition
The size of this matrix and x must be the same.
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}

◆ operator&()

template<typename OtherDerived>
auto operator& ( const MatrixBase< OtherDerived > & x) const
inline

Returns the element-wise intersection of this matrix with another.

Creates a copy of this matrix and performs element-wise intersection assignment with matrix x, then returns the result.

Parameters
xThe matrix to intersect with.
Returns
A new matrix representing the intersection.
423{
424 auto y = *this;
425 return y &= x;
426}

◆ operator|()

template<typename OtherDerived>
auto operator| ( const MatrixBase< OtherDerived > & x) const
inline

Returns the element-wise union of this matrix with another.

Creates a copy of this matrix and performs element-wise union assignment with the interval-casted matrix x, then returns the result.

Parameters
xThe matrix to union with.
Returns
A new matrix representing the union.
439{
440 auto y = *this;
441 return y |= x.template cast<codac2::Interval>();
442}

◆ bisect()

template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
auto bisect ( Index i,
float ratio = 0.49 ) const
inline

Bisects the interval at the given index into two sub-interval matrices.

Splits the interval at element index i into two parts according to the ratio and returns a pair of matrices having the two resulting sub-intervals.

Parameters
iIndex of the element to bisect (must be within valid range).
ratioRatio to determine the split point within the interval (default 0.49, must lie in [0,1]).
Returns
A pair of matrices resulting from the bisection.
Precondition
The element at index i must be bisectable.
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}
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
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

◆ bisect_largest()

template<int R = RowsAtCompileTime, int C = ColsAtCompileTime>
requires IsIntervalDomain<Scalar>
auto bisect_largest ( float ratio = 0.49) const
inline

Bisects the interval with the largest diameter in the matrix.

Uses max_diam_index() to find the element with the largest diameter and bisects it using the given ratio.

Parameters
ratioRatio to determine the split point within the interval (default 0.49).
Returns
A pair of matrices resulting from the bisection of the largest diameter interval.
498{
499 return bisect(max_diam_index(), ratio);
500}
Index max_diam_index() const
Returns the index of the element with the maximum diameter.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:196