Skip to content

Commit cac3edd

Browse files
eigen: Don't require conformability on length-0 dimensions (#38)
1 parent c5d41eb commit cac3edd

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

include/pybind11/eigen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ template <bool EigenRowMajor> struct EigenConformable {
100100
return
101101
!negativestrides &&
102102
(props::inner_stride == Eigen::Dynamic || props::inner_stride == stride.inner() ||
103-
(EigenRowMajor ? cols : rows) == 1) &&
103+
(EigenRowMajor ? cols : rows) <= 1) &&
104104
(props::outer_stride == Eigen::Dynamic || props::outer_stride == stride.outer() ||
105-
(EigenRowMajor ? rows : cols) == 1);
105+
(EigenRowMajor ? rows : cols) <= 1);
106106
}
107107
operator bool() const { return conformable; }
108108
};

tests/test_eigen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,12 @@ TEST_SUBMODULE(eigen, m) {
359359
m.def("get_elem_rm_nocopy", [](Eigen::Ref<const Eigen::Matrix<long, -1, -1, Eigen::RowMajor>> &m) -> long { return m(2, 1); },
360360
py::arg().noconvert());
361361

362-
// test_issue738
362+
// test_issue738_issue2038
363363
// Issue #738: 1xN or Nx1 2D matrices were neither accepted nor properly copied with an
364364
// incompatible stride value on the length-1 dimension--but that should be allowed (without
365365
// requiring a copy!) because the stride value can be safely ignored on a size-1 dimension.
366+
// Issue #2039: 0xN (col-major) or Nx0 (row-major) matrices were not accepted properly, due to a similar
367+
// situation.
366368
m.def("iss738_f1", &adjust_matrix<const Eigen::Ref<const Eigen::MatrixXd> &>, py::arg().noconvert());
367369
m.def("iss738_f2", &adjust_matrix<const Eigen::Ref<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>, py::arg().noconvert());
368370

tests/test_eigen.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,14 +753,18 @@ def test_sparse_signature(doc):
753753
""" # noqa: E501 line too long
754754

755755

756-
def test_issue738():
757-
"""Ignore strides on a length-1 dimension (even if they would be incompatible length > 1)"""
756+
def test_issue738_issue2038():
757+
"""Ignore strides on a length-0 or -1 dimension (even if they would be incompatible
758+
length > 1)"""
758759
assert np.all(m.iss738_f1(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
759760
assert np.all(m.iss738_f1(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
760761

761762
assert np.all(m.iss738_f2(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
762763
assert np.all(m.iss738_f2(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
763764

765+
assert np.all(m.iss738_f1(np.zeros((0, 2))) == np.zeros((0, 2)))
766+
assert np.all(m.iss738_f2(np.zeros((2, 0))) == np.zeros((2, 0)))
767+
764768

765769
def test_issue1105():
766770
"""Issue 1105: 1xN or Nx1 input arrays weren't accepted for eigen

0 commit comments

Comments
 (0)