RcppCore / RcppArmadillo

Rcpp integration for the Armadillo templated linear algebra library
193 stars 56 forks source link

`arma::umat` does not correspond to an integer matrix in R #366

Closed stla closed 2 years ago

stla commented 2 years ago

Hello,

I expected to get a matrix M with int entries since arma::umat is a matrix of unsigned integers, right?

cpp <- '
arma::umat tcp(const arma::umat & A){
  arma::umat M(A*A.t());
  return M;
}
'
library(Rcpp)
cppFunction(
  cpp,
  depends = "RcppArmadillo"
)

A <- toeplitz(c(1L, 2L))
M <- tcp(A)
str(M)
# num [1:2, 1:2] 5 4 4 5

Is it a bug or am I misunderstanding something?

> xfun::session_info("RcppArmadillo")
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042), RStudio 2021.9.0.351

Locale:
  LC_COLLATE=French_Belgium.1252  LC_CTYPE=French_Belgium.1252   
  LC_MONETARY=French_Belgium.1252 LC_NUMERIC=C                   
  LC_TIME=French_Belgium.1252    

Package version:
  graphics_4.1.2           grDevices_4.1.2          methods_4.1.2           
  Rcpp_1.0.8               RcppArmadillo_0.10.8.1.0 stats_4.1.2             
  utils_4.1.2  
eddelbuettel commented 2 years ago

R only has its integer type integer aka int32_t, not uint32_t, so there is nothing we can do here.

As the range for uint32_t is larger, mapping could be 'lossy' on the range which is likely we decided (long-ago) to map to numeric aka double.

eddelbuettel commented 2 years ago

BTW there is the bit64 package with its integer64 type which is used in a number of packages so you could write yourself a custom converter. But that is outside of the scope of RcppArmadillo.