knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(RAutoDiff)
This document provides an overview of the different functions currently
implemented (overloaded) for used with the Automatic Differentiation provided in
package RAutoDiff
. Most of the functions are written in order to "work"
in the same way as the corresponding functions taking regular R-variables
as inputs. Therefore this document provides only limited description of these, and the documentation elsewhere is also rather sparse.
The package provides three S4 classes and one class union:
fAD
and fAD2
represent numeric vectors (slot val
), the Jacobian matrix (slot jac
) of that vector with respect to the independent variables, and in the case of fAD2
, an array of Hessian matrices (slot hessian
, with respect to independent variables).ADtype
is simply the union of fAD
and fAD2
.AD_matrix
is a matrix class were the matrix element values (slot vals
) are stored as an ADtype
(in row-major order). In what remains, fAD
and fAD2
should have a similar behavior (in the
sense that the methods available for manipulating these should have the
same names) as the built in numeric vector class numeric
, and AD_matrix
should have a similar behavior as built in numeric matrix
class
Examples: (in general please avoid accessing and manipulating the slots of the classes directly)
x.n <- c(1,2) x <- independent2(x.n) print(x@val) # value of variable print(x@jac) # jacobian wrt to x.n print(x@hessian) # hessians of x[i] with respect to x.n m <- diag(x) # diagonal 2x2 AD_matrix print(m@nrow) print(m@ncol) print(m@vals)
In general, calculation with fAD
-types is somewhat faster (close to fully vectorized) than for fAD2
(several functions have for-loops), and hence fAD2
should be avoided if you do not need second derivatives.
independent()
creates an fAD
with values equal to the arguement and Jacobian equal to the identityindependent()
creates an fAD2
with values equal to the arguement and Jacobian equal to the identity, and all Hessians equal to zero.value()
Numeric value of the ADtype
or AD_matrix
(also overloaded as identity operations for numeric
and matrix
)gradient()
Extract gradient of a scalar argument (length 1 ADtype
or 1 by 1 AD_matrix
)jacobian()
Extract Jacobian of a vector (i.e. ADtype
)hessian()
Extract Hessian from a scalar fAD2
ADtype
s[
,[<-
e.g x[1]<-x[2]
or x[1:2] <- x[2]
length()
rep.int()
and rep_len()
e.g. rep.int(x,4)
show()
All of the below apply also for mixed (numeric
and ADtype
) class argument, e.g. 3 + x
or x * c(1,2.5)
. The methods recycle element in case of different sizes of the arguments.
+
, -
, *
, /
, ^
==
, !=
>
, >=
, <
, <=
+
, -
e.g. f <- -x
exp()
, log()
sqrt()
square()
(square(x)
same as x^2
but faster, also available for numeric
)lgamma()
sin()
, cos()
abs()
sum()
prod()
min()
, max()
, pmax()
, pmin()
AD_matrix
[
,[<-
e.g x[1,1]<-x[2,2]
or x[1,] <- x[2]
length()
, nrow()
, ncol()
matrix()
where argument data
is an ADtype
diag()
extract diagonal or create diagonal matrixshow()
dim()
Including between mixed types:
+
, -
, *
, /
, ^
+
, -
exp()
, log()
sqrt()
square()
lgamma()
sin()
, cos()
abs()
%*%
for various combinations of argumentsbacksolve()
and forwardsolve()
t()
isSymmetric()
solve()
cholL()
, chol()
and solve.chol()
for SPD matricessum()
, colSums()
, rowSums()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.