Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename Derived, int UnrollCount, int InnerSize>
20 struct all_unroller
21 {
22  enum {
23  IsRowMajor = (int(Derived::Flags) & int(RowMajor)),
24  i = (UnrollCount-1) / InnerSize,
25  j = (UnrollCount-1) % InnerSize
26  };
27 
28  EIGEN_DEVICE_FUNC static inline bool run(const Derived &mat)
29  {
30  return all_unroller<Derived, UnrollCount-1, InnerSize>::run(mat) && mat.coeff(IsRowMajor ? i : j, IsRowMajor ? j : i);
31  }
32 };
33 
34 template<typename Derived, int InnerSize>
35 struct all_unroller<Derived, 0, InnerSize>
36 {
37  EIGEN_DEVICE_FUNC static inline bool run(const Derived &/*mat*/) { return true; }
38 };
39 
40 template<typename Derived, int InnerSize>
41 struct all_unroller<Derived, Dynamic, InnerSize>
42 {
43  EIGEN_DEVICE_FUNC static inline bool run(const Derived &) { return false; }
44 };
45 
46 template<typename Derived, int UnrollCount, int InnerSize>
47 struct any_unroller
48 {
49  enum {
50  IsRowMajor = (int(Derived::Flags) & int(RowMajor)),
51  i = (UnrollCount-1) / InnerSize,
52  j = (UnrollCount-1) % InnerSize
53  };
54 
55  EIGEN_DEVICE_FUNC static inline bool run(const Derived &mat)
56  {
57  return any_unroller<Derived, UnrollCount-1, InnerSize>::run(mat) || mat.coeff(IsRowMajor ? i : j, IsRowMajor ? j : i);
58  }
59 };
60 
61 template<typename Derived, int InnerSize>
62 struct any_unroller<Derived, 0, InnerSize>
63 {
64  EIGEN_DEVICE_FUNC static inline bool run(const Derived & /*mat*/) { return false; }
65 };
66 
67 template<typename Derived, int InnerSize>
68 struct any_unroller<Derived, Dynamic, InnerSize>
69 {
70  EIGEN_DEVICE_FUNC static inline bool run(const Derived &) { return false; }
71 };
72 
73 } // end namespace internal
74 
82 template<typename Derived>
83 EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::all() const
84 {
85  typedef internal::evaluator<Derived> Evaluator;
86  enum {
87  unroll = SizeAtCompileTime != Dynamic
88  && SizeAtCompileTime * (int(Evaluator::CoeffReadCost) + int(NumTraits<Scalar>::AddCost)) <= EIGEN_UNROLLING_LIMIT,
89  };
90  Evaluator evaluator(derived());
91  if(unroll)
92  return internal::all_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic, InnerSizeAtCompileTime>::run(evaluator);
93  else
94  {
95  for(Index i = 0; i < derived().outerSize(); ++i)
96  for(Index j = 0; j < derived().innerSize(); ++j)
97  if (!evaluator.coeff(IsRowMajor ? i : j, IsRowMajor ? j : i)) return false;
98  return true;
99  }
100 }
101 
106 template<typename Derived>
107 EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::any() const
108 {
109  typedef internal::evaluator<Derived> Evaluator;
110  enum {
111  unroll = SizeAtCompileTime != Dynamic
112  && SizeAtCompileTime * (int(Evaluator::CoeffReadCost) + int(NumTraits<Scalar>::AddCost)) <= EIGEN_UNROLLING_LIMIT,
113  };
114  Evaluator evaluator(derived());
115  if(unroll)
116  return internal::any_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic, InnerSizeAtCompileTime>::run(evaluator);
117  else
118  {
119  for(Index i = 0; i < derived().outerSize(); ++i)
120  for(Index j = 0; j < derived().innerSize(); ++j)
121  if (evaluator.coeff(IsRowMajor ? i : j, IsRowMajor ? j : i)) return true;
122  return false;
123  }
124 }
125 
130 template<typename Derived>
131 EIGEN_DEVICE_FUNC inline Eigen::Index DenseBase<Derived>::count() const
132 {
133  return derived().template cast<bool>().template cast<Index>().sum();
134 }
135 
140 template<typename Derived>
141 EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::hasNaN() const
142 {
143 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
144  return derived().array().isNaN().any();
145 #else
146  return !((derived().array()==derived().array()).all());
147 #endif
148 }
149 
154 template<typename Derived>
155 EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::allFinite() const
156 {
157 #if EIGEN_COMP_MSVC || (defined __FAST_MATH__)
158  return derived().array().isFinite().all();
159 #else
160  return !((derived()-derived()).hasNaN());
161 #endif
162 }
163 
164 } // end namespace Eigen
165 
166 #endif // EIGEN_ALLANDANY_H
Index count() const
Definition: BooleanRedux.h:131
bool hasNaN() const
Definition: BooleanRedux.h:141
bool any() const
Definition: BooleanRedux.h:107
bool all() const
Definition: BooleanRedux.h:83
bool allFinite() const
Definition: BooleanRedux.h:155
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:189
@ RowMajor
Definition: Constants.h:323
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231