Fuzzy.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_FUZZY_H
27 #define EIGEN_FUZZY_H
28 
29 namespace Eigen {
30 
31 namespace internal
32 {
33 
34 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
35 struct isApprox_selector
36 {
37  static bool run(const Derived& x, const OtherDerived& y, typename Derived::RealScalar prec)
38  {
39  using std::min;
40  typename internal::nested<Derived,2>::type nested(x);
41  typename internal::nested<OtherDerived,2>::type otherNested(y);
42  return (nested - otherNested).cwiseAbs2().sum() <= prec * prec * (min)(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum());
43  }
44 };
45 
46 template<typename Derived, typename OtherDerived>
47 struct isApprox_selector<Derived, OtherDerived, true>
48 {
49  static bool run(const Derived& x, const OtherDerived& y, typename Derived::RealScalar)
50  {
51  return x.matrix() == y.matrix();
52  }
53 };
54 
55 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
56 struct isMuchSmallerThan_object_selector
57 {
58  static bool run(const Derived& x, const OtherDerived& y, typename Derived::RealScalar prec)
59  {
60  return x.cwiseAbs2().sum() <= abs2(prec) * y.cwiseAbs2().sum();
61  }
62 };
63 
64 template<typename Derived, typename OtherDerived>
65 struct isMuchSmallerThan_object_selector<Derived, OtherDerived, true>
66 {
67  static bool run(const Derived& x, const OtherDerived&, typename Derived::RealScalar)
68  {
69  return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
70  }
71 };
72 
73 template<typename Derived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
74 struct isMuchSmallerThan_scalar_selector
75 {
76  static bool run(const Derived& x, const typename Derived::RealScalar& y, typename Derived::RealScalar prec)
77  {
78  return x.cwiseAbs2().sum() <= abs2(prec * y);
79  }
80 };
81 
82 template<typename Derived>
83 struct isMuchSmallerThan_scalar_selector<Derived, true>
84 {
85  static bool run(const Derived& x, const typename Derived::RealScalar&, typename Derived::RealScalar)
86  {
87  return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
88  }
89 };
90 
91 } // end namespace internal
92 
93 
111 template<typename Derived>
112 template<typename OtherDerived>
114  const DenseBase<OtherDerived>& other,
115  RealScalar prec
116 ) const
117 {
118  return internal::isApprox_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
119 }
120 
134 template<typename Derived>
136  const typename NumTraits<Scalar>::Real& other,
137  RealScalar prec
138 ) const
139 {
140  return internal::isMuchSmallerThan_scalar_selector<Derived>::run(derived(), other, prec);
141 }
142 
153 template<typename Derived>
154 template<typename OtherDerived>
156  const DenseBase<OtherDerived>& other,
157  RealScalar prec
158 ) const
159 {
160  return internal::isMuchSmallerThan_object_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
161 }
162 
163 } // end namespace Eigen
164 
165 #endif // EIGEN_FUZZY_H