-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunction-reference.Rmd
More file actions
232 lines (184 loc) · 4.79 KB
/
function-reference.Rmd
File metadata and controls
232 lines (184 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
title: "Function reference"
output:
rmarkdown::html_vignette:
toc: true
vignette: >
%\VignetteIndexEntry{Function reference}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(armacmp)
```
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.
```{r, eval=FALSE}
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](http://arma.sourceforge.net/faq.html) when used together with Armadillo operations.
## Operators
### Assignments
Example:
```{r, eval = FALSE}
compile(function(X) {
X2 <- X + 1
X2 <- X2 + 1
return(X2)
})
```
### 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
```{r, eval=FALSE}
if_clause <- compile(function(X) {
# infers that test needs to be bool (auto)
test <- sum(log(X)) < 10
X_new <- X
if (test) {
X_new <- (t(X) %*% X) + 10
} else if (sum(X) < 10) {
X_new <- t(X) %*% X
} else {
X_new <- (t(X) %*% X) + 10
}
return(X_new)
})
```
Just make sure all return statements return the same type.
Also `if/else` cannot be used as an expression:
```{r, eval=FALSE}
# this is not possible
x <- if (y) 1 else 2
```
### For loops
For loops are supported:
```{r, eval=FALSE}
# currently only seq_len is available to create sequences of ints
x <- seq_len(n)
for (i in x) {
...
}
```
Example:
```{r, eval=FALSE}
for_loop <- compile(function(X, offset = type_scalar_numeric()) {
X_new <- X
# only seq_len is currently supported
for (i in seq_len(20)) {
X_new <- log(t(X_new) %*% X_new + i + offset)
}
return(X_new)
})
```
### While loops
```{r, eval=FALSE}
x <- 0
while (x < 10) {
x <- x + 1
}
```
### Functions
You can also define functions within your functions (lambdas).
```{r}
fun <- compile(function(X) {
square <- function(y) {
return(y^2)
}
Y <- square(X)
return(Y)
})
fun(matrix(1:10))
```
Using lambdas it is possible to define recursive functions as well.
```{r}
is_even <- compile(function(x = type_scalar_numeric()) {
is_even_internal <- function(y = type_scalar_numeric()) {
if (y == 0) return(TRUE)
if (y < 0) return(FALSE)
return(is_even_internal(y - 2), type = type_scalar_logical())
}
return(is_even_internal(x))
})
is_even(5)
is_even(4)
```