4#include <boost/numeric/interval.hpp>
28template<
typename ValueType>
75 return boost::numeric::width(*m_interval_);
86 return boost::numeric::median(*m_interval_);
108 bool empty()
const {
return !m_interval_; }
118 return m_interval_->lower();
129 return m_interval_->upper();
145 return !
empty() && std::isfinite(value) &&
lower() <= value &&
159 if(other.
empty()) {
return true; }
292 void assert_not_empty_()
const {
293 if(
empty()) {
throw std::domain_error(
"Interval is empty"); }
301 std::optional<interval_t> m_interval_;
307template<
typename ValueType>
310 if(
empty()) {
return other; }
311 if(other.
empty()) {
return *
this; }
313 throw std::domain_error(
"Intervals do not overlap");
319template<
typename ValueType>
330template<
typename ValueType>
334 }
else if(rhs.
empty()) {
337 m_interval_ = *m_interval_ + *rhs.m_interval_;
342template<
typename ValueType>
426 m_interval_ = *m_interval_ * *rhs.m_interval_;
430template<
typename ValueType>
434 throw std::domain_error(
"Division by zero");
436 m_interval_ = *m_interval_ / *rhs.m_interval_;
440template<
typename ValueType>
442 if(
empty()) {
return "[∅]"; }
443 return "[" + std::to_string(
lower()) +
", " + std::to_string(
upper()) +
"]";
460template<
typename ValueType>
480template<
typename T1,
typename T2>
482 if constexpr(!std::is_same_v<T1, T2>)
return false;
483 if(lhs.
empty() && rhs.
empty()) {
return true; }
484 if(lhs.
empty() || rhs.
empty()) {
return false; }
498template<
typename T1,
typename T2>
500 return !(lhs == rhs);
527 if(rhs.
empty()) {
return false; }
528 if(lhs.
empty()) {
return true; }
685 if(rhs == 0) {
throw std::domain_error(
"Division by zero"); }
692 if(rhs.
empty()) {
return rhs; }
693 if(rhs.
contains(0)) {
throw std::domain_error(
"Division by zero"); }
708template<
typename ValueType>
709struct hash<
sigma::Interval<ValueType>> {
714 if(i.
empty()) {
return 0; }
715 std::size_t hash_low = std::hash<ValueType>()(i.
lower());
716 std::size_t hash_high = std::hash<ValueType>()(i.
upper());
717 std::size_t seed = hash_low;
718 seed ^= hash_high + 0x9e3779b9 + (seed << 6) + (seed >> 2);
Models a closed numeric interval.
Definition interval.hpp:29
Interval & operator-=(const Interval &rhs)
In-place subtraction of another interval.
Definition interval.hpp:231
Interval set_intersection(const Interval &other) const
Returns the intersection of this interval and another interval.
Definition interval.hpp:320
value_t median() const
Returns the midpoint of the interval.
Definition interval.hpp:84
value_t width() const
Returns the distance between the interval's bounds.
Definition interval.hpp:73
Interval< T > operator/(Interval< T > lhs, const Interval< T > &rhs)
Division of two intervals.
Definition interval.hpp:678
Interval & operator-=(value_t rhs)
In-place subtraction of a scalar.
Definition interval.hpp:240
value_t value_t
Definition interval.hpp:32
Interval & operator+=(value_t rhs)
In-place addition of a scalar.
Definition interval.hpp:222
Interval< T > widen(const Interval< T > &a, typename Interval< T >::value_t point)
Widens an interval just enough to contain a point.
Definition interval.hpp:570
value_t upper() const
Definition interval.hpp:127
Interval< T > operator-(Interval< T > lhs, const Interval< T > &rhs)
Subtraction of two intervals.
Definition interval.hpp:619
std::string print_interval_form() const
Print the interval in interval form.
Definition interval.hpp:441
value_t lower() const
Definition interval.hpp:116
Interval(value_t lower, value_t upper)
Construct an interval from two bounds.
Definition interval.hpp:60
Interval & operator/=(value_t rhs)
In-place division by a scalar.
Definition interval.hpp:277
Interval set_union(const Interval &other) const
Returns the union of this interval and another interval.
Definition interval.hpp:308
bool contains(value_t value) const
Whether a scalar lies in this interval.
Definition interval.hpp:144
Interval & operator*=(const Interval &rhs)
In-place multiplication by another interval.
Definition interval.hpp:343
bool operator==(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Compare two intervals for equality.
Definition interval.hpp:481
std::ostream & operator<<(std::ostream &os, const Interval< ValueType > &i)
Overload stream insertion to print an interval.
Definition interval.hpp:461
Interval()
Default constructor.
Definition interval.hpp:40
value_t radius() const
Returns the half-width of the interval.
Definition interval.hpp:98
Interval operator-() const
Negation of an interval.
Definition interval.hpp:201
bool operator>=(const Interval< T > &lhs, const Interval< T > &rhs)
Same ordering as operator<, negated and reversed.
Definition interval.hpp:550
Interval & operator/=(const Interval &rhs)
In-place division by another interval.
Definition interval.hpp:431
bool empty() const
Is *this the empty interval?
Definition interval.hpp:108
Interval & operator+=(const Interval &rhs)
In-place addition of another interval.
Definition interval.hpp:331
bool contains(const Interval &other) const
Is other fully contained in this interval?
Definition interval.hpp:158
Interval< T > operator*(Interval< T > lhs, const Interval< T > &rhs)
Multiplication of two intervals.
Definition interval.hpp:647
bool operator<=(const Interval< T > &lhs, const Interval< T > &rhs)
Same ordering as operator<, negated.
Definition interval.hpp:543
bool operator<(const Interval< T > &lhs, const Interval< T > &rhs)
Orders two intervals, primarily by their median.
Definition interval.hpp:526
Interval & operator*=(value_t rhs)
In-place multiplication by a scalar.
Definition interval.hpp:258
bool operator>(const Interval< T > &lhs, const Interval< T > &rhs)
Same ordering as operator<, with the operands reversed.
Definition interval.hpp:536
Interval< T > operator+(Interval< T > lhs, const Interval< T > &rhs)
Addition of two intervals.
Definition interval.hpp:591
Interval(value_t value)
Construct an interval from a single value.
Definition interval.hpp:50
bool operator!=(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Compare two intervals for inequality.
Definition interval.hpp:499
Components for compatibility with Eigen.
Convenience header for interval operations.
boost::numeric::interval< T, interval_policies_t< T > > boost_interval_t
The boost interval type sigma::Interval wraps.
Definition policies.hpp:485
The primary namespace for the sigma library.
Definition affine.hpp:12
Interval< T > operator/(Interval< T > lhs, T rhs)
Definition interval.hpp:684
Interval< float > IFloat
Typedef for an interval of floats.
Definition interval.hpp:698
Interval< T > operator-(Interval< T > lhs, T rhs)
Definition interval.hpp:625
Interval< T > operator*(Interval< T > lhs, T rhs)
Definition interval.hpp:653
Interval< T > operator+(Interval< T > lhs, T rhs)
Definition interval.hpp:597
Interval< double > IDouble
Typedef for an interval of doubles.
Definition interval.hpp:701
Rounding and checking policies used by the boost interval sigma wraps.
size_t operator()(const sigma::Interval< ValueType > &i) const
Definition interval.hpp:713