Hermes
interval.h
Go to the documentation of this file.
1 
32 #ifndef HERMES_HERMES_NUMERIC_INTERVAL_H
33 #define HERMES_HERMES_NUMERIC_INTERVAL_H
34 
36 #include <hermes/numeric/numeric.h>
37 
38 namespace hermes {
39 
40 // *********************************************************************************************************************
41 // Interval
42 // *********************************************************************************************************************
44 template<typename T>
45 class Interval : public MathElement<T, 2u> {
46  static_assert(std::is_same<T, f32>::value || std::is_same<T, f64>::value || std::is_same<T, float>::value
47  || std::is_same<T, double>::value || std::is_same<T, i32>::value || std::is_same<T, i64>::value,
48  "Interval must hold an numeric type!");
49 public:
50  // *******************************************************************************************************************
51  // STATIC METHODS
52  // *******************************************************************************************************************
58  if (r == 0)
59  return {c, c};
60  return {Numbers::subRoundDown(c, r), Numbers::addRoundUp(c, r)};
61  }
62  // *******************************************************************************************************************
63  // CONSTRUCTORS
64  // *******************************************************************************************************************
73  HERMES_DEVICE_CALLABLE Interval(T l, T h) : low(l), high(h) {}
74  // assignment
75  // *******************************************************************************************************************
76  // OPERATORS
77  // *******************************************************************************************************************
78  // unary
81  HERMES_DEVICE_CALLABLE explicit operator T() const { return center(); }
85  // arithmetic
91  }
97  }
106  return {Numbers::min({lp[0], lp[1], lp[2], lp[3]}),
107  Numbers::max({hp[0], hp[1], hp[2], hp[3]})};
108  }
113  Interval r = i;
114  if (r.low < 0 && r.high > 0)
115  return {Numbers::lowest<T>(), Numbers::greatest<T>()};
120  return {Numbers::min({lq[0], lq[1], lq[2], lq[3]}), Numbers::max({hq[0], hq[1], hq[2], hq[3]})};
121  }
122  // boolean
123  HERMES_DEVICE_CALLABLE bool operator==(const Interval<T> &b) const {
124  return Check::is_equal(low, b.low) && Check::is_equal(high, b.high);
125  }
126  // *******************************************************************************************************************
127  // METHODS
128  // *******************************************************************************************************************
132  [[nodiscard]] HERMES_DEVICE_CALLABLE bool contains(T v) const { return v >= low && v <= high; }
135  [[nodiscard]] HERMES_DEVICE_CALLABLE T center() const { return (low + high) / 2; }
138  [[nodiscard]] HERMES_DEVICE_CALLABLE T radius() const { return (high - low) / 2; }
141  [[nodiscard]] HERMES_DEVICE_CALLABLE T width() const { return high - low; }
144  [[nodiscard]] HERMES_DEVICE_CALLABLE bool isExact() const { return high - low == 0; }
147  [[nodiscard]] HERMES_DEVICE_CALLABLE Interval sqr() const {
148  real_t alow = std::abs(low), ahigh = std::abs(high);
149  if (alow > ahigh)
150  Numbers::swap(alow, ahigh);
151  if (contains(0))
152  return Interval(0, Numbers::mulRoundUp(ahigh, ahigh));
153  return Interval(Numbers::mulRoundDown(alow, alow), Numbers::mulRoundUp(ahigh, ahigh));
154  }
157  [[nodiscard]] HERMES_DEVICE_CALLABLE Interval sqrt() const {
159  }
160  // *******************************************************************************************************************
161  // PUBLIC FIELDS
162  // *******************************************************************************************************************
163  T low{0};
164  T high{0};
165 };
166 
167 // *********************************************************************************************************************
168 // ARITHMETIC
169 // *********************************************************************************************************************
170 #define ARITHMETIC_OP(OP) \
171 template<typename T> \
172 HERMES_DEVICE_CALLABLE Interval<T> operator OP (T f, const Interval<T>& i) { \
173  return Interval<T>(f) OP i; } \
174 template<typename T> \
175 HERMES_DEVICE_CALLABLE Interval<T> operator OP (const Interval<T>& i, T f) { \
176  return i OP Interval<T>(f); }
177 ARITHMETIC_OP(+)
178 ARITHMETIC_OP(-)
179 ARITHMETIC_OP(*)
180 ARITHMETIC_OP(/)
181 #undef ARITHMETIC_OP
182 // *********************************************************************************************************************
183 // IO
184 // *********************************************************************************************************************
190 template<typename T>
191 std::ostream &operator<<(std::ostream &os, const Interval<T> &i) {
192  os << "[" << i.low << " " << i.high << "]";
193  return os;
194 }
195 
196 }
197 
198 #endif //HERMES_HERMES_NUMERIC_INTERVAL_H
199 
Represents a numeric interval that supports interval arithmetic.
Definition: interval.h:45
HERMES_DEVICE_CALLABLE T center() const
Gets interval center value.
Definition: interval.h:135
HERMES_DEVICE_CALLABLE Interval operator-() const
Negates interval.
Definition: interval.h:84
HERMES_DEVICE_CALLABLE Interval operator-(const Interval &i) const
Uses interval arithmetic subtraction.
Definition: interval.h:95
HERMES_DEVICE_CALLABLE Interval(T l, T h)
Construct from interval values.
Definition: interval.h:73
static HERMES_DEVICE_CALLABLE Interval withRadius(real_t c, real_t r)
Constructs interval from center and radius.
Definition: interval.h:57
HERMES_DEVICE_CALLABLE Interval sqrt() const
Computes arithmetic interval square root.
Definition: interval.h:157
HERMES_DEVICE_CALLABLE Interval operator/(const Interval &i) const
Uses interval arithmetic division.
Definition: interval.h:112
HERMES_DEVICE_CALLABLE T width() const
Gets interval diameter.
Definition: interval.h:141
HERMES_DEVICE_CALLABLE Interval(T v)
Constructs from center value.
Definition: interval.h:69
HERMES_DEVICE_CALLABLE Interval operator+(const Interval &i) const
Uses interval arithmetic addition.
Definition: interval.h:89
HERMES_DEVICE_CALLABLE Interval()
Default constructor.
Definition: interval.h:66
HERMES_DEVICE_CALLABLE bool isExact() const
Checks if interval contains a single value.
Definition: interval.h:144
T low
lowest interval value
Definition: interval.h:163
HERMES_DEVICE_CALLABLE bool contains(T v) const
Checks if this interval contains v.
Definition: interval.h:132
HERMES_DEVICE_CALLABLE T radius() const
Gets interval radius.
Definition: interval.h:138
HERMES_DEVICE_CALLABLE Interval sqr() const
Computes arithmetic interval square.
Definition: interval.h:147
HERMES_DEVICE_CALLABLE Interval operator*(const Interval &i) const
Uses interval arithmetic multiplication.
Definition: interval.h:101
T high
greatest interval value
Definition: interval.h:164
Interface used by all basic geometric entities.
Definition: math_element.h:44
float real_t
default floating point type
Definition: defs.h:75
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45
#define ARITHMETIC_OP(OP)
asd
Definition: index.h:408
Base class for all geometric objects.
Number functions.
static constexpr HERMES_DEVICE_CALLABLE bool is_equal(T a, T b)
Checks if two numbers are at most 1e-8 apart.
Definition: numeric.h:847
static HERMES_DEVICE_CALLABLE real_t addRoundDown(real_t a, real_t b)
Adds and rounds down to the next smaller float value.
Definition: numeric.h:594
static constexpr HERMES_DEVICE_CALLABLE T min(const T &a, const T &b)
Computes minimum between two numbers.
Definition: numeric.h:117
static HERMES_DEVICE_CALLABLE real_t subRoundUp(real_t a, real_t b)
Subtracts and rounds up to the next float value.
Definition: numeric.h:639
static HERMES_DEVICE_CALLABLE void swap(T &a, T &b)
Swaps values.
Definition: numeric.h:184
static HERMES_DEVICE_CALLABLE real_t divRoundUp(real_t a, real_t b)
Divides and rounds up to the next float value.
Definition: numeric.h:579
static HERMES_DEVICE_CALLABLE real_t divRoundDown(real_t a, real_t b)
Divides and rounds down to the next smaller float value.
Definition: numeric.h:564
static HERMES_DEVICE_CALLABLE real_t mulRoundUp(real_t a, real_t b)
Multiplies and rounds up to the next float value.
Definition: numeric.h:549
static HERMES_DEVICE_CALLABLE real_t addRoundUp(real_t a, real_t b)
Adds and rounds up to the next float value.
Definition: numeric.h:609
static constexpr HERMES_DEVICE_CALLABLE T max(const T &a, const T &b)
Computes maximum between two numbers.
Definition: numeric.h:127
static HERMES_DEVICE_CALLABLE real_t sqrtRoundDown(real_t a)
Computes square root rounded down to the next smaller float value.
Definition: numeric.h:653
static HERMES_DEVICE_CALLABLE real_t mulRoundDown(real_t a, real_t b)
Multiplies and rounds down to the next smaller float value.
Definition: numeric.h:534
static HERMES_DEVICE_CALLABLE real_t sqrtRoundUp(real_t a)
Computes square root rounded up to the next float value.
Definition: numeric.h:667
static HERMES_DEVICE_CALLABLE real_t subRoundDown(real_t a, real_t b)
Subtracts and rounds down to the next smaller float value.
Definition: numeric.h:624