JacobianMat {lazy.mat} R Documentation

## Calculation of Jacobian Matrix

### Description

When f is a scalar valued function,
` Jacobian[j] = d f(x) / d x[j] `.
Otherwise,
` Jacobian[i,j] = d f(x)[i] / d x[j] `

### Usage

```JacobianMat(..x.., ..func.., ..eps.. = 1e-06, ...)
```

### Arguments

 `..x..` A vector of parameters `..func..` function which has x as the first argument `..eps..` small value `...` additional parameters to "..func.."

### Details

This program calculates the Jacobian matrix of "..func.." with respect to "param" where "..func.." is the scalar or vector valued function and "param" is a scalar or vector valued parameters.

The additional paramters to "..func.." can be passed through ... argument.
The values of those variables whose values are not defined in "..func.." will be obtained from the environment in which "..func.." is defined.

Original version was Gradmat in Sec4NotF09.pdf.
Statistical Computing with R by Eric Slud, Math. Dept., UMCP October 21, 2009

### Value

When ..func.. is a scalar valued function,
` Jacobian[j] = d func(x) / d x[j] `.
Otherwise
` Jacobian[i,j] = d func(x)[i] / d x[j] ` .

### Examples

```
# scalar valued target function w/o additional arguments
# value of a and d are from parent env.
f <- function(x){ a*log(x)^2+d*x+1 }
dfdx <- function(x, a){
cat("\nThe value to be used:  a = ", a, ", d = ", d, "\n")
2*a*log(x)/x+d
}
a <- 1; d <- 2; x <- 1
Jac <-JacobianMat( x, f )
Print( Jac, dfdx( x, a ) )

# scalar valued target function with one additional argument
# a is an argument: d is from parent emv
ff <- function(x, a){ a*log(x)^2+d*x+1 }
dffdx <- function(x, a){
cat("\nThe value to be used:  a = ", a, ", d = ", d, "\n")
2*a*log(x)/x+d
}
a <- NA; d <- 2; x <- 1
Jac <-JacobianMat( x, ff, a=1 )
Print( Jac, dffdx( x, a=1 ) )

temp <- function( x, a, d ){
# scalar valued target function
ff <- function(x, a){ a*log(x)^2+d*x+1 }
dfdx <- function(x, a){
cat("\nThe value to be used:  a = ", a, ", d = ", d, "\n")
2*a*log(x)/x+d
}
# This d will be used when evaluating ff
Print("in temp", d)
Jac <-JacobianMat( x, ff, a=a )
Print( Jac, dfdx( x, a ) )
}
Print("in global", d)
temp( x=1, a=1, d=2 )
temp( x=1, a=1, d=5 )

# vector valued function w/o additional parameters
# a and b are from parent env.
f2 <- function( x ){
f1 <- a[1]*x[1] + a[2]*x[2]^2
f2 <- b[1]*x[1]^2 + b[2]*x[2]
f3 <- a[1]*x[1]^2 + b[1]*x[2]^2
return( c(f1,f2,f3) )
}
# analytic Jacobian
df2dx <- function( x ){
df1dx1 <- a[1] ; df1dx2 <- 2*a[2]*x[2]
df2dx1 <- 2*b[1]*x[1]; df2dx2 <- b[2]
df3dx1 <- 2*a[1]*x[1]; df3dx2 <- 2*b[1]*x[2]
Jac <- matrix(c(df1dx1, df2dx1, df3dx1, df1dx2, df2dx2, df3dx2), 3)
return( Jac )
}
a=1:2; b=2:3; x=1:2
Jac=JacobianMat( x, f2 )
Print(df2dx(x), Jac)

# vector valued function with an additional parameter
ff2 <- function( x, a ){
f1 <- a[1]*x[1] + a[2]*x[2]^2
f2 <- b[1]*x[1]^2 + b[2]*x[2]
f3 <- a[1]*x[1]^2 + b[1]*x[2]^2
return( c(f1,f2,f3) )
}
# analytic Jacobian
dff2dx <- function( x, a ){
df1dx1 <- a[1] ; df1dx2 <- 2*a[2]*x[2]
df2dx1 <- 2*b[1]*x[1]; df2dx2 <- b[2]
df3dx1 <- 2*a[1]*x[1]; df3dx2 <- 2*b[1]*x[2]
Jac <- matrix(c(df1dx1, df2dx1, df3dx1, df1dx2, df2dx2, df3dx2), 3)
return( Jac )
}
a=NA; b=2:3; x=1:2
Jac=JacobianMat( x, ff2, a=1:2 )
Print(dff2dx(x,a=1:2), Jac)

```

[Package lazy.mat version 0.1.3 Index]