This documents describes all language elements that can be compiled to Armadillo flavored C++ code.

Function signature

armacmp always compiles functions. Every function needs to have a return statement with an optional type argument.

my_fun <- compile(function(X, y = type_colvec())) {
  return(X %*% y, type = type_colvec())
}

You can define your inputs using the standard function syntax. By default paramters are of type type_matrix. But they can have other types such as:

  • type_colvec - a column vector
  • type_rowvec - a row vector
  • type_scalar_integer - a single int value
  • type_scalar_numeric - a single double value
  • type_scalar_bool - a single boolean value

All functions need to return a value using the return function. The return function can have an optional return type argument with return(value, type = type_rowvec())

Type system

The package supports only a very limited number of types. For scalar values these are bool, int and double. All matrices and vectors are of type arma::mat/colvec/rowvec for double matrices, arma::imat/icolvec/irowvec for integer matrices and arma::umat/ucolvec/urowvec for unsigned integer matrices (used for indicies).

All operations are performed either on Armadillo types or on scalar types and not on R’s data structures.

The package tries to figure out the correct types of variables and functions, but you can give type hints. If the package cannot figure out the type, the C++ type auto is used which can have suboptimal performance when used together with Armadillo operations.

Operators

Assignments

Example:

Multiplication

  • %*% - matrix multiplication translates to the * operator on both arma::mat types and regular variables/expressions.
  • * - elementwise multiplication gets translated to % for arma::mat types and to * otherwise.

The rest

The following operators are translated to their C++ counter-part.

Functions

General math functions

  • x ^ y
  • exp
  • abs
  • log
  • sum
  • min
  • max
  • sqrt
  • cos
  • acos
  • cosh
  • acosh
  • sin
  • asin
  • sinh
  • asinh
  • tan
  • atan
  • tanh
  • atanh

Linear algebra

  • t
  • chol
  • diag
  • QR
  • svd
  • solve
  • ncol
  • nrow
  • crossprod
  • tcrossprod
  • det
  • [i, j]
  • [i, ]
  • [, j]
  • [i]
  • length

Rest

  • cumsum
  • sort
  • unique
  • cumsum
  • cumprod
  • pnorm
  • seq_len returns a arma::colvec (note that is corresponds to a sequence of numerics)
  • a:b returns an arma::colvec from a to b (note that is corresponds to a sequence of numerics)
  • rep.int returns a arma::colvec (note that is corresponds to a sequence of numerics)

Control Flow

Currently if/else, for and while loops are supported.

if/else

Just make sure all return statements return the same type.

Also if/else cannot be used as an expression:

Functions

You can also define functions within your functions (lambdas).

Using lambdas it is possible to define recursive functions as well.