// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2025 The Eigen Authors // // 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 test_realview(const T&) { using Scalar = typename T::Scalar; using RealScalar = typename NumTraits::Real; constexpr Index minRows = T::RowsAtCompileTime == Dynamic ? 1 : T::RowsAtCompileTime; constexpr Index maxRows = T::MaxRowsAtCompileTime == Dynamic ? (EIGEN_TEST_MAX_SIZE / 2) : T::MaxRowsAtCompileTime; constexpr Index minCols = T::ColsAtCompileTime == Dynamic ? 1 : T::ColsAtCompileTime; constexpr Index maxCols = T::MaxColsAtCompileTime == Dynamic ? (EIGEN_TEST_MAX_SIZE / 2) : T::MaxColsAtCompileTime; constexpr Index rowFactor = (NumTraits::IsComplex && !T::IsRowMajor) ? 2 : 1; constexpr Index colFactor = (NumTraits::IsComplex && T::IsRowMajor) ? 2 : 1; constexpr Index sizeFactor = NumTraits::IsComplex ? 2 : 1; Index rows = internal::random(minRows, maxRows); Index cols = internal::random(minCols, maxCols); T A(rows, cols), B, C; VERIFY(A.realView().rows() == rowFactor * A.rows()); VERIFY(A.realView().cols() == colFactor * A.cols()); VERIFY(A.realView().size() == sizeFactor * A.size()); RealScalar alpha = internal::random(RealScalar(1), RealScalar(2)); A.setRandom(); VERIFY_IS_APPROX(A.matrix().squaredNorm(), A.realView().matrix().squaredNorm()); // test re-sizing realView during assignment B.realView() = A.realView(); VERIFY_IS_APPROX(A, B); VERIFY_IS_APPROX(A.realView(), B.realView()); // B = A * alpha for (Index r = 0; r < rows; r++) { for (Index c = 0; c < cols; c++) { B.coeffRef(r, c) = A.coeff(r, c) * Scalar(alpha); } } VERIFY_IS_APPROX(B.realView(), A.realView() * alpha); C = A; C.realView() *= alpha; VERIFY_IS_APPROX(B, C); alpha = internal::random(RealScalar(1), RealScalar(2)); A.setRandom(); // B = A / alpha for (Index r = 0; r < rows; r++) { for (Index c = 0; c < cols; c++) { B.coeffRef(r, c) = A.coeff(r, c) / Scalar(alpha); } } VERIFY_IS_APPROX(B.realView(), A.realView() / alpha); C = A; C.realView() /= alpha; VERIFY_IS_APPROX(B, C); } template void test_realview_driver() { // if Rows == 1, don't test ColMajor as it is not a valid array using ColMajorMatrixType = Matrix; using ColMajorArrayType = Array; // if Cols == 1, don't test RowMajor as it is not a valid array using RowMajorMatrixType = Matrix; using RowMajorArrayType = Array; test_realview(ColMajorMatrixType()); test_realview(ColMajorArrayType()); test_realview(RowMajorMatrixType()); test_realview(RowMajorArrayType()); } template void test_realview_driver_complex() { test_realview_driver(); test_realview_driver, Rows, Cols, MaxRows, MaxCols>(); test_realview_driver(); test_realview_driver, Rows, Cols, MaxRows, MaxCols>(); test_realview_driver(); test_realview_driver, Rows, Cols, MaxRows, MaxCols>(); } EIGEN_DECLARE_TEST(realview) { for (int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1((test_realview_driver_complex())); CALL_SUBTEST_2((test_realview_driver_complex())); CALL_SUBTEST_3((test_realview_driver_complex())); CALL_SUBTEST_4((test_realview_driver_complex())); CALL_SUBTEST_5((test_realview_driver_complex<17, Dynamic, 17, Dynamic>())); CALL_SUBTEST_6((test_realview_driver_complex())); CALL_SUBTEST_7((test_realview_driver_complex<17, 19, 17, 19>())); CALL_SUBTEST_8((test_realview_driver_complex())); CALL_SUBTEST_9((test_realview_driver_complex<1, Dynamic>())); CALL_SUBTEST_10((test_realview_driver_complex<1, 1>())); } }