sigma  1.0.0
Loading...
Searching...
No Matches
thresholded_affine.hpp
Go to the documentation of this file.
1#pragma once
2#include <cmath>
4
8
9namespace sigma {
10
28template<typename ValueType>
30public:
32 using size_type = std::size_t;
33
35 using value_t = ValueType;
36
39
42
45
48
60 struct Threshold {
64 explicit Threshold(value_t v) : value(v) {}
65 };
66
67 // --- Constructors and Assignment ----------------------------------------
68
76 ThresholdedAffine() : m_threshold_(default_threshold().value) {}
77
89
100 Threshold t = default_threshold()) :
101 ThresholdedAffine(interval_t(lo, hi), t) {
102 apply_threshold_();
103 }
104
113 explicit ThresholdedAffine(const interval_t& interval,
114 Threshold t = default_threshold()) :
115 m_affine_(interval), m_threshold_(t.value) {
116 apply_threshold_();
117 }
118
129 Threshold t = default_threshold()) :
130 m_affine_(center, std::move(radii)), m_threshold_(t.value) {
131 apply_threshold_();
132 }
133
146 m_affine_(std::move(a)), m_threshold_(threshold) {
147 apply_threshold_();
148 }
149
157 ThresholdedAffine(const ThresholdedAffine& other) = default;
158
164 ThresholdedAffine(ThresholdedAffine&& other) noexcept = default;
165
176
185 ThresholdedAffine& operator=(ThresholdedAffine&& other) noexcept = default;
186
187 // -- State Accessors -----------------------------------------------------
188
194 static constexpr Threshold default_threshold() { return Threshold(0.001); }
195
205 if(m_affine_.empty()) return interval_t{};
206 auto r = m_affine_.radius();
207 return interval_t(m_affine_.center() - r, m_affine_.center() + r);
208 }
209
215 value_t center() const { return m_affine_.center(); }
216
222 const error_terms_t& error_terms() const { return m_affine_.error_terms(); }
223
229 value_t radius() const { return m_affine_.radius(); }
230
236 void set_center(value_t c) { m_affine_.set_center(c); }
237
244 void add_error_term(error_term_t error_term, value_t r) {
245 m_affine_.add_error_term(error_term, r);
246 apply_threshold_();
247 }
248
255 bool contains(value_t v) const { return range().contains(v); }
256
263 bool contains(const interval_t& i) const { return range().contains(i); }
264
271 bool contains(const ThresholdedAffine& other) const {
272 return range().contains(other.range());
273 }
274
280 bool empty() const noexcept { return m_affine_.empty(); }
281
288 std::string print_affine_form() const {
289 return m_affine_.print_affine_form();
290 }
291
297 std::string print_interval_form() const {
298 return range().print_interval_form();
299 }
300
306 const affine_t& affine() const { return m_affine_; }
307
313 value_t threshold() const { return m_threshold_; }
314
315 // -- Arithmetic Operators ------------------------------------------------
316
325 auto result = *this;
326 result.m_affine_ = -m_affine_;
327 return result;
328 }
329
337 m_affine_ += value;
338 return *this;
339 }
340
352 m_affine_ += other.m_affine_;
353 apply_threshold_();
354 return *this;
355 }
356
359 return ThresholdedAffine(*this) += value;
360 }
361
364 return ThresholdedAffine(*this) += other;
365 }
366
374 m_affine_ -= value;
375 return *this;
376 }
377
389 m_affine_ -= other.m_affine_;
390 apply_threshold_();
391 return *this;
392 }
393
396 return ThresholdedAffine(*this) -= value;
397 }
398
401 return ThresholdedAffine(*this) -= other;
402 }
403
411 m_affine_ *= value;
412 apply_threshold_();
413 return *this;
414 }
415
426 m_affine_ *= other.m_affine_;
427 apply_threshold_();
428 return *this;
429 }
430
433 return ThresholdedAffine(*this) *= value;
434 }
435
438 return ThresholdedAffine(*this) *= other;
439 }
440
448 m_affine_ /= value;
449 apply_threshold_();
450 return *this;
451 }
452
464 // Minimum absolute value of other over its interval.
465 value_t other_total = other.m_affine_.radius();
466 value_t b_min = std::fabs(other.m_affine_.center()) - other_total;
467 if(b_min <= value_t(0)) {
468 throw std::domain_error(
469 "ThresholdedAffine division by interval containing zero");
470 }
471
472 m_affine_ /= other.m_affine_;
473
474 apply_threshold_();
475 return *this;
476 }
477
480 return ThresholdedAffine(*this) /= value;
481 }
482
485 return ThresholdedAffine(*this) /= other;
486 }
487
488 // -- Comparison Operators ------------------------------------------------
489
496 bool operator==(const ThresholdedAffine& other) const {
497 return m_affine_ == other.m_affine_ &&
498 m_threshold_ == other.m_threshold_;
499 }
500
507 bool operator!=(const ThresholdedAffine& other) const {
508 return !(*this == other);
509 }
510
511private:
519 void apply_threshold_() {
520 if(m_affine_.empty()) return;
521 auto total_r = m_affine_.radius();
522
523 auto terms = m_affine_.error_terms();
524 if(terms.empty()) return;
525
526 error_terms_t new_terms;
527 for(auto&& [sym, coeff] : terms) {
528 if(std::fabs(coeff) < std::numeric_limits<value_t>::epsilon()) {
529 continue;
530 } else if(total_r != value_t(0) &&
531 std::fabs(coeff) / total_r < m_threshold_) {
532 continue;
533 } else {
534 new_terms[sym] = coeff;
535 }
536 }
537 m_affine_ = affine_t(m_affine_.center(), std::move(new_terms));
538 }
539
541 affine_t m_affine_;
542
545 value_t m_threshold_;
546};
547
548// -- Non-member functions ----------------------------------------------------
549
555template<typename ValueType>
556std::ostream& operator<<(std::ostream& os,
558 os << a.range();
559 return os;
560}
561
567template<typename ValueType>
570 return a * value;
571}
572
573// -- Operations --------------------------------------------------------------
574
580template<typename T>
584
590template<typename T>
594
601template<typename T>
605
611template<typename T>
615
622template<typename T>
626
634template<typename T, typename U>
638
641
644
645} // namespace sigma
Defines the Affine class.
Implements affine arithmetic.
Definition affine.hpp:48
std::unordered_map< error_term_t, value_t > error_terms_t
Type used to map error terms to their radii.
Definition affine.hpp:60
const error_terms_t & error_terms() const
Returns the error terms of the affine form.
Definition affine.hpp:213
value_t center() const
Returns the center of the affine form.
Definition affine.hpp:194
size_type error_term_t
Opaque type used to store error term information.
Definition affine.hpp:57
Interval< value_t > interval_t
Type of an interval.
Definition affine.hpp:63
bool empty() const noexcept
Checks if this affine form is representing an empty interval.
Definition affine.hpp:313
value_t radius() const
Returns the radius of the affine form.
Definition affine.hpp:830
Implements affine arithmetic that drops negligible error terms.
Definition thresholded_affine.hpp:29
std::size_t size_type
Definition thresholded_affine.hpp:32
std::string print_affine_form() const
Returns a string of the affine form (tracked terms only).
Definition thresholded_affine.hpp:288
ThresholdedAffine operator-() const
Returns the additive inverse of *this.
Definition thresholded_affine.hpp:324
static constexpr Threshold default_threshold()
Definition thresholded_affine.hpp:194
ThresholdedAffine< T > pow(const ThresholdedAffine< T > &a, const U &exp)
Power of a ThresholdedAffine.
Definition thresholded_affine.hpp:635
ThresholdedAffine & operator/=(value_t value)
Divides *this by a scalar.
Definition thresholded_affine.hpp:447
value_t threshold() const
Definition thresholded_affine.hpp:313
ThresholdedAffine(ThresholdedAffine &&other) noexcept=default
Move constructor.
ThresholdedAffine operator*(const ThresholdedAffine &other) const
Multiplies *this by another ThresholdedAffine.
Definition thresholded_affine.hpp:437
ThresholdedAffine(const interval_t &interval, Threshold t=default_threshold())
Constructs a ThresholdedAffine from an interval.
Definition thresholded_affine.hpp:113
bool operator==(const ThresholdedAffine &other) const
Checks equality of two ThresholdedAffine forms.
Definition thresholded_affine.hpp:496
ThresholdedAffine operator-(value_t value) const
Returns the difference of *this and a scalar.
Definition thresholded_affine.hpp:395
ThresholdedAffine operator+(const ThresholdedAffine &other) const
Returns the sum of *this and another ThresholdedAffine.
Definition thresholded_affine.hpp:363
Affine< float > affine_t
Definition thresholded_affine.hpp:47
bool contains(value_t v) const
Returns whether *this contains a scalar value v.
Definition thresholded_affine.hpp:255
ThresholdedAffine(const ThresholdedAffine &other)=default
Copy constructor.
ThresholdedAffine & operator=(ThresholdedAffine &&other) noexcept=default
Move assignment operator.
ThresholdedAffine operator+(value_t value) const
Returns the sum of *this and a scalar.
Definition thresholded_affine.hpp:358
ThresholdedAffine(affine_t a, value_t threshold)
Constructs a ThresholdedAffine by wrapping an Affine form.
Definition thresholded_affine.hpp:145
typename Affine< float >::interval_t interval_t
Definition thresholded_affine.hpp:44
ThresholdedAffine< T > exp(const ThresholdedAffine< T > &a)
Exponential of a ThresholdedAffine.
Definition thresholded_affine.hpp:612
ThresholdedAffine(value_t center, Threshold t=default_threshold())
Constructs a ThresholdedAffine from a center value.
Definition thresholded_affine.hpp:86
ThresholdedAffine(value_t lo, value_t hi, Threshold t=default_threshold())
Constructs a ThresholdedAffine from a lower and upper bound.
Definition thresholded_affine.hpp:99
void add_error_term(error_term_t error_term, value_t r)
Adds an error term to the affine form and applies thresholding.
Definition thresholded_affine.hpp:244
ThresholdedAffine operator/(value_t value) const
Divides *this by a scalar.
Definition thresholded_affine.hpp:479
ThresholdedAffine< T > log(const ThresholdedAffine< T > &a)
Natural logarithm of a ThresholdedAffine.
Definition thresholded_affine.hpp:623
ThresholdedAffine & operator-=(value_t value)
Subtracts a scalar from *this.
Definition thresholded_affine.hpp:373
bool contains(const interval_t &i) const
Returns whether *this contains an interval.
Definition thresholded_affine.hpp:263
ThresholdedAffine(value_t center, error_terms_t radii, Threshold t=default_threshold())
Constructs a ThresholdedAffine from a center and error terms.
Definition thresholded_affine.hpp:128
ThresholdedAffine operator/(const ThresholdedAffine &other) const
Divides *this by another ThresholdedAffine.
Definition thresholded_affine.hpp:484
value_t radius() const
Returns the total radius: the sum of the tracked terms.
Definition thresholded_affine.hpp:229
ThresholdedAffine & operator+=(const ThresholdedAffine &other)
Adds another ThresholdedAffine to *this.
Definition thresholded_affine.hpp:351
ThresholdedAffine< ValueType > operator*(ValueType value, const ThresholdedAffine< ValueType > &a)
Multiplies a scalar by a ThresholdedAffine.
Definition thresholded_affine.hpp:568
ThresholdedAffine< T > sqrt(const ThresholdedAffine< T > &a)
Square root of a ThresholdedAffine.
Definition thresholded_affine.hpp:602
float value_t
Definition thresholded_affine.hpp:35
ThresholdedAffine & operator/=(const ThresholdedAffine &other)
Divides *this by another ThresholdedAffine.
Definition thresholded_affine.hpp:463
typename Affine< float >::error_term_t error_term_t
Definition thresholded_affine.hpp:38
ThresholdedAffine & operator=(const ThresholdedAffine &other)=default
Copy assignment operator.
ThresholdedAffine & operator*=(const ThresholdedAffine &other)
Multiplies *this by another ThresholdedAffine.
Definition thresholded_affine.hpp:425
ThresholdedAffine & operator*=(value_t value)
Multiplies *this by a scalar.
Definition thresholded_affine.hpp:410
interval_t range() const
Returns the interval represented by *this.
Definition thresholded_affine.hpp:204
void set_center(value_t c)
Sets the center of the affine form.
Definition thresholded_affine.hpp:236
ThresholdedAffine & operator+=(value_t value)
Adds a scalar to *this.
Definition thresholded_affine.hpp:336
const error_terms_t & error_terms() const
Returns the error terms of the tracked affine form.
Definition thresholded_affine.hpp:222
ThresholdedAffine operator*(value_t value) const
Multiplies *this by a scalar.
Definition thresholded_affine.hpp:432
ThresholdedAffine & operator-=(const ThresholdedAffine &other)
Subtracts another ThresholdedAffine from *this.
Definition thresholded_affine.hpp:388
ThresholdedAffine< T > fabs(const ThresholdedAffine< T > &a)
Absolute value of a ThresholdedAffine (alias for abs).
Definition thresholded_affine.hpp:591
ThresholdedAffine< T > abs(const ThresholdedAffine< T > &a)
Absolute value of a ThresholdedAffine.
Definition thresholded_affine.hpp:581
bool empty() const noexcept
Returns whether *this is empty.
Definition thresholded_affine.hpp:280
std::string print_interval_form() const
Returns a string of the interval form.
Definition thresholded_affine.hpp:297
typename Affine< float >::error_terms_t error_terms_t
Definition thresholded_affine.hpp:41
const affine_t & affine() const
Returns the underlying Affine form (tracked terms only).
Definition thresholded_affine.hpp:306
bool contains(const ThresholdedAffine &other) const
Returns whether *this contains another ThresholdedAffine's range.
Definition thresholded_affine.hpp:271
bool operator!=(const ThresholdedAffine &other) const
Checks inequality of two ThresholdedAffine forms.
Definition thresholded_affine.hpp:507
ThresholdedAffine operator-(const ThresholdedAffine &other) const
Returns the difference of *this and another ThresholdedAffine.
Definition thresholded_affine.hpp:400
value_t center() const
Definition thresholded_affine.hpp:215
ThresholdedAffine()
Constructs an empty ThresholdedAffine.
Definition thresholded_affine.hpp:76
std::ostream & operator<<(std::ostream &os, const ThresholdedAffine< ValueType > &a)
Outputs the range of a ThresholdedAffine to a stream.
Definition thresholded_affine.hpp:556
The primary namespace for the sigma library.
Definition affine.hpp:12
ThresholdedAffine< double > TADouble
Typedef for a thresholded affine form of doubles.
Definition thresholded_affine.hpp:643
Affine< T > exp(const Affine< T > &a)
Calculate the exponential of an affine form.
Definition exponents.ipp:30
Affine< T > pow(const Affine< T > &a, const U &exp)
Calculate the power of an affine form.
Definition exponents.ipp:71
Affine< T > abs(const Affine< T > &a)
Absolute Value of an affine form.
Definition basic.ipp:8
Affine< T > log(const Affine< T > &a)
Calculate the natural logarithm of an affine form.
Definition exponents.ipp:50
Affine< T > fabs(const Affine< T > &a)
Absolute Value of an affine form.
Definition basic.ipp:25
Affine< T > sqrt(const Affine< T > &a)
Calculate the square root of an affine form.
Definition exponents.ipp:8
ThresholdedAffine< float > TAFloat
Typedef for a thresholded affine form of floats.
Definition thresholded_affine.hpp:640
Threshold(value_t v)
Constructs a Threshold with the given value.
Definition thresholded_affine.hpp:64
value_t value
The relative threshold value (e.g. 0.01 for 1%).
Definition thresholded_affine.hpp:62