https://github.com/rcppcore/rcpparmadillo
Rcpp integration for the Armadillo templated linear algebra library
https://github.com/rcppcore/rcpparmadillo
armadillo c-plus-plus r r-package rcpp rcpparmadillo
Last synced: 4 days ago
JSON representation
Rcpp integration for the Armadillo templated linear algebra library
- Host: GitHub
- URL: https://github.com/rcppcore/rcpparmadillo
- Owner: RcppCore
- Created: 2013-11-02T18:33:00.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2025-04-26T11:51:51.000Z (22 days ago)
- Last Synced: 2025-05-13T22:51:36.826Z (4 days ago)
- Topics: armadillo, c-plus-plus, r, r-package, rcpp, rcpparmadillo
- Language: C++
- Homepage:
- Size: 10.8 MB
- Stars: 202
- Watchers: 19
- Forks: 56
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
Awesome Lists containing this project
README
## RcppArmadillo: R and Armadillo via Rcpp
[](https://github.com/RcppCore/RcppArmadillo/actions?query=workflow%3Aci)
[](https://www.gnu.org/licenses/gpl-2.0.html)
[](https://cran.r-project.org/package=RcppArmadillo)
[](https://packages.debian.org/sid/r-cran-rcpparmadillo)
[](https://rcppcore.r-universe.dev/RcppArmadillo)
[](https://cran.r-project.org/package=RcppArmadillo)
[](https://app.codecov.io/github/RcppCore/RcppArmadillo?branch=master)
[](https://github.com/RcppCore/RcppArmadillo)
[](https://www.r-pkg.org/pkg/RcppArmadillo)
[](https://www.r-pkg.org/pkg/RcppArmadillo)
[](https://cran.r-project.org/package=RcppArmadillo)
[](https://cran.r-project.org/package=RcppArmadillo)
[](https://cran.r-project.org/package=RcppArmadillo)
[](https://doi.org/10.1016/j.csda.2013.02.005)### Synopsis
RcppArmadillo provides an interface from R to and from [Armadillo][armadillo] by utilising the [Rcpp
R/C++ interface library][rcpp].### What is Armadillo?
[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](https://arma.sourceforge.net/docs.html) deliberately similar to Matlab (TM). See
[its website][armadillo] more information about Armadillo.### So give me an example!
Glad you asked. Here is a light-weight and fast implementation of linear regression:
```c++
#include
// [[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()`](https://cran.r-project.org/package=Rcpp/vignettes/Rcpp-attributes.pdf)
the file above to compile the function. A version is also included in the
package [as the `fastLm()`](https://github.com/RcppCore/RcppArmadillo/blob/master/R/fastLm.R)
function.The `RcppArmadillo/Lighter` header includes [Rcpp][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][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][cran] about once every
other month, and widely-used by other CRAN packages as can be seen from the [CRAN package page][cran
pkg]. 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](http://dx.doi.org/10.1016/j.csda.2013.02.005)
in CSDA (2014), as well as an introductory vignette for the sparse
matrix conversions.### Installation
RcppArmadillo is a [CRAN package][cran pkg], and lives otherwise in its own habitat on
[GitHub](https://github.com/RcppCore/RcppArmadillo) within the
[RcppCore](https://github.com/RcppCore) GitHub organization.Run
```r
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)
[armadillo]: https://arma.sourceforge.net
[rcpp]: https://www.rcpp.org
[cran]: https://cran.r-project.org
[cran pkg]: https://cran.r-project.org/package=RcppArmadillo