RcppArmadillo

repository·master·Indexed 42 days ago

https://github.com/rcppcore/rcpparmadillo

A seamless interface between R and the Armadillo linear algebra library using Rcpp. It enables developers to write high-performance C++ code using Matlab-like syntax, providing automatic mapping from R objects to Armadillo classes such as arma::mat and arma::imat.

Tokens
980
Snippets
3
Records
5
Agent score
3%

What's inside RcppArmadillo

  1. Armadillo class mapping and types

    master

    RcppArmadillo provides automatic mapping from R objects to Armadillo classes. Common types include:

    • arma::mat: A convenience shortcut for a matrix of type double.
    • arma::imat: An integer matrix.

    Functions can use 'const reference' semantics (e.g., const arma::mat& X) to ensure efficient data transfer without unnecessary copying and to signal that the variable will not be altered.

  2. Use RcppArmadillo in C++ source files

    master

    To use Armadillo classes (like arma::mat or arma::colvec) in a C++ file intended for R, you must include the appropriate header and declare the dependency using an Rcpp attribute.

    For faster compilation, you can use the #include <RcppArmadillo/Lighter> header instead of the standard RcppArmadillo header. This uses the Rcpp/Lighter mechanism which excludes some heavy compile-time features (like 'Rcpp Modules') to save approximately 15% of total compilation time.

    Key requirements:

    1. Include <RcppArmadillo/Lighter> or <RcppArmadillo>.
    2. Use the // [[Rcpp::depends(RcppArmadillo)]] attribute to ensure the R environment provides the necessary headers and linking.
    #include <RcppArmadillo/Lighter>
    // [[Rcpp::depends(RcppArmadillo)]]
    
    // [[Rcpp::export]]
    Rcpp::List fastLm(const arma::mat& X, const arma::colvec& y) {
        int n = X.n_rows, k = X.n_cols;
    
        arma::colvec coef = arma::solve(X, y);     // fit model y ~ X
        arma::colvec res  = y - X*coef;            // residuals
        double s2 = arma::dot(res, res) / (n - k); // std.errors of coefficients
        arma::colvec std_err = arma::sqrt(s2 * arma::diagvec(arma::pinv(arma::trans(X)*X)));
    
        return Rcpp::List::create(Rcpp::Named("coefficients") = coef,
                                  Rcpp::Named("stderr")       = std_err,
                                  Rcpp::Named("df.residual")  = n - k);
    }
  3. Upgrade the Armadillo version in rcpparmadillo

    master

    If you need to upgrade to a newer release of the Armadillo library within the rcpparmadillo repository, follow these steps:

    1. Copy the new Armadillo files into the repository.
    2. Replace the existing armadillo_bits directory with the armadillo_bits directory from the new Armadillo source.
    3. Ignore or remove armadillo_bits/config.hpp.cmake, as rcpparmadillo does not use it for configuration.
  4. Use RcppArmadillo with cppFunction() in R

    master

    You can define C++ functions using Armadillo directly in an R session using Rcpp::cppFunction(). You must pass depends="RcppArmadillo" to the function to allow access to Armadillo's headers and classes.

    Example: Accessing the Armadillo hess() method to compute a Hessenberg matrix.

    library(Rcpp)   # for cppFunction()
    cppFunction("arma::mat hd(const arma::mat& m) { return hess(m); }",
                  depends="RcppArmadillo")
    set.seed(20251122)
    m <- matrix(rnorm(49), 7, 7)
    hd(m)  # compute Hessenberg decom using the function we just created