carma icon indicating copy to clipboard operation
carma copied to clipboard

Add support for sparse matrices

Open N-Wouda opened this issue 3 years ago • 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.

N-Wouda avatar Aug 29 '20 15:08 N-Wouda

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.

RUrlus avatar Aug 30 '20 07:08 RUrlus

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.

N-Wouda avatar Sep 01 '20 07:09 N-Wouda

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

RUrlus avatar Sep 01 '20 08:09 RUrlus

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);
}

peekxc avatar Sep 16 '21 16:09 peekxc