![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> 00005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> 00006 // 00007 // This Source Code Form is subject to the terms of the Mozilla 00008 // Public License v. 2.0. If a copy of the MPL was not distributed 00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00010 00011 #ifndef EIGEN_FUZZY_H 00012 #define EIGEN_FUZZY_H 00013 00014 namespace Eigen { 00015 00016 namespace internal 00017 { 00018 00019 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger> 00020 struct isApprox_selector 00021 { 00022 EIGEN_DEVICE_FUNC 00023 static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec) 00024 { 00025 typename internal::nested_eval<Derived,2>::type nested(x); 00026 typename internal::nested_eval<OtherDerived,2>::type otherNested(y); 00027 return (nested - otherNested).cwiseAbs2().sum() <= prec * prec * numext::mini(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum()); 00028 } 00029 }; 00030 00031 template<typename Derived, typename OtherDerived> 00032 struct isApprox_selector<Derived, OtherDerived, true> 00033 { 00034 EIGEN_DEVICE_FUNC 00035 static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar&) 00036 { 00037 return x.matrix() == y.matrix(); 00038 } 00039 }; 00040 00041 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger> 00042 struct isMuchSmallerThan_object_selector 00043 { 00044 EIGEN_DEVICE_FUNC 00045 static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec) 00046 { 00047 return x.cwiseAbs2().sum() <= numext::abs2(prec) * y.cwiseAbs2().sum(); 00048 } 00049 }; 00050 00051 template<typename Derived, typename OtherDerived> 00052 struct isMuchSmallerThan_object_selector<Derived, OtherDerived, true> 00053 { 00054 EIGEN_DEVICE_FUNC 00055 static bool run(const Derived& x, const OtherDerived&, const typename Derived::RealScalar&) 00056 { 00057 return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix(); 00058 } 00059 }; 00060 00061 template<typename Derived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger> 00062 struct isMuchSmallerThan_scalar_selector 00063 { 00064 EIGEN_DEVICE_FUNC 00065 static bool run(const Derived& x, const typename Derived::RealScalar& y, const typename Derived::RealScalar& prec) 00066 { 00067 return x.cwiseAbs2().sum() <= numext::abs2(prec * y); 00068 } 00069 }; 00070 00071 template<typename Derived> 00072 struct isMuchSmallerThan_scalar_selector<Derived, true> 00073 { 00074 EIGEN_DEVICE_FUNC 00075 static bool run(const Derived& x, const typename Derived::RealScalar&, const typename Derived::RealScalar&) 00076 { 00077 return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix(); 00078 } 00079 }; 00080 00081 } // end namespace internal 00082 00083 00101 template<typename Derived> 00102 template<typename OtherDerived> 00103 bool DenseBase<Derived>::isApprox( 00104 const DenseBase<OtherDerived>& other, 00105 const RealScalar& prec 00106 ) const 00107 { 00108 return internal::isApprox_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec); 00109 } 00110 00124 template<typename Derived> 00125 bool DenseBase<Derived>::isMuchSmallerThan( 00126 const typename NumTraits<Scalar>::Real& other, 00127 const RealScalar& prec 00128 ) const 00129 { 00130 return internal::isMuchSmallerThan_scalar_selector<Derived>::run(derived(), other, prec); 00131 } 00132 00143 template<typename Derived> 00144 template<typename OtherDerived> 00145 bool DenseBase<Derived>::isMuchSmallerThan( 00146 const DenseBase<OtherDerived>& other, 00147 const RealScalar& prec 00148 ) const 00149 { 00150 return internal::isMuchSmallerThan_object_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec); 00151 } 00152 00153 } // end namespace Eigen 00154 00155 #endif // EIGEN_FUZZY_H