Eigen  3.3.3
BooleanRedux.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_ALLANDANY_H
00011 #define EIGEN_ALLANDANY_H
00012 
00013 namespace Eigen { 
00014 
00015 namespace internal {
00016 
00017 template<typename Derived, int UnrollCount>
00018 struct all_unroller
00019 {
00020   typedef typename Derived::ExpressionTraits Traits;
00021   enum {
00022     col = (UnrollCount-1) / Traits::RowsAtCompileTime,
00023     row = (UnrollCount-1) % Traits::RowsAtCompileTime
00024   };
00025 
00026   static inline bool run(const Derived &mat)
00027   {
00028     return all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col);
00029   }
00030 };
00031 
00032 template<typename Derived>
00033 struct all_unroller<Derived, 0>
00034 {
00035   static inline bool run(const Derived &/*mat*/) { return true; }
00036 };
00037 
00038 template<typename Derived>
00039 struct all_unroller<Derived, Dynamic>
00040 {
00041   static inline bool run(const Derived &) { return false; }
00042 };
00043 
00044 template<typename Derived, int UnrollCount>
00045 struct any_unroller
00046 {
00047   typedef typename Derived::ExpressionTraits Traits;
00048   enum {
00049     col = (UnrollCount-1) / Traits::RowsAtCompileTime,
00050     row = (UnrollCount-1) % Traits::RowsAtCompileTime
00051   };
00052   
00053   static inline bool run(const Derived &mat)
00054   {
00055     return any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col);
00056   }
00057 };
00058 
00059 template<typename Derived>
00060 struct any_unroller<Derived, 0>
00061 {
00062   static inline bool run(const Derived & /*mat*/) { return false; }
00063 };
00064 
00065 template<typename Derived>
00066 struct any_unroller<Derived, Dynamic>
00067 {
00068   static inline bool run(const Derived &) { return false; }
00069 };
00070 
00071 } // end namespace internal
00072 
00080 template<typename Derived>
00081 inline bool DenseBase<Derived>::all() const
00082 {
00083   typedef internal::evaluator<Derived> Evaluator;
00084   enum {
00085     unroll = SizeAtCompileTime != Dynamic
00086           && SizeAtCompileTime * (Evaluator::CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00087   };
00088   Evaluator evaluator(derived());
00089   if(unroll)
00090     return internal::all_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(evaluator);
00091   else
00092   {
00093     for(Index j = 0; j < cols(); ++j)
00094       for(Index i = 0; i < rows(); ++i)
00095         if (!evaluator.coeff(i, j)) return false;
00096     return true;
00097   }
00098 }
00099 
00104 template<typename Derived>
00105 inline bool DenseBase<Derived>::any() const
00106 {
00107   typedef internal::evaluator<Derived> Evaluator;
00108   enum {
00109     unroll = SizeAtCompileTime != Dynamic
00110           && SizeAtCompileTime * (Evaluator::CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00111   };
00112   Evaluator evaluator(derived());
00113   if(unroll)
00114     return internal::any_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(evaluator);
00115   else
00116   {
00117     for(Index j = 0; j < cols(); ++j)
00118       for(Index i = 0; i < rows(); ++i)
00119         if (evaluator.coeff(i, j)) return true;
00120     return false;
00121   }
00122 }
00123 
00128 template<typename Derived>
00129 inline Eigen::Index DenseBase<Derived>::count() const
00130 {
00131   return derived().template cast<bool>().template cast<Index>().sum();
00132 }
00133 
00138 template<typename Derived>
00139 inline bool DenseBase<Derived>::hasNaN() const
00140 {
00141 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
00142   return derived().array().isNaN().any();
00143 #else
00144   return !((derived().array()==derived().array()).all());
00145 #endif
00146 }
00147 
00152 template<typename Derived>
00153 inline bool DenseBase<Derived>::allFinite() const
00154 {
00155 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
00156   return derived().array().isFinite().all();
00157 #else
00158   return !((derived()-derived()).hasNaN());
00159 #endif
00160 }
00161     
00162 } // end namespace Eigen
00163 
00164 #endif // EIGEN_ALLANDANY_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends