Eigen  3.3.7
BooleanRedux.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_ALLANDANY_H
11 #define EIGEN_ALLANDANY_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename Derived, int UnrollCount>
18 struct all_unroller
19 {
20  typedef typename Derived::ExpressionTraits Traits;
21  enum {
22  col = (UnrollCount-1) / Traits::RowsAtCompileTime,
23  row = (UnrollCount-1) % Traits::RowsAtCompileTime
24  };
25 
26  static inline bool run(const Derived &mat)
27  {
28  return all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col);
29  }
30 };
31 
32 template<typename Derived>
33 struct all_unroller<Derived, 0>
34 {
35  static inline bool run(const Derived &/*mat*/) { return true; }
36 };
37 
38 template<typename Derived>
39 struct all_unroller<Derived, Dynamic>
40 {
41  static inline bool run(const Derived &) { return false; }
42 };
43 
44 template<typename Derived, int UnrollCount>
45 struct any_unroller
46 {
47  typedef typename Derived::ExpressionTraits Traits;
48  enum {
49  col = (UnrollCount-1) / Traits::RowsAtCompileTime,
50  row = (UnrollCount-1) % Traits::RowsAtCompileTime
51  };
52 
53  static inline bool run(const Derived &mat)
54  {
55  return any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col);
56  }
57 };
58 
59 template<typename Derived>
60 struct any_unroller<Derived, 0>
61 {
62  static inline bool run(const Derived & /*mat*/) { return false; }
63 };
64 
65 template<typename Derived>
66 struct any_unroller<Derived, Dynamic>
67 {
68  static inline bool run(const Derived &) { return false; }
69 };
70 
71 } // end namespace internal
72 
80 template<typename Derived>
81 inline bool DenseBase<Derived>::all() const
82 {
83  typedef internal::evaluator<Derived> Evaluator;
84  enum {
85  unroll = SizeAtCompileTime != Dynamic
86  && SizeAtCompileTime * (Evaluator::CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
87  };
88  Evaluator evaluator(derived());
89  if(unroll)
90  return internal::all_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(evaluator);
91  else
92  {
93  for(Index j = 0; j < cols(); ++j)
94  for(Index i = 0; i < rows(); ++i)
95  if (!evaluator.coeff(i, j)) return false;
96  return true;
97  }
98 }
99 
104 template<typename Derived>
105 inline bool DenseBase<Derived>::any() const
106 {
107  typedef internal::evaluator<Derived> Evaluator;
108  enum {
109  unroll = SizeAtCompileTime != Dynamic
110  && SizeAtCompileTime * (Evaluator::CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
111  };
112  Evaluator evaluator(derived());
113  if(unroll)
114  return internal::any_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(evaluator);
115  else
116  {
117  for(Index j = 0; j < cols(); ++j)
118  for(Index i = 0; i < rows(); ++i)
119  if (evaluator.coeff(i, j)) return true;
120  return false;
121  }
122 }
123 
128 template<typename Derived>
130 {
131  return derived().template cast<bool>().template cast<Index>().sum();
132 }
133 
138 template<typename Derived>
139 inline bool DenseBase<Derived>::hasNaN() const
140 {
141 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
142  return derived().array().isNaN().any();
143 #else
144  return !((derived().array()==derived().array()).all());
145 #endif
146 }
147 
152 template<typename Derived>
153 inline bool DenseBase<Derived>::allFinite() const
154 {
155 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
156  return derived().array().isFinite().all();
157 #else
158  return !((derived()-derived()).hasNaN());
159 #endif
160 }
161 
162 } // end namespace Eigen
163 
164 #endif // EIGEN_ALLANDANY_H
bool allFinite() const
Definition: BooleanRedux.h:153
Namespace containing all symbols from the Eigen library.
Definition: Core:306
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
bool hasNaN() const
Definition: BooleanRedux.h:139
bool all() const
Definition: BooleanRedux.h:81
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
bool any() const
Definition: BooleanRedux.h:105
Definition: Eigen_Colamd.h:50
Index count() const
Definition: BooleanRedux.h:129
const int Dynamic
Definition: Constants.h:21