3#include <boost/numeric/interval.hpp>
22template<
typename ValueType>
73 return boost::numeric::width(*m_interval_);
84 return boost::numeric::median(*m_interval_);
106 bool empty()
const {
return !m_interval_; }
148 return m_interval_->lower();
159 return m_interval_->upper();
310 void assert_not_empty_()
const {
311 if(
empty()) {
throw std::domain_error(
"Interval is empty"); }
315 using interval_t = boost::numeric::interval<value_t>;
318 bool m_is_left_open_ =
false;
320 bool m_is_right_open_ =
false;
323 std::optional<interval_t> m_interval_;
329template<
typename ValueType>
339template<
typename ValueType>
341 if(
empty()) {
return false; }
344 if(value <
lower() || value >
upper()) {
return false; }
355template<
typename ValueType>
357 if(other.
empty()) {
return true; }
358 if(
empty()) {
return false; }
381template<
typename ValueType>
384 if(
empty()) {
return other; }
385 if(other.
empty()) {
return *
this; }
387 throw std::domain_error(
"Intervals do not overlap");
391 bool lower_is_open =
false;
392 bool upper_is_open =
false;
412 return Interval(low, hi, lower_is_open, upper_is_open);
415template<
typename ValueType>
446 return Interval(low, hi, lower_is_open, upper_is_open);
449template<
typename ValueType>
453 }
else if(rhs.
empty()) {
456 m_interval_ = *m_interval_ + *rhs.m_interval_;
463template<
typename ValueType>
548 }
else if(
width() == 0) {
550 auto ll = l * rhs.
lower();
551 auto lh = l * rhs.
upper();
552 value_t low = std::min(ll, lh);
553 value_t high = std::max(ll, lh);
560 return *
this =
Interval(low, high, lower_is_open, upper_is_open);
561 }
else if(rhs.
width() == 0) {
563 return *
this = (rhs * (*this));
569 auto low = std::min(std::min(ll, lh), std::min(hl, hh));
570 auto high = std::max(std::max(ll, lh), std::max(hl, hh));
576 }
else if(low == hl) {
578 }
else if(low == hh) {
585 }
else if(high == hl) {
587 }
else if(high == ll) {
591 return *
this =
Interval(low, high, lower_is_open, upper_is_open);
594template<
typename ValueType>
600 return *
this *=
value_t(1.0) / rhs;
603template<
typename ValueType>
605 if(
empty()) {
return "[∅]"; }
606 std::string left_open_str =
left_open() ?
"(" :
"[";
607 std::string right_open_str =
right_open() ?
")" :
"]";
609 return left_open_str + std::to_string(
lower()) +
", " +
610 std::to_string(
upper()) + right_open_str;
627template<
typename ValueType>
647template<
typename T1,
typename T2>
649 if constexpr(!std::is_same_v<T1, T2>)
return false;
650 if(lhs.
empty() && rhs.
empty()) {
return true; }
651 if(lhs.
empty() || rhs.
empty()) {
return false; }
667template<
typename T1,
typename T2>
669 return !(lhs == rhs);
780 if(rhs == 0) {
throw std::domain_error(
"Division by zero"); }
781 return lhs *= T(1.0 / rhs);
787 if(rhs.
empty()) {
return rhs; }
788 if(rhs.
contains(0)) {
throw std::domain_error(
"Division by zero"); }
804template<
typename ValueType>
805struct hash<
sigma::Interval<ValueType>> {
809 std::size_t hash_low = std::hash<ValueType>()(i.
lower());
810 std::size_t hash_high = std::hash<ValueType>()(i.
upper());
811 std::size_t seed = hash_low;
812 seed ^= hash_high + 0x9e3779b9 + (seed << 6) + (seed >> 2);
Models a numeric interval.
Definition interval.hpp:23
Interval & operator-=(const Interval &rhs)
In-place subtraction of another interval.
Definition interval.hpp:249
Interval set_intersection(const Interval &other) const
Returns the intersection of this interval and another interval.
Definition interval.hpp:416
value_t median() const
Returns the midpoint of the interval.
Definition interval.hpp:82
value_t width() const
Returns the distance between the interval's bounds.
Definition interval.hpp:71
Interval< T > operator/(Interval< T > lhs, const Interval< T > &rhs)
Division of two intervals.
Definition interval.hpp:773
Interval & operator-=(value_t rhs)
In-place subtraction of a scalar.
Definition interval.hpp:258
bool right_closed() const
Is upeer contained in the interval?
Definition interval.hpp:138
value_t value_t
Definition interval.hpp:26
Interval & operator+=(value_t rhs)
In-place addition of a scalar.
Definition interval.hpp:240
value_t upper() const
Definition interval.hpp:157
Interval< T > operator-(Interval< T > lhs, const Interval< T > &rhs)
Subtraction of two intervals.
Definition interval.hpp:714
bool left_open() const
Definition interval.hpp:114
std::string print_interval_form() const
Print the interval in interval form.
Definition interval.hpp:604
value_t lower() const
Definition interval.hpp:146
bool right_open() const
Definition interval.hpp:130
Interval & operator/=(value_t rhs)
In-place division by a scalar.
Definition interval.hpp:294
Interval set_union(const Interval &other) const
Returns the union of this interval and another interval.
Definition interval.hpp:382
bool contains(value_t value) const
Whether a scalar lies in this interval.
Definition interval.hpp:340
Interval & operator*=(const Interval &rhs)
In-place multiplication by another interval.
Definition interval.hpp:464
bool operator==(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Compare two intervals for equality.
Definition interval.hpp:648
bool left_closed() const
Is lower() contained in the interval?
Definition interval.hpp:122
std::ostream & operator<<(std::ostream &os, const Interval< ValueType > &i)
Overload stream insertion to print an interval.
Definition interval.hpp:628
Interval()
Default constructor.
Definition interval.hpp:34
value_t radius() const
Returns the half-width of the interval.
Definition interval.hpp:96
Interval operator-() const
Negation of an interval.
Definition interval.hpp:219
Interval & operator/=(const Interval &rhs)
In-place division by another interval.
Definition interval.hpp:595
bool empty() const
Is *this the empty interval?
Definition interval.hpp:106
Interval & operator+=(const Interval &rhs)
In-place addition of another interval.
Definition interval.hpp:450
bool contains(const Interval &other) const
Is other fully contained in this interval?
Definition interval.hpp:356
Interval< T > operator*(Interval< T > lhs, const Interval< T > &rhs)
Multiplication of two intervals.
Definition interval.hpp:742
Interval & operator*=(value_t rhs)
In-place multiplication by a scalar.
Definition interval.hpp:276
Interval< T > operator+(Interval< T > lhs, const Interval< T > &rhs)
Addition of two intervals.
Definition interval.hpp:686
Interval(value_t value)
Construct an interval from a single value.
Definition interval.hpp:44
Interval(value_t lower, value_t upper, bool left_open=false, bool right_open=false)
Construct an interval from two bounds.
Definition interval.hpp:330
bool operator!=(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Compare two intervals for inequality.
Definition interval.hpp:668
Components for compatibility with Eigen.
Convenience header for interval operations.
The primary namespace for the sigma library.
Definition affine.hpp:12
Interval< T > operator/(Interval< T > lhs, T rhs)
Definition interval.hpp:779
Interval< float > IFloat
Typedef for an interval of floats.
Definition interval.hpp:794
Interval< T > operator-(Interval< T > lhs, T rhs)
Definition interval.hpp:720
Interval< T > operator*(Interval< T > lhs, T rhs)
Definition interval.hpp:748
Interval< T > operator+(Interval< T > lhs, T rhs)
Definition interval.hpp:692
Interval< double > IDouble
Typedef for an interval of doubles.
Definition interval.hpp:797
size_t operator()(const sigma::Interval< ValueType > &i) const
Definition interval.hpp:808