Fix CwiseUnaryView.

This commit is contained in:
Antonio Sánchez
2024-03-11 19:08:30 +00:00
committed by Rasmus Munk Larsen
parent 352ede96e4
commit 1d4369c2ff
7 changed files with 154 additions and 44 deletions

View File

@@ -18,7 +18,9 @@ namespace Eigen {
namespace internal {
template <typename ViewOp, typename MatrixType, typename StrideType>
struct traits<CwiseUnaryView<ViewOp, MatrixType, StrideType> > : traits<MatrixType> {
typedef typename result_of<ViewOp(const typename traits<MatrixType>::Scalar&)>::type Scalar;
typedef typename std::result_of<ViewOp(typename traits<MatrixType>::Scalar&)>::type ScalarRef;
static_assert(std::is_reference<ScalarRef>::value, "Views must return a reference type.");
typedef remove_all_t<ScalarRef> Scalar;
typedef typename MatrixType::Nested MatrixTypeNested;
typedef remove_all_t<MatrixTypeNested> MatrixTypeNested_;
enum {
@@ -112,7 +114,7 @@ class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense>
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryViewImpl)
EIGEN_DEVICE_FUNC inline Scalar* data() { return &(this->coeffRef(0)); }
EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeff(0)); }
EIGEN_DEVICE_FUNC inline const Scalar* data() const { return &(this->coeffRef(0)); }
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index innerStride() const {
return StrideType::InnerStrideAtCompileTime != 0
@@ -128,8 +130,25 @@ class CwiseUnaryViewImpl<ViewOp, MatrixType, StrideType, Dense>
sizeof(Scalar);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col) {
return internal::evaluator<Derived>(derived()).coeffRef(row, col);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index) {
return internal::evaluator<Derived>(derived()).coeffRef(index);
}
protected:
EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(CwiseUnaryViewImpl)
// Allow const access to coeffRef for the case of direct access being enabled.
EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index index) const {
return const_cast<CwiseUnaryViewImpl*>(this)->coeffRef(index);
}
EIGEN_DEVICE_FUNC inline const Scalar& coeffRef(Index row, Index col) const {
return const_cast<CwiseUnaryViewImpl*>(this)->coeffRef(row, col);
}
};
} // end namespace Eigen

View File

@@ -286,9 +286,10 @@ struct functor_traits<scalar_imag_op<Scalar>> {
template <typename Scalar>
struct scalar_real_ref_op {
typedef typename NumTraits<Scalar>::Real result_type;
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type& operator()(const Scalar& a) const {
return numext::real_ref(*const_cast<Scalar*>(&a));
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type& operator()(const Scalar& a) const {
return numext::real_ref(a);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type& operator()(Scalar& a) const { return numext::real_ref(a); }
};
template <typename Scalar>
struct functor_traits<scalar_real_ref_op<Scalar>> {
@@ -303,8 +304,9 @@ struct functor_traits<scalar_real_ref_op<Scalar>> {
template <typename Scalar>
struct scalar_imag_ref_op {
typedef typename NumTraits<Scalar>::Real result_type;
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type& operator()(const Scalar& a) const {
return numext::imag_ref(*const_cast<Scalar*>(&a));
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type& operator()(Scalar& a) const { return numext::imag_ref(a); }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const result_type& operator()(const Scalar& a) const {
return numext::imag_ref(a);
}
};
template <typename Scalar>

View File

@@ -118,7 +118,7 @@ EIGEN_DEVICE_FUNC inline const CwiseUnaryOp<CustomUnaryOp, const Derived> unaryE
return CwiseUnaryOp<CustomUnaryOp, const Derived>(derived(), func);
}
/// \returns an expression of a custom coefficient-wise unary operator \a func of *this
/// \returns a const expression of a custom coefficient-wise unary operator \a func of *this
///
/// The template parameter \a CustomUnaryOp is the type of the functor
/// of the custom unary operator.
@@ -137,6 +137,21 @@ EIGEN_DEVICE_FUNC inline const CwiseUnaryView<CustomViewOp, const Derived> unary
return CwiseUnaryView<CustomViewOp, const Derived>(derived(), func);
}
/// \returns a non-const expression of a custom coefficient-wise unary view \a func of *this
///
/// The template parameter \a CustomUnaryOp is the type of the functor
/// of the custom unary operator.
///
EIGEN_DOC_UNARY_ADDONS(unaryViewExpr, unary function)
///
/// \sa unaryExpr, binaryExpr class CwiseUnaryOp
///
template <typename CustomViewOp>
EIGEN_DEVICE_FUNC inline CwiseUnaryView<CustomViewOp, Derived> unaryViewExpr(
const CustomViewOp& func = CustomViewOp()) {
return CwiseUnaryView<CustomViewOp, Derived>(derived(), func);
}
/// \returns a non const expression of the real part of \c *this.
///
EIGEN_DOC_UNARY_ADDONS(real, real part function)