Skip to content

Rcpp integration for the Armadillo templated linear algebra library

Notifications You must be signed in to change notification settings

RcppCore/RcppArmadillo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

331194d · Apr 14, 2025
Aug 10, 2017
Mar 11, 2025
Feb 12, 2025
Apr 14, 2025
May 13, 2022
Feb 11, 2025
Feb 18, 2024
Feb 23, 2022
Feb 18, 2025
Jul 26, 2023
Oct 30, 2023
Jun 4, 2021
Oct 26, 2020
Feb 6, 2017
Jul 26, 2023
Apr 14, 2025
Mar 28, 2025
Oct 31, 2023
Jul 8, 2024
Oct 19, 2022
Mar 26, 2025
Mar 26, 2025

Repository files navigation

RcppArmadillo: R and Armadillo via Rcpp

CI License CRAN Debian package r-universe Dependencies Coverage Status Last Commit Downloads (monthly) Downloads (total) CRAN use CRAN indirect BioConductor use CSDA

Synopsis

RcppArmadillo provides an interface from R to and from Armadillo by utilising the Rcpp R/C++ interface library.

What is Armadillo?

Armadillo is a high-quality linear algebra library for the C++ language, aiming towards a good balance between speed and ease of use. It provides high-level syntax and functionality deliberately similar to Matlab (TM). See its website more information about Armadillo.

So give me an example!

Glad you asked. Here is a light-weight and fast implementation of linear regression:

#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);
}

You can Rcpp::sourceCpp() the file above to compile the function. A version is also included in the package as the fastLm() function.

The RcppArmadillo/Lighter header includes Rcpp via its Rcpp/Lighter header which precludes some more compile-time heavy features such as 'Rcpp Modules' which we may not need. See the Rcpp docs more details about 'Light', 'Lighter' and 'Lightest'. In the example above, the switch saves about 15% of total compilation time.

Status

The package is mature yet under active development with releases to CRAN about once every other month, and widely-used by other CRAN packages as can be seen from the CRAN package page. As of April 2024, there are 1135 CRAN packages using RcppArmadillo.

Documentation

The package contains a pdf vignette which is a pre-print of the paper by Eddelbuettel and Sanderson in CSDA (2014), as well as an introductory vignette for the sparse matrix conversions.

Installation

RcppArmadillo is a CRAN package, and lives otherwise in its own habitat on GitHub within the RcppCore GitHub organization.

Run

install.packages("RcppArmadillo")

to install from your nearest CRAN mirror.

Authors

Dirk Eddelbuettel, Romain Francois, Doug Bates, Binxiang Ni, and Conrad Sanderson

License

GPL (>= 2)

About

Rcpp integration for the Armadillo templated linear algebra library

Topics

Resources

Citation

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published