mlpack
range_impl.hpp
Go to the documentation of this file.
1 
11 #ifndef MLPACK_CORE_MATH_RANGE_IMPL_HPP
12 #define MLPACK_CORE_MATH_RANGE_IMPL_HPP
13 
14 #include "range.hpp"
15 #include <float.h>
16 #include <sstream>
17 
18 namespace mlpack {
19 namespace math {
20 
24 template<typename T>
26  lo(std::numeric_limits<T>::max()),
27  hi(-std::numeric_limits<T>::max()) { /* nothing else to do */ }
28 
32 template<typename T>
33 inline RangeType<T>::RangeType(const T point) :
34  lo(point), hi(point) { /* nothing else to do */ }
35 
39 template<typename T>
40 inline RangeType<T>::RangeType(const T lo, const T hi) :
41  lo(lo), hi(hi) { /* nothing else to do */ }
42 
46 template<typename T>
47 inline T RangeType<T>::Width() const
48 {
49  if (lo < hi)
50  return (hi - lo);
51  else
52  return 0.0;
53 }
54 
58 template<typename T>
59 inline T RangeType<T>::Mid() const
60 {
61  return (hi + lo) / 2;
62 }
63 
67 template<typename T>
69 {
70  if (rhs.lo < lo)
71  lo = rhs.lo;
72  if (rhs.hi > hi)
73  hi = rhs.hi;
74 
75  return *this;
76 }
77 
78 template<typename T>
80 {
81  return RangeType<T>((rhs.lo < lo) ? rhs.lo : lo,
82  (rhs.hi > hi) ? rhs.hi : hi);
83 }
84 
89 template<typename T>
91 {
92  if (rhs.lo > lo)
93  lo = rhs.lo;
94  if (rhs.hi < hi)
95  hi = rhs.hi;
96 
97  return *this;
98 }
99 
100 template<typename T>
101 inline RangeType<T> RangeType<T>::operator&(const RangeType<T>& rhs) const
102 {
103  return RangeType<T>((rhs.lo > lo) ? rhs.lo : lo,
104  (rhs.hi < hi) ? rhs.hi : hi);
105 }
106 
110 template<typename T>
112 {
113  lo *= d;
114  hi *= d;
115 
116  // Now if we've negated, we need to flip things around so the bound is valid.
117  if (lo > hi)
118  {
119  double tmp = hi;
120  hi = lo;
121  lo = tmp;
122  }
123 
124  return *this;
125 }
126 
127 template<typename T>
128 inline RangeType<T> RangeType<T>::operator*(const T d) const
129 {
130  double nlo = lo * d;
131  double nhi = hi * d;
132 
133  if (nlo <= nhi)
134  return RangeType<T>(nlo, nhi);
135  else
136  return RangeType<T>(nhi, nlo);
137 }
138 
139 // Symmetric case.
140 template<typename T>
141 inline RangeType<T> operator*(const T d, const RangeType<T>& r)
142 {
143  double nlo = r.lo * d;
144  double nhi = r.hi * d;
145 
146  if (nlo <= nhi)
147  return RangeType<T>(nlo, nhi);
148  else
149  return RangeType<T>(nhi, nlo);
150 }
151 
155 template<typename T>
156 inline bool RangeType<T>::operator==(const RangeType<T>& rhs) const
157 {
158  return (lo == rhs.lo) && (hi == rhs.hi);
159 }
160 
161 template<typename T>
162 inline bool RangeType<T>::operator!=(const RangeType<T>& rhs) const
163 {
164  return (lo != rhs.lo) || (hi != rhs.hi);
165 }
166 
171 template<typename T>
172 inline bool RangeType<T>::operator<(const RangeType<T>& rhs) const
173 {
174  return hi < rhs.lo;
175 }
176 
177 template<typename T>
178 inline bool RangeType<T>::operator>(const RangeType<T>& rhs) const
179 {
180  return lo > rhs.hi;
181 }
182 
186 template<typename T>
187 inline bool RangeType<T>::Contains(const T d) const
188 {
189  return d >= lo && d <= hi;
190 }
191 
195 template<typename T>
196 inline bool RangeType<T>::Contains(const RangeType<T>& r) const
197 {
198  return lo <= r.hi && hi >= r.lo;
199 }
200 
202 template<typename T>
203 template<typename Archive>
204 void RangeType<T>::serialize(Archive& ar, const uint32_t /* version */)
205 {
206  ar(CEREAL_NVP(hi));
207  ar(CEREAL_NVP(lo));
208 }
209 
210 } // namespace math
211 } // namespace mlpack
212 
213 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: pointer_wrapper.hpp:23
Simple real-valued range.
Definition: range.hpp:19
RangeType()
The upper bound.
Definition: range_impl.hpp:25
Definition of the Range class, which represents a simple range with a lower and upper bound...