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 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_FUZZY_H
12 #define EIGEN_FUZZY_H
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal
19 {
20 
21 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
22 struct isApprox_selector
23 {
25  static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec)
26  {
27  typename internal::nested_eval<Derived,2>::type nested(x);
28  typename internal::nested_eval<OtherDerived,2>::type otherNested(y);
29  return (nested.matrix() - otherNested.matrix()).cwiseAbs2().sum() <= prec * prec * numext::mini(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum());
30  }
31 };
32 
33 template<typename Derived, typename OtherDerived>
34 struct isApprox_selector<Derived, OtherDerived, true>
35 {
37  static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar&)
38  {
39  return x.matrix() == y.matrix();
40  }
41 };
42 
43 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
44 struct isMuchSmallerThan_object_selector
45 {
47  static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec)
48  {
49  return x.cwiseAbs2().sum() <= numext::abs2(prec) * y.cwiseAbs2().sum();
50  }
51 };
52 
53 template<typename Derived, typename OtherDerived>
54 struct isMuchSmallerThan_object_selector<Derived, OtherDerived, true>
55 {
57  static bool run(const Derived& x, const OtherDerived&, const typename Derived::RealScalar&)
58  {
59  return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
60  }
61 };
62 
63 template<typename Derived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
64 struct isMuchSmallerThan_scalar_selector
65 {
67  static bool run(const Derived& x, const typename Derived::RealScalar& y, const typename Derived::RealScalar& prec)
68  {
69  return x.cwiseAbs2().sum() <= numext::abs2(prec * y);
70  }
71 };
72 
73 template<typename Derived>
74 struct isMuchSmallerThan_scalar_selector<Derived, true>
75 {
77  static bool run(const Derived& x, const typename Derived::RealScalar&, const typename Derived::RealScalar&)
78  {
79  return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
80  }
81 };
82 
83 } // end namespace internal
84 
85 
103 template<typename Derived>
104 template<typename OtherDerived>
106  const DenseBase<OtherDerived>& other,
107  const RealScalar& prec
108 ) const
109 {
110  return internal::isApprox_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
111 }
112 
126 template<typename Derived>
128  const typename NumTraits<Scalar>::Real& other,
129  const RealScalar& prec
130 ) const
131 {
132  return internal::isMuchSmallerThan_scalar_selector<Derived>::run(derived(), other, prec);
133 }
134 
145 template<typename Derived>
146 template<typename OtherDerived>
148  const DenseBase<OtherDerived>& other,
149  const RealScalar& prec
150 ) const
151 {
152  return internal::isMuchSmallerThan_object_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
153 }
154 
155 } // end namespace Eigen
156 
157 #endif // EIGEN_FUZZY_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:68
bool isMuchSmallerThan(const RealScalar &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
bool isApprox(const DenseBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Fuzzy.h:105
const Scalar & y
bool abs2(bool x)
EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
: InteropHeaders
Definition: Core:139
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231