blob: d59f9d921851e0ebf6aa0bd8306bd449cc014f85 [file]
// This file is triangularView of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2010 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
#define TEST_CHECK_STATIC_ASSERTIONS
#include "main.h"
// This file tests the basic selfadjointView API,
// the related products and decompositions are tested in specific files.
template <typename MatrixType>
void selfadjoint(const MatrixType& m) {
using Scalar = typename MatrixType::Scalar;
Index rows = m.rows();
Index cols = m.cols();
MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols), m4(rows, cols);
m1.diagonal() = m1.diagonal().real().template cast<Scalar>();
// check selfadjoint to dense
m3 = m1.template selfadjointView<Upper>();
VERIFY_IS_APPROX(MatrixType(m3.template triangularView<Upper>()), MatrixType(m1.template triangularView<Upper>()));
VERIFY_IS_APPROX(m3, m3.adjoint());
m3 = m1.template selfadjointView<Lower>();
VERIFY_IS_APPROX(MatrixType(m3.template triangularView<Lower>()), MatrixType(m1.template triangularView<Lower>()));
VERIFY_IS_APPROX(m3, m3.adjoint());
m3 = m1.template selfadjointView<Upper>();
m4 = m2;
m4 += m1.template selfadjointView<Upper>();
VERIFY_IS_APPROX(m4, m2 + m3);
m3 = m1.template selfadjointView<Lower>();
m4 = m2;
m4 -= m1.template selfadjointView<Lower>();
VERIFY_IS_APPROX(m4, m2 - m3);
Scalar s = internal::random<Scalar>();
m4 = s * m1.template selfadjointView<Upper>();
VERIFY_IS_APPROX(m4, MatrixType((s * m1).template selfadjointView<Upper>()));
m4 = m1.template selfadjointView<Upper>() * s;
VERIFY_IS_APPROX(m4, MatrixType((m1 * s).template selfadjointView<Upper>()));
m4 = s * m1.template selfadjointView<Lower>();
VERIFY_IS_APPROX(m4, MatrixType((s * m1).template selfadjointView<Lower>()));
m4 = m1.template selfadjointView<Lower>() * s;
VERIFY_IS_APPROX(m4, MatrixType((m1 * s).template selfadjointView<Lower>()));
// l1Norm: reads only the stored triangle but must agree with the L1 norm of
// the materialized full matrix. Upper and Lower views of the same (stored-
// full) self-adjoint matrix must return the same value; complex scalars
// behave identically since |conj(x)| = |x|.
using RealScalar = typename NumTraits<Scalar>::Real;
m3 = m1.template selfadjointView<Upper>(); // m3 is now fully self-adjoint
RealScalar ref_l1 = m3.cwiseAbs().colwise().sum().maxCoeff();
VERIFY_IS_APPROX(m3.template selfadjointView<Upper>().l1Norm(), ref_l1);
VERIFY_IS_APPROX(m3.template selfadjointView<Lower>().l1Norm(), ref_l1);
// Either triangle alone still gives the correct L1 norm even if the other
// half is zero (the view conjure it back via symmetry).
MatrixType upperOnly = MatrixType::Zero(rows, cols);
upperOnly.template triangularView<Upper>() = m3;
VERIFY_IS_APPROX(upperOnly.template selfadjointView<Upper>().l1Norm(), ref_l1);
MatrixType lowerOnly = MatrixType::Zero(rows, cols);
lowerOnly.template triangularView<Lower>() = m3;
VERIFY_IS_APPROX(lowerOnly.template selfadjointView<Lower>().l1Norm(), ref_l1);
}
// l1Norm switches from a per-column to a streaming form at a small size and its column pass has
// packet tails, so sweep sizes around the switch and across packet boundaries: a mis-sized
// segment would otherwise hide between the random sizes above.
template <typename Scalar>
void selfadjoint_l1norm_sizes() {
using MatrixType = Matrix<Scalar, Dynamic, Dynamic>;
using RealScalar = typename NumTraits<Scalar>::Real;
for (Index n : {Index(0), Index(1), Index(2), Index(15), Index(16), Index(17), Index(63), Index(64), Index(65),
Index(127), Index(128), Index(129)}) {
MatrixType m = MatrixType::Random(n, n);
MatrixType full = m.template selfadjointView<Lower>();
RealScalar ref = n == 0 ? RealScalar(0) : full.cwiseAbs().colwise().sum().maxCoeff();
VERIFY_IS_APPROX(m.template selfadjointView<Lower>().l1Norm(), ref);
VERIFY_IS_APPROX(full.template selfadjointView<Upper>().l1Norm(), ref);
}
}
// The vectorized complex path squares the parts, so entries beyond the square-root range of the
// scalar must come back through the scalar fallback: squares that overflow, and squares that
// land in the denormals and lose precision.
template <typename Scalar>
void selfadjoint_l1norm_range() {
using MatrixType = Matrix<Scalar, Dynamic, Dynamic>;
using RealScalar = typename NumTraits<Scalar>::Real;
Index n = 70;
MatrixType m = MatrixType::Random(n, n);
RealScalar big = numext::sqrt(NumTraits<RealScalar>::highest()) * RealScalar(1e3);
RealScalar small = numext::sqrt((std::numeric_limits<RealScalar>::min)()) * RealScalar(1e-3);
for (RealScalar scale : {big, small}) {
MatrixType ms = m * scale;
MatrixType full = ms.template selfadjointView<Lower>();
RealScalar ref = full.cwiseAbs().colwise().sum().maxCoeff();
VERIFY_IS_APPROX(ms.template selfadjointView<Lower>().l1Norm(), ref);
VERIFY_IS_APPROX(full.template selfadjointView<Upper>().l1Norm(), ref);
}
}
// half and bfloat16 accumulate the norm in float, so only the final rounding to the scalar separates
// the result from a float reference, not the size of the matrix.
template <typename Scalar>
void selfadjoint_l1norm_lowprec() {
using MatrixType = Matrix<Scalar, Dynamic, Dynamic>;
for (Index n : {Index(8), Index(64), Index(300)}) {
MatrixType m = MatrixType::Random(n, n).template selfadjointView<Lower>();
float ref = m.template cast<float>().cwiseAbs().colwise().sum().maxCoeff();
float tol = 2 * float(NumTraits<Scalar>::epsilon()) * ref;
VERIFY(numext::abs(float(m.template selfadjointView<Lower>().l1Norm()) - ref) <= tol);
VERIFY(numext::abs(float(m.template selfadjointView<Upper>().l1Norm()) - ref) <= tol);
}
}
// Narrow integers promote when added: the column totals must be materialized in the scalar type
// before they are compared (n=2 takes the per-column form, n=8 the column pass).
template <typename Scalar>
void selfadjoint_l1norm_integer() {
for (Index n : {Index(2), Index(8)}) {
using MatrixType = Matrix<Scalar, Dynamic, Dynamic>;
// Random() spans the whole range; keep the column sums representable.
MatrixType m = (MatrixType::Random(n, n) / Scalar(NumTraits<Scalar>::highest() / 16)).eval();
m = m.template selfadjointView<Lower>();
Scalar ref = m.template cast<int>().cwiseAbs().colwise().sum().maxCoeff();
VERIFY_IS_EQUAL(m.template selfadjointView<Lower>().l1Norm(), ref);
VERIFY_IS_EQUAL(m.template selfadjointView<Upper>().l1Norm(), ref);
}
}
template <typename MatrixType>
void selfadjoint_l1norm_empty(const MatrixType& m) {
using RealScalar = typename NumTraits<typename MatrixType::Scalar>::Real;
VERIFY_IS_EQUAL(m.template selfadjointView<Lower>().l1Norm(), RealScalar(0));
VERIFY_IS_EQUAL(m.template selfadjointView<Upper>().l1Norm(), RealScalar(0));
}
// With a zero-sized fixed dimension col() and row() are not vectors, so the kernels must not take their head() or
// tail(); the row-major form also instantiates the streaming pass on the transpose.
template <typename Scalar>
void selfadjoint_l1norm_zero_sized() {
selfadjoint_l1norm_empty(Matrix<Scalar, 0, 0>());
selfadjoint_l1norm_empty(Matrix<Scalar, 0, 0, RowMajor>());
selfadjoint_l1norm_empty(Matrix<Scalar, Dynamic, 0, ColMajor, 0, 0>());
}
// nestedExpression() on a non-const view keeps the operand's constness: callable for a const operand, writable for a
// mutable one.
template <typename = void>
void selfadjoint_nested_expression() {
const Matrix3d a = Matrix3d::Identity();
auto lower = a.selfadjointView<Lower>();
VERIFY(&lower.nestedExpression() == &a);
Matrix3d b = Matrix3d::Zero();
auto upper = b.selfadjointView<Upper>();
upper.nestedExpression()(0, 1) = 1;
VERIFY_IS_EQUAL(b(0, 1), 1.0);
}
void bug_159() {
Matrix3d m = Matrix3d::Random().selfadjointView<Lower>();
EIGEN_UNUSED_VARIABLE(m);
}
template <unsigned int UpLo, typename MatrixType>
EIGEN_DONT_INLINE void verify_selfadjoint_copy(const MatrixType& m, const MatrixType& dense) {
for (Index j = 0; j < m.cols(); ++j)
for (Index i = 0; i < m.rows(); ++i)
VERIFY_IS_EQUAL(dense(i, j), (UpLo == Upper ? i <= j : i >= j) ? m(i, j) : numext::conj(m(j, i)));
}
// GCC PR target/124138 miscompiled this copy for 4x4 double with AVX-512 (see SelfAdjointView.h). It needs the copy
// materialized in a function of its own, and a reference built without the mirrored stores that GCC also miscompiles.
template <unsigned int UpLo, typename MatrixType>
EIGEN_DONT_INLINE void selfadjoint_copy(const MatrixType& m) {
const MatrixType dense = m.template selfadjointView<UpLo>();
verify_selfadjoint_copy<UpLo>(m, dense);
}
template <typename MatrixType>
void selfadjoint_copy_fixed() {
const MatrixType m = MatrixType::Random();
selfadjoint_copy<Upper>(m);
selfadjoint_copy<Lower>(m);
}
#if EIGEN_GNUC_STRICT_AT_LEAST(6, 0, 0)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-attributes"
#endif
template <typename Scalar, int UpLo, int Order, typename Packet>
void check_selfadjoint_l1norm_explicit_case(Index n) {
using MatrixType = Matrix<Scalar, Dynamic, Dynamic, Order>;
using RealScalar = typename NumTraits<Scalar>::Real;
STATIC_CHECK((std::is_same<typename internal::selfadjoint_l1norm<MatrixType, UpLo, Packet>::L1NormImpl,
internal::selfadjoint_l1norm_impl<Scalar, Packet>>::value));
MatrixType m = MatrixType::Random(n, n);
m.diagonal() = m.diagonal().real().template cast<Scalar>();
MatrixType full = m.template selfadjointView<UpLo>();
RealScalar ref = full.cwiseAbs().colwise().sum().maxCoeff();
VERIFY_IS_APPROX((internal::selfadjoint_l1norm<MatrixType, UpLo, Packet>(m).run()), ref);
VERIFY_IS_APPROX((internal::selfadjoint_l1_norm<UpLo, MatrixType, Packet>(m)), ref);
}
template <int UpLo, int Order, typename Packet>
void check_selfadjoint_l1norm_explicit_lowprec_case(Index n) {
using MatrixType = Matrix<half, Dynamic, Dynamic, Order>;
STATIC_CHECK((std::is_same<typename internal::selfadjoint_l1norm<MatrixType, UpLo, Packet>::L1NormImpl,
internal::selfadjoint_l1norm_impl<half, Packet>>::value));
MatrixType m = MatrixType::Random(n, n);
MatrixType full = m.template selfadjointView<UpLo>();
float ref = full.template cast<float>().cwiseAbs().colwise().sum().maxCoeff();
float tol = 2 * float(NumTraits<half>::epsilon()) * ref;
float v1 = float(internal::selfadjoint_l1norm<MatrixType, UpLo, Packet>(m).run());
float v2 = float(internal::selfadjoint_l1_norm<UpLo, MatrixType, Packet>(m));
VERIFY((numext::isfinite)(v1) && numext::abs(v1 - ref) <= tol);
VERIFY((numext::isfinite)(v2) && numext::abs(v2 - ref) <= tol);
}
template <typename Scalar, typename Packet>
void check_selfadjoint_l1norm_explicit_matrix_modes() {
for (Index n : {Index(2), Index(20), Index(65)}) {
check_selfadjoint_l1norm_explicit_case<Scalar, Lower, ColMajor, Packet>(n);
check_selfadjoint_l1norm_explicit_case<Scalar, Lower, RowMajor, Packet>(n);
check_selfadjoint_l1norm_explicit_case<Scalar, Upper, ColMajor, Packet>(n);
check_selfadjoint_l1norm_explicit_case<Scalar, Upper, RowMajor, Packet>(n);
}
}
template <typename Real>
void check_selfadjoint_l1norm_explicit_real() {
using DefaultPacket = internal::selfadjoint_l1norm_packet_t<Real>;
using HalfPacket = typename internal::unpacket_traits<DefaultPacket>::half;
STATIC_CHECK(
(std::is_same<typename internal::selfadjoint_l1norm_real_lanes<Real, HalfPacket>::Packet, HalfPacket>::value));
STATIC_CHECK((!internal::unpacket_traits<HalfPacket>::vectorizable ||
std::is_base_of<
internal::selfadjoint_l1norm_packet_impl<internal::selfadjoint_l1norm_real_lanes<Real, HalfPacket>>,
internal::selfadjoint_l1norm_impl<Real, HalfPacket>>::value));
check_selfadjoint_l1norm_explicit_matrix_modes<Real, Real>();
check_selfadjoint_l1norm_explicit_matrix_modes<Real, HalfPacket>();
}
template <typename Real, typename Packet,
bool Enabled = internal::unpacket_traits<Packet>::vectorizable &&
internal::selfadjoint_l1norm_plain_real_view<Packet>::value>
struct check_selfadjoint_l1norm_complex_lanes {
static constexpr bool value = true;
};
template <typename Real, typename Packet>
struct check_selfadjoint_l1norm_complex_lanes<Real, Packet, true> {
static constexpr bool value =
std::is_same<typename internal::selfadjoint_l1norm_complex_lanes<Real, Packet>::Packet, Packet>::value &&
std::is_base_of<
internal::selfadjoint_l1norm_packet_impl<internal::selfadjoint_l1norm_complex_lanes<Real, Packet>>,
internal::selfadjoint_l1norm_impl<std::complex<Real>, Packet>>::value;
};
template <typename Real>
void check_selfadjoint_l1norm_explicit_complex() {
using Complex = std::complex<Real>;
using DefaultPacket = internal::selfadjoint_l1norm_packet_t<Complex>;
using HalfPacket = typename internal::unpacket_traits<DefaultPacket>::half;
STATIC_CHECK((check_selfadjoint_l1norm_complex_lanes<Real, HalfPacket>::value));
check_selfadjoint_l1norm_explicit_matrix_modes<Complex, Complex>();
check_selfadjoint_l1norm_explicit_matrix_modes<Complex, HalfPacket>();
}
void selfadjoint_l1norm_packet_param_check() {
STATIC_CHECK((
!internal::packet_traits<float>::Vectorizable ||
!std::is_same<internal::selfadjoint_l1norm_impl<float>, internal::selfadjoint_l1norm_impl<float, float>>::value));
STATIC_CHECK(
(!internal::packet_traits<float>::Vectorizable ||
!std::is_same<internal::selfadjoint_l1norm_impl<half>, internal::selfadjoint_l1norm_impl<half, float>>::value));
STATIC_CHECK((!internal::packet_traits<float>::Vectorizable ||
!std::is_same<internal::selfadjoint_l1norm<MatrixXf, Lower>,
internal::selfadjoint_l1norm<MatrixXf, Lower, float>>::value));
STATIC_CHECK((!internal::selfadjoint_l1norm_plain_real_view<
typename internal::packet_traits<std::complex<double>>::type>::value ||
!std::is_same<internal::selfadjoint_l1norm_impl<std::complex<double>>,
internal::selfadjoint_l1norm_impl<std::complex<double>, std::complex<double>>>::value));
STATIC_CHECK((!internal::selfadjoint_l1norm_plain_real_view<
typename internal::packet_traits<std::complex<double>>::type>::value ||
!std::is_same<internal::selfadjoint_l1norm<MatrixXcd, Lower>,
internal::selfadjoint_l1norm<MatrixXcd, Lower, std::complex<double>>>::value));
check_selfadjoint_l1norm_explicit_real<float>();
check_selfadjoint_l1norm_explicit_real<double>();
check_selfadjoint_l1norm_explicit_complex<float>();
check_selfadjoint_l1norm_explicit_complex<double>();
using HalfAccDefault = internal::selfadjoint_l1norm_packet_t<half>;
using HalfAccHalf = typename internal::unpacket_traits<HalfAccDefault>::half;
for (Index n : {Index(2), Index(20), Index(65)}) {
check_selfadjoint_l1norm_explicit_lowprec_case<Lower, ColMajor, float>(n);
check_selfadjoint_l1norm_explicit_lowprec_case<Upper, ColMajor, float>(n);
check_selfadjoint_l1norm_explicit_lowprec_case<Upper, RowMajor, float>(n);
check_selfadjoint_l1norm_explicit_lowprec_case<Lower, ColMajor, HalfAccHalf>(n);
check_selfadjoint_l1norm_explicit_lowprec_case<Upper, ColMajor, HalfAccHalf>(n);
check_selfadjoint_l1norm_explicit_lowprec_case<Upper, RowMajor, HalfAccHalf>(n);
}
using Complex = std::complex<double>;
MatrixXcd m = MatrixXcd::Random(5, 5);
Matrix<Complex, Dynamic, 1> sums = Matrix<Complex, Dynamic, 1>::Zero(5);
internal::selfadjoint_l1norm_impl<Complex, Complex> scalar_impl;
scalar_impl.accumulate(sums, m, 0, 1, 2, 5);
VERIFY(scalar_impl.inRange(5));
VERIFY_IS_APPROX(numext::real(sums(0)), m.col(0).segment(2, 3).cwiseAbs().sum());
VERIFY_IS_APPROX(numext::real(sums(1)), m.col(1).segment(2, 3).cwiseAbs().sum());
for (Index i = 2; i < 5; ++i) {
VERIFY_IS_APPROX(numext::real(sums(i)), scalar_impl.abs(m(i, 0)) + scalar_impl.abs(m(i, 1)));
}
}
#if EIGEN_GNUC_STRICT_AT_LEAST(6, 0, 0)
#pragma GCC diagnostic pop
#endif
EIGEN_DECLARE_TEST(selfadjoint) {
for (int i = 0; i < g_repeat; i++) {
int s = internal::random<int>(1, EIGEN_TEST_MAX_SIZE);
CALL_SUBTEST_1(selfadjoint(Matrix<float, 1, 1>()));
CALL_SUBTEST_2(selfadjoint(Matrix<float, 2, 2>()));
CALL_SUBTEST_3(selfadjoint(Matrix3cf()));
CALL_SUBTEST_4(selfadjoint(MatrixXcd(s, s)));
CALL_SUBTEST_5(selfadjoint(Matrix<float, Dynamic, Dynamic, RowMajor>(s, s)));
CALL_SUBTEST_6(selfadjoint(Matrix<std::complex<float>, Dynamic, Dynamic, RowMajor>(s, s)));
TEST_SET_BUT_UNUSED_VARIABLE(s);
}
CALL_SUBTEST_1(bug_159());
CALL_SUBTEST_2(selfadjoint_copy_fixed<Matrix4d>());
CALL_SUBTEST_2((selfadjoint_copy_fixed<Matrix<double, 4, 4, RowMajor> >()));
CALL_SUBTEST_3(selfadjoint_copy_fixed<Matrix4cd>());
CALL_SUBTEST_1(selfadjoint_nested_expression<>());
CALL_SUBTEST_4(selfadjoint_l1norm_sizes<double>());
CALL_SUBTEST_4(selfadjoint_l1norm_sizes<std::complex<double> >());
CALL_SUBTEST_6(selfadjoint_l1norm_sizes<std::complex<float> >());
CALL_SUBTEST_4(selfadjoint_l1norm_range<std::complex<double> >());
CALL_SUBTEST_6(selfadjoint_l1norm_range<std::complex<float> >());
CALL_SUBTEST_1(selfadjoint_l1norm_integer<short>());
CALL_SUBTEST_1(selfadjoint_l1norm_integer<int>());
CALL_SUBTEST_1(selfadjoint_l1norm_zero_sized<int>());
CALL_SUBTEST_4(selfadjoint_l1norm_zero_sized<double>());
CALL_SUBTEST_4(selfadjoint_l1norm_zero_sized<std::complex<double> >());
CALL_SUBTEST_6(selfadjoint_l1norm_zero_sized<std::complex<float> >());
CALL_SUBTEST_7(selfadjoint_l1norm_lowprec<half>());
CALL_SUBTEST_7(selfadjoint_l1norm_lowprec<bfloat16>());
CALL_SUBTEST_4(selfadjoint_l1norm_packet_param_check());
}