Skip to content

Commit

Permalink
test/sparse: extend test for Row and Col major matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarpent committed Jan 26, 2024
1 parent 54d4329 commit f9ef66b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
4 changes: 4 additions & 0 deletions unittest/python/test_sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import sparse_matrix
from scipy.sparse import csc_matrix, csr_matrix

m = sparse_matrix.emptyMatrix()
assert m.shape == (0, 0)
Expand All @@ -22,3 +23,6 @@

diag_mat_copy = sparse_matrix.copy(diag_mat)
assert (diag_mat_copy != diag_mat).nnz == 0

diag_mat_csr = csr_matrix(diag_mat)
assert (sparse_matrix.copy(diag_mat_csr) != diag_mat_csr).nnz == 0
35 changes: 15 additions & 20 deletions unittest/sparse_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ Eigen::SparseMatrix<Scalar, Options> diagonal(
return mat;
}

template <typename Scalar, int Options>
void matrix1x1_input(const Eigen::Matrix<Scalar, 1, 1>& mat) {
std::cout << mat << std::endl;
}

template <typename Scalar, int Options>
Eigen::SparseMatrix<Scalar, Options> emptyVector() {
return Eigen::SparseMatrix<Scalar, Options>();
Expand All @@ -61,24 +56,24 @@ Eigen::SparseMatrix<Scalar, Options> copy(
return mat;
}

BOOST_PYTHON_MODULE(sparse_matrix) {
using namespace Eigen;
template <typename Scalar, int Options>
void expose_functions() {
namespace bp = boost::python;
eigenpy::enableEigenPy();
bp::def("vector1x1", vector1x1<Scalar, Options>);
bp::def("matrix1x1", matrix1x1<Scalar, Options>);

typedef Eigen::SparseMatrix<double> SparseMatrixD;
eigenpy::EigenToPyConverter<SparseMatrixD>::registration();
eigenpy::EigenFromPyConverter<SparseMatrixD>::registration();
bp::def("print", print<Scalar, Options>);
bp::def("copy", copy<Scalar, Options>);
bp::def("diagonal", diagonal<Scalar, Options>);

bp::def("vector1x1", vector1x1<double, Eigen::ColMajor>);
bp::def("matrix1x1", matrix1x1<double, Eigen::ColMajor>);
bp::def("matrix1x1", matrix1x1_input<double, Eigen::ColMajor>);
bp::def("matrix1x1_int", matrix1x1_input<int, Eigen::ColMajor>);
bp::def("emptyVector", emptyVector<Scalar, Options>);
bp::def("emptyMatrix", emptyMatrix<Scalar, Options>);
}

bp::def("print", print<double, Eigen::ColMajor>);
bp::def("copy", copy<double, Eigen::ColMajor>);
bp::def("diagonal", diagonal<double, Eigen::ColMajor>);
BOOST_PYTHON_MODULE(sparse_matrix) {
namespace bp = boost::python;
eigenpy::enableEigenPy();

bp::def("emptyVector", emptyVector<double, Eigen::ColMajor>);
bp::def("emptyMatrix", emptyMatrix<double, Eigen::ColMajor>);
expose_functions<double, Eigen::ColMajor>();
expose_functions<double, Eigen::RowMajor>();
}

0 comments on commit f9ef66b

Please sign in to comment.