nalzok
nalzok

Reputation: 16147

Can I modify a row in-place with Rcpp?

Say, I would like to increase the first row of a matrix by one. The obvious approach is A.row(0) = A.row(0) + 1;, but it creates a new row instead of modifying the existing one, and may cause some performance issue when the matrix is large.

From a previous answer, I learned that I can do a point aliasing, but it only works for the whole matrix, not for individual rows.

library(Rcpp)
cppFunction('
void increaseFirstRow(NumericMatrix& A) {
    NumericVector B = A.row(0);
    B = B + 1;
}')
A <- diag(c(1.0, 2.0, 3.0))
increaseFirstRow(A)

The output is shown below. Note that the first row is not changed.

> A
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    2    0
[3,]    0    0    3

Upvotes: 2

Views: 757

Answers (2)

coatless
coatless

Reputation: 20746

Under the formulation above, I think you wanted to obtain a reference to specific parts of the matrix. The following work across matrix types:

*Matrix::Row = x( 0 , Rcpp::_);  // first row
*Matrix::Column = x( Rcpp::_ , 0); // first column
*Matrix::Sub = x( Rcpp::Range(0, 1) , Rcpp::Range(2, 3)); // first 2 rows and 3 -4th column.

In your case, that would be:

#include <Rcpp.h>

// [[Rcpp::export]]
void row_ref(Rcpp::NumericMatrix M) {
    // Create a reference to the 1st row in M.
    Rcpp::NumericMatrix::Row x = M.row(0);
    // Increase the first row in M.
    x = x + 10; 
}

Example:

(A <- diag(c(1.0, 2.0, 3.0)))
#      [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    2    0
# [3,]    0    0    3

row_ref(A)

A
#      [,1] [,2] [,3]
# [1,]   11   10   10
# [2,]    0    2    0
# [3,]    0    0    3

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368609

Here is a simple solution in RcppArmadillo, and, following an edit, in Rcpp itself:

Code with Example

#include <RcppArmadillo.h>

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

// [[Rcpp::export]]
void incRow(arma::mat& M) {
  M.row(0) = M.row(0) + 1;
}

// [[Rcpp::export]]
void incRow2(Rcpp::NumericMatrix M) {
  M(0,Rcpp::_) = M(0,Rcpp::_) + 1;
}


/*** R
A <- diag(c(1.0, 2.0, 3.0))
incRow(A)
A
incRow2(A)
A
*/

Output

R> Rcpp::sourceCpp("/tmp/armarow.cpp")

R> A <- diag(c(1.0, 2.0, 3.0))

R> incRow(A)

R> A
     [,1] [,2] [,3]
[1,]    2    1    1
[2,]    0    2    0
[3,]    0    0    3

R> incRow2(A)

R> A
     [,1] [,2] [,3]
[1,]    3    2    2
[2,]    0    2    0
[3,]    0    0    3
R> 

Upvotes: 2

Related Questions