alugowski/fast_matrix_market

Add armadillo bindings

theAeon opened this issue · 7 comments

Interested in using this library with armadillo. Should be a fairly simple task to bind to the MAT/SP_MAT/VEC types-let me know if you're willing to take a crack at it before I take a crack at it myself. Thanks!

actually, you know what, I don't actually know that one's needed. you can literally use the armadillo constructor with your existing array form-

        fast_matrix_market::matrix_market_header header;
        std::ifstream ifs(this->m_Bfile_name);
        std::vector<double> Bmem;
        fast_matrix_market::read_matrix_market_array(ifs, header, Bmem);
        arma::mat B(Bmem);
        B.reshape(header.nrows, header.ncols);

and for sparse:

fast_matrix_market::matrix_market_header headerA;
        std::ifstream ifsA(this->m_Afile_name);
        std::vector<UWORD> rowA;
        std::vector<UWORD> colA;
        std::vector<double> valueA;
        fast_matrix_market::read_matrix_market_triplet(ifsA, headerA, rowA, colA, valueA);
        arma::uvec urowa(rowA);
        arma::uvec ucola(colA);
        arma::umat ucoo = join_rows(urowa, ucola).t();
        arma::Col uvala(valueA);
        arma::spmat A(ucoo, uvala, headerA.nrows, headerA.ncols);

That looks right. Does this serve your purposes?

The only way I can think of to improve is to write directly into the Mat instead of using an intermediate std::vector. That may require a fast_matrix_market improvement.

Writes should be doable with direct reads from both Mat and SpMat. As far as I can tell from a quick glance, SpMat has the standard CSC arrays so csc_formatter should take care of it.

Yup-worked great for what i needed it to do. Might end up integrating it into more depending on how a meeting in the next few weeks goes.

Appreciate it!

I looked into the Armadillo docs a bit more, and your dense matrix code has a subtle bug. Armadillo dense matrices are column-major (see https://arma.sourceforge.net/docs.html#Mat). FMM supports row or column, but this code:

fast_matrix_market::read_matrix_market_array(ifs, header, Bmem);

uses the default of row_major.

So your matrices had their values transposed.

I added code to support any vector that supports resize() and begin(), not just std::vector. With that actual Armadillo bindings are somewhat trivial, so I went ahead and wrote them.

https://github.com/alugowski/fast_matrix_market/blob/main/README.Armadillo.md

They're already in main, I'll cut a release soon.

I looked into the Armadillo docs a bit more, and your dense matrix code has a subtle bug. Armadillo dense matrices are column-major (see https://arma.sourceforge.net/docs.html#Mat). FMM supports row or column, but this code:

fast_matrix_market::read_matrix_market_array(ifs, header, Bmem);

uses the default of row_major.

So your matrices had their values transposed.

Well, that would explain why I had to throw in an extra transpose at the end of my initialization. Good catch,

I added code to support any vector that supports resize() and begin(), not just std::vector. With that actual Armadillo bindings are somewhat trivial, so I went ahead and wrote them.

https://github.com/alugowski/fast_matrix_market/blob/main/README.Armadillo.md

They're already in main, I'll cut a release soon.

appreciate it!