0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
mshaffer
  • 959
  • 1
  • 9
  • 19
  • 1
    Please try to at least read _a little bit_ of the Rcpp documentation. The [Extending R with C++: A Brief Introduction to Rcpp](https://cloud.r-project.org/web/packages/Rcpp/vignettes/Rcpp-introduction.pdf) vignette is just that. Peppering us here with questions that have been asked and answered before -- and are documented -- wastes your time and ours. In short, you want `Rcpp.package.skeleton()`, or its variants in RcppArmadillo and RcppEigen, or the RStudio helpers. – Dirk Eddelbuettel Jul 31 '22 at 23:00
  • I can compile my own package WITHOUT Rcpp components. I am trying to figure out how to add Rcpp to a traditional package format. I guess you are saying `start with the skeleton`? – mshaffer Jul 31 '22 at 23:24
  • This white paper is helpful, thank you! – mshaffer Jul 31 '22 at 23:29

0 Answers0