Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_ALLANDANY_H
0011 #define EIGEN_ALLANDANY_H
0012 
0013 namespace Eigen { 
0014 
0015 namespace internal {
0016 
0017 template<typename Derived, int UnrollCount, int Rows>
0018 struct all_unroller
0019 {
0020   enum {
0021     col = (UnrollCount-1) / Rows,
0022     row = (UnrollCount-1) % Rows
0023   };
0024 
0025   EIGEN_DEVICE_FUNC static inline bool run(const Derived &mat)
0026   {
0027     return all_unroller<Derived, UnrollCount-1, Rows>::run(mat) && mat.coeff(row, col);
0028   }
0029 };
0030 
0031 template<typename Derived, int Rows>
0032 struct all_unroller<Derived, 0, Rows>
0033 {
0034   EIGEN_DEVICE_FUNC static inline bool run(const Derived &/*mat*/) { return true; }
0035 };
0036 
0037 template<typename Derived, int Rows>
0038 struct all_unroller<Derived, Dynamic, Rows>
0039 {
0040   EIGEN_DEVICE_FUNC static inline bool run(const Derived &) { return false; }
0041 };
0042 
0043 template<typename Derived, int UnrollCount, int Rows>
0044 struct any_unroller
0045 {
0046   enum {
0047     col = (UnrollCount-1) / Rows,
0048     row = (UnrollCount-1) % Rows
0049   };
0050   
0051   EIGEN_DEVICE_FUNC static inline bool run(const Derived &mat)
0052   {
0053     return any_unroller<Derived, UnrollCount-1, Rows>::run(mat) || mat.coeff(row, col);
0054   }
0055 };
0056 
0057 template<typename Derived, int Rows>
0058 struct any_unroller<Derived, 0, Rows>
0059 {
0060   EIGEN_DEVICE_FUNC static inline bool run(const Derived & /*mat*/) { return false; }
0061 };
0062 
0063 template<typename Derived, int Rows>
0064 struct any_unroller<Derived, Dynamic, Rows>
0065 {
0066   EIGEN_DEVICE_FUNC static inline bool run(const Derived &) { return false; }
0067 };
0068 
0069 } // end namespace internal
0070 
0071 /** \returns true if all coefficients are true
0072   *
0073   * Example: \include MatrixBase_all.cpp
0074   * Output: \verbinclude MatrixBase_all.out
0075   *
0076   * \sa any(), Cwise::operator<()
0077   */
0078 template<typename Derived>
0079 EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::all() const
0080 {
0081   typedef internal::evaluator<Derived> Evaluator;
0082   enum {
0083     unroll = SizeAtCompileTime != Dynamic
0084           && SizeAtCompileTime * (int(Evaluator::CoeffReadCost) + int(NumTraits<Scalar>::AddCost)) <= EIGEN_UNROLLING_LIMIT
0085   };
0086   Evaluator evaluator(derived());
0087   if(unroll)
0088     return internal::all_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic, internal::traits<Derived>::RowsAtCompileTime>::run(evaluator);
0089   else
0090   {
0091     for(Index j = 0; j < cols(); ++j)
0092       for(Index i = 0; i < rows(); ++i)
0093         if (!evaluator.coeff(i, j)) return false;
0094     return true;
0095   }
0096 }
0097 
0098 /** \returns true if at least one coefficient is true
0099   *
0100   * \sa all()
0101   */
0102 template<typename Derived>
0103 EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::any() const
0104 {
0105   typedef internal::evaluator<Derived> Evaluator;
0106   enum {
0107     unroll = SizeAtCompileTime != Dynamic
0108           && SizeAtCompileTime * (int(Evaluator::CoeffReadCost) + int(NumTraits<Scalar>::AddCost)) <= EIGEN_UNROLLING_LIMIT
0109   };
0110   Evaluator evaluator(derived());
0111   if(unroll)
0112     return internal::any_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic, internal::traits<Derived>::RowsAtCompileTime>::run(evaluator);
0113   else
0114   {
0115     for(Index j = 0; j < cols(); ++j)
0116       for(Index i = 0; i < rows(); ++i)
0117         if (evaluator.coeff(i, j)) return true;
0118     return false;
0119   }
0120 }
0121 
0122 /** \returns the number of coefficients which evaluate to true
0123   *
0124   * \sa all(), any()
0125   */
0126 template<typename Derived>
0127 EIGEN_DEVICE_FUNC inline Eigen::Index DenseBase<Derived>::count() const
0128 {
0129   return derived().template cast<bool>().template cast<Index>().sum();
0130 }
0131 
0132 /** \returns true is \c *this contains at least one Not A Number (NaN).
0133   *
0134   * \sa allFinite()
0135   */
0136 template<typename Derived>
0137 inline bool DenseBase<Derived>::hasNaN() const
0138 {
0139 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
0140   return derived().array().isNaN().any();
0141 #else
0142   return !((derived().array()==derived().array()).all());
0143 #endif
0144 }
0145 
0146 /** \returns true if \c *this contains only finite numbers, i.e., no NaN and no +/-INF values.
0147   *
0148   * \sa hasNaN()
0149   */
0150 template<typename Derived>
0151 inline bool DenseBase<Derived>::allFinite() const
0152 {
0153 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
0154   return derived().array().isFinite().all();
0155 #else
0156   return !((derived()-derived()).hasNaN());
0157 #endif
0158 }
0159     
0160 } // end namespace Eigen
0161 
0162 #endif // EIGEN_ALLANDANY_H