Update Rcpp::NumericMatrix passed by reference using RcppArmadillo submat()
Asked Answered
A

1

0

Following on this question, I am trying to understand how to efficiently update a subset of a Rccp::NumericMatrix data type.

I have the following scenario:

  • Rcpp::NumericMatrix m of 5 x 5 that needs few rows and columns updated.
  • It will be passed by reference to a function (void return type) that will convert it to an arma::mat, and update the respective submat().
  • At this point I don't understand how to "apply" the changes that occurred inside the function to the m matrix that was passed to the function.

The code looks like this:

#include <iostream>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]


// [[Rcpp::export]]
void updateMatrix(const Rcpp::NumericMatrix &m)
{
    std::cout << m << std::endl;

    Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) = Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) + 1;

    std::cout << m << std::endl;
}

To run it from R I use:

m = matrix(0, 5, 5)

updateMatrix(m)

And the results are:

> updateMatrix(m)
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000

0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000

It's the first time I'm using Rcpp and RcppArmadillo and they are absolutely amazing. I appreciate any help with this scenario.

Appleby answered 14/5, 2017 at 9:32 Comment(0)
S
3

The left side of your assignment in updateMatrix creates a temporary that is discarded after assignment. Therefore, m doesn't change at all. The code can't work as you expected as the that would mean the type of m would change. Look below:

#include <typeinfo>
#include <iostream>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]


// [[Rcpp::export]]
void updateMatrix(const Rcpp::NumericMatrix &m)
{
  std::cout << m << std::endl;

  std::cout << typeid(m).name() << std::endl;

  arma::mat m2 = Rcpp::as<arma::mat>(m);

  std::cout << typeid(m2).name() << std::endl;

  m2.submat(0, 0, 3, 3) = Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) + 1;

  std::cout << m2 << std::endl;
}

Running this code gives:

> m = matrix(0, 5, 5)
> updateMatrix(m)
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000

N4Rcpp6MatrixILi14ENS_15PreserveStorageEEE
N4arma3MatIdEE
   1.0000   1.0000   1.0000   1.0000        0
   1.0000   1.0000   1.0000   1.0000        0
   1.0000   1.0000   1.0000   1.0000        0
   1.0000   1.0000   1.0000   1.0000        0
        0        0        0        0        0
Sonata answered 14/5, 2017 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.