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)

View File

@ -10,7 +10,7 @@ using namespace Eigen;
void foo() {
MatrixXf m;
CwiseUnaryView<internal::scalar_real_ref_op<double>, CV_QUALIFIER MatrixXf>(m).coeffRef(0, 0) = 1.0f;
CwiseUnaryView<internal::scalar_real_ref_op<float>, CV_QUALIFIER MatrixXf>(m).coeffRef(0, 0) = 1.0f;
}
int main() {}

View File

@ -218,7 +218,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(unaryview)
ei_add_test(mapstaticmethods)
ei_add_test(array_cwise)
ei_add_test(array_for_matrix)

109
test/unaryview.cpp Normal file
View File

@ -0,0 +1,109 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2021 Andrew Johnson <andrew.johnson@arjohnsonau.com>
//
// 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 <int OuterStride, int InnerStride, typename VectorType>
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_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const Scalar& v) const { return v; }
};
CwiseUnaryView<view_op, VectorType, Stride<OuterStride, InnerStride>> vec_view(vec);
VERIFY(vec_view.outerStride() == (OuterStride == 0 ? 0 : OuterStride));
VERIFY(vec_view.innerStride() == (InnerStride == 0 ? 1 : InnerStride));
}
void test_mutable_unaryview() {
struct Vec3 {
double x;
double y;
double z;
};
Eigen::Vector<Vec3, 3> m;
auto x_view = m.unaryViewExpr([](Vec3& v) -> double& { return v.x; });
auto y_view = m.unaryViewExpr([](Vec3& v) -> double& { return v.y; });
auto z_view = m.unaryViewExpr([](Vec3& v) -> double& { return v.z; });
x_view.setConstant(1);
y_view.setConstant(2);
z_view.setConstant(3);
for (int i = 0; i < m.size(); ++i) {
VERIFY_IS_EQUAL(m(i).x, 1);
VERIFY_IS_EQUAL(m(i).y, 2);
VERIFY_IS_EQUAL(m(i).z, 3);
}
}
void test_unaryview_solve() {
// Random upper-triangular system.
Eigen::MatrixXd A = Eigen::MatrixXd::Random(5, 5);
A.triangularView<Eigen::Lower>().setZero();
A.diagonal().setRandom();
Eigen::VectorXd b = Eigen::VectorXd::Random(5);
struct trivial_view_op {
double& operator()(double& x) const { return x; }
const double& operator()(const double& x) const { return x; }
};
// Non-const view:
{
auto b_view = b.unaryViewExpr(trivial_view_op());
b_view(0) = 1; // Allows modification.
Eigen::VectorXd x = A.triangularView<Eigen::Upper>().solve(b_view);
VERIFY_IS_APPROX(A * x, b);
}
// Const view:
{
const auto b_view = b.unaryViewExpr(trivial_view_op());
Eigen::VectorXd x = A.triangularView<Eigen::Upper>().solve(b_view);
VERIFY_IS_APPROX(A * x, b);
}
// Non-const view of const matrix:
{
const Eigen::VectorXd const_b = b;
auto b_view = const_b.unaryViewExpr(trivial_view_op());
Eigen::VectorXd x = A.triangularView<Eigen::Upper>().solve(b_view);
VERIFY_IS_APPROX(A * x, b);
}
// Const view of const matrix:
{
const Eigen::VectorXd const_b = b;
const auto b_view = const_b.unaryViewExpr(trivial_view_op());
Eigen::VectorXd x = A.triangularView<Eigen::Upper>().solve(b_view);
VERIFY_IS_APPROX(A * x, b);
}
// Eigen::MatrixXd out =
// mat_in.real()
// .triangularView<Eigen::Upper>()
// .solve(mat_in.unaryViewExpr([&](const auto& x){ return std::real(x); }));
}
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())));
CALL_SUBTEST_4(test_mutable_unaryview());
CALL_SUBTEST_4(test_unaryview_solve());
}

View File

@ -1,35 +0,0 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2021 Andrew Johnson <andrew.johnson@arjohnsonau.com>
//
// 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 <int OuterStride, int InnerStride, typename VectorType>
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_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const Scalar& v) const { return v; }
};
CwiseUnaryView<view_op, VectorType, Stride<OuterStride, InnerStride>> 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())));
}