Eigen  3.2.91
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  && Evaluator::CoeffReadCost != Dynamic
87  && NumTraits<Scalar>::AddCost != Dynamic
88  && SizeAtCompileTime * (Evaluator::CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
89  };
90  Evaluator evaluator(derived());
91  if(unroll)
92  return internal::all_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(evaluator);
93  else
94  {
95  for(Index j = 0; j < cols(); ++j)
96  for(Index i = 0; i < rows(); ++i)
97  if (!evaluator.coeff(i, j)) return false;
98  return true;
99  }
100 }
101 
106 template<typename Derived>
107 inline bool DenseBase<Derived>::any() const
108 {
109  typedef internal::evaluator<Derived> Evaluator;
110  enum {
111  unroll = SizeAtCompileTime != Dynamic
112  && Evaluator::CoeffReadCost != Dynamic
113  && NumTraits<Scalar>::AddCost != Dynamic
114  && SizeAtCompileTime * (Evaluator::CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
115  };
116  Evaluator evaluator(derived());
117  if(unroll)
118  return internal::any_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(evaluator);
119  else
120  {
121  for(Index j = 0; j < cols(); ++j)
122  for(Index i = 0; i < rows(); ++i)
123  if (evaluator.coeff(i, j)) return true;
124  return false;
125  }
126 }
127 
132 template<typename Derived>
133 inline Eigen::Index DenseBase<Derived>::count() const
134 {
135  return derived().template cast<bool>().template cast<Index>().sum();
136 }
137 
142 template<typename Derived>
143 inline bool DenseBase<Derived>::hasNaN() const
144 {
145  return !((derived().array()==derived().array()).all());
146 }
147 
152 template<typename Derived>
153 inline bool DenseBase<Derived>::allFinite() const
154 {
155  return !((derived()-derived()).hasNaN());
156 }
157 
158 } // end namespace Eigen
159 
160 #endif // EIGEN_ALLANDANY_H
bool any() const
Definition: BooleanRedux.h:107
bool allFinite() const
Definition: BooleanRedux.h:153
Definition: LDLT.h:16
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:107
bool all() const
Definition: BooleanRedux.h:81
bool hasNaN() const
Definition: BooleanRedux.h:143
Definition: Eigen_Colamd.h:54
Index count() const
Definition: BooleanRedux.h:133