Movatterモバイル変換


[0]ホーム

URL:


RcppArmadillo: R andArmadillo via Rcpp

CILicenseCRANDebian packager-universeDependenciesCoverage StatusLast CommitDownloads (monthly)Downloads (total)CRAN useCRAN indirectBioConductor useCSDA

Synopsis

RcppArmadillo provides an interface from R to and fromArmadillo by utilising theRcpp R/C++ interface library.

What is Armadillo?

Armadillo is ahigh-quality linear algebra library for the C++ language, aiming towardsa good balance between speed and ease of use. It provides high-levelsyntax andfunctionalitydeliberately similar to Matlab (TM). Seeits website more informationabout Armadillo.

So give me an example!

Glad you asked. Here is a light-weight and fast implementation oflinear 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;// residualsdouble 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 canRcpp::sourceCpp()the file above to compile the function. A version is also included inthe packageasthefastLm() function.

TheRcppArmadillo/Lighter header includesRcpp via itsRcpp/Lighterheader which precludes some more compile-time heavy features such as‘Rcpp Modules’ which we may not need. See theRcpp docs more details about ‘Light’,‘Lighter’ and ‘Lightest’. In the example above, the switch saves about15% of total compilation time.

You can also experiment directly at the R prompt. The next examplecreates a one-liner to access the Armadillohess() methodto compute aHessenbergmatrix, a decomposition into an ‘almost triangular’ matrix. (Wechose this example as base R does not have this functionality; it is amore interesting example than providing yet another QR, SVD, or Eigendecomposition though these are of course equally easily accessible fromArmadillo.)

>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        [,1]     [,2]      [,3]      [,4]       [,5]      [,6]      [,7][1,]1.157451.64589-0.8099260.220417-1.6050835-0.947760-0.972432[2,]2.80695-2.656130.1193200.484858-0.08774640.913440-0.488142[3,]0.000001.95335-1.4364091.879113-0.1532293-0.532077-0.319914[4,]0.000000.00000-1.253221-1.6944140.24971111.2332311.055706[5,]0.000000.000000.000000-3.333499-1.4800146-0.913523-0.667435[6,]0.000000.000000.0000000.000000-1.2050704-0.2943691.254402[7,]0.000000.000000.0000000.0000000.00000000.324987-1.504329>

Note how we can once again access an Armadillo matrix via themat class in thearma namespace. Heremat is a convenience shortcut for a matrix of typedouble, there is alsoimat for an integermatrix and more, see the Armadillo documentation. We tellRcpp::cppFunction() about Armadillo via thedepends= argument, it allows it use information theRcppArmadillo package provides about its header filesetc.

The convenience of an automatic mapping from R directly into theArmadillo classes is a key advantage. The package also permits thestandard ‘const reference’ calling semantic ensuring efficient transferand signalling that the variable will not be altered.

Status

The package is mature yet under active development with releases toCRAN about once every othermonth, and widely-used by other CRAN packages as can be seen from theCRAN packagepage. As of August 2025, there are 1266 CRAN packages usingRcppArmadillo.

As of Armadillo 15.0.0, the minimum compilation standard is C++14.However, as several hundred CRAN packages still impose C++11 as theircompilation standard, the RcppArmadillo package also includes the finalversion allowing C++11, namely Armadillo 14.6.3, as a fallback used whenC++11 compilation is detected. Conversion to and compilation under C++14or later is encouraged. Rdefaultsto C++17 since version 4.3.0. SeeGitHub issue#475 for more about choosing between ‘legacy’ Armadillo 14.6.3 or‘current’ Armadillo 15.0.1 or later.

Performance

Performance is excellent, and on par or exceeding the performance ofanother package (or two, following a renaming) claiming otherwise. Seethispost and itsrepo for a detaileddebunking of that claim.

Documentation

The package contains a pdf vignette which is a pre-print of thepaper byEddelbuettel and Sanderson in CSDA (2014), as well as anintroductory vignette for the sparse matrix conversions.

Installation

RcppArmadillo is aCRANpackage, and lives otherwise in its own habitat onGitHub within theRcppCore GitHuborganization.

Run

install.packages("RcppArmadillo")

to install from your nearest CRAN mirror.

Authors

Dirk Eddelbuettel, Romain Francois, Doug Bates, Binxiang Ni, andConrad Sanderson

License

GPL (>= 2)


[8]ページ先頭

©2009-2025 Movatter.jp