Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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
15namespace Eigen {
16
17namespace internal {
18
19template<typename Derived, int UnrollCount, int Rows>
20struct all_unroller
21{
22 enum {
23 col = (UnrollCount-1) / Rows,
24 row = (UnrollCount-1) % Rows
25 };
26
27 EIGEN_DEVICE_FUNC static inline bool run(const Derived &mat)
28 {
29 return all_unroller<Derived, UnrollCount-1, Rows>::run(mat) && mat.coeff(row, col);
30 }
31};
32
33template<typename Derived, int Rows>
34struct all_unroller<Derived, 0, Rows>
35{
36 EIGEN_DEVICE_FUNC static inline bool run(const Derived &/*mat*/) { return true; }
37};
38
39template<typename Derived, int Rows>
40struct all_unroller<Derived, Dynamic, Rows>
41{
42 EIGEN_DEVICE_FUNC static inline bool run(const Derived &) { return false; }
43};
44
45template<typename Derived, int UnrollCount, int Rows>
46struct any_unroller
47{
48 enum {
49 col = (UnrollCount-1) / Rows,
50 row = (UnrollCount-1) % Rows
51 };
52
53 EIGEN_DEVICE_FUNC static inline bool run(const Derived &mat)
54 {
55 return any_unroller<Derived, UnrollCount-1, Rows>::run(mat) || mat.coeff(row, col);
56 }
57};
58
59template<typename Derived, int Rows>
60struct any_unroller<Derived, 0, Rows>
61{
62 EIGEN_DEVICE_FUNC static inline bool run(const Derived & /*mat*/) { return false; }
63};
64
65template<typename Derived, int Rows>
66struct any_unroller<Derived, Dynamic, Rows>
67{
68 EIGEN_DEVICE_FUNC static inline bool run(const Derived &) { return false; }
69};
70
71} // end namespace internal
72
80template<typename Derived>
81EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::all() const
82{
83 typedef internal::evaluator<Derived> Evaluator;
84 enum {
85 unroll = SizeAtCompileTime != Dynamic
86 && SizeAtCompileTime * (int(Evaluator::CoeffReadCost) + int(NumTraits<Scalar>::AddCost)) <= EIGEN_UNROLLING_LIMIT
87 };
88 Evaluator evaluator(derived());
89 if(unroll)
90 return internal::all_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic, internal::traits<Derived>::RowsAtCompileTime>::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
104template<typename Derived>
105EIGEN_DEVICE_FUNC inline bool DenseBase<Derived>::any() const
106{
107 typedef internal::evaluator<Derived> Evaluator;
108 enum {
109 unroll = SizeAtCompileTime != Dynamic
110 && SizeAtCompileTime * (int(Evaluator::CoeffReadCost) + int(NumTraits<Scalar>::AddCost)) <= EIGEN_UNROLLING_LIMIT
111 };
112 Evaluator evaluator(derived());
113 if(unroll)
114 return internal::any_unroller<Evaluator, unroll ? int(SizeAtCompileTime) : Dynamic, internal::traits<Derived>::RowsAtCompileTime>::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
128template<typename Derived>
129EIGEN_DEVICE_FUNC inline Eigen::Index DenseBase<Derived>::count() const
130{
131 return derived().template cast<bool>().template cast<Index>().sum();
132}
133
138template<typename Derived>
139EIGEN_DEVICE_FUNC 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
152template<typename Derived>
153EIGEN_DEVICE_FUNC 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
Index count() const
Definition: BooleanRedux.h:129
bool hasNaN() const
Definition: BooleanRedux.h:139
bool any() const
Definition: BooleanRedux.h:105
bool all() const
Definition: BooleanRedux.h:81
bool allFinite() const
Definition: BooleanRedux.h:153
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:189
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
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:235