mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Fix EIGEN_NO_AUTOMATIC_RESIZING not resizing empty destinations
libeigen/eigen!2219 Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
This commit is contained in:
@@ -810,6 +810,9 @@ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize_if_allowed(DstXprTyp
|
|||||||
(dst.size() == 0 || (DstXprType::IsVectorAtCompileTime ? (dst.size() == src.size())
|
(dst.size() == 0 || (DstXprType::IsVectorAtCompileTime ? (dst.size() == src.size())
|
||||||
: (dst.rows() == dstRows && dst.cols() == dstCols))) &&
|
: (dst.rows() == dstRows && dst.cols() == dstCols))) &&
|
||||||
"Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
|
"Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
|
||||||
|
if (dst.size() == 0) {
|
||||||
|
dst.resize(dstRows, dstCols);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
dst.resize(dstRows, dstCols);
|
dst.resize(dstRows, dstCols);
|
||||||
eigen_assert(dst.rows() == dstRows && dst.cols() == dstCols);
|
eigen_assert(dst.rows() == dstRows && dst.cols() == dstCols);
|
||||||
|
|||||||
@@ -685,6 +685,7 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type {
|
|||||||
eigen_assert((this->size() == 0 || (IsVectorAtCompileTime ? (this->size() == other.size())
|
eigen_assert((this->size() == 0 || (IsVectorAtCompileTime ? (this->size() == other.size())
|
||||||
: (rows() == other.rows() && cols() == other.cols()))) &&
|
: (rows() == other.rows() && cols() == other.cols()))) &&
|
||||||
"Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
|
"Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
|
||||||
|
if (this->size() == 0) resizeLike(other);
|
||||||
EIGEN_ONLY_USED_FOR_DEBUG(other);
|
EIGEN_ONLY_USED_FOR_DEBUG(other);
|
||||||
#else
|
#else
|
||||||
resizeLike(other);
|
resizeLike(other);
|
||||||
|
|||||||
@@ -217,6 +217,7 @@ ei_add_test(indexed_view)
|
|||||||
ei_add_test(reshape)
|
ei_add_test(reshape)
|
||||||
ei_add_test(swap)
|
ei_add_test(swap)
|
||||||
ei_add_test(resize)
|
ei_add_test(resize)
|
||||||
|
ei_add_test(no_automatic_resizing)
|
||||||
ei_add_test(conservative_resize)
|
ei_add_test(conservative_resize)
|
||||||
ei_add_test(product_small)
|
ei_add_test(product_small)
|
||||||
ei_add_test(product_large)
|
ei_add_test(product_large)
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ EIGEN_STRONG_INLINE DstXprType& copy_using_evaluator(const PlainObjectBase<DstXp
|
|||||||
eigen_assert((dst.size() == 0 || (IsVectorAtCompileTime ? (dst.size() == src.size())
|
eigen_assert((dst.size() == 0 || (IsVectorAtCompileTime ? (dst.size() == src.size())
|
||||||
: (dst.rows() == src.rows() && dst.cols() == src.cols()))) &&
|
: (dst.rows() == src.rows() && dst.cols() == src.cols()))) &&
|
||||||
"Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
|
"Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
|
||||||
|
if (dst.size() == 0) dst.const_cast_derived().resizeLike(src.derived());
|
||||||
#else
|
#else
|
||||||
dst.const_cast_derived().resizeLike(src.derived());
|
dst.const_cast_derived().resizeLike(src.derived());
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
106
test/no_automatic_resizing.cpp
Normal file
106
test/no_automatic_resizing.cpp
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// This file is part of Eigen, a lightweight C++ template library
|
||||||
|
// for linear algebra.
|
||||||
|
//
|
||||||
|
// Copyright (C) 2026 Rasmus Munk Larsen <rmlarsen@gmail.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/.
|
||||||
|
|
||||||
|
// Test that EIGEN_NO_AUTOMATIC_RESIZING still allows assignment to
|
||||||
|
// default-constructed (empty) matrices and arrays.
|
||||||
|
|
||||||
|
#define EIGEN_NO_AUTOMATIC_RESIZING
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
template <typename Scalar>
|
||||||
|
void testNoAutomaticResizing() {
|
||||||
|
using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
|
||||||
|
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
|
||||||
|
using RowVector = Eigen::Matrix<Scalar, 1, Eigen::Dynamic>;
|
||||||
|
using Array = Eigen::Array<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
|
||||||
|
using ArrayVector = Eigen::Array<Scalar, Eigen::Dynamic, 1>;
|
||||||
|
|
||||||
|
const Index rows = internal::random<Index>(1, 50);
|
||||||
|
const Index cols = internal::random<Index>(1, 50);
|
||||||
|
|
||||||
|
// Assignment of Zero expression to default-constructed matrix.
|
||||||
|
{
|
||||||
|
Matrix M;
|
||||||
|
M = Matrix::Zero(rows, cols);
|
||||||
|
VERIFY_IS_EQUAL(M.rows(), rows);
|
||||||
|
VERIFY_IS_EQUAL(M.cols(), cols);
|
||||||
|
VERIFY_IS_EQUAL(M.norm(), Scalar(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assignment of Zero expression to default-constructed array.
|
||||||
|
{
|
||||||
|
Array A;
|
||||||
|
A = Array::Zero(rows, cols);
|
||||||
|
VERIFY_IS_EQUAL(A.rows(), rows);
|
||||||
|
VERIFY_IS_EQUAL(A.cols(), cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assignment of Ones expression to default-constructed matrix.
|
||||||
|
{
|
||||||
|
Matrix M;
|
||||||
|
M = Matrix::Ones(rows, cols);
|
||||||
|
VERIFY_IS_EQUAL(M.rows(), rows);
|
||||||
|
VERIFY_IS_EQUAL(M.cols(), cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assignment of Random expression to default-constructed matrix.
|
||||||
|
{
|
||||||
|
Matrix M;
|
||||||
|
M = Matrix::Random(rows, cols);
|
||||||
|
VERIFY_IS_EQUAL(M.rows(), rows);
|
||||||
|
VERIFY_IS_EQUAL(M.cols(), cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assignment from another matrix to default-constructed matrix.
|
||||||
|
{
|
||||||
|
Matrix src = Matrix::Random(rows, cols);
|
||||||
|
Matrix dst;
|
||||||
|
dst = src;
|
||||||
|
VERIFY_IS_EQUAL(dst.rows(), rows);
|
||||||
|
VERIFY_IS_EQUAL(dst.cols(), cols);
|
||||||
|
VERIFY_IS_APPROX(dst, src);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vector assignment to default-constructed vector.
|
||||||
|
{
|
||||||
|
Vector v;
|
||||||
|
v = Vector::Zero(rows);
|
||||||
|
VERIFY_IS_EQUAL(v.size(), rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
// RowVector assignment to default-constructed row vector.
|
||||||
|
{
|
||||||
|
RowVector v;
|
||||||
|
v = RowVector::Zero(cols);
|
||||||
|
VERIFY_IS_EQUAL(v.size(), cols);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Array vector assignment to default-constructed array vector.
|
||||||
|
{
|
||||||
|
ArrayVector v;
|
||||||
|
v = ArrayVector::Zero(rows);
|
||||||
|
VERIFY_IS_EQUAL(v.size(), rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Column access after Zero initialization (reproducer for reported bug).
|
||||||
|
{
|
||||||
|
Array A;
|
||||||
|
A = Array::Zero(rows, cols);
|
||||||
|
for (Index j = 0; j < cols; ++j) {
|
||||||
|
auto c = A.col(j);
|
||||||
|
VERIFY_IS_EQUAL(c.rows(), rows);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EIGEN_DECLARE_TEST(no_automatic_resizing) {
|
||||||
|
CALL_SUBTEST_1(testNoAutomaticResizing<float>());
|
||||||
|
CALL_SUBTEST_2(testNoAutomaticResizing<double>());
|
||||||
|
CALL_SUBTEST_3(testNoAutomaticResizing<std::complex<double>>());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user