🚀使用Armadillo自动将线性代数R代码编译为C++

armacmp

The goal of armacmp is to create a DSL to formulate linear algebra code in R that is compiled to C++ using the Armadillo Template Library. It also offers an mathematical optimization that uses RcppEnsmallen to optimize functions in C++.

The scope of the package is linear algebra and Armadillo. It is not meant to evolve into a general purpose R to C++ transpiler.

This is currently an experimental prototype with most certainly bugs or unexpected behaviour. However I would be happy for any type of feedback, alpha testers, feature requests and potential use cases.

Potential use cases:

Speed up your code :)

Quickly estimate Rcpp speedup gain for linear algebra code

Learn how R linear algebra code can be expressed in C++ using armacmp_compile and use the code as a starting point for further development.

Mathematical optimization with arma_optim

…

Installation

remotes::install_github("dirkschumacher/armacmp")

Caveats and limitations

speed: R is already really fast when it comes to linear algebra operations. So simply compiling your code to C++ might not give you a significant and relevant speed boost. The best way to check is to measure it yourself and see for your specific use-case, if compiling your code to C++ justifies the additional complexity.

NAs: there is currently no NA handling. In fact everything is assumed to be double (if you use matrices/vectors).

numerical stability: Note that your C++ code might produce different results in certain situations. Always validate before you use it for important applications.

Example

You can compile R like code to C++. Not all R functions are supported.

When does armacmp improve performance?

It really depends on the use-case and your code. In general Armadillo can combine linear algebra operations. For example the addition of 4 matrices A + B + C + D can be done in a single for loop. Armadillo can detect that and generates efficient code.

So whenever you combine many different operations, armacmpmight be helpful in speeding things up.

We gather some examples on the wiki to further explore if compiling linear algebra code to C++ actually makes sense for pure speed reasons.

Related projects

nCompiler - Code-generate C++ from R. Inspired the approach to compile R functions directly instead of just a code block as in the initial version.

Contribute

armacmp is experimental and has a volatile codebase. The best way to contribute is to write issues/report bugs/propose features and test the package with your specific use-case.

Code of conduct

Please note that the ‘armacmp’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.