aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2015-10-21 13:49:13 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2015-10-21 13:49:13 +0200
commit5ca2e25967ee82df3c2347223ad8a5cde5070eb6 (patch)
tree4a0d024d2ae9dfa143f680a82b30e414a1851c97
parent8afd0ce9552e64001012f20c89b6f56daf120896 (diff)
parent0af63493fdb12cea2fdfbabfbddbd4db44587047 (diff)
merge
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/README.md13
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/TensorBase.h26
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/TensorConversion.h4
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h27
-rw-r--r--unsupported/Eigen/CXX11/src/Tensor/TensorMacros.h11
-rw-r--r--unsupported/test/cxx11_tensor_reduction.cpp17
6 files changed, 95 insertions, 3 deletions
diff --git a/unsupported/Eigen/CXX11/src/Tensor/README.md b/unsupported/Eigen/CXX11/src/Tensor/README.md
index 87e57cebb..407485090 100644
--- a/unsupported/Eigen/CXX11/src/Tensor/README.md
+++ b/unsupported/Eigen/CXX11/src/Tensor/README.md
@@ -1149,6 +1149,19 @@ are the smallest of the reduced values.
Reduce a tensor using the prod() operator. The resulting values
are the product of the reduced values.
+### &lt;Operation&gt; all(const Dimensions& new_dims)
+### &lt;Operation&gt; all()
+Reduce a tensor using the all() operator. Casts tensor to bool and then checks
+whether all elements are true. Runs through all elements rather than
+short-circuiting, so may be significantly inefficient.
+
+### &lt;Operation&gt; any(const Dimensions& new_dims)
+### &lt;Operation&gt; any()
+Reduce a tensor using the any() operator. Casts tensor to bool and then checks
+whether any element is true. Runs through all elements rather than
+short-circuiting, so may be significantly inefficient.
+
+
### &lt;Operation&gt; reduce(const Dimensions& new_dims, const Reducer& reducer)
Reduce a tensor using a user-defined reduction operator. See ```SumReducer```
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h b/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h
index 477e4a174..c00f67950 100644
--- a/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h
@@ -363,6 +363,32 @@ class TensorBase<Derived, ReadOnlyAccessors>
return TensorReductionOp<internal::MinReducer<CoeffReturnType>, const DimensionList<Index, NumDimensions>, const Derived>(derived(), in_dims, internal::MinReducer<CoeffReturnType>());
}
+ template <typename Dims> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
+ const TensorReductionOp<internal::AndReducer, const Dims, const TensorConversionOp<bool, const Derived> >
+ all(const Dims& dims) const {
+ return cast<bool>().reduce(dims, internal::AndReducer());
+ }
+
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
+ const TensorReductionOp<internal::AndReducer, const DimensionList<Index, NumDimensions>, const TensorConversionOp<bool, const Derived> >
+ all() const {
+ DimensionList<Index, NumDimensions> in_dims;
+ return cast<bool>().reduce(in_dims, internal::AndReducer());
+ }
+
+ template <typename Dims> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
+ const TensorReductionOp<internal::OrReducer, const Dims, const TensorConversionOp<bool, const Derived> >
+ any(const Dims& dims) const {
+ return cast<bool>().reduce(dims, internal::OrReducer());
+ }
+
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
+ const TensorReductionOp<internal::OrReducer, const DimensionList<Index, NumDimensions>, const TensorConversionOp<bool, const Derived> >
+ any() const {
+ DimensionList<Index, NumDimensions> in_dims;
+ return cast<bool>().reduce(in_dims, internal::OrReducer());
+ }
+
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const TensorTupleReducerOp<
internal::ArgMaxTupleReducer<Tuple<Index, CoeffReturnType> >,
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorConversion.h b/unsupported/Eigen/CXX11/src/Tensor/TensorConversion.h
index 4ca978ab4..e9d3437b7 100644
--- a/unsupported/Eigen/CXX11/src/Tensor/TensorConversion.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorConversion.h
@@ -124,8 +124,8 @@ class TensorConversionOp : public TensorBase<TensorConversionOp<TargetType, XprT
typedef typename internal::traits<TensorConversionOp>::StorageKind StorageKind;
typedef typename internal::traits<TensorConversionOp>::Index Index;
typedef typename internal::nested<TensorConversionOp>::type Nested;
- typedef typename XprType::CoeffReturnType CoeffReturnType;
- typedef typename XprType::PacketReturnType PacketReturnType;
+ typedef Scalar CoeffReturnType;
+ typedef Packet PacketReturnType;
typedef typename NumTraits<Scalar>::Real RealScalar;
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConversionOp(const XprType& xpr)
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h
index ed259399b..a98c6a2e3 100644
--- a/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorFunctors.h
@@ -219,6 +219,33 @@ template <typename T> struct ProdReducer
};
+struct AndReducer
+{
+ static const bool PacketAccess = false;
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(bool t, bool* accum) const {
+ *accum = *accum && t;
+ }
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool initialize() const {
+ return true;
+ }
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool finalize(bool accum) const {
+ return accum;
+ }
+};
+
+struct OrReducer {
+ static const bool PacketAccess = false;
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(bool t, bool* accum) const {
+ *accum = *accum || t;
+ }
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool initialize() const {
+ return false;
+ }
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool finalize(bool accum) const {
+ return accum;
+ }
+};
+
// Argmin/Argmax reducers
template <typename T> struct ArgMaxTupleReducer
{
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorMacros.h b/unsupported/Eigen/CXX11/src/Tensor/TensorMacros.h
index 6d9cc4f38..939de5f11 100644
--- a/unsupported/Eigen/CXX11/src/Tensor/TensorMacros.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorMacros.h
@@ -26,8 +26,17 @@
* void foo(){}
*/
+// SFINAE requires variadic templates
#ifdef EIGEN_HAS_VARIADIC_TEMPLATES
-#define EIGEN_HAS_SFINAE
+ // SFINAE doesn't work for gcc <= 4.7
+ #ifdef EIGEN_COMP_GNUC
+ #if EIGEN_GNUC_AT_LEAST(4,8)
+ #define EIGEN_HAS_SFINAE
+ #endif
+ #else
+ #define EIGEN_HAS_SFINAE
+ #endif
+
#endif
#define EIGEN_SFINAE_ENABLE_IF( __condition__ ) \
diff --git a/unsupported/test/cxx11_tensor_reduction.cpp b/unsupported/test/cxx11_tensor_reduction.cpp
index b2c85a879..e8180c061 100644
--- a/unsupported/test/cxx11_tensor_reduction.cpp
+++ b/unsupported/test/cxx11_tensor_reduction.cpp
@@ -180,6 +180,23 @@ static void test_simple_reductions() {
VERIFY_IS_APPROX(mean1(0), mean2(0));
}
+
+ {
+ Tensor<int, 1> ints(10);
+ std::iota(ints.data(), ints.data() + ints.dimension(0), 0);
+
+ TensorFixedSize<bool, Sizes<1> > all;
+ all = ints.all();
+ VERIFY(!all(0));
+ all = (ints >= ints.constant(0)).all();
+ VERIFY(all(0));
+
+ TensorFixedSize<bool, Sizes<1> > any;
+ any = (ints > ints.constant(10)).any();
+ VERIFY(!any(0));
+ any = (ints < ints.constant(1)).any();
+ VERIFY(any(0));
+ }
}
template <int DataLayout>