J {ast2ast} | R Documentation |

An R function is translated to C++ source code and afterwards the code is compiled.

The result can be an external pointer (*XPtr*) or an *R* function.

The default value is an R function.

Further information can be found in the vignette: *Detailed Documentation*.

```
J(
f,
y,
x,
output = "R",
types_of_args = "SEXP",
return_type = "SEXP",
reference = FALSE,
verbose = FALSE,
getsource = FALSE
)
```

`f` |
The function which should be translated from R to C++. |

`y` |
The variables to compute the derivatives of (the dependent variable). For example: df/dx |

`x` |
The variables to which respect the variables are calcualted (the independent variable). For example: df/dx |

`output` |
If set to "R"" an R function wrapping the C++ code is returned. |

`types_of_args` |
define the types of the arguments passed to the function as an character vector.
This is an optional input if using "XPtr" as output. |

`return_type` |
is a character defining the type which the function returns. The default value is "SEXP"" as this is the only possibility for output "R". |

`reference` |
If set to TRUE the arguments are passed by reference (not possible if output is "R"). |

`verbose` |
If set to TRUE the output of the compilation process is printed. |

`getsource` |
If set to TRUE the function is not compiled and instead the C++ source code itself is returned. |

**The types numeric vector and numeric matrix are supported.
Notably, it is possible that the variables change the type within the function. **

For example in the following code the variable

```
f <- function(a, b, c) {
a[c(1, 2, 3)] <- 1
b <- vector(10)
c <- vector(1)
}
fcpp <- ast2ast::translate(f)
a <- c(1, 2, 3)
b <- c(1, 2, 3)
c <- c(1, 2, 3)
fcpp(a, b,c)
print(a)
print(b)
print(c)
```

```
f <- function() {
a_db = 3.14
b = 3.14
}
fcpp <- ast2ast::translate(f, verbose = TRUE)
fcpp()
```

In R every object is under the hood a

In contrast if an external pointer is created other types can be specified which are passed to the function or returned from it. The default value is a variable of type

assignment: = and <-

allocation: vector and matrix

information about objects: length and dim

Basic operations: +, -, *, /

Indices:

*'[]'*.**The function 'at' cannot be used! Beyond that only integer values are allowed within the brackets.**mathematical functions: sin, asin, sinh, cos, acos, cosh, tan, atan, tanh, sqrt, log, ^ and exp

concatenate objects: c

control flow: for, if, else if, else

comparison: ==, !=, >, <, >= and <=

printing: print

returning objects: return

catmull-rome spline: cmr

to get a range of numbers the ':' function can be used

is.na and is.infinite can be used to test for NA and Inf.

For more details see: `dfdr::jacobian()`

If output is set to *R* an R function is returned. Thus, the C++ code can directly be called within R.

In contrast a function which returns an external pointer is generated if the output is set to *XPtr*.

```
# Further examples can be found in the vignettes.
## Not run:
# simple example
f <- function(y) {
ydot <- vector(length = 2)
a <- 1.1
b <- 0.4
c <- 0.1
d <- 0.4
ydot[1] <- y[1]*a - y[1]*y[2]*b
ydot[2] <- y[2]*y[1]*c - y[2]*d
return(ydot)
}
jac <- ast2ast::J(f, ydot, y, verbose = TRUE)
jac(c(10, 11))
## End(Not run)
```

[Package *ast2ast* version 0.3.1 Index]