sigma  1.0.0
Loading...
Searching...
No Matches
affine.hpp
Go to the documentation of this file.
1#pragma once
2#include <atomic>
3#include <optional>
5#include <sstream>
6#include <unordered_map>
7
11
12namespace sigma {
13
47template<typename ValueType>
48class Affine {
49public:
51 using size_type = std::size_t;
52
54 using value_t = ValueType;
55
58
60 using error_terms_t = std::unordered_map<error_term_t, value_t>;
61
64
65 // --- Constructors and Assignment ----------------------------------------
66
73 Affine() = default;
74
85
99 Affine(value_t lo, value_t hi) : Affine(interval_t(lo, hi)) {}
100
113 explicit Affine(const interval_t& interval) {
114 if(interval.empty()) { return; }
115 m_center_ = interval.median();
116 if(interval.radius() > 0) {
117 m_error_terms_[make_error_term()] = interval.radius();
118 }
119 }
120
134 m_center_(center), m_error_terms_(std::move(radii)) {}
135
143 Affine(const Affine& other) = default;
144
151 Affine(Affine&& other) noexcept = default;
152
162 Affine& operator=(const Affine& other) = default;
163
172 Affine& operator=(Affine&& other) noexcept = default;
173
174 // -- State Accessors -----------------------------------------------------
175
186
194 value_t center() const {
195 assert_not_empty_();
196 return *m_center_;
197 }
198
213 const error_terms_t& error_terms() const { return m_error_terms_; }
214
225
236 void set_center(value_t center) { m_center_ = center; }
237
252 if(empty()) set_center(value_t{0});
253 m_error_terms_[error_term] = radius;
254 }
255
268 bool contains(value_t value) const;
269
284 bool contains(const interval_t& interval) const;
285
299 bool contains(const Affine& affine) const {
300 return contains(affine.range());
301 }
302
313 bool empty() const noexcept { return !m_center_; }
314
327 std::string print_affine_form() const;
328
343 std::string print_interval_form() const {
344 return range().print_interval_form();
345 }
346
347 // -- Arithmetic Operators ------------------------------------------------
348
362
376 if(empty()) { return *this = Affine(value); }
377 (*m_center_) += value;
378 return *this;
379 }
380
398 Affine& operator+=(const Affine& other);
399
413 Affine operator+(value_t value) const { return Affine(*this) += value; }
414
428 Affine operator+(const Affine& other) const {
429 return Affine(*this) += other;
430 }
431
445 if(empty()) { return *this = Affine(-value); }
446 (*m_center_) -= value;
447 return *this;
448 }
449
467 Affine& operator-=(const Affine& other);
468
482 Affine operator-(value_t value) const { return Affine(*this) -= value; }
483
497 Affine operator-(const Affine& other) const {
498 return Affine(*this) -= other;
499 }
500
516 if(empty()) { return *this; }
517 (*m_center_) *= value;
518 for(auto&& [error_symbol, error_term_i] : m_error_terms_) {
519 error_term_i *= value;
520 }
521 return *this;
522 }
523
546 Affine& operator*=(const Affine& other);
547
561 Affine operator*(value_t value) const { return Affine(*this) *= value; }
562
576 Affine operator*(const Affine& other) const {
577 return Affine(*this) *= other;
578 }
579
596 if(value == 0) { throw std::domain_error("Division by zero"); }
597 return *this *= value_t(1.0 / value);
598 }
599
616 Affine& operator/=(const Affine& other);
617
630 Affine operator/(value_t value) const { return Affine(*this) /= value; }
631
647 Affine operator/(const Affine& other) const {
648 return Affine(*this) /= other;
649 }
650
674 value_t delta) const {
675 value_t new_center = empty() ? zeta : alpha * center() + zeta;
676 error_terms_t new_error_terms;
677 for(auto&& [error_symbol, error_term_i] : m_error_terms_) {
678 new_error_terms[error_symbol] = alpha * error_term_i;
679 }
680 new_error_terms[make_error_term()] = delta;
681 return Affine(new_center, std::move(new_error_terms));
682 }
683
708
709 // -- Comparison Operators ------------------------------------------------
710
726 bool operator==(const Affine& other) const {
727 if(empty() != other.empty()) { return false; }
728 if(empty()) { return true; }
729 if(m_center_ != other.m_center_) { return false; }
730 return m_error_terms_ == other.m_error_terms_;
731 }
732
748 bool operator!=(const Affine& other) const { return !(*this == other); }
749
768 bool operator<(const Affine& other) const {
769 if(other.empty()) { return false; }
770 if(empty()) { return true; }
771 return center() < other.center();
772 }
773
775 bool operator>(const Affine& other) const { return other < *this; }
776
778 bool operator<=(const Affine& other) const { return !(other < *this); }
779
781 bool operator>=(const Affine& other) const { return !(*this < other); }
782
785 static std::atomic<size_type> s_next_id{0};
786 return s_next_id.fetch_add(1);
787 }
788
789private:
791 void assert_not_empty_() const {
792 if(empty()) { throw std::domain_error("Affine form is empty"); }
793 }
794
797 std::optional<value_t> m_center_;
798
801 error_terms_t m_error_terms_;
802};
803
804// -- Non-member functions
805// ---------------------------------------------------
806
823template<typename ValueType>
824std::ostream& operator<<(std::ostream& os, const Affine<ValueType>& a) {
825 os << a.range();
826 return os;
827}
828
847template<typename ValueType>
849 return a * value;
850}
851
852// -- Out-of-line definitions
853// ------------------------------------------------
854
855template<typename ValueType>
857 if(empty()) { return interval_t(); }
858 auto r = radius();
859 return interval_t(center() - r, center() + r);
860}
861
862template<typename ValueType>
864 assert_not_empty_();
865 value_t r = 0;
866 for(auto&& [error_symbol, error_term_i] : m_error_terms_) {
867 r += std::fabs(error_term_i);
868 }
869 return r;
870}
871
872template<typename ValueType>
873auto Affine<ValueType>::contains(value_t value) const -> bool {
874 if(empty()) { return false; }
875 return range().contains(value);
876}
877
878template<typename ValueType>
879auto Affine<ValueType>::contains(const interval_t& interval) const -> bool {
880 if(interval.empty()) { return true; }
881 if(empty()) { return false; }
882 return range().contains(interval);
883}
884
885template<typename ValueType>
887 if(empty()) { return "∅"; }
888 std::stringstream ss;
889 ss << center();
890 for(auto&& [error_symbol, error_term_i] : m_error_terms_) {
891 ss << " +/- " << error_term_i;
892 }
893 return ss.str();
894}
895
896template<typename ValueType>
898 if(empty()) { return *this; }
899 value_t new_center = -center();
900 error_terms_t new_error_terms;
901 for(auto&& [error_symbol, error_term_i] : m_error_terms_) {
902 new_error_terms[error_symbol] = -error_term_i;
903 }
904 return Affine(new_center, new_error_terms);
905}
906
907template<typename ValueType>
909 if(empty()) { return *this = other; }
910 if(other.empty()) { return *this; }
911 value_t new_center = center() + other.center();
912 error_terms_t new_error_terms = m_error_terms_;
913 for(auto&& [error_symbol, error_term_i] : other.m_error_terms_) {
914 new_error_terms[error_symbol] += error_term_i;
915 }
916 return *this = Affine(new_center, new_error_terms);
917}
918
919template<typename ValueType>
921 return *this += -other;
922}
923
924template<typename ValueType>
926 if(empty() || other.empty()) { return *this = Affine(); }
927 value_t new_center = center() * other.center();
928 error_terms_t new_error_terms;
929 value_t new_radius = 0;
930 for(auto&& [error_symbol, error_term_i] : m_error_terms_) {
931 new_error_terms[error_symbol] = error_term_i * other.center();
932 new_radius += std::fabs(new_error_terms[error_symbol]);
933 }
934 for(auto&& [error_symbol, error_term_j] : other.m_error_terms_) {
935 new_error_terms[error_symbol] += error_term_j * center();
936 new_radius += std::fabs(new_error_terms[error_symbol]);
937 }
938 auto correction = radius() * other.radius();
939 new_error_terms[make_error_term()] = correction;
940 return *this = Affine(new_center, new_error_terms);
941}
942
943template<typename ValueType>
945 // Multiply by this by 1 / other
946 return *this *= other.multiplicative_inverse();
947}
948
949template<typename ValueType>
951 assert_not_empty_();
952 if(contains(0)) { throw std::domain_error("Division by zero"); }
953 // Compute the affine transformation which transforms other to 1 / other
954 auto other_range = range();
955 auto abs_inf = std::fabs(other_range.lower());
956 auto abs_sup = std::fabs(other_range.upper());
957 auto a = std::min(abs_inf, abs_sup);
958 auto b = std::max(abs_inf, abs_sup);
959 auto alpha = value_t(-1.0) / (b * b);
960 auto lo = value_t(1.0) / a - alpha * a;
961 auto hi = value_t(2.0) / b;
962 interval_t interval(std::min(lo, hi), std::max(lo, hi));
963 auto zeta = std::fabs(interval.median());
964 auto delta = interval.radius();
965 return apply_affine_transform(alpha, zeta, delta);
966}
967
970
973
974} // namespace sigma
975
Components for compatibility with Eigen.
Convenience header for affine form operations.
Implements affine arithmetic.
Definition affine.hpp:48
Affine operator+(const Affine &other) const
Returns the sum of *this and other.
Definition affine.hpp:428
Affine & operator/=(value_t value)
Overwrites *this with the quotient of *this and value.
Definition affine.hpp:595
Affine & operator-=(value_t value)
Overwrites *this with the difference of *this and value.
Definition affine.hpp:444
bool contains(const Affine &affine) const
Checks if affine is contained within *this.
Definition affine.hpp:299
Affine< ValueType > operator*(ValueType value, const Affine< ValueType > &a)
Multiplies a scalar by an affine form.
Definition affine.hpp:848
Affine & operator-=(const Affine &other)
Overwrites *this with the difference of *this and other.
Definition affine.hpp:920
Affine & operator+=(value_t value)
Overwrites *this with the sum of *this and value.
Definition affine.hpp:375
Affine(value_t center, error_terms_t radii)
Construct an affine form from a center value and a map of errors.
Definition affine.hpp:133
bool operator==(const Affine &other) const
Checks if *this and other represent the same affine form.
Definition affine.hpp:726
Affine operator+(value_t value) const
Returns the sum of *this and value.
Definition affine.hpp:413
Affine apply_affine_transform(value_t alpha, value_t zeta, value_t delta) const
Applies an affine transformation to *this.
Definition affine.hpp:673
Affine operator/(value_t value) const
Returns the quotient of *this and value.
Definition affine.hpp:630
bool operator<=(const Affine &other) const
Same ordering as operator<, negated.
Definition affine.hpp:778
bool contains(const interval_t &interval) const
Checks if interval is contained within *this.
Definition affine.hpp:879
Affine()=default
Constructs an empty affine form.
ValueType value_t
Type used for storing floating point values.
Definition affine.hpp:54
Affine(Affine &&other) noexcept=default
Constructs an affine form by moving other.
bool operator<(const Affine &other) const
Whether *this's center precedes other's.
Definition affine.hpp:768
std::unordered_map< error_term_t, value_t > error_terms_t
Type used to map error terms to their radii.
Definition affine.hpp:60
std::string print_interval_form() const
Creates a string of the interval form of *this.
Definition affine.hpp:343
Affine(value_t lo, value_t hi)
Constructs an affine form from a lower and upper bound.
Definition affine.hpp:99
const error_terms_t & error_terms() const
Returns the error terms of the affine form.
Definition affine.hpp:213
Affine multiplicative_inverse() const
Returns the multiplicative inverse of *this.
Definition affine.hpp:950
Affine(value_t center)
Constructs an affine form from a center value.
Definition affine.hpp:84
std::string print_affine_form() const
Creates a string of the affine form of *this.
Definition affine.hpp:886
value_t center() const
Definition affine.hpp:194
Affine(const Affine &other)=default
Makes a deep copy of other.
bool operator>(const Affine &other) const
Same ordering as operator<, with the operands reversed.
Definition affine.hpp:775
Affine operator-(value_t value) const
Returns the difference of *this and value.
Definition affine.hpp:482
size_type error_term_t
Opaque type used to store error term information.
Definition affine.hpp:57
void set_center(value_t center)
Sets the center of the affine form.
Definition affine.hpp:236
Interval< value_t > interval_t
Type of an interval.
Definition affine.hpp:63
Affine operator*(const Affine &other) const
Returns the product of *this and other.
Definition affine.hpp:576
Affine & operator*=(value_t value)
Overwrites *this with the product of *this and value.
Definition affine.hpp:515
static error_term_t make_error_term()
Returns a process-wide unique integer ID for a new error term symbol.
Definition affine.hpp:784
bool operator!=(const Affine &other) const
Checks if *this and other represent different affine forms.
Definition affine.hpp:748
Affine & operator*=(const Affine &other)
Overwrites *this with the product of *this and other.
Definition affine.hpp:925
Affine & operator=(Affine &&other) noexcept=default
Moves the value of other to this affine form.
std::size_t size_type
Type used for indexing and offsets.
Definition affine.hpp:51
Affine operator-(const Affine &other) const
Returns the difference of *this and other.
Definition affine.hpp:497
void add_error_term(error_term_t error_term, value_t radius)
Adds an error term to the affine form.
Definition affine.hpp:251
Affine operator*(value_t value) const
Returns the product of *this and value.
Definition affine.hpp:561
bool empty() const noexcept
Checks if this affine form is representing an empty interval.
Definition affine.hpp:313
std::ostream & operator<<(std::ostream &os, const Affine< ValueType > &a)
Outputs the range of an affine form to an output stream.
Definition affine.hpp:824
Affine & operator/=(const Affine &other)
Overwrites *this with the quotient of *this and other.
Definition affine.hpp:944
bool operator>=(const Affine &other) const
Same ordering as operator<, negated and reversed.
Definition affine.hpp:781
interval_t range() const
Returns the interval represented by the affine form.
Definition affine.hpp:856
bool contains(value_t value) const
Checks if the interval represented by *this contains value.
Definition affine.hpp:873
Affine operator/(const Affine &other) const
Returns the quotient of *this and other.
Definition affine.hpp:647
Affine & operator+=(const Affine &other)
Overwrites *this with the sum of *this and other.
Definition affine.hpp:908
Affine(const interval_t &interval)
Constructs an affine form from an interval.
Definition affine.hpp:113
Affine operator-() const
Returns the additive inverse of *this.
Definition affine.hpp:897
Affine & operator=(const Affine &other)=default
Assigns the value of other to this affine form.
value_t radius() const
Returns the radius of the affine form.
Definition affine.hpp:863
Models a closed numeric interval.
Definition interval.hpp:29
value_t median() const
Returns the midpoint of the interval.
Definition interval.hpp:84
std::string print_interval_form() const
Print the interval in interval form.
Definition interval.hpp:441
value_t radius() const
Returns the half-width of the interval.
Definition interval.hpp:98
bool empty() const
Is *this the empty interval?
Definition interval.hpp:108
Defines the Interval class.
The primary namespace for the sigma library.
Definition affine.hpp:12
Affine< double > ADouble
Typedef for an affine form of doubles.
Definition affine.hpp:972
Affine< float > AFloat
Typedef for an affine form of floats.
Definition affine.hpp:969