Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 6 include/ndarray/converter/eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ template <typename T, int N, int C, typename XprKind_, int Rows_, int Cols_>
struct PyConverter< EigenView<T,N,C,XprKind_,Rows_,Cols_> > {

static bool fromPythonStage1(PyPtr & p) {
auto array = reinterpret_cast<PyArrayObject*>(p.get());
// add or remove dimensions with size one so we have the right number of dimensions
if (PyArray_Check(p.get())) {
if ((Rows_ == 1 || Cols_ == 1) && N == 2) {
Expand All @@ -41,17 +40,18 @@ struct PyConverter< EigenView<T,N,C,XprKind_,Rows_,Cols_> > {
shape[1] = 1;
}
PyArray_Dims dims = { shape, 2 };
PyPtr r(PyArray_Newshape(array, &dims, NPY_ANYORDER));
PyPtr r(PyArray_Newshape(reinterpret_cast<PyArrayObject*>(p.get()), &dims, NPY_ANYORDER));
if (!r) return false;
p.swap(r);
} else if (N == 1) {
PyPtr r(PyArray_Squeeze(array));
PyPtr r(PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(p.get())));
if (!r) return false;
p.swap(r);
}
} // else let the Array converter raise the exception
if (!PyConverter< Array<T,N,C> >::fromPythonStage1(p)) return false;
// check whether the size is correct if it's static
auto array = reinterpret_cast<PyArrayObject*>(p.get());
if (N == 2) {
if (Rows_ != Eigen::Dynamic && PyArray_DIM(array, 0) != Rows_) {
PyErr_SetString(PyExc_ValueError, "incorrect number of rows for matrix");
Expand Down
7 changes: 7 additions & 0 deletions 7 tests/pybind11_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,12 @@ def testNone(self):
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(m2), 4)
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(None), 5)
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(), 5)

def testFullySpecifiedMatrix(self):
# Failure on this specific case was not caught by other unit tests
a = numpy.array([[ 1., 0.], [ 0., 1.]])
b = numpy.array([ 0., 0.])
self.assert_(pybind11_test_mod.acceptFullySpecifiedMatrix(a, b))

if __name__ == "__main__":
unittest.main()
5 changes: 5 additions & 0 deletions 5 tests/pybind11_test_mod.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ struct MatrixOwner {
explicit MatrixOwner() : member(MemberMatrix::Zero()) {}
};

bool acceptFullySpecifiedMatrix(Eigen::Matrix<double, 2, 2, 0, 2, 2> const & a, Eigen::Matrix<double, 2, 1, 0, 2, 1> const & b) {
return true;
};

PYBIND11_PLUGIN(pybind11_test_mod) {
pybind11::module mod("pybind11_test_mod", "Tests for the ndarray library");

Expand Down Expand Up @@ -159,6 +163,7 @@ PYBIND11_PLUGIN(pybind11_test_mod) {
mod.def("acceptNoneArray", acceptNoneArray, "array"_a = nullptr);
mod.def("acceptNoneMatrixXd", acceptNoneMatrixXd, "matrix"_a = nullptr);
mod.def("acceptNoneMatrix2d", acceptNoneMatrix2d, "matrix"_a = nullptr);
mod.def("acceptFullySpecifiedMatrix", acceptFullySpecifiedMatrix);

return mod.ptr();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.