diff --git a/Eigen/src/Core/CoreEvaluators.h b/Eigen/src/Core/CoreEvaluators.h index 4a20312f7..df475094a 100644 --- a/Eigen/src/Core/CoreEvaluators.h +++ b/Eigen/src/Core/CoreEvaluators.h @@ -812,11 +812,11 @@ protected: // -------------------- CwiseUnaryView -------------------- -template -struct unary_evaluator, IndexBased> - : evaluator_base > +template +struct unary_evaluator, IndexBased> + : evaluator_base > { - typedef CwiseUnaryView XprType; + typedef CwiseUnaryView XprType; enum { CoeffReadCost = int(evaluator::CoeffReadCost) + int(functor_traits::Cost), diff --git a/Eigen/src/Core/CwiseUnaryView.h b/Eigen/src/Core/CwiseUnaryView.h index 9fc1dcd0c..b25471154 100644 --- a/Eigen/src/Core/CwiseUnaryView.h +++ b/Eigen/src/Core/CwiseUnaryView.h @@ -15,8 +15,8 @@ namespace Eigen { namespace internal { -template -struct traits > +template +struct traits > : traits { typedef typename result_of< @@ -30,17 +30,22 @@ struct traits > MatrixTypeInnerStride = inner_stride_at_compile_time::ret, // need to cast the sizeof's from size_t to int explicitly, otherwise: // "error: no integral type can represent all of the enumerator values - InnerStrideAtCompileTime = MatrixTypeInnerStride == Dynamic - ? int(Dynamic) - : int(MatrixTypeInnerStride) * int(sizeof(typename traits::Scalar) / sizeof(Scalar)), - OuterStrideAtCompileTime = outer_stride_at_compile_time::ret == Dynamic - ? int(Dynamic) - : outer_stride_at_compile_time::ret * int(sizeof(typename traits::Scalar) / sizeof(Scalar)) + InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0 + ? (MatrixTypeInnerStride == Dynamic + ? int(Dynamic) + : int(MatrixTypeInnerStride) * int(sizeof(typename traits::Scalar) / sizeof(Scalar))) + : int(StrideType::InnerStrideAtCompileTime), + + OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0 + ? (outer_stride_at_compile_time::ret == Dynamic + ? int(Dynamic) + : outer_stride_at_compile_time::ret * int(sizeof(typename traits::Scalar) / sizeof(Scalar))) + : int(StrideType::OuterStrideAtCompileTime) }; }; } -template +template class CwiseUnaryViewImpl; /** \class CwiseUnaryView @@ -56,12 +61,12 @@ class CwiseUnaryViewImpl; * * \sa MatrixBase::unaryViewExpr(const CustomUnaryOp &) const, class CwiseUnaryOp */ -template -class CwiseUnaryView : public CwiseUnaryViewImpl::StorageKind> +template +class CwiseUnaryView : public CwiseUnaryViewImpl::StorageKind> { public: - typedef typename CwiseUnaryViewImpl::StorageKind>::Base Base; + typedef typename CwiseUnaryViewImpl::StorageKind>::Base Base; EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView) typedef typename internal::ref_selector::non_const_type MatrixTypeNested; typedef typename internal::remove_all::type NestedExpression; @@ -93,22 +98,22 @@ class CwiseUnaryView : public CwiseUnaryViewImpl +template class CwiseUnaryViewImpl - : public internal::generic_xpr_base >::type + : public internal::generic_xpr_base >::type { public: - typedef typename internal::generic_xpr_base >::type Base; + typedef typename internal::generic_xpr_base >::type Base; }; -template -class CwiseUnaryViewImpl - : public internal::dense_xpr_base< CwiseUnaryView >::type +template +class CwiseUnaryViewImpl + : public internal::dense_xpr_base< CwiseUnaryView >::type { public: - typedef CwiseUnaryView Derived; - typedef typename internal::dense_xpr_base< CwiseUnaryView >::type Base; + typedef CwiseUnaryView Derived; + typedef typename internal::dense_xpr_base< CwiseUnaryView >::type Base; EIGEN_DENSE_PUBLIC_INTERFACE(Derived) EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl) @@ -118,12 +123,16 @@ class CwiseUnaryViewImpl EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const { - return derived().nestedExpression().innerStride() * sizeof(typename internal::traits::Scalar) / sizeof(Scalar); + return StrideType::InnerStrideAtCompileTime != 0 + ? int(StrideType::InnerStrideAtCompileTime) + : derived().nestedExpression().innerStride() * sizeof(typename internal::traits::Scalar) / sizeof(Scalar); } EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index outerStride() const { - return derived().nestedExpression().outerStride() * sizeof(typename internal::traits::Scalar) / sizeof(Scalar); + return StrideType::OuterStrideAtCompileTime != 0 + ? int(StrideType::OuterStrideAtCompileTime) + : derived().nestedExpression().outerStride() * sizeof(typename internal::traits::Scalar) / sizeof(Scalar); } protected: EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl) diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h index 2f335999a..cb12c77e3 100644 --- a/Eigen/src/Core/util/ForwardDeclarations.h +++ b/Eigen/src/Core/util/ForwardDeclarations.h @@ -78,7 +78,6 @@ template class Transpose; template class Conjugate; template class CwiseNullaryOp; template class CwiseUnaryOp; -template class CwiseUnaryView; template class CwiseBinaryOp; template class CwiseTernaryOp; template class Solve; @@ -108,6 +107,7 @@ template class RefBase; template,OuterStride<> >::type > class Ref; +template> class CwiseUnaryView; template class TriangularBase; template class TriangularView; diff --git a/cmake/EigenSmokeTestList.cmake b/cmake/EigenSmokeTestList.cmake index 0e8f3b424..498d5299a 100644 --- a/cmake/EigenSmokeTestList.cmake +++ b/cmake/EigenSmokeTestList.cmake @@ -61,6 +61,9 @@ set(ei_smoke_test_list mapped_matrix_1 mapstaticmethods_1 mapstride_1 + unaryviewstride_1 + unaryviewstride_2 + unaryviewstride_3 matrix_square_root_1 meta minres_2 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c41855a5d..374b390d0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -194,6 +194,7 @@ ei_add_test(commainitializer) ei_add_test(smallvectors) ei_add_test(mapped_matrix) ei_add_test(mapstride) +ei_add_test(unaryviewstride) ei_add_test(mapstaticmethods) ei_add_test(array_cwise) ei_add_test(array_for_matrix) diff --git a/test/unaryviewstride.cpp b/test/unaryviewstride.cpp new file mode 100644 index 000000000..08d097fb6 --- /dev/null +++ b/test/unaryviewstride.cpp @@ -0,0 +1,39 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2021 Andrew Johnson +// +// This Source Code Form is subject to the terms of the Mozilla +// Public License v. 2.0. If a copy of the MPL was not distributed +// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "main.h" + +template void unaryview_stride(const VectorType& m) +{ + typedef typename VectorType::Scalar Scalar; + Index rows = m.rows(); + Index cols = m.cols(); + VectorType vec = VectorType::Random(rows, cols); + + struct view_op { + EIGEN_EMPTY_STRUCT_CTOR(view_op) + EIGEN_DEVICE_FUNC + EIGEN_STRONG_INLINE const Scalar& + operator()(const Scalar& v) const { return v; } + }; + + CwiseUnaryView> vec_view(vec); + VERIFY(vec_view.outerStride() == (OuterStride == 0 ? 0 : OuterStride)); + VERIFY(vec_view.innerStride() == (InnerStride == 0 ? 1 : InnerStride)); +} + +EIGEN_DECLARE_TEST(unaryviewstride) +{ + CALL_SUBTEST_1(( unaryview_stride<1,2>(MatrixXf()) )); + CALL_SUBTEST_1(( unaryview_stride<0,0>(MatrixXf()) )); + CALL_SUBTEST_2(( unaryview_stride<1,2>(VectorXf()) )); + CALL_SUBTEST_2(( unaryview_stride<0,0>(VectorXf()) )); + CALL_SUBTEST_3(( unaryview_stride<1,2>(RowVectorXf()) )); + CALL_SUBTEST_3(( unaryview_stride<0,0>(RowVectorXf()) )); +}