sigma  1.0.0
Loading...
Searching...
No Matches
interval.hpp
Go to the documentation of this file.
1#pragma once
2#include <boost/numeric/interval.hpp>
3#include <iostream>
4#include <type_traits>
5
9
10namespace sigma {
11
20template<typename ValueType>
21class Interval {
22public:
24 using value_t = ValueType;
25
32 Interval() : m_interval_(0.0, 0.0) {}
33
42 Interval(value_t value) : m_interval_(value, value) {}
43
52
59 value_t lower() const { return m_interval_.lower(); }
60
67 value_t upper() const { return m_interval_.upper(); }
68
81 bool contains(value_t value) const {
82 return value >= lower() && value <= upper();
83 }
84
91 value_t median() const { return boost::numeric::median(m_interval_); }
92
101 value_t radius() const {
102 return boost::numeric::width(m_interval_) / value_t{2};
103 }
104
105 // -- Arithmetic in-place operators ----------------------------------------
106
115 m_interval_ += rhs.m_interval_;
116 return *this;
117 }
118
127 m_interval_ += rhs;
128 return *this;
129 }
130
139 m_interval_ -= rhs.m_interval_;
140 return *this;
141 }
142
151 m_interval_ -= rhs;
152 return *this;
153 }
154
163 m_interval_ *= rhs.m_interval_;
164 return *this;
165 }
166
175 m_interval_ *= rhs;
176 return *this;
177 }
178
187 m_interval_ /= rhs.m_interval_;
188 return *this;
189 }
190
199 m_interval_ /= rhs;
200 return *this;
201 }
202
203private:
205 boost::numeric::interval<value_t> m_interval_;
206
207}; // class Interval
208
209// -- Utility functions --------------------------------------------------------
210
223template<typename ValueType>
224std::ostream& operator<<(std::ostream& os, const Interval<ValueType>& i) {
225 os << i.median() << "+/-" << i.radius();
226 return os;
227}
228
239template<typename T1, typename T2>
240bool operator==(const Interval<T1>& lhs, const Interval<T2>& rhs) {
241 if constexpr(!std::is_same_v<T1, T2>) return false;
242 return lhs.lower() == rhs.lower() && lhs.upper() == rhs.upper();
243}
244
255template<typename T1, typename T2>
256bool operator!=(const Interval<T1>& lhs, const Interval<T2>& rhs) {
257 return !(lhs == rhs);
258}
259
273template<typename T1, typename T2>
274bool operator<(const Interval<T1>& lhs, const Interval<T2>& rhs) {
275 return lhs.upper() < rhs.lower();
276}
277
288template<typename T1, typename T2>
289bool operator>(const Interval<T1>& lhs, const Interval<T2>& rhs) {
290 return rhs < lhs;
291}
292
303template<typename T1, typename T2>
304bool operator<=(const Interval<T1>& lhs, const Interval<T2>& rhs) {
305 return (lhs == rhs) || (lhs < rhs);
306}
307
318template<typename T1, typename T2>
319bool operator>=(const Interval<T1>& lhs, const Interval<T2>& rhs) {
320 return (lhs == rhs) || (lhs > rhs);
321}
322
323// -- Arithmetic free functions ------------------------------------------------
324
335template<typename T>
337 return Interval<T>(-a.upper(), -a.lower());
338}
339
351template<typename T>
353 return lhs += rhs;
354}
355
357template<typename T>
359 return lhs += rhs;
360}
361
363template<typename T>
365 return rhs + lhs;
366}
367
379template<typename T>
381 return lhs -= rhs;
382}
383
385template<typename T>
387 return lhs -= rhs;
388}
389
391template<typename T>
393 return Interval<T>(lhs, lhs) -= rhs;
394}
395
407template<typename T>
409 return lhs *= rhs;
410}
411
413template<typename T>
415 return lhs *= rhs;
416}
417
419template<typename T>
421 return rhs * lhs;
422}
423
438template<typename T>
440 return lhs /= rhs;
441}
442
444template<typename T>
446 return lhs /= rhs;
447}
448
450template<typename T>
452 return Interval<T>(lhs, lhs) /= rhs;
453}
454
457
460
461} // namespace sigma
Models a numeric interval.
Definition interval.hpp:21
Interval & operator-=(const Interval &rhs)
In-place subtraction of another interval.
Definition interval.hpp:138
value_t median() const
Returns the midpoint of the interval.
Definition interval.hpp:91
Interval< T > operator/(Interval< T > lhs, const Interval< T > &rhs)
Division of two intervals.
Definition interval.hpp:439
bool operator>=(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Whether one interval is greater than or equal to another.
Definition interval.hpp:319
Interval & operator-=(value_t rhs)
In-place subtraction of a scalar.
Definition interval.hpp:150
float value_t
Definition interval.hpp:24
Interval & operator+=(const Interval &rhs)
In-place addition of another interval.
Definition interval.hpp:114
Interval & operator+=(value_t rhs)
In-place addition of a scalar.
Definition interval.hpp:126
value_t upper() const
Definition interval.hpp:67
Interval & operator*=(const Interval &rhs)
In-place multiplication by another interval.
Definition interval.hpp:162
Interval< T > operator-(Interval< T > lhs, const Interval< T > &rhs)
Subtraction of two intervals.
Definition interval.hpp:380
Interval & operator/=(const Interval &rhs)
In-place division by another interval.
Definition interval.hpp:186
value_t lower() const
Definition interval.hpp:59
Interval< T > operator-(const Interval< T > &a)
Negation of an interval.
Definition interval.hpp:336
Interval(value_t lower, value_t upper)
Construct an interval from lower and upper bounds.
Definition interval.hpp:51
Interval & operator/=(value_t rhs)
In-place division by a scalar.
Definition interval.hpp:198
bool contains(value_t value) const
Whether a scalar lies in this interval (endpoints included).
Definition interval.hpp:81
bool operator==(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Compare two intervals for equality.
Definition interval.hpp:240
std::ostream & operator<<(std::ostream &os, const Interval< ValueType > &i)
Overload stream insertion to print an interval.
Definition interval.hpp:224
Interval()
Default constructor.
Definition interval.hpp:32
value_t radius() const
Returns the half-width of the interval.
Definition interval.hpp:101
Interval< T > operator*(Interval< T > lhs, const Interval< T > &rhs)
Multiplication of two intervals.
Definition interval.hpp:408
bool operator<(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Whether one interval is certainly less than another.
Definition interval.hpp:274
Interval & operator*=(value_t rhs)
In-place multiplication by a scalar.
Definition interval.hpp:174
bool operator<=(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Whether one interval is less than or equal to another.
Definition interval.hpp:304
Interval< T > operator+(Interval< T > lhs, const Interval< T > &rhs)
Addition of two intervals.
Definition interval.hpp:352
bool operator>(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Whether one interval is greater than another.
Definition interval.hpp:289
Interval(value_t value)
Construct an interval from a single value.
Definition interval.hpp:42
bool operator!=(const Interval< T1 > &lhs, const Interval< T2 > &rhs)
Compare two intervals for inequality.
Definition interval.hpp:256
The primary namespace for the sigma library.
Definition operation_common.hpp:10
Interval< T > operator/(Interval< T > lhs, T rhs)
Definition interval.hpp:445
Interval< float > IFloat
Typedef for an interval of floats.
Definition interval.hpp:456
Interval< T > operator-(Interval< T > lhs, T rhs)
Definition interval.hpp:386
Interval< T > operator*(Interval< T > lhs, T rhs)
Definition interval.hpp:414
Interval< T > operator+(Interval< T > lhs, T rhs)
Definition interval.hpp:358
Interval< double > IDouble
Typedef for an interval of doubles.
Definition interval.hpp:459