R code differentiated with automatic differentiation. Here you can differentiate R functions interactively. The differentiated codes are shown in the Output textarea above.
With the ADR package you can differentiate R functions automatically:
install.packages('https://r-adr.de/download/adr_2.0.264.tar.gz', repos=NULL)
.Then run:
adrDiffFor(function(x, c) x^c, list(3, 2))
, for the forward mode of AD, or
adrDiffRev(function(x, c) x^c, list(3, 2))
, for the reverse mode of AD
adrHessRev(function(x, c) x^c, list(3, 2))
, forward-over-reverse mode for Hessians
Either command will send the function definition to this server form and then run the resulting differentiated code.
This is the list of R language features and functions that are supported by ADR. The elements listed here may be used in the user code, depending on the AD method, as shown by the letters F, R, H, and D:
Letter | Remark |
---|---|
F | forward mode with adrDiffFor |
R | reverse mode with adrDiffRev |
H | forward-over-reverse mode with adrHessRev |
D | finite differences with adrDiffFD or adrHessFD |
Modes | Remark | ||||
---|---|---|---|---|---|
if | F | R | H | D | incl. if expressions |
for | F | R | H | D | |
while | F | R | H | D | |
break | F | R | H | D | |
next | F | R | H | D | |
function calls | F | R | H | D | incl. recursive |
named arguments | F | D | |||
default parameters | F | D | |||
function handles | F | R | H | D | e.g. after myfun <- sin will differentiate myfun correctly |
Modes | Remark | ||||
---|---|---|---|---|---|
numeric | F | R | H | D | |
complex | F | R | H | D | forward and reverse mode may return different imaginary parts, namely the LHS and RHS of the second Cauchy-Riemann equation |
array | F | R | H | D | |
Matrix | F | R | H | D | |
list | F | R | H | D | not as independent var. or function result |
The list of common R functions that can be differentiated with ADR. In most cases you can add support for any missing function f by providing a function df_f in your scope. For the reverse mode, you have to provide rec_f, a_f, and ret_f.
In the remaining cases ADR will deparse the function f and send the code to the ADR server. Hopefully the differentiated code will then be able to provide the correct derivative.
Modes | Remark | ||||
---|---|---|---|---|---|
Conj | F | R | H | D | |
Im | F | R | H | D | |
Re | F | R | H | D | |
abs | F | R | H | D | |
acos | F | R | D | ||
acosh | F | R | D | ||
aperm | F | R | H | D | |
append | F | R | D | ||
array | F | R | D | ||
asin | F | R | D | ||
asinh | F | R | D | ||
atan | F | R | D | ||
atan2 | F | D | |||
atanh | F | R | D | ||
c | F | R | H | D | |
cbind | F | R | H | D | |
colMeans | F | R | H | D | R: dims=1 |
colSums | F | R | H | D | R: dims=1 |
colnames | F | R | H | D | |
colnames<- | F | R | H | D | |
convolve | F | R | H | D | R,H: type='open' only |
cos | F | R | H | D | |
cosh | F | R | D | ||
diag | F | R | D | ||
diff | F | R | D | ||
dim | F | R | H | D | |
dim<- | F | R | H | D | |
dimnames | F | R | H | D | |
dimnames<- | F | R | H | D | |
dnorm | F | R | H | D | |
do.call | F | R | D | ||
exp | F | R | D | ||
fft | F | R | H | D | |
is.array | F | R | H | D | |
is.complex | F | R | D | cannot be overloaded, for H use !is.numeric | |
is.double | F | R | H | D | |
is.function | F | R | H | D | |
is.list | F | R | H | D | |
is.matrix | F | R | H | D | |
is.null | F | R | H | D | |
is.numeric | F | R | H | D | |
kronecker | F | R | D | ||
lapply | F | R | D | ||
length | F | R | H | D | |
list | F | R | H | D | |
log | F | R | D | ||
mvfft | F | R | H | D | |
names | F | R | H | D | |
names<- | F | R | H | D | |
norm | F | R | H | D | norm(,"2") only |
pmax | F | D | |||
pmin | F | D | |||
pnorm | F | R | H | D | |
qr | F | R | D | ||
qr.Q | F | R | D | ||
qr.R | F | R | D | ||
qr.qty | F | D | |||
qr.qy | F | D | |||
range | F | R | D | ||
rbind | F | R | H | D | |
rep | F | R | H | D | |
rep.int | F | R | D | ||
rev | F | R | D | ||
rownames | F | R | H | D | |
rownames<- | F | R | H | D | |
sin | F | R | H | D | |
sinh | F | R | D | ||
solve | F | R | D | ||
sqrt | F | R | H | D | |
sum | F | R | H | D | |
svd | F | R | D | Derivative of the singular values only, as of yet | |
t | F | R | H | D | |
tan | F | R | H | D | |
tanh | F | R | D |