# J: Construct a function to calculate the Jacobian of a function. In RcppEigenAD: Compiles 'C++' Code using 'Rcpp', 'Eigen' and 'CppAD' to Produce First and Second Order Partial Derivatives

## Description

Constructs a function to calculate the Jacobian of a function produced either using sourceCppAD or the composition operator %.%. The returned function has the same argument signature as f but returns a matrix representing the Jacobian of f evaluated at the functions arguments. The partial derivatives are formed with respect to the arguments specified when f was created with sourceCppAD.

In what follows it is assumed that f has a single matrix argument (the one with which f is differentiated with respect to). When this is not the case, the other arguments will be considered constant at the point the Jacobian is evaluated at. Consequently, the structure of the output of the function produced by J is unchanged by the additional arguments. The Jacobian matrix {\bf J} is organised as follows.

If f:{\bf R}^{n} \rightarrow {\bf R}^{m} where {\bf Y}_{n_{Y} \times m_{Y}} = f({\bf X}_{n_{X} \times m_{X}}) and n=n_{X}m_{X}, m=n_{Y}m_{Y} then by numbering the elements of the matrices row-wise so that,

{\bf Y} = ≤ft[ \begin{array}{ccc} y_{1} & … & y_{m_{Y}} \\ y_{m_{Y}+1} & … & y_{2m_{Y}} \\ \vdots & \ddots & \vdots \\ y_{(n_{Y}-1)m_{Y}+1} & … & y_{n_{Y}m_{Y}} \end{array} \right]

and

{\bf X} = ≤ft[ \begin{array}{ccc} x_{1} & … & x_{m_{X}} \\ x_{m_{X}+1} & … & x_{2m_{X}} \\ \vdots & \ddots & \vdots \\ x_{(n_{X}-1)m_{X}+1} & … & x_{n_{X}m_{X}} \end{array} \right]

then the m \times n Jacobian matrix is given by

\begingroup \renewcommand*{\arraystretch}{1.5} {\bf J} = ≤ft[ \begin{array}{ccc} \frac{\partial y_{1}}{\partial x_1} & … & \frac{\partial y_{1}}{\partial x_n} \\ \vdots & \ddots & \vdots \\ \frac{\partial y_{m}}{\partial x_1} & … & \frac{\partial y_{m}}{\partial x_n} \end{array} \right] \endgroup

## Usage

 1 J(f) 

## Arguments

 f A function created using either sourceCppAD or the composition operator %.%.

## Value

A function which computes the Jacobian of the function

## Examples

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # define f as the eigen vectors of its argument X # calculated using the Eigen library library(RcppEigenAD) f<-sourceCppAD(' ADmat f(const ADmat& X) { Eigen::EigenSolver es(X); return es.pseudoEigenvectors(); } ') Jf<-J(f) X<-matrix(c(1,2,3,4),2,2) Jmat<-Jf(X) Jmat # the Jacobian matrix of first derivatives Jmat[2,3] # the derivative of f(X)[1,2] with respect to X[2,1] 

RcppEigenAD documentation built on May 2, 2019, 5:34 a.m.