Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sparse matrices #59

Open
N-Wouda opened this issue Aug 29, 2020 · 4 comments
Open

Add support for sparse matrices #59

N-Wouda opened this issue Aug 29, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@N-Wouda
Copy link

N-Wouda commented Aug 29, 2020

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 N-Wouda changed the title Add support for sparse Armadillo matrices Add support for sparse matrices Aug 29, 2020
@RUrlus
Copy link
Owner

RUrlus commented Aug 30, 2020

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 RUrlus added the enhancement New feature or request label Aug 30, 2020
@N-Wouda
Copy link
Author

N-Wouda commented Sep 1, 2020

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.

@RUrlus
Copy link
Owner

RUrlus commented Sep 1, 2020

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

@peekxc
Copy link

peekxc commented Sep 16, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants