EricH
EricH

Reputation: 23

Access and modify the non zero elements of sparse matrix of class arma::sp_mat using Rcpp code

I'm suffering about accessing and updating the non zero elements of sparse matrix of class arma:sp_mat in Armadillo using RcppArmadillo. For example, in the Matrix R package, if B is a sparse matrix of class dgCMatrix, one can access and modify its non zero elements by doing:

B@x[] = xx

where xx is the new vector containing the actual non-zero elements. Can someone help me doing the same thing with Armadillo code?

Upvotes: 1

Views: 777

Answers (1)

coatless
coatless

Reputation: 20746

Unfortunately, there is no nice accessor that returns the location of entries in sp_mat.

To get this information, we first calculate the number of elements in the list, create a location umat, and then construct a new sp_mat using a batch constructor as recommended by the API docs.

Approach Code

#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::depends(RcppArmadillo)]]


// Obtains a list of coordinates from the sparse matrix by using iterators
// First calculates total number of points and, then, obtains list.
// [[Rcpp::export]]
arma::umat get_locations(arma::sp_mat& B)
{

    // Make const iterator
    arma::sp_mat::const_iterator start = B.begin();
    arma::sp_mat::const_iterator end   = B.end();

    // Calculate number of points
    int n = std::distance(start, end);

    // Kill process if no values are found (very sparse matrix)
    if (n <= 0) { Rcpp::stop("No values found!"); }

    // Build a location storage matrix
    arma::umat locs(2, n);

    // Create a vector to store each row information in. (Row, Col)
    arma::uvec temp(2);

    // Start collecting locations
    arma::sp_mat::const_iterator it = start; 
    for(int i = 0; i < n; ++i)
    {
        temp(0) = it.row();
        temp(1) = it.col();
        locs.col(i) = temp;
        ++it; // increment
    }

    return locs;
}

// Updates the sparse matrix by constructing a new one
// [[Rcpp::export]]
arma::sp_mat update_sp_matrix(arma::sp_mat& B, arma::vec values) 
{

    // Get all the locatoins
    arma::umat locs = get_locations(B);

    // Make sure we have the correct number
    if (locs.n_rows != values.n_elem) { 
        Rcpp::stop("Length mismatch between locations and supplied values!");
    }

    // The documentation recommends using batch constructor to rebuild matrix
    B = arma::sp_mat(locs, values, B.n_rows, B.n_cols);

    return B;
}

Testing Functions

// Generates a sparse matrix interally to test with
// Dimensions are 10 x 10 with only 2 points filled in.
arma::sp_mat make_test_sp()
{

    // creates a matrix C++98 style
    arma::umat locs;
    locs << 4 << 7 << arma::endr
         << 6 << 7 << arma::endr;

    // creates a vector C++98 style
    arma::vec vals;
    vals << 4.5 << 8.2 << arma::endr;

    arma::sp_mat B(locs, vals, 10, 10);

    return B;
}

// Main runner calls the built in test generation function.
// [[Rcpp::export]]
arma::sp_mat test_me() 
{
    arma::sp_mat B = make_test_sp();
    arma::vec temp = arma::ones<arma::vec>(2);
    return update_sp_matrix(B, temp);
}

Edit

Changed the get_location() code to reflect the sp_mat structure of

2 x N

instead of

N x 2

Thanks to @EricH's comment

Upvotes: 3

Related Questions