Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:56:13

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
0005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_FUZZY_H
0012 #define EIGEN_FUZZY_H
0013 
0014 namespace Eigen { 
0015 
0016 namespace internal
0017 {
0018 
0019 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
0020 struct isApprox_selector
0021 {
0022   EIGEN_DEVICE_FUNC
0023   static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec)
0024   {
0025     typename internal::nested_eval<Derived,2>::type nested(x);
0026     typename internal::nested_eval<OtherDerived,2>::type otherNested(y);
0027     return (nested - otherNested).cwiseAbs2().sum() <= prec * prec * numext::mini(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum());
0028   }
0029 };
0030 
0031 template<typename Derived, typename OtherDerived>
0032 struct isApprox_selector<Derived, OtherDerived, true>
0033 {
0034   EIGEN_DEVICE_FUNC
0035   static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar&)
0036   {
0037     return x.matrix() == y.matrix();
0038   }
0039 };
0040 
0041 template<typename Derived, typename OtherDerived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
0042 struct isMuchSmallerThan_object_selector
0043 {
0044   EIGEN_DEVICE_FUNC
0045   static bool run(const Derived& x, const OtherDerived& y, const typename Derived::RealScalar& prec)
0046   {
0047     return x.cwiseAbs2().sum() <= numext::abs2(prec) * y.cwiseAbs2().sum();
0048   }
0049 };
0050 
0051 template<typename Derived, typename OtherDerived>
0052 struct isMuchSmallerThan_object_selector<Derived, OtherDerived, true>
0053 {
0054   EIGEN_DEVICE_FUNC
0055   static bool run(const Derived& x, const OtherDerived&, const typename Derived::RealScalar&)
0056   {
0057     return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
0058   }
0059 };
0060 
0061 template<typename Derived, bool is_integer = NumTraits<typename Derived::Scalar>::IsInteger>
0062 struct isMuchSmallerThan_scalar_selector
0063 {
0064   EIGEN_DEVICE_FUNC
0065   static bool run(const Derived& x, const typename Derived::RealScalar& y, const typename Derived::RealScalar& prec)
0066   {
0067     return x.cwiseAbs2().sum() <= numext::abs2(prec * y);
0068   }
0069 };
0070 
0071 template<typename Derived>
0072 struct isMuchSmallerThan_scalar_selector<Derived, true>
0073 {
0074   EIGEN_DEVICE_FUNC
0075   static bool run(const Derived& x, const typename Derived::RealScalar&, const typename Derived::RealScalar&)
0076   {
0077     return x.matrix() == Derived::Zero(x.rows(), x.cols()).matrix();
0078   }
0079 };
0080 
0081 } // end namespace internal
0082 
0083 
0084 /** \returns \c true if \c *this is approximately equal to \a other, within the precision
0085   * determined by \a prec.
0086   *
0087   * \note The fuzzy compares are done multiplicatively. Two vectors \f$ v \f$ and \f$ w \f$
0088   * are considered to be approximately equal within precision \f$ p \f$ if
0089   * \f[ \Vert v - w \Vert \leqslant p\,\min(\Vert v\Vert, \Vert w\Vert). \f]
0090   * For matrices, the comparison is done using the Hilbert-Schmidt norm (aka Frobenius norm
0091   * L2 norm).
0092   *
0093   * \note Because of the multiplicativeness of this comparison, one can't use this function
0094   * to check whether \c *this is approximately equal to the zero matrix or vector.
0095   * Indeed, \c isApprox(zero) returns false unless \c *this itself is exactly the zero matrix
0096   * or vector. If you want to test whether \c *this is zero, use internal::isMuchSmallerThan(const
0097   * RealScalar&, RealScalar) instead.
0098   *
0099   * \sa internal::isMuchSmallerThan(const RealScalar&, RealScalar) const
0100   */
0101 template<typename Derived>
0102 template<typename OtherDerived>
0103 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isApprox(
0104   const DenseBase<OtherDerived>& other,
0105   const RealScalar& prec
0106 ) const
0107 {
0108   return internal::isApprox_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
0109 }
0110 
0111 /** \returns \c true if the norm of \c *this is much smaller than \a other,
0112   * within the precision determined by \a prec.
0113   *
0114   * \note The fuzzy compares are done multiplicatively. A vector \f$ v \f$ is
0115   * considered to be much smaller than \f$ x \f$ within precision \f$ p \f$ if
0116   * \f[ \Vert v \Vert \leqslant p\,\vert x\vert. \f]
0117   *
0118   * For matrices, the comparison is done using the Hilbert-Schmidt norm. For this reason,
0119   * the value of the reference scalar \a other should come from the Hilbert-Schmidt norm
0120   * of a reference matrix of same dimensions.
0121   *
0122   * \sa isApprox(), isMuchSmallerThan(const DenseBase<OtherDerived>&, RealScalar) const
0123   */
0124 template<typename Derived>
0125 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isMuchSmallerThan(
0126   const typename NumTraits<Scalar>::Real& other,
0127   const RealScalar& prec
0128 ) const
0129 {
0130   return internal::isMuchSmallerThan_scalar_selector<Derived>::run(derived(), other, prec);
0131 }
0132 
0133 /** \returns \c true if the norm of \c *this is much smaller than the norm of \a other,
0134   * within the precision determined by \a prec.
0135   *
0136   * \note The fuzzy compares are done multiplicatively. A vector \f$ v \f$ is
0137   * considered to be much smaller than a vector \f$ w \f$ within precision \f$ p \f$ if
0138   * \f[ \Vert v \Vert \leqslant p\,\Vert w\Vert. \f]
0139   * For matrices, the comparison is done using the Hilbert-Schmidt norm.
0140   *
0141   * \sa isApprox(), isMuchSmallerThan(const RealScalar&, RealScalar) const
0142   */
0143 template<typename Derived>
0144 template<typename OtherDerived>
0145 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isMuchSmallerThan(
0146   const DenseBase<OtherDerived>& other,
0147   const RealScalar& prec
0148 ) const
0149 {
0150   return internal::isMuchSmallerThan_object_selector<Derived, OtherDerived>::run(derived(), other.derived(), prec);
0151 }
0152 
0153 } // end namespace Eigen
0154 
0155 #endif // EIGEN_FUZZY_H