independent2: Set independent variable for first and second order...

Description Usage Arguments Value Examples

View source: R/rad.R

Description

Set independent variable for first and second order derivatives

Usage

1

Arguments

x

numeric vector to be differentiated with respect to

Value

a fAD2 vector with value being x, jacobian being the identity, and hessian being zero

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
x <- independent2(c(0,1))
y <- sum(square(x))/2
print(gradient(y)) # should be c(0,1)
print(hessian(y)) # should be identity
 
# define some function
f <- function(x){ 
M <- matrix(x,2,2)
return(sum(solve(M,c(-1,2))))
}

# run function with regular variables
print(f(c(1,-1,2,4)))

# run function with second order AD-variables
x <- independent2(c(1,-1,2,4))
y <- f(x)
print(value(y))
print(gradient(y))
print(hessian(y))

torekleppe/RAutoDiff documentation built on Dec. 23, 2021, noon