|
|
RcppArmadillo | |||||
|
Bio Code rprotobuf rinside rcpp changelog rcppexamples rcpparmadillo rpostgresql rdieharder littler random digest rquantlib beancounter smtm yahooquote octave-mt octave-pg Linux Quantian About Blog
|
OverviewRcppArmadillo provides an interface from R to and from Armadillo by utilising the Rcpp R/C++ interface library.So what is Armadillo?Armadillo is a C++ linear algebra library aiming towards a good balance between speed and ease of use. Integer, floating point and complex numbers are supported, as well as a subset of trigonometric and statistics functions. Various matrix decompositions are provided through optional integration with LAPACK and ATLAS libraries.A delayed evaluation approach is employed (during compile time) to combine several operations into one and reduce (or eliminate) the need for temporaries. This is accomplished through recursive templates and template meta-programming. This library is useful if C++ has been decided as the language of choice (due to speed and/or integration capabilities), rather than another language. So give me an example!Sure, glad you asked. Here is a simple implementation of a fast linear regression (provided by RcppArmadillo via thefastLm()
function):
#include <RcppArmadillo.h> extern "C" SEXP fastLm(SEXP ys, SEXP Xs) { Rcpp::NumericVector yr(ys); // creates Rcpp vector from SEXP Rcpp::NumericMatrix Xr(Xs); // creates Rcpp matrix from SEXP int n = Xr.nrow(), k = Xr.ncol(); arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy arma::colvec y(yr.begin(), yr.size(), false); arma::colvec coef = arma::solve(X, y); // fit model y ~ X arma::colvec resid = y - X*coef; // residuals double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) ); // std.error of estimate arma::colvec stderrest = arma::sqrt( sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) ); return Rcpp::List::create( Rcpp::Named("coefficients") = coef, Rcpp::Named("stderr") = stderrest ) ; } Where do I get itRcppArmadillo is now a CRAN package, lives otherwise in its own habitat on R-Forge within Rcpp and can also be downloaded from the local Rcpp archive.AuthorsRcppArmadillo is being written by Romain Francois, Dirk Eddelbuettel and Doug Bates using Rcpp and building on top of the excellent Armadillo library by Conrad Sanderson.LicenseRcppArmadillo is licensed under the GNU GPL version 2 or later. |
|||||