blob: a01dda3bc8cee7c5a81bc43fbdd10939dffcf222 [file] [edit]
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2013 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// 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/.
// SPDX-License-Identifier: MPL-2.0
// This unit test cannot be easily written to work with EIGEN_DEFAULT_TO_ROW_MAJOR
#ifdef EIGEN_DEFAULT_TO_ROW_MAJOR
#undef EIGEN_DEFAULT_TO_ROW_MAJOR
#endif
#define TEST_ENABLE_TEMPORARY_TRACKING
#define TEST_CHECK_STATIC_ASSERTIONS
#include "main.h"
#include "random_for_arithmetic.h"
// test Ref.h
// Deal with i387 extended precision
#if EIGEN_ARCH_i386 && !(EIGEN_ARCH_x86_64)
#if EIGEN_COMP_GNUC_STRICT
#pragma GCC optimize("-ffloat-store")
#else
#undef VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(X, Y) VERIFY_IS_APPROX(X, Y)
#endif
#endif
template <typename MatrixType>
void ref_matrix(const MatrixType &m) {
using Scalar = typename MatrixType::Scalar;
using RealScalar = typename MatrixType::RealScalar;
using DynMatrixType = Matrix<Scalar, Dynamic, Dynamic, MatrixType::Options>;
using RealDynMatrixType = Matrix<RealScalar, Dynamic, Dynamic, MatrixType::Options>;
using RefMat = Ref<MatrixType>;
using RefDynMat = Ref<DynMatrixType>;
using ConstRefDynMat = Ref<const DynMatrixType>;
using RefRealMatWithStride = Ref<RealDynMatrixType, 0, Stride<Dynamic, Dynamic>>;
Index rows = m.rows(), cols = m.cols();
MatrixType m1 = random_for_arithmetic<MatrixType>(rows, cols), m2 = m1;
Index i = internal::random<Index>(0, rows - 1);
Index j = internal::random<Index>(0, cols - 1);
Index brows = internal::random<Index>(1, rows - i);
Index bcols = internal::random<Index>(1, cols - j);
RefMat rm0 = m1;
VERIFY_IS_EQUAL(rm0, m1);
RefDynMat rm1 = m1;
VERIFY_IS_EQUAL(rm1, m1);
RefDynMat rm2 = m1.block(i, j, brows, bcols);
VERIFY_IS_EQUAL(rm2, m1.block(i, j, brows, bcols));
rm2.setOnes();
m2.block(i, j, brows, bcols).setOnes();
VERIFY_IS_EQUAL(m1, m2);
m2.block(i, j, brows, bcols) = random_for_arithmetic<DynMatrixType>(brows, bcols);
rm2 = m2.block(i, j, brows, bcols);
VERIFY_IS_EQUAL(m1, m2);
ConstRefDynMat rm3 = m1.block(i, j, brows, bcols);
m1.block(i, j, brows, bcols) *= 2;
m2.block(i, j, brows, bcols) *= 2;
VERIFY_IS_EQUAL(rm3, m2.block(i, j, brows, bcols));
RefRealMatWithStride rm4 = m1.real();
VERIFY_IS_EQUAL(rm4, m2.real());
rm4.array() += 1;
m2.real().array() += 1;
VERIFY_IS_EQUAL(m1, m2);
}
template <typename VectorType>
void ref_vector(const VectorType &m) {
using Scalar = typename VectorType::Scalar;
using RealScalar = typename VectorType::RealScalar;
using DynMatrixType = Matrix<Scalar, Dynamic, 1, VectorType::Options>;
using MatrixType = Matrix<Scalar, Dynamic, Dynamic, ColMajor>;
using RealDynMatrixType = Matrix<RealScalar, Dynamic, 1, VectorType::Options>;
using RefMat = Ref<VectorType>;
using RefDynMat = Ref<DynMatrixType>;
using ConstRefDynMat = Ref<const DynMatrixType>;
using RefRealMatWithStride = Ref<RealDynMatrixType, 0, InnerStride<>>;
using RefMatWithStride = Ref<DynMatrixType, 0, InnerStride<>>;
Index size = m.size();
VectorType v1 = random_for_arithmetic<VectorType>(size), v2 = v1;
MatrixType mat1 = random_for_arithmetic<MatrixType>(size, size), mat2 = mat1,
mat3 = random_for_arithmetic<MatrixType>(size, size);
Index i = internal::random<Index>(0, size - 1);
Index bsize = internal::random<Index>(1, size - i);
{
RefMat rm0 = v1;
VERIFY_IS_EQUAL(rm0, v1);
}
{
RefMat rm0 = v1.block(0, 0, size, 1);
VERIFY_IS_EQUAL(rm0, v1);
}
{
RefDynMat rv1 = v1;
VERIFY_IS_EQUAL(rv1, v1);
}
{
RefDynMat rv1 = v1.block(0, 0, size, 1);
VERIFY_IS_EQUAL(rv1, v1);
}
RefDynMat rv2 = v1.segment(i, bsize);
VERIFY_IS_EQUAL(rv2, v1.segment(i, bsize));
rv2.setOnes();
v2.segment(i, bsize).setOnes();
VERIFY_IS_EQUAL(v1, v2);
v2.segment(i, bsize) = random_for_arithmetic<DynMatrixType>(bsize);
rv2 = v2.segment(i, bsize);
VERIFY_IS_EQUAL(v1, v2);
ConstRefDynMat rm3 = v1.segment(i, bsize);
v1.segment(i, bsize) *= 2;
v2.segment(i, bsize) *= 2;
VERIFY_IS_EQUAL(rm3, v2.segment(i, bsize));
RefRealMatWithStride rm4 = v1.real();
VERIFY_IS_EQUAL(rm4, v2.real());
rm4.array() += 1;
v2.real().array() += 1;
VERIFY_IS_EQUAL(v1, v2);
RefMatWithStride rm5 = mat1.row(i).transpose();
VERIFY_IS_EQUAL(rm5, mat1.row(i).transpose());
rm5.array() += 1;
mat2.row(i).array() += 1;
VERIFY_IS_EQUAL(mat1, mat2);
rm5.noalias() = rm4.transpose() * mat3;
mat2.row(i) = v2.real().transpose() * mat3;
VERIFY_IS_APPROX(mat1, mat2);
}
template <typename Scalar, int Rows, int Cols>
void ref_vector_fixed_sizes() {
using RowMajorMatrixType = Matrix<Scalar, Rows, Cols, RowMajor>;
using ColMajorMatrixType = Matrix<Scalar, Rows, Cols, ColMajor>;
using RowVectorType = Matrix<Scalar, 1, Cols>;
using ColVectorType = Matrix<Scalar, Rows, 1>;
using RowVectorTransposeType = Matrix<Scalar, Cols, 1>;
using ColVectorTransposeType = Matrix<Scalar, 1, Rows>;
using DynamicStride = Stride<Dynamic, Dynamic>;
RowMajorMatrixType mr = RowMajorMatrixType::Random();
ColMajorMatrixType mc = ColMajorMatrixType::Random();
Index i = internal::random<Index>(0, Rows - 1);
Index j = internal::random<Index>(0, Cols - 1);
// Reference ith row.
Ref<RowVectorType, 0, DynamicStride> mr_ri = mr.row(i);
VERIFY_IS_EQUAL(mr_ri, mr.row(i));
Ref<RowVectorType, 0, DynamicStride> mc_ri = mc.row(i);
VERIFY_IS_EQUAL(mc_ri, mc.row(i));
// Reference jth col.
Ref<ColVectorType, 0, DynamicStride> mr_cj = mr.col(j);
VERIFY_IS_EQUAL(mr_cj, mr.col(j));
Ref<ColVectorType, 0, DynamicStride> mc_cj = mc.col(j);
VERIFY_IS_EQUAL(mc_cj, mc.col(j));
// Reference the transpose of row i.
Ref<RowVectorTransposeType, 0, DynamicStride> mr_rit = mr.row(i);
VERIFY_IS_EQUAL(mr_rit, mr.row(i).transpose());
Ref<RowVectorTransposeType, 0, DynamicStride> mc_rit = mc.row(i);
VERIFY_IS_EQUAL(mc_rit, mc.row(i).transpose());
// Reference the transpose of col j.
Ref<ColVectorTransposeType, 0, DynamicStride> mr_cjt = mr.col(j);
VERIFY_IS_EQUAL(mr_cjt, mr.col(j).transpose());
Ref<ColVectorTransposeType, 0, DynamicStride> mc_cjt = mc.col(j);
VERIFY_IS_EQUAL(mc_cjt, mc.col(j).transpose());
// Const references without strides.
Ref<const RowVectorType> cmr_ri = mr.row(i);
VERIFY_IS_EQUAL(cmr_ri, mr.row(i));
Ref<const RowVectorType> cmc_ri = mc.row(i);
VERIFY_IS_EQUAL(cmc_ri, mc.row(i));
Ref<const ColVectorType> cmr_cj = mr.col(j);
VERIFY_IS_EQUAL(cmr_cj, mr.col(j));
Ref<const ColVectorType> cmc_cj = mc.col(j);
VERIFY_IS_EQUAL(cmc_cj, mc.col(j));
Ref<const RowVectorTransposeType> cmr_rit = mr.row(i);
VERIFY_IS_EQUAL(cmr_rit, mr.row(i).transpose());
Ref<const RowVectorTransposeType> cmc_rit = mc.row(i);
VERIFY_IS_EQUAL(cmc_rit, mc.row(i).transpose());
Ref<const ColVectorTransposeType> cmr_cjt = mr.col(j);
VERIFY_IS_EQUAL(cmr_cjt, mr.col(j).transpose());
Ref<const ColVectorTransposeType> cmc_cjt = mc.col(j);
VERIFY_IS_EQUAL(cmc_cjt, mc.col(j).transpose());
}
template <typename PlainObjectType>
void check_const_correctness(const PlainObjectType &) {
// verify that ref-to-const don't have LvalueBit
using ConstPlainObjectType = std::add_const_t<PlainObjectType>;
VERIFY(!(internal::traits<Ref<ConstPlainObjectType>>::Flags & LvalueBit));
VERIFY(!(internal::traits<Ref<ConstPlainObjectType, Aligned>>::Flags & LvalueBit));
VERIFY(!(Ref<ConstPlainObjectType>::Flags & LvalueBit));
VERIFY(!(Ref<ConstPlainObjectType, Aligned>::Flags & LvalueBit));
}
void test_ref_alignment_matching() {
using PlainObject = Matrix<float, Dynamic, 1, DontAlign>;
using UnalignedMap = Map<PlainObject, Unaligned>;
using Aligned8Map = Map<PlainObject, Aligned8>;
using Aligned16Map = Map<PlainObject, Aligned16>;
using Aligned32Map = Map<PlainObject, Aligned32>;
using UnalignedRefTraits = internal::traits<Ref<PlainObject, Unaligned>>;
using Aligned16RefTraits = internal::traits<Ref<PlainObject, Aligned16>>;
static_assert(int(internal::traits<PlainObject>::Alignment) == int(Unaligned),
"the test plain object must not provide an alignment guarantee");
static_assert(UnalignedRefTraits::template match<UnalignedMap>::AlignmentMatch,
"an unaligned Ref must accept an unaligned source");
static_assert(!Aligned16RefTraits::template match<UnalignedMap>::AlignmentMatch,
"an aligned Ref must reject a source without an alignment guarantee");
static_assert(!Aligned16RefTraits::template match<Aligned8Map>::AlignmentMatch,
"the source alignment must satisfy the Ref alignment");
static_assert(Aligned16RefTraits::template match<Aligned16Map>::AlignmentMatch,
"a Ref must accept a source with matching alignment");
static_assert(Aligned16RefTraits::template match<Aligned32Map>::AlignmentMatch,
"a Ref must accept a source with stronger alignment");
alignas(16) float data[8] = {};
Aligned16Map map(data, 8);
Ref<PlainObject, Aligned16> ref(map);
VERIFY(ref.data() == data);
}
// A runtime stride of 0 repeats a coefficient; a Ref must not read it as the default stride.
void test_ref_zero_strides() {
const float data[6] = {1, 2, 3, 4, 5, 6};
Map<const VectorXf, 0, InnerStride<>> repeated(data, 4, InnerStride<>(0));
const Ref<const VectorXf, 0, InnerStride<>> repeatedRef(repeated);
VERIFY_IS_EQUAL(repeatedRef, VectorXf::Constant(4, data[0]));
Map<const MatrixXf, 0, OuterStride<>> repeatedCols(data, 2, 3, OuterStride<>(0));
const MatrixXf expectedCols = Vector2f(data[0], data[1]).replicate(1, 3);
const Ref<const MatrixXf, 0, OuterStride<>> colsRef(repeatedCols);
VERIFY_IS_EQUAL(colsRef, expectedCols);
const Ref<const MatrixXf, 0, Stride<Dynamic, Dynamic>> colsStrideRef(repeatedCols);
VERIFY_IS_EQUAL(colsStrideRef, expectedCols);
const VectorXf v = VectorXf::Random(5);
const Ref<const VectorXf, 0, InnerStride<>> indexedRef(v(seqN(2, 3, fix<0>)));
VERIFY_IS_EQUAL(indexedRef, VectorXf::Constant(3, v(2)));
}
// A Ref whose outer stride is the default (0) must report inner * rows (inner * cols if row-major), the
// layout Map and the direct-bind test in Ref::construct assume. Verifies reads, writes, and reuse of the strides
// of a Ref bound to `src`.
template <typename RefType, typename Src>
void check_ref_default_outer_stride(Src &src, bool expect_direct) {
using Plain = typename Src::PlainObject;
const Plain expected = src;
RefType r(src);
VERIFY_IS_EQUAL(r.rows(), expected.rows());
VERIFY_IS_EQUAL(r.cols(), expected.cols());
if (expect_direct) {
VERIFY(r.data() == src.data());
VERIFY_IS_EQUAL(r.innerStride(), src.innerStride());
VERIFY_IS_EQUAL(r.outerStride(), src.outerStride());
}
VERIFY_IS_EQUAL(r, expected);
for (Index j = 0; j < expected.cols(); ++j)
for (Index i = 0; i < expected.rows(); ++i) VERIFY_IS_EQUAL(r.coeff(i, j), expected.coeff(i, j));
// Pass the strides on to a Map.
Map<const Plain, 0, Stride<Dynamic, Dynamic>> m(r.data(), r.rows(), r.cols(),
Stride<Dynamic, Dynamic>(r.outerStride(), r.innerStride()));
VERIFY_IS_EQUAL(m, expected);
// Pass the Ref on to a product.
VERIFY_IS_APPROX((r.transpose() * r).eval(), (expected.transpose() * expected).eval());
VERIFY_IS_APPROX((r * expected.transpose()).eval(), (expected * expected.transpose()).eval());
}
template <typename RefType, typename Src>
void check_ref_default_outer_stride_write(Src &src) {
using Plain = typename Src::PlainObject;
const Plain values = Plain::Random(src.rows(), src.cols());
RefType r(src);
r = values;
VERIFY_IS_EQUAL(src, values);
r.setZero();
VERIFY(src.isZero());
}
// Every second row of a column-major (column of a row-major) matrix, and the transposed selection.
template <typename Mat>
auto every_other_inner(Mat &A, std::false_type) {
return A(seq(0, Eigen::placeholders::last, 2), Eigen::placeholders::all);
}
template <typename Mat>
auto every_other_inner(Mat &A, std::true_type) {
return A(Eigen::placeholders::all, seq(0, Eigen::placeholders::last, 2));
}
// Matrix of runtime size whose storage is interleaved with `stride` unused coefficients.
template <typename Mat>
void check_ref_default_outer_stride_matrix(Index rows, Index cols) {
using Scalar = typename Mat::Scalar;
using RefDyn = Ref<Mat, 0, InnerStride<>>;
using CRefDyn = Ref<const Mat, 0, InnerStride<>>;
using Plain = Matrix<Scalar, Dynamic, Dynamic, Mat::Options>;
for (Index s = 1; s <= 3; ++s) {
Plain buf = Plain::Random(s * rows * cols, 1);
Map<Mat, 0, InnerStride<>> m(buf.data(), rows, cols, InnerStride<>(s));
check_ref_default_outer_stride<RefDyn>(m, true);
check_ref_default_outer_stride<CRefDyn>(m, true);
check_ref_default_outer_stride_write<RefDyn>(m);
}
{
Plain buf = Plain::Random(2 * rows * cols, 1);
Map<Mat, 0, InnerStride<2>> m(buf.data(), rows, cols);
check_ref_default_outer_stride<Ref<Mat, 0, InnerStride<2>>>(m, true);
check_ref_default_outer_stride<Ref<const Mat, 0, InnerStride<2>>>(m, true);
check_ref_default_outer_stride_write<Ref<Mat, 0, InnerStride<2>>>(m);
check_ref_default_outer_stride<Ref<Mat, 0, InnerStride<Dynamic>>>(m, true);
}
// Every second row (column-major) or column (row-major) of a matrix: inner stride 2, outer stride of the source.
Mat A = Mat::Random(2 * rows, 2 * cols);
constexpr bool row_major = (int(Mat::Options) & int(RowMajor)) != 0;
auto sub = every_other_inner(A, internal::bool_constant<row_major>());
auto sub_sized = every_other_inner(A, internal::bool_constant<!row_major>());
// The first has inner stride 2 and outer stride == 2 * size of its inner dimension, so a default Ref binds to it.
// The second keeps the unit inner stride and a larger outer stride: only a copy matches.
VERIFY_IS_EQUAL(sub.innerStride(), 2);
check_ref_default_outer_stride<RefDyn>(sub, true);
check_ref_default_outer_stride<CRefDyn>(sub, true);
check_ref_default_outer_stride_write<RefDyn>(sub);
check_ref_default_outer_stride<CRefDyn>(sub_sized, false);
// Outer stride that is not inner * rows: copied.
Plain buf = Plain::Random(3 * (rows + 3) * (cols + 3), 1);
Map<Mat, 0, Stride<Dynamic, Dynamic>> padded(buf.data(), rows, cols,
Stride<Dynamic, Dynamic>((row_major ? cols + 3 : rows + 3) * 2, 2));
check_ref_default_outer_stride<CRefDyn>(padded, false);
// A Ref of a Ref.
RefDyn first(sub);
check_ref_default_outer_stride<CRefDyn>(first, true);
}
template <typename Scalar, int Storage>
void check_ref_default_outer_stride_fixed() {
using Mat = Matrix<Scalar, 4, 3, Storage>;
using Sq = Matrix<Scalar, 3, 3, Storage>;
Matrix<Scalar, 24, 1> buf = Matrix<Scalar, 24, 1>::Random();
{
Map<Mat, 0, InnerStride<2>> m(buf.data());
check_ref_default_outer_stride<Ref<Mat, 0, InnerStride<>>>(m, true);
check_ref_default_outer_stride<Ref<const Mat, 0, InnerStride<>>>(m, true);
check_ref_default_outer_stride<Ref<Mat, 0, InnerStride<2>>>(m, true);
check_ref_default_outer_stride<Ref<const Mat, 0, InnerStride<2>>>(m, true);
check_ref_default_outer_stride_write<Ref<Mat, 0, InnerStride<2>>>(m);
}
{
Map<Sq, 0, InnerStride<>> m(buf.data(), 3, 3, InnerStride<>(2));
check_ref_default_outer_stride<Ref<Sq, 0, InnerStride<>>>(m, true);
check_ref_default_outer_stride<Ref<const Sq, 0, InnerStride<>>>(m, true);
check_ref_default_outer_stride_write<Ref<Sq, 0, InnerStride<>>>(m);
}
}
template <typename Vec>
void check_ref_default_outer_stride_vector(Index n) {
using Scalar = typename Vec::Scalar;
for (Index s = 1; s <= 3; ++s) {
Matrix<Scalar, Dynamic, 1> buf = Matrix<Scalar, Dynamic, 1>::Random(s * n);
Map<Vec, 0, InnerStride<>> v(buf.data(), n, InnerStride<>(s));
check_ref_default_outer_stride<Ref<Vec, 0, InnerStride<>>>(v, true);
check_ref_default_outer_stride<Ref<const Vec, 0, InnerStride<>>>(v, true);
check_ref_default_outer_stride_write<Ref<Vec, 0, InnerStride<>>>(v);
}
// A row of a column-major matrix is a strided vector, which a column vector Ref binds to by transposing.
Matrix<Scalar, Dynamic, Dynamic> A = Matrix<Scalar, Dynamic, Dynamic>::Random(n, n + 1);
Ref<Vec, 0, InnerStride<>> r(A.row(n / 2));
const Ref<const Vec, 0, InnerStride<>> cr(A.row(n / 2));
VERIFY(r.data() == &A(n / 2, 0) && cr.data() == r.data());
VERIFY_IS_EQUAL(r.outerStride(), r.size() * r.innerStride());
VERIFY_IS_EQUAL(cr.outerStride(), cr.size() * cr.innerStride());
for (Index k = 0; k < A.cols(); ++k) VERIFY_IS_EQUAL(r(k), A(n / 2, k));
VERIFY_IS_EQUAL(cr, r);
}
void test_ref_default_outer_stride() {
for (int i = 0; i < 3; ++i) {
const Index r = internal::random<Index>(1, 5), c = internal::random<Index>(1, 5);
check_ref_default_outer_stride_matrix<MatrixXf>(r + 1, c + 1);
check_ref_default_outer_stride_matrix<Matrix<float, Dynamic, Dynamic, RowMajor>>(r + 1, c + 1);
check_ref_default_outer_stride_matrix<MatrixXd>(r + 1, c + 1);
check_ref_default_outer_stride_matrix<MatrixXcf>(r + 1, c + 1);
check_ref_default_outer_stride_matrix<Matrix<int, Dynamic, Dynamic, RowMajor>>(r + 1, c + 1);
check_ref_default_outer_stride_vector<VectorXf>(r + 1);
check_ref_default_outer_stride_vector<RowVectorXf>(r + 1);
check_ref_default_outer_stride_vector<VectorXcd>(r + 1);
}
check_ref_default_outer_stride_fixed<float, ColMajor>();
check_ref_default_outer_stride_fixed<float, RowMajor>();
check_ref_default_outer_stride_fixed<std::complex<double>, ColMajor>();
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_1(Ref<VectorXf> a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_2(const Ref<const VectorXf> &a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_3(Ref<VectorXf, 0, InnerStride<>> a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_4(const Ref<const VectorXf, 0, InnerStride<>> &a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_5(Ref<MatrixXf, 0, OuterStride<>> a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_6(const Ref<const MatrixXf, 0, OuterStride<>> &a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
template <typename B>
EIGEN_DONT_INLINE void call_ref_7(Ref<Matrix<float, Dynamic, 3>> a, const B &b) {
VERIFY_IS_EQUAL(a, b);
}
void call_ref() {
VectorXcf ca = VectorXcf::Random(10);
VectorXf a = VectorXf::Random(10);
RowVectorXf b = RowVectorXf::Random(10);
MatrixXf A = MatrixXf::Random(10, 10);
RowVector3f c = RowVector3f::Random();
const VectorXf &ac(a);
VectorBlock<VectorXf> ab(a, 0, 3);
VERIFY_EVALUATION_COUNT(call_ref_1(a, a), 0);
VERIFY_EVALUATION_COUNT(call_ref_1(b, b.transpose()), 0);
// call_ref_1(ac,a<c); // does not compile because ac is const
VERIFY_EVALUATION_COUNT(call_ref_1(ab, ab), 0);
VERIFY_EVALUATION_COUNT(call_ref_1(a.head(4), a.head(4)), 0);
VERIFY_EVALUATION_COUNT(call_ref_1(A.col(3), A.col(3)), 0);
// call_ref_1(A.row(3),A.row(3)); // does not compile because innerstride!=1
VERIFY_EVALUATION_COUNT(call_ref_3(A.row(3), A.row(3).transpose()), 0);
VERIFY_EVALUATION_COUNT(call_ref_4(A.row(3), A.row(3).transpose()), 0);
// call_ref_1(a+a, a+a); // does not compile for obvious reason
MatrixXf tmp = A * A.col(1);
VERIFY_EVALUATION_COUNT(call_ref_2(A * A.col(1), tmp), 1); // evaluated into a temp
VERIFY_EVALUATION_COUNT(call_ref_2(ac.head(5), ac.head(5)), 0);
VERIFY_EVALUATION_COUNT(call_ref_2(a, a), 0);
VERIFY_EVALUATION_COUNT(call_ref_2(ab, ab), 0);
VERIFY_EVALUATION_COUNT(call_ref_2(a.head(4), a.head(4)), 0);
tmp = a + a;
VERIFY_EVALUATION_COUNT(call_ref_2(a + a, tmp), 1); // evaluated into a temp
VERIFY_EVALUATION_COUNT(call_ref_2(ca.imag(), ca.imag()), 1); // evaluated into a temp
VERIFY_EVALUATION_COUNT(call_ref_4(ac.head(5), ac.head(5)), 0);
tmp = a + a;
VERIFY_EVALUATION_COUNT(call_ref_4(a + a, tmp), 1); // evaluated into a temp
VERIFY_EVALUATION_COUNT(call_ref_4(ca.imag(), ca.imag()), 0);
VERIFY_EVALUATION_COUNT(call_ref_5(a, a), 0);
VERIFY_EVALUATION_COUNT(call_ref_5(a.head(3), a.head(3)), 0);
VERIFY_EVALUATION_COUNT(call_ref_5(A, A), 0);
// call_ref_5(A.transpose(),A.transpose()); // does not compile because storage order does not match
VERIFY_EVALUATION_COUNT(call_ref_5(A.block(1, 1, 2, 2), A.block(1, 1, 2, 2)), 0);
VERIFY_EVALUATION_COUNT(call_ref_5(b, b),
0); // storage order do not match, but this is a degenerate case that should work
VERIFY_EVALUATION_COUNT(call_ref_5(a.row(3), a.row(3)), 0);
VERIFY_EVALUATION_COUNT(call_ref_6(a, a), 0);
VERIFY_EVALUATION_COUNT(call_ref_6(a.head(3), a.head(3)), 0);
VERIFY_EVALUATION_COUNT(call_ref_6(A.row(3), A.row(3)),
1); // evaluated into a temp thouth it could be avoided by viewing it as a 1xn matrix
tmp = A + A;
VERIFY_EVALUATION_COUNT(call_ref_6(A + A, tmp), 1); // evaluated into a temp
VERIFY_EVALUATION_COUNT(call_ref_6(A, A), 0);
VERIFY_EVALUATION_COUNT(call_ref_6(A.transpose(), A.transpose()),
1); // evaluated into a temp because the storage orders do not match
VERIFY_EVALUATION_COUNT(call_ref_6(A.block(1, 1, 2, 2), A.block(1, 1, 2, 2)), 0);
VERIFY_EVALUATION_COUNT(call_ref_7(c, c), 0);
}
using RowMatrixXd = Matrix<double, Dynamic, Dynamic, RowMajor>;
int test_ref_overload_fun1(Ref<MatrixXd>) { return 1; }
int test_ref_overload_fun1(Ref<RowMatrixXd>) { return 2; }
int test_ref_overload_fun1(Ref<MatrixXf>) { return 3; }
int test_ref_overload_fun2(Ref<const MatrixXd>) { return 4; }
int test_ref_overload_fun2(Ref<const MatrixXf>) { return 5; }
void test_ref_ambiguous(const Ref<const ArrayXd> &A, Ref<ArrayXd> B) {
B = A;
B = A - A;
}
// See also bug 969
void test_ref_overloads() {
MatrixXd Ad, Bd;
RowMatrixXd rAd, rBd;
VERIFY(test_ref_overload_fun1(Ad) == 1);
VERIFY(test_ref_overload_fun1(rAd) == 2);
MatrixXf Af, Bf;
VERIFY(test_ref_overload_fun2(Ad) == 4);
VERIFY(test_ref_overload_fun2(Ad + Bd) == 4);
VERIFY(test_ref_overload_fun2(Af + Bf) == 5);
ArrayXd A, B;
test_ref_ambiguous(A, B);
}
template <typename Ref_>
struct RefDerived : Ref_ {
using Ref_::m_object;
};
template <typename MatrixType, typename Derived>
void test_cref_move_ctor(const DenseBase<Derived> &expr) {
using CRef = Ref<const MatrixType>;
using CRefDerived = RefDerived<CRef>;
const bool owns_data = !bool(internal::traits<CRef>::template match<Derived>::type::value);
CRef cref1(expr);
const double *data1 = cref1.data(), *obj_data1 = static_cast<CRefDerived &>(cref1).m_object.data();
VERIFY(test_is_equal(data1, obj_data1, owns_data));
CRef cref2(std::move(cref1));
VERIFY_IS_EQUAL(std::uintptr_t(data1), std::uintptr_t(cref1.data()));
const double *data2 = cref2.data(), *obj_data2 = static_cast<CRefDerived &>(cref2).m_object.data();
VERIFY(test_is_equal(data1, data2, MatrixType::MaxSizeAtCompileTime == Dynamic || !owns_data));
VERIFY(test_is_equal(data1, obj_data2, MatrixType::MaxSizeAtCompileTime == Dynamic && owns_data));
}
template <typename MatrixType>
void test_contiguous_ref_no_copy(const PlainObjectBase<MatrixType> &obj) {
using Ref_ = Ref<MatrixType, Unaligned, Stride<0, 0>>;
using CRef_ = Ref<const MatrixType, Unaligned, Stride<0, 0>>;
MatrixType m(obj);
Ref_ ref(m);
VERIFY(test_is_equal(ref.data(), m.data(), true));
CRef_ cref(m);
VERIFY(test_is_equal(cref.data(), m.data(), true));
}
EIGEN_DECLARE_TEST(ref) {
for (int i = 0; i < g_repeat; i++) {
CALL_SUBTEST_1(ref_vector(Matrix<float, 1, 1>()));
CALL_SUBTEST_1(check_const_correctness(Matrix<float, 1, 1>()));
CALL_SUBTEST_2(ref_vector(Vector4d()));
CALL_SUBTEST_2(check_const_correctness(Matrix4d()));
CALL_SUBTEST_3(ref_vector(Vector4cf()));
CALL_SUBTEST_4(ref_vector(VectorXcf(8)));
CALL_SUBTEST_5(ref_vector(VectorXi(12)));
CALL_SUBTEST_5(check_const_correctness(VectorXi(12)));
CALL_SUBTEST_1(ref_matrix(Matrix<float, 1, 1>()));
CALL_SUBTEST_2(ref_matrix(Matrix4d()));
CALL_SUBTEST_1(ref_matrix(Matrix<float, 3, 5>()));
CALL_SUBTEST_4(ref_matrix(MatrixXcf(internal::random<int>(1, 10), internal::random<int>(1, 10))));
CALL_SUBTEST_4(ref_matrix(Matrix<std::complex<double>, 10, 15>()));
CALL_SUBTEST_5(ref_matrix(MatrixXi(internal::random<int>(1, 10), internal::random<int>(1, 10))));
CALL_SUBTEST_6(call_ref());
CALL_SUBTEST_8((ref_vector_fixed_sizes<float, 3, 5>()));
CALL_SUBTEST_8((ref_vector_fixed_sizes<float, 15, 10>()));
}
CALL_SUBTEST_7(test_ref_overloads());
CALL_SUBTEST_9(test_cref_move_ctor<VectorXd>(VectorXd::Ones(9)));
CALL_SUBTEST_9(test_cref_move_ctor<VectorXd>(VectorXd(9)));
CALL_SUBTEST_9(test_cref_move_ctor<Vector3d>(Vector3d::Ones()));
CALL_SUBTEST_9(test_cref_move_ctor<Vector3d>(Vector3d()));
CALL_SUBTEST_9(test_cref_move_ctor<MatrixXd>(MatrixXd::Ones(9, 5)));
CALL_SUBTEST_9(test_cref_move_ctor<MatrixXd>(MatrixXd(9, 5)));
CALL_SUBTEST_9(test_cref_move_ctor<Matrix3d>(Matrix3d::Ones()));
CALL_SUBTEST_9(test_cref_move_ctor<Matrix3d>(Matrix3d()));
CALL_SUBTEST_10(test_contiguous_ref_no_copy(VectorXd(9)));
CALL_SUBTEST_10(test_contiguous_ref_no_copy(Vector3d()));
CALL_SUBTEST_10(test_contiguous_ref_no_copy(MatrixXd(9, 5)));
CALL_SUBTEST_10(test_contiguous_ref_no_copy(Matrix3d()));
CALL_SUBTEST_10(test_ref_alignment_matching());
CALL_SUBTEST_10(test_ref_zero_strides());
CALL_SUBTEST_10(test_ref_default_outer_stride());
}