R doesn't address bits for negative integers correctly. The easiest work around is to just rely on C++ functions:
Rcpp::cppFunction("long long RShift(long long a, int b) { return a >> b;}");
Rcpp::cppFunction("long long LShift(long long a, int b) { return a << b;}");
e.g.,
y = -1732584194;
RShift(y, 16);
bitwShiftR(y, 16);
Similarly, I can include an external file with C++ Matrix Multiplication:
Rcpp::sourceCpp("multiply.cpp");
where the file is:
// [[Rcpp::depends(RcppArmadillo, RcppEigen)]]
#include <RcppArmadillo.h>
#include <RcppEigen.h>
// [[Rcpp::export]]
SEXP armaMatMult(arma::mat A, arma::mat B){
arma::mat C = A * B;
return Rcpp::wrap(C);
}
// [[Rcpp::export]]
SEXP eigenMatTrans(Eigen::MatrixXd A){
Eigen::MatrixXd C = A.transpose();
return Rcpp::wrap(C);
}
// [[Rcpp::export]]
SEXP eigenMatMult(Eigen::MatrixXd A, Eigen::MatrixXd B){
Eigen::MatrixXd C = A * B;
return Rcpp::wrap(C);
}
// [[Rcpp::export]]
SEXP eigenMapMatMult(const Eigen::Map<Eigen::MatrixXd> A, Eigen::Map<Eigen::MatrixXd> B){
Eigen::MatrixXd C = A * B;
return Rcpp::wrap(C);
}
e.g.,
A = matrix(rnorm(10000), 100, 100); # fully populated, 100 x 100, relatively small
B = matrix(rnorm(10000), 100, 100);
library(microbenchmark);
microbenchmark::microbenchmark(
t(A),
eigenMatTrans(A)
);
microbenchmark::microbenchmark(
A%*%B,
armaMatMult(A, B),
eigenMatMult(A, B),
eigenMapMatMult(A, B)
);
So in the example, I have Rcpp "functions" as a string or a file.
How can I tell the "package-installer" to install and register these functions when the package is installed? So they are compiled ONCE and registered with help syntax? My intuition is telling me the .Call(
function may be involved.