Skip to content

Commit

Permalink
Eigen: treat 1x1 matrices as matrices and not scalars
Browse files Browse the repository at this point in the history
- also adds a test for return value passing, which was (ultimately) not
  found to be the reason of the reported issue
  • Loading branch information
wjakob committed Apr 2, 2023
1 parent 9120bac commit 445781f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/nanobind/eigen/dense.h
Expand Up @@ -26,7 +26,7 @@ template <typename T> using DMap = Eigen::Map<T, 0, DStride>;
NAMESPACE_BEGIN(detail)

template <typename T>
constexpr int NumDimensions = int(T::MaxSizeAtCompileTime) == 1 ? 0 : bool(T::IsVectorAtCompileTime) ? 1 : 2;
constexpr int NumDimensions = bool(T::IsVectorAtCompileTime) ? 1 : 2;

template <typename T>
using array_for_eigen_t = ndarray<
Expand Down
1 change: 0 additions & 1 deletion src/nb_ndarray.cpp
Expand Up @@ -504,7 +504,6 @@ ndarray_handle *ndarray_create(void *value, size_t ndim, const size_t *shape_in,
--i;
}
}

ndarray->dltensor.data = (void *) value_rounded;
ndarray->dltensor.device.device_type = device_type;
ndarray->dltensor.device.device_id = device_id;
Expand Down
10 changes: 10 additions & 0 deletions tests/test_eigen.cpp
Expand Up @@ -117,6 +117,16 @@ NB_MODULE(test_eigen_ext, m) {
return m;
});

/// issue #166
using Matrix1d = Eigen::Matrix<double,1,1>;
try {
m.def(
"default_arg", [](Matrix1d a, Matrix1d b) { return a + b; },
"a"_a = Matrix1d::Zero(), "b"_a = Matrix1d::Zero());
} catch (...) {
// Ignore (NumPy not installed, etc.)
}

struct Buffer {
uint32_t x[30] { };

Expand Down
5 changes: 5 additions & 0 deletions tests/test_eigen.py
Expand Up @@ -243,3 +243,8 @@ def test_sparse_failures():
# undo sabotage of the module
sys.path = sys_path
scipy.sparse.csr_matrix = csr_matrix

@needs_numpy_and_eigen
def test_eigen_scalar_default():
x = t.default_arg()
assert x==0

0 comments on commit 445781f

Please sign in to comment.