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 // Eigen is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Lesser General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 3 of the License, or (at your option) any later version. 00011 // 00012 // Alternatively, you can redistribute it and/or 00013 // modify it under the terms of the GNU General Public License as 00014 // published by the Free Software Foundation; either version 2 of 00015 // the License, or (at your option) any later version. 00016 // 00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY 00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of the GNU Lesser General Public 00023 // License and a copy of the GNU General Public License along with 00024 // Eigen. If not, see <http://www.gnu.org/licenses/>. 00025 00026 #ifndef EIGEN_FUZZY_H 00027 #define EIGEN_FUZZY_H 00028 00029 // TODO support small integer types properly i.e. do exact compare on coeffs --- taking a HS norm is guaranteed to cause integer overflow. 00030 00031 /** \returns \c true if \c *this is approximately equal to \a other, within the precision 00032 * determined by \a prec. 00033 * 00034 * \note The fuzzy compares are done multiplicatively. Two vectors \f$ v \f$ and \f$ w \f$ 00035 * are considered to be approximately equal within precision \f$ p \f$ if 00036 * \f[ \Vert v - w \Vert \leqslant p\,\min(\Vert v\Vert, \Vert w\Vert). \f] 00037 * For matrices, the comparison is done using the Hilbert-Schmidt norm (aka Frobenius norm 00038 * L2 norm). 00039 * 00040 * \note Because of the multiplicativeness of this comparison, one can't use this function 00041 * to check whether \c *this is approximately equal to the zero matrix or vector. 00042 * Indeed, \c isApprox(zero) returns false unless \c *this itself is exactly the zero matrix 00043 * or vector. If you want to test whether \c *this is zero, use internal::isMuchSmallerThan(const 00044 * RealScalar&, RealScalar) instead. 00045 * 00046 * \sa internal::isMuchSmallerThan(const RealScalar&, RealScalar) const 00047 */ 00048 template<typename Derived> 00049 template<typename OtherDerived> 00050 bool DenseBase<Derived>::isApprox( 00051 const DenseBase<OtherDerived>& other, 00052 RealScalar prec 00053 ) const 00054 { 00055 const typename internal::nested<Derived,2>::type nested(derived()); 00056 const typename internal::nested<OtherDerived,2>::type otherNested(other.derived()); 00057 // std::cerr << typeid(Derived).name() << " => " << typeid(typename internal::nested<Derived,2>::type).name() << "\n"; 00058 // std::cerr << typeid(OtherDerived).name() << " => " << typeid(typename internal::nested<OtherDerived,2>::type).name() << "\n"; 00059 // return false; 00060 return (nested - otherNested).cwiseAbs2().sum() <= prec * prec * std::min(nested.cwiseAbs2().sum(), otherNested.cwiseAbs2().sum()); 00061 } 00062 00063 /** \returns \c true if the norm of \c *this is much smaller than \a other, 00064 * within the precision determined by \a prec. 00065 * 00066 * \note The fuzzy compares are done multiplicatively. A vector \f$ v \f$ is 00067 * considered to be much smaller than \f$ x \f$ within precision \f$ p \f$ if 00068 * \f[ \Vert v \Vert \leqslant p\,\vert x\vert. \f] 00069 * 00070 * For matrices, the comparison is done using the Hilbert-Schmidt norm. For this reason, 00071 * the value of the reference scalar \a other should come from the Hilbert-Schmidt norm 00072 * of a reference matrix of same dimensions. 00073 * 00074 * \sa isApprox(), isMuchSmallerThan(const DenseBase<OtherDerived>&, RealScalar) const 00075 */ 00076 template<typename Derived> 00077 bool DenseBase<Derived>::isMuchSmallerThan( 00078 const typename NumTraits<Scalar>::Real& other, 00079 RealScalar prec 00080 ) const 00081 { 00082 return derived().cwiseAbs2().sum() <= prec * prec * other * other; 00083 } 00084 00085 /** \returns \c true if the norm of \c *this is much smaller than the norm of \a other, 00086 * within the precision determined by \a prec. 00087 * 00088 * \note The fuzzy compares are done multiplicatively. A vector \f$ v \f$ is 00089 * considered to be much smaller than a vector \f$ w \f$ within precision \f$ p \f$ if 00090 * \f[ \Vert v \Vert \leqslant p\,\Vert w\Vert. \f] 00091 * For matrices, the comparison is done using the Hilbert-Schmidt norm. 00092 * 00093 * \sa isApprox(), isMuchSmallerThan(const RealScalar&, RealScalar) const 00094 */ 00095 template<typename Derived> 00096 template<typename OtherDerived> 00097 bool DenseBase<Derived>::isMuchSmallerThan( 00098 const DenseBase<OtherDerived>& other, 00099 RealScalar prec 00100 ) const 00101 { 00102 return derived().cwiseAbs2().sum() <= prec * prec * other.derived().cwiseAbs2().sum(); 00103 } 00104 00105 #endif // EIGEN_FUZZY_H
Page generated by Doxygen 1.7.1 for MRPT 0.9.4 SVN: at Mon Jan 10 23:33:19 UTC 2011 |