Dear List,

I wonder if there is a way to convert a big matrix to ‘float’ instead of 
‘double’ within a Rcpp program. The reason for using float is mainly for 
performance improvement.

For instance, I have a simple function named ‘print_bigmat’ as shown below.

As shown in the output, ‘double_bigmat' will save the correct values of the 
original matrix ‘x’ but not ‘float_bigmat’.

// [[Rcpp::export]]
int print_bigmat(SEXP pBigMat) {
    
    XPtr<BigMatrix> xpMat(pBigMat);
    
    const mat& double_bigmat = arma::Mat<double>((double *)xpMat->matrix(), 
xpMat->nrow(), xpMat->ncol(), false);
    
    const fmat& float_bigmat = arma::Mat<float>((float *)xpMat->matrix(), 
xpMat->nrow(), xpMat->ncol(), false);
    
    Rcout << double_bigmat << endl;
    
    Rcout << float_bigmat << endl;
    
    return 0;
}


Output:

> x <- matrix(rnorm(10), nrow=2)
> x
            [,1]        [,2]      [,3]       [,4]       [,5]
[1,] -0.05514382 -0.03943825 1.4145593 -0.1161918  2.3282466
[2,] -1.22023371 -0.35592125 0.7714512  0.6865120 -0.3504811
> print_bigmat(as.big.matrix(x)@address)
  -0.0551  -0.0394   1.4146  -0.1162   2.3282
  -1.2202  -0.3559   0.7715   0.6865  -0.3505

  -3.3865e-14  -8.6552e+04   4.5441e-07  -5.0912e+23  -1.5184e+34
  -1.3456e+00  -1.9025e+00  -1.2828e+00  -1.6780e+00   1.9268e+00

[1] 0
> 


Thanks much,
Yue


_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to