Code-Eli  0.3.6
tolerance.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_util_tolerance_hpp
14 #define eli_util_tolerance_hpp
15 
16 #include <cmath> // std::cmath
17 #include <limits> // std::numeric_limits
18 
19 #include "eli/code_eli.hpp"
20 
21 namespace eli
22 {
23  namespace util
24  {
25  template<typename data__>
26  class tolerance
27  {
28  public:
29  tolerance() : abs_tol(10000*std::numeric_limits<data__>::epsilon()),
30  rel_tol(std::sqrt(std::numeric_limits<data__>::epsilon()))
31  {
32  }
33  tolerance(const data__ &abs, const data__ &rel) : abs_tol(abs), rel_tol(rel) {}
36 
38  {
39  if (this == &tol)
40  return *this;
41 
42  abs_tol=tol.abs_tol;
43  rel_tol=tol.rel_tol;
44 
45  return *this;
46  }
47 
48  bool operator==(const tolerance<data__> &tol) const
49  {
50  if (this == &tol)
51  return true;
52 
53  if (abs_tol!=tol.abs_tol)
54  return false;
55 
56  if (rel_tol!=tol.rel_tol)
57  return false;
58 
59  return true;
60  }
61 
62  bool operator!=(const tolerance<data__> &tol) const
63  {
64  return !operator==(tol);
65  }
66 
67  data__ get_relative_tolerance() const {return rel_tol;}
68  data__ get_absolute_tolerance() const {return abs_tol;}
69 
70  template<typename Derived1, typename Derived2>
71  bool exactly_equal(const Eigen::MatrixBase<Derived1> &m1, const Eigen::MatrixBase<Derived2> &m2) const
72  {
73  if (m1.rows()!=m2.rows())
74  return false;
75  if (m1.cols()!=m2.cols())
76  return false;
77 
78  return (m1==m2);
79  }
80 
81  bool exactly_equal(const data__ &t1, const data__ &t2) const
82  {
83  return (t1==t2);
84  }
85 
86  template<typename type2__>
87  bool exactly_equal(const data__ &t1, const type2__ &t2) const
88  {
89  return (t1==static_cast<data__>(t2));
90  }
91 
92  template<typename type1__>
93  bool exactly_equal(const type1__ &t1, const data__ &t2) const
94  {
95  return exactly_equal(t2, t1);
96  }
97 
98  template<typename Derived1, typename Derived2>
99  bool approximately_equal(const Eigen::MatrixBase<Derived1> &m1, const Eigen::MatrixBase<Derived2> &m2) const
100  {
101  typename Derived1::Index i, j;
102 
103  if (m1.rows()!=m2.rows())
104  return false;
105  if (m1.cols()!=m2.cols())
106  return false;
107 
108  for (i=0; i<m1.rows(); ++i)
109  {
110  for (j=0; j<m1.cols(); ++j)
111  {
112  if (!approximately_equal(m1(i,j), m2(i,j)))
113  return false;
114  }
115  }
116 
117  return true;
118  }
119 
120  bool approximately_equal(const data__ &t1, const data__ &t2) const
121  {
122  data__ diff(std::abs(t1-t2));
123  if (diff<=abs_tol)
124  {
125  return true;
126  }
127 
128  data__ denom(std::max(std::abs(t1), std::abs(t2)));
129  if (diff/denom<=rel_tol)
130  {
131  return true;
132  }
133 
134  return false;
135  }
136 
137  template<typename type2__>
138  bool approximately_equal(const data__ &t1, const type2__ &ti2) const
139  {
140  data__ t2=static_cast<data__>(ti2);
141 
142  return approximately_equal(t1, t2);
143  }
144 
145  template<typename type1__>
146  bool approximately_equal(const type1__ &ti1, const data__ &t2) const
147  {
148  return approximately_equal(t2, ti1);
149  }
150 
151  bool approximately_less_than(const data__ &t1, const data__ &t2) const
152  {
153  if (approximately_equal(t1, t2))
154  return false;
155 
156  return (t1<t2);
157  }
158 
159  template<typename type2__>
160  bool approximately_less_than(const data__ &t1, const type2__ &ti2) const
161  {
162  if (approximately_equal(t1, ti2))
163  return false;
164 
165  data__ t2=static_cast<data__>(ti2);
166  return (t1<t2);
167  }
168 
169  template<typename type1__>
170  bool approximately_less_than(const type1__ &ti1, const data__ &t2) const
171  {
172  if (approximately_equal(ti1, t2))
173  return false;
174 
175  data__ t1=static_cast<data__>(ti1);
176  return (t1<t2);
177  }
178 
179  private:
180  data__ abs_tol;
181  data__ rel_tol;
182  };
183  }
184 }
185 #endif
Definition: math.hpp:20
tolerance(const tolerance< data__ > &tol)
Definition: tolerance.hpp:34
bool approximately_equal(const data__ &t1, const data__ &t2) const
Definition: tolerance.hpp:120
tolerance(const data__ &abs, const data__ &rel)
Definition: tolerance.hpp:33
STL namespace.
bool approximately_less_than(const type1__ &ti1, const data__ &t2) const
Definition: tolerance.hpp:170
bool approximately_equal(const Eigen::MatrixBase< Derived1 > &m1, const Eigen::MatrixBase< Derived2 > &m2) const
Definition: tolerance.hpp:99
data__ rel_tol
Definition: tolerance.hpp:181
bool approximately_less_than(const data__ &t1, const type2__ &ti2) const
Definition: tolerance.hpp:160
data__ get_absolute_tolerance() const
Definition: tolerance.hpp:68
~tolerance()
Definition: tolerance.hpp:35
bool approximately_less_than(const data__ &t1, const data__ &t2) const
Definition: tolerance.hpp:151
Definition: tolerance.hpp:26
bool approximately_equal(const data__ &t1, const type2__ &ti2) const
Definition: tolerance.hpp:138
bool operator==(const tolerance< data__ > &tol) const
Definition: tolerance.hpp:48
bool exactly_equal(const data__ &t1, const type2__ &t2) const
Definition: tolerance.hpp:87
data__ abs_tol
Definition: tolerance.hpp:180
bool approximately_equal(const type1__ &ti1, const data__ &t2) const
Definition: tolerance.hpp:146
bool exactly_equal(const Eigen::MatrixBase< Derived1 > &m1, const Eigen::MatrixBase< Derived2 > &m2) const
Definition: tolerance.hpp:71
tolerance & operator=(const tolerance< data__ > &tol)
Definition: tolerance.hpp:37
bool exactly_equal(const type1__ &t1, const data__ &t2) const
Definition: tolerance.hpp:93
bool exactly_equal(const data__ &t1, const data__ &t2) const
Definition: tolerance.hpp:81
tolerance()
Definition: tolerance.hpp:29
bool operator!=(const tolerance< data__ > &tol) const
Definition: tolerance.hpp:62
data__ get_relative_tolerance() const
Definition: tolerance.hpp:67