I am now trying to convert R code to Rcpp code.
The R code is
hh <- function(A,B,j){
aa <- A[j,-j] %*% B[j,-j] ## A and B are n*m matrixes
return(aa)
}
> set.seed(123)
> A <- matrix(runif(30),5,6)
> B <- matrix(rnorm(30),5,6)
> j <- 2
> hh(A,B,j)
> [,1]
> [1,] 0.9702774
My Rcpp code is
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
double hh(arma::mat A, arma::mat B, arma::uword j){
arma::mat Bj = B.shed_col(j); /* error occurs */
arma::mat Ak = A.row(j);
double aa = Ak.shed_col(j) * arma::trans(Bj.row(j)); /* error occurs */
return aa;
}
The error should be about the use of .shed_row/.shed_col. I have Googled .shed_row, however, did not have an idea yet to address the issue I encountered here. Do you have any idea? Thank you in advance!
Further Update:
Now we consider using .shed_row/.shed_col
in for-loop in the function.
Specifically, my Rcpp code is the following
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat ggcpp(arma::mat A, arma::mat B){
/*we assume here A and B are n*n square matrices.*/
int Ac = A.n_cols;
int Bc = B.n_cols;
arma::mat C(Ac,Bc);
for(arma::uword i = 0; i < Ac; i++){
A.shed_col(i);
for(arma::uword j = 0; j < Bc; j++){
B.shed_col(j);
C(i,j) = arma::as_scalar(A.row(i) * B.row(j).t());
}
}
return C;
}
The equivalent R code is the following
gg <- function(A,B){
ac <- ncol(A)
bc <- ncol(B)
C <- matrix(NA,ac,bc)
for(i in 1:ac){
for(j in 1:bc){
C[i,j] <- A[i,-i] %*% B[j,-j]
}
}
return(C)
}
My R code works. It has been tested. However, I am having troubles with the Rcpp code.
I tried many ways and mainly encountered two errors :
- Error in ggcpp(a1, a2) : as_scalar(): incompatible dimensions
- Error in ggcpp(a1, a2) : Mat::shed_col(): index out of bounds
Here, a1
and a2
are two randomly generated 6*6 matrices.
Do you have any idea? Appreciated!!!
Answer to Further Update:
Note:
row_erase
andcol_erase
are borrowed from here.