sigma  1.0.0
Loading...
Searching...
No Matches
triplex.hpp
Go to the documentation of this file.
1#pragma once
6#include <cmath>
7#include <functional>
8#include <iostream>
9#include <stdexcept>
10#include <tuple>
11#include <type_traits>
12#include <utility>
13
17
18namespace sigma {
19
41template<typename IntervalType>
42class Triplex {
43public:
45 using interval_t = IntervalType;
46
48 using value_t = typename interval_t::value_t;
49
57 Triplex() = default;
58
67 Triplex(value_t value) : Triplex(interval_t(value), value) {}
68
85
104 template<typename I = interval_t,
105 std::enable_if_t<is_general_interval_v<I>, int> = 0>
107 bool right_open = false) :
108 Triplex(checked_bounds_(lower, nominal, upper, left_open, right_open),
109 nominal) {}
110
124 m_interval_(std::move(interval)), m_nominal_(nominal) {
125 if(!m_interval_.contains(m_nominal_)) {
126 throw std::domain_error("The nominal value of a triplex must be a "
127 "member of its interval");
128 }
129 }
130
131 // -- Accessors ------------------------------------------------------------
132
139 bool empty() const { return m_interval_.empty(); }
140
147 value_t lower() const {
148 assert_not_empty_();
149 return m_interval_.lower();
150 }
151
163 value_t nominal() const {
164 assert_not_empty_();
165 return m_nominal_;
166 }
167
174 value_t upper() const {
175 assert_not_empty_();
176 return m_interval_.upper();
177 }
178
185 value_t width() const {
186 assert_not_empty_();
187 return m_interval_.width();
188 }
189
198 value_t median() const {
199 assert_not_empty_();
200 return m_interval_.median();
201 }
202
209 value_t lower_radius() const { return nominal() - lower(); }
210
217 value_t upper_radius() const { return upper() - nominal(); }
218
225 const interval_t& interval() const { return m_interval_; }
226
238 template<typename I = interval_t,
239 std::enable_if_t<is_general_interval_v<I>, int> = 0>
240 bool left_open() const {
241 return m_interval_.left_open();
242 }
243
255 template<typename I = interval_t,
256 std::enable_if_t<is_general_interval_v<I>, int> = 0>
257 bool left_closed() const {
258 return m_interval_.left_closed();
259 }
260
272 template<typename I = interval_t,
273 std::enable_if_t<is_general_interval_v<I>, int> = 0>
274 bool right_open() const {
275 return m_interval_.right_open();
276 }
277
289 template<typename I = interval_t,
290 std::enable_if_t<is_general_interval_v<I>, int> = 0>
291 bool right_closed() const {
292 return m_interval_.right_closed();
293 }
294
295 // -- Set queries ----------------------------------------------------------
296
305 bool contains(value_t value) const { return m_interval_.contains(value); }
306
318 bool contains(const Triplex& other) const {
319 return m_interval_.contains(other.m_interval_);
320 }
321
322 // -- Arithmetic in-place operators ----------------------------------------
323 //
324 // A value_t operand converts implicitly to the degenerate triplex, so
325 // these also cover arithmetic between a triplex and a scalar.
326
337 if(empty()) { return Triplex(); }
338 return detail_::make_triplex_(-m_interval_, -m_nominal_);
339 }
340
351
362
372
386
387 // -- Arithmetic free functions --------------------------------------------
388 //
389 // These are hidden friends so that a value_t operand converts implicitly:
390 // template argument deduction never considers conversions.
391
403 friend Triplex operator+(Triplex lhs, const Triplex& rhs) {
404 return lhs += rhs;
405 }
406
418 friend Triplex operator-(Triplex lhs, const Triplex& rhs) {
419 return lhs -= rhs;
420 }
421
433 friend Triplex operator*(Triplex lhs, const Triplex& rhs) {
434 return lhs *= rhs;
435 }
436
449 friend Triplex operator/(Triplex lhs, const Triplex& rhs) {
450 return lhs /= rhs;
451 }
452
453private:
456
458 struct unchecked_tag_ {};
459
467 Triplex(interval_t interval, value_t nominal, unchecked_tag_) :
468 m_interval_(std::move(interval)), m_nominal_(nominal) {}
469
485 template<typename... Args>
486 static interval_t checked_bounds_(value_t lower, value_t nominal,
487 value_t upper, Args... args) {
488 if(!(lower <= nominal && nominal <= upper)) {
489 throw std::domain_error(
490 "A triplex requires lower <= nominal <= upper");
491 }
492 return interval_t(lower, upper, args...);
493 }
494
496 void assert_not_empty_() const {
497 if(empty()) { throw std::domain_error("Triplex is empty"); }
498 }
499
501 interval_t m_interval_;
502
504 value_t m_nominal_ = value_t(0);
505
506}; // class Triplex
507
508// -- Out-of-line Definitions --------------------------------------------------
509
510// The bounds are computed first and the nominal value second. interval_t's
511// operations restore the rounding mode before they return, so the nominal value
512// is computed in round-to-nearest, as plain floating point would compute it.
513
514template<typename IntervalType>
516 if(empty()) { return *this = rhs; }
517 if(rhs.empty()) { return *this; }
518 auto bounds = m_interval_ + rhs.m_interval_;
519 value_t nominal = m_nominal_ + rhs.m_nominal_;
520 return *this = detail_::make_triplex_(std::move(bounds), nominal);
521}
522
523template<typename IntervalType>
525 if(empty()) { return *this = -rhs; }
526 if(rhs.empty()) { return *this; }
527 auto bounds = m_interval_ - rhs.m_interval_;
528 value_t nominal = m_nominal_ - rhs.m_nominal_;
529 return *this = detail_::make_triplex_(std::move(bounds), nominal);
530}
531
532template<typename IntervalType>
534 if(empty() || rhs.empty()) { return *this = Triplex(); }
535 auto bounds = m_interval_ * rhs.m_interval_;
536 value_t nominal = m_nominal_ * rhs.m_nominal_;
537 return *this = detail_::make_triplex_(std::move(bounds), nominal);
538}
539
540template<typename IntervalType>
542 if(empty() || rhs.empty()) { return *this = Triplex(); }
543 auto bounds = m_interval_ / rhs.m_interval_;
544 value_t nominal = m_nominal_ / rhs.m_nominal_;
545 return *this = detail_::make_triplex_(std::move(bounds), nominal);
546}
547
548// -- Utility functions --------------------------------------------------------
549
565template<typename IntervalType>
566std::ostream& operator<<(std::ostream& os, const Triplex<IntervalType>& t) {
567 if(t.empty()) { return os << "∅"; }
568 bool is_left_open = false;
569 bool is_right_open = false;
571 is_left_open = t.left_open();
572 is_right_open = t.right_open();
573 }
574 return os << (is_left_open ? "(" : "[") << t.lower() << ", " << t.nominal()
575 << ", " << t.upper() << (is_right_open ? ")" : "]");
576}
577
593template<typename I1, typename I2>
594bool operator==(const Triplex<I1>& lhs, const Triplex<I2>& rhs) {
595 if constexpr(!std::is_same_v<I1, I2>) {
596 return false;
597 } else {
598 if(lhs.empty() || rhs.empty()) { return lhs.empty() && rhs.empty(); }
599 return lhs.interval() == rhs.interval() &&
600 lhs.nominal() == rhs.nominal();
601 }
602}
603
616template<typename I1, typename I2>
617bool operator!=(const Triplex<I1>& lhs, const Triplex<I2>& rhs) {
618 return !(lhs == rhs);
619}
620
643template<typename IntervalType>
645 const Triplex<IntervalType>& rhs) {
646 if(rhs.empty()) { return false; }
647 if(lhs.empty()) { return true; }
648 const auto key = [](const Triplex<IntervalType>& t) {
649 bool is_left_open = false;
650 bool is_right_open = false;
652 is_left_open = t.left_open();
653 is_right_open = t.right_open();
654 }
655 return std::make_tuple(t.nominal(), t.lower(), t.upper(), is_left_open,
656 is_right_open);
657 };
658 return key(lhs) < key(rhs);
659}
660
663template<typename IntervalType>
665 const Triplex<IntervalType>& rhs) {
666 return rhs < lhs;
667}
668
671template<typename IntervalType>
673 const Triplex<IntervalType>& rhs) {
674 return !(rhs < lhs);
675}
676
679template<typename IntervalType>
681 const Triplex<IntervalType>& rhs) {
682 return !(lhs < rhs);
683}
684
687
690
691} // namespace sigma
692
693namespace std {
694
696template<typename IntervalType>
697struct hash<sigma::Triplex<IntervalType>> {
701 using value_t = typename IntervalType::value_t;
702 if(t.empty()) { return 0; }
703 std::size_t seed = std::hash<IntervalType>()(t.interval());
704 std::size_t hash_nominal = std::hash<value_t>()(t.nominal());
705 seed ^= hash_nominal + 0x9e3779b9 + (seed << 6) + (seed >> 2);
706 return seed;
707 }
708};
709
710} // namespace std
711
float value_t
Definition interval.hpp:32
Models an interval together with a best estimate inside it.
Definition triplex.hpp:42
Triplex(interval_t interval, value_t nominal)
Constructs a triplex from an interval and a nominal value.
Definition triplex.hpp:123
sigma::Interval< float > interval_t
Definition triplex.hpp:45
bool right_open() const
Definition triplex.hpp:274
value_t lower_radius() const
Returns the distance from the lower bound to the nominal value.
Definition triplex.hpp:209
Triplex & operator/=(const Triplex &rhs)
In-place division by another triplex.
Definition triplex.hpp:541
value_t upper() const
Definition triplex.hpp:174
bool right_closed() const
Is upper() a member of *this?
Definition triplex.hpp:291
Triplex(value_t lower, value_t nominal, value_t upper)
Constructs a triplex from its bounds and its nominal value.
Definition triplex.hpp:83
value_t median() const
Returns the midpoint of the bounds.
Definition triplex.hpp:198
bool contains(const Triplex &other) const
Whether the bounds of other lie within the bounds of *this.
Definition triplex.hpp:318
Triplex(value_t value)
Constructs the degenerate triplex for an exactly known value.
Definition triplex.hpp:67
Triplex()=default
Default constructor.
std::ostream & operator<<(std::ostream &os, const Triplex< IntervalType > &t)
Overload stream insertion to print a triplex.
Definition triplex.hpp:566
Triplex & operator*=(const Triplex &rhs)
In-place multiplication by another triplex.
Definition triplex.hpp:533
bool empty() const
Is *this the empty triplex?
Definition triplex.hpp:139
bool contains(value_t value) const
Whether a scalar lies within the bounds.
Definition triplex.hpp:305
friend Triplex operator+(Triplex lhs, const Triplex &rhs)
Addition of two triplex numbers.
Definition triplex.hpp:403
bool operator>(const Triplex< IntervalType > &lhs, const Triplex< IntervalType > &rhs)
Same ordering as operator<, with the operands reversed.
Definition triplex.hpp:664
bool operator!=(const Triplex< I1 > &lhs, const Triplex< I2 > &rhs)
Compare two triplex numbers for inequality.
Definition triplex.hpp:617
Triplex & operator-=(const Triplex &rhs)
In-place subtraction of another triplex.
Definition triplex.hpp:524
const interval_t & interval() const
Definition triplex.hpp:225
friend Triplex operator*(Triplex lhs, const Triplex &rhs)
Multiplication of two triplex numbers.
Definition triplex.hpp:433
friend Triplex operator/(Triplex lhs, const Triplex &rhs)
Division of two triplex numbers.
Definition triplex.hpp:449
value_t upper_radius() const
Returns the distance from the nominal value to the upper bound.
Definition triplex.hpp:217
value_t lower() const
Definition triplex.hpp:147
value_t nominal() const
Definition triplex.hpp:163
Triplex & operator+=(const Triplex &rhs)
In-place addition of another triplex.
Definition triplex.hpp:515
typename interval_t::value_t value_t
Definition triplex.hpp:48
bool left_open() const
Definition triplex.hpp:240
bool left_closed() const
Is lower() a member of *this?
Definition triplex.hpp:257
Triplex operator-() const
Negation of a triplex.
Definition triplex.hpp:336
friend Triplex operator-(Triplex lhs, const Triplex &rhs)
Subtraction of two triplex numbers.
Definition triplex.hpp:418
value_t width() const
Returns the distance between the bounds.
Definition triplex.hpp:185
bool operator<=(const Triplex< IntervalType > &lhs, const Triplex< IntervalType > &rhs)
Same ordering as operator<, negated.
Definition triplex.hpp:672
bool operator>=(const Triplex< IntervalType > &lhs, const Triplex< IntervalType > &rhs)
Same ordering as operator<, negated and reversed.
Definition triplex.hpp:680
bool operator<(const Triplex< IntervalType > &lhs, const Triplex< IntervalType > &rhs)
Orders two triplex numbers, primarily by their nominal values.
Definition triplex.hpp:644
Triplex(value_t lower, value_t nominal, value_t upper, bool left_open, bool right_open=false)
Constructs a triplex whose bounds may be open.
Definition triplex.hpp:106
bool operator==(const Triplex< I1 > &lhs, const Triplex< I2 > &rhs)
Compare two triplex numbers for equality.
Definition triplex.hpp:594
Defines the GeneralInterval class.
Defines the Interval class.
Defines the function every triplex operation uses to build its result.
Triplex< IntervalType > make_triplex_(IntervalType bounds, typename IntervalType::value_t nominal)
Assembles a triplex from the result of an operation.
Definition make_triplex.hpp:44
The primary namespace for the sigma library.
Definition affine.hpp:12
Triplex< Interval< double > > TPDouble
Typedef for a triplex with closed double bounds.
Definition triplex.hpp:689
constexpr bool is_general_interval_v
Convenience variable for the value of is_general_interval.
Definition traits.hpp:24
Triplex< Interval< float > > TPFloat
Typedef for a triplex with closed float bounds.
Definition triplex.hpp:686
size_t operator()(const sigma::Triplex< IntervalType > &t) const
Definition triplex.hpp:700
Type traits for the interval types.
Components for compatibility with Eigen.
Convenience header for triplex operations.