Skip to content

Commit

Permalink
remove warnings; working and failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alecjacobson committed Nov 8, 2024
1 parent 602fabc commit 0959945
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 0 additions & 3 deletions include/nanobind/eigen/sparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,12 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
}
if (object data_o = obj.attr("data"); !data_caster.from_python(data_o, flags, cleanup))
return false;
ScalarNDArray& values = data_caster.value;

if (object indices_o = obj.attr("indices"); !indices_caster.from_python(indices_o, flags, cleanup))
return false;
StorageIndexNDArray& inner_indices = indices_caster.value;

if (object indptr_o = obj.attr("indptr"); !indptr_caster.from_python(indptr_o, flags, cleanup))
return false;
StorageIndexNDArray& outer_indices = indptr_caster.value;

object shape_o = obj.attr("shape"), nnz_o = obj.attr("nnz");
try {
Expand Down
9 changes: 9 additions & 0 deletions tests/test_eigen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <nanobind/eigen/dense.h>
#include <nanobind/eigen/sparse.h>
#include <nanobind/trampoline.h>
#include <iostream>

namespace nb = nanobind;

Expand Down Expand Up @@ -166,8 +167,16 @@ NB_MODULE(test_eigen_ext, m) {
assert(!m.isCompressed());
return m.markAsRValue();
});
// This function doesn't appear to be called in tests/test_eigen.py
m.def("sparse_complex", []() -> Eigen::SparseMatrix<std::complex<double>> { return {}; });

m.def("sparse_map_c", [](const Eigen::Map<const SparseMatrixC> &) { });
m.def("sparse_map_r", [](const Eigen::Map<const SparseMatrixR> &) { });
m.def("sparse_update_map_to_zero_c", [](nb::object obj) {
Eigen::Map<SparseMatrixC> c = nb::cast<Eigen::Map<SparseMatrixC>>(obj);
for (int i = 0; i < c.nonZeros(); ++i) { c.valuePtr()[i] = 0; }
});

/// issue #166
using Matrix1d = Eigen::Matrix<double,1,1>;
try {
Expand Down
22 changes: 22 additions & 0 deletions tests/test_eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,25 @@ def test14_single_element():
a = np.array([[1]], dtype=np.uint32)
assert a.ndim == 2 and a.shape == (1, 1)
t.addMXuCC(a, a)

@needs_numpy_and_eigen
def test15_sparse_map():
pytest.importorskip("scipy")
import scipy
c = scipy.sparse.csc_matrix([[1, 0], [0, 1]], dtype=float)
# These should be copy-less
t.sparse_map_c(c)
r = scipy.sparse.csr_matrix([[1, 0], [0, 1]], dtype=float)
t.sparse_map_r(r)
# These should be ok, but will copy(?)
t.sparse_map_c(r)
t.sparse_map_r(c)

t.sparse_update_map_to_zero_c(c);
# check that c.sum() is zero now
assert c.sum() == 0





0 comments on commit 0959945

Please sign in to comment.