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 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 template <typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
23  EIGEN_DEVICE_FUNC static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec) {
25  typename internal::nested_eval<OtherDerived, 2>::type otherNested(y);
26  return (nested.matrix() - otherNested.matrix()).cwiseAbs2().sum() <=
27  prec * prec * numext::mini(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum());
28  }
29 };
30 
31 template <typename Derived, typename OtherDerived>
32 struct isApprox_selector<Derived, OtherDerived, true> {
33  EIGEN_DEVICE_FUNC static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar&) {
34  return x.matrix() == y.matrix();
35  }
36 };
37 
38 template <typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
40  EIGEN_DEVICE_FUNC static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec) {
41  return x.cwiseAbs2().sum() <= numext::abs2(prec) * y.cwiseAbs2().sum();
42  }
43 };
44 
45 template <typename Derived, typename OtherDerived>
46 struct isMuchSmallerThan_object_selector<Derived, OtherDerived, true> {
47  EIGEN_DEVICE_FUNC static bool run(const Derived& x, const OtherDerived&, const typename Derived::RealScalar&) {
48  return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
49  }
50 };
51 
52 template <typename Derived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
54  EIGEN_DEVICE_FUNC static bool run(const Derived& x, const typename Derived::RealScalar& y,
55  const typename Derived::RealScalar& prec) {
56  return x.cwiseAbs2().sum() <= numext::abs2(prec * y);
57  }
58 };
59 
60 template <typename Derived>
61 struct isMuchSmallerThan_scalar_selector<Derived, true> {
62  EIGEN_DEVICE_FUNC static bool run(const Derived& x, const typename Derived::RealScalar&,
63  const typename Derived::RealScalar&) {
64  return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
65  }
66 };
67 
68 } // end namespace internal
69 
87 template <typename Derived>
88 template <typename OtherDerived>
90  const RealScalar& prec) const {
91  return internal::isApprox_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
92 }
93 
107 template <typename Derived>
109  const RealScalar& prec) const {
110  return internal::isMuchSmallerThan_scalar_selector<Derived>::run(derived(), other, prec);
111 }
112 
123 template <typename Derived>
124 template <typename OtherDerived>
126  const RealScalar& prec) const {
127  return internal::isMuchSmallerThan_object_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
128 }
129 
130 } // end namespace Eigen
131 
132 #endif // EIGEN_FUZZY_H
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:69
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const RealScalar &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
EIGEN_DEVICE_FUNC bool isApprox(const DenseBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Fuzzy.h:89
const Scalar & y
Definition: RandomImpl.h:36
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
Definition: Eigen_Colamd.h:49
double Zero
Definition: pseudosolid_node_update_elements.cc:35
list x
Definition: plotDoE.py:28
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
static EIGEN_DEVICE_FUNC bool run(const Derived &x, const OtherDerived &y, const typename Derived::RealScalar &)
Definition: Fuzzy.h:33
static EIGEN_DEVICE_FUNC bool run(const Derived &x, const OtherDerived &y, const typename Derived::RealScalar &prec)
Definition: Fuzzy.h:23
static EIGEN_DEVICE_FUNC bool run(const Derived &x, const OtherDerived &, const typename Derived::RealScalar &)
Definition: Fuzzy.h:47
static EIGEN_DEVICE_FUNC bool run(const Derived &x, const OtherDerived &y, const typename Derived::RealScalar &prec)
Definition: Fuzzy.h:40
static EIGEN_DEVICE_FUNC bool run(const Derived &x, const typename Derived::RealScalar &, const typename Derived::RealScalar &)
Definition: Fuzzy.h:62
static EIGEN_DEVICE_FUNC bool run(const Derived &x, const typename Derived::RealScalar &y, const typename Derived::RealScalar &prec)
Definition: Fuzzy.h:54
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: TensorTraits.h:152