Add support for sparse matrices
N-Wouda opened this issue · 4 comments
This is something I'm missing a bit: a way to convert scipy.sparse
's csc_matrix
into an equivalent arma::sp_mat
matrix (and back). I plan to write a converter myself as I need this, but I can imagine it makes a nice addition to the project. If you want, I can open a PR for this soon.
Definitely, would be happy to accept a PR for this.
I would prefer a separate header, e.g. sparse_converters.h
, for the conversions as converters.h
is already a bit longer/dense.
Great! This is a side project for me, so development is a little slow. I expect to have something ready near the end of the month.
No rush, it's the same for me. For the approach keep in mind that the dev/0.4.0
branch is quite different from the current master. converters.h
and utils.h
are stable where only arraystore.h
is subject to change from the current commit
For anyone who comes across this in the future---if one can guarantee csc format from scipy, then here's a quick a dirty solution in pybind11:
void to_sparse(const py::object& S, arma::sp_mat& out){
py::tuple shape = S.attr("shape").cast< py::tuple >();
const size_t nr = shape[0].cast< size_t >(), nc = shape[1].cast< size_t >();
arma::uvec ind = carma::arr_to_col(S.attr("indices").cast< py::array_t< arma::uword > >());
arma::uvec ind_ptr = carma::arr_to_col(S.attr("indptr").cast< py::array_t< arma::uword > >());
arma::vec data = carma::arr_to_col(S.attr("data").cast< py::array_t< double > >());
out = arma::sp_mat(ind, ind_ptr, data, nr, nc);
}