The package ast2ast translates an R function into a C++ function and returns an external pointer (XPtr) to this function. The scope of ast2ast is to generate functions that can be used during solving ode-systems (derivative function or jacobian function) or during optimization. More generally, the translated function can be used in fields where it is necessary to evaluate a function very often. Especially when the function is evaluated by C++ the generated external pointer is very sufficient.
First of all, the supported objects and functions listed below are explained in detail. Here, the arguments which have to be passed to the functions are described and it is explained what the function returns. Furthermore, for each function, a small example is given showing how to use it. Moreover, it is explained how the function differs from R equivalents. If another differences are detected please report them.
This paragraph explains how the examples in this vignette are executed. First of all, an Rcpp function is created, which executes the output of the function translate. If you want to know how these functions work in detail you can go to the vignette Information for Package authors. In the examples, only the R code is shown to show how to write the code.
Supported objects:
Supported functions:
There exist two containers that can be used in ast2ast functions. Both containers can only hold the numeric type of R (which is equivalent to double). The first container is a vector and the second one is a matrix. It is possible to declare a variable of a scalar numeric data type. This is done by adding _db (e.g. varname_db) to the end of the variable. Each time _db is found the variable is declared as a scalar numeric data type. In this case, the object cannot change its type!
It is pivotal to follow the rules of variable naming in C++. For instance, it is not allowed to use ‘.’ in variable names. Moreover, the following functions are implemented in C++ and thus it is not possible to use these names:
getlength | VVSIN | VVacos | VVtanh |
getattributes | sinus | acosinus | tangensh |
is_matrix | VVsinh | VVCOSH | VVMINUS |
VEC | sinush | cosinush | VSMINUS |
at | VVasin | VVtan | SVMINUS |
d2i | asinus | tangens | VVPLUS |
i2d | VVCOS | VVatan | VSPLUS |
ass | cosinus | atangens | SVPLUS |
VVTIMES | It | exp | matrix |
VSTIMES | STORE | combine | NA |
SVTIMES | for_ | coca | NA |
VVDIV | li | cd | NA |
VSDIV | cmr | colon | NA |
SVDIV | VVEXP | length | NA |
subassign | VVlog | dim | NA |
subset | ln | vector | NA |
Features of ast2ast
Example 1
<- function(variable) {
f <- 1
variable
}library(ast2ast)
<- translate(f)
fetr <- 10
x <- byref(fetr, x) output
## x before call of function:
## 10
## x after call of function:
## 1
output
## NULL
Caveats:
In Example 2 the various ways of declaring variables are presented. To assign a value to a variable you can use <- or =. As already mentioned only numeric values are possible. If you want to assign a vector you can use either the c or vector function. The c function works in the same way as R and can handle any combinations of scalars, vectors or matrices. The function vector differs in two ways from the R equivalent. First of all, you cannot use terms such as vector(length = size) as this is not possible in C++. In contrast, you just write vector(size). The R function rep is not available in ast2ast but it is possible to write vector(value, size) which in R would be written as rep(value, size). A third way to use the vector function is to pass another vector and the size e.g. vector(other_vector, size). The matrix function works in the same way as the vector function. However, instead of the size, two arguments are needed the number of rows and the number of columns.
Example 2
<- function() {
f <- 1
a <- 3.14
a_db = 2
b <- c(1, 2, 3)
c = vector(2)
d <- vector(3.14, 4)
e <- vector(c, 3)
f <- matrix(2, 2)
g <- matrix(6, 2, 2)
h <- matrix(e, 2, 2)
i
print("a")
print(a)
print(a_db)
print()
print("b")
print(b)
print()
print("c")
print(c)
print()
print("d")
print(d)
print()
print("e")
print(e)
print()
print("f")
print(f)
print()
print("g")
print(g)
print()
print("h")
print(h)
print()
print("i")
print(i)
print()
}library(ast2ast)
<- translate(f)
fetr vardec(fetr)
As usual in R it is possible to use basic arithmetic operations on scalars, vectors and matrices (Example 3).
Example 3
<- function() {
f
<- 2
a <- 3
b print("scalar operations")
print(a + b)
print(a - b)
print(a / b)
print(a * b)
print()
print("vector & scalar operations")
<- c(1, 2, 3)
a <- 4
b print(a + b)
print(b - a)
print()
print("2 vectors (same length)")
<- 6:8
a <- 1:3
b print(a / b)
<- 1:6
a <- 1:3
b print(a / b)
print("2 vectors (different length)")
print("multiple of each other")
<- 1:6
a <- 1:3
b print(a / b)
print("not a multiple of each other")
<- 1:5
a <- 1:3
b print(a / b) # different to R no warning
print()
print("matrix & scalar operations")
<- 3
a <- matrix(3, 2, 2)
b print(a*b)
print(b + 4)
print()
print("matrix & vector operations")
<- 5:6
a <- matrix(3, 2, 2)
b print(b - a)
print(a / b)
print()
print("matrix & matrix operations")
<- matrix(3, 2, 2)
a <- matrix(4, 2, 1) # difference to R!
b print(a + b)
print()
print("mixed operations")
<- 1
a <- 2:5
b <- matrix(50, 2, 2)
c <- a + b - c/2
d print(d)
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)
If you want to subset a vector or a matrix object you can use either [] or the at function. The [] is slower than at but more powerful (Example 4).
The following objects can be passed to [] when using a vector or matrix:
nothing
numeric scalar
logical
vector
matrix
result of comparison
caveat: it is not possible to pass the results of calculations!
In case of a matrix, it is possible to pass one of the above objects to access specific rows or columns respectively ([rows, cols]).
In contrast to [], the at function accepts only a scalar or two scalars for vectors or matrices, respectively. Thus, only a single element is accessed by this function! However, this function works faster. The result of at cannot be subsetted further. The at function returns the numeric type which is used when a variable is declared with the extension _db.
Example 4
<- function() {
f
print("pass nothing")
<- 1:8
a print(a)
<- 100
a[] print(a)
print()
print("pass logical")
<- 1:8
a print(a)
TRUE] <- 100
a[print(a)
print()
print("pass scalar")
<- 1:8
a print(a)
1] <- 100
a[print(a)
print()
print("pass vector")
<- 1:8
a <- 2:5
b print(a)
<- 100
a[b] print(a)
print()
print("pass result of ==")
<- 1:8
a < 5] <- 100
a[a print(a)
print()
print("pass result of !=")
<- 1:8
a <- c(1, 2, 3, 0, 0, 0, 0, 8)
b != b] <- 100
a[a print(a)
print()
print("pass result of <=")
<- 1:8
a <- c(1, 2, 3, 0, 0, 0, 0, 8)
b <= b] <- 100
a[a print(a)
print()
print("pass result of >=")
<- 1:8
a <- c(1, 2, 3, 0, 0, 0, 0, 9)
b >= b] <- 100
a[a print(a)
print()
print("pass result of >")
<- 1:8
a <- c(0, 2, 3, 0, 0, 0, 0, 9)
b > b] <- 100
a[a print(a)
print()
print("pass result of <")
<- 1:8
a <- c(0, 2, 3, 0, 0, 0, 0, 9)
b < b] <- 100
a[a print(a)
print()
print("pass scalar, scalar")
<- matrix(3, 4, 4)
a 1, 1] <- 100
a[print(a)
print()
print("pass vector, vector")
<- matrix(3, 4, 4)
a <- c(1, 3)
b <- c(2, 4)
c <- 100
a[b, c] print(a)
print()
print("pass ==, >=")
<- matrix(1:16, 4, 4)
a <- 1:4
b <- c(1, 8, 3, 8)
c == c, b >= c] <- 100
a[b print(a)
print()
print("at")
<- 1:16
a at(a, 2) <- 100
print(a)
print()
print("at")
<- matrix(1:16, 4, 4)
a at(a, 1, 4) <- 100
print(a)
print()
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)
There exist three helper function. The length function returns the number of elements of a vector or matrix. The dim function returns the number of rows and columns of a matrix. The : function can be used to create a range of numbers. For example 1:3 creates a vector with the elements 1, 2 and 3 or 1.1:5.3 returns a vector with the elements 1.1, 2.1, 3.1, 4.1 and 5.1. See Example 5 in order to see how the functions can be applied.
Example 5
<- function() {
f <- 1:4
a print(a)
<- 1.1:5.2
a print(a)
<- 1:16
a print(length(a))
<- matrix(1:4, 2, 2)
b print(dim(b))
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)
As usual in R it is possible to compare two objects using one of the following options (Example 6):
Example 6
<- function() {
f <- 1:4
a <- c(1, 2, 3, 5)
b <- 9
c print(a == b)
print(a <= b)
print(a >= b)
print(a != b)
print(a < c)
print(a > c)
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)
It is possible to write for-loops and ‘if’, ‘else if’, and ‘else’ branches as in native R (Example 7).
for(index in variable){
# do whatever
}
for(index in 1:length(variable){
# do whatever
}
Example 7
<- function() {
f <- 1:4
a for(i in a) {
print(i)
}
for(i in 1:length(a)) {
<- i + i
a[i]
}
for(i in 1:length(a)) {
if(i < 4 && i > 1) {
print(i)
}
}
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)
Using the function print as common in R (see Examples 2, 3, 4, 5, 6):
Following mathematical functions are available (see Example 8):
Example 8
<- function() {
f <- 1:4
a print(sin(a))
print(cos(a))
print(a^2)
print(exp(a, 3))
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)
To interpolate values, the ‘cmr’ function can be used. The function needs three arguments (see Example 9):
Example 9
<- function() {
f <- c(0, 1, 0.5, 2.5, 3.5, 4.5, 4)
dep <- 1:7
indep
<- c(0.5, 1, 1.5, 2, 2.5,
evalpoints 3, 3.5, 4, 4.5, 5,
5.5, 6, 6.5)
for(i in evalpoints) {
print(cmr(i, indep, dep))
}
}
library(ast2ast)
<- translate(f)
fetr call_fct(fetr)