Learn R Programming

ComplexAnalysis (version 1.0)

matrix.derv: Matrix derivative

Description

Evaluate the derivative of the following types ($d$ denotes the "curly d" in partial derivatives):
  • Type 0: scalar-by-scalar $dy/dx$
  • Type 1: (gradient) scalar-by-vector $dy/d$$x$
  • Type 2: vector-by-scalar $d$$y$$/dx$
  • Type 3: (Jacobian) vector-by-vector $d$$y$$/d$$x$
  • Type 4: scalar-by-matrix $dy/d$$X$
  • Type 5: matrix-by-scalar $d$$Y$$/dx$

Note that this is only for real variables although one may modify the source code allowing for some complex-valued ones. Yet correct result is not guaranteed.

Usage

matrix.derv(Y, X0, order = 1, rel.tol=.Machine$double.eps^0.5)

Arguments

Y
One single function $y$ (for type 0, 1 & 4), a vector of functions $y$ (for type 2 & 3) or a matrix of functions $Y$ (for type 5) whose derivative to be evaluated. For a vector or a matrix of functions, the variables and their order must be the same across all functions. Also see Examples.
X0
Evaluation points of the derivative. It can be a scalar $x$ (type 0, 2 & 5), a vector $x$ (type 1 & 3) or a matrix $X$ (type 4). Note that (1) the number of elements in X0 must match the number of variables in each of the functions in Y and, (2) the order of the elements in X0 follows that of the variables in each of the functions in Y. Also see Examples.
order
The order of the derivative. Must be a non-zero natural number.
rel.tol
Relative tolerance of the R built-in function integrate in calculating the derivative.

Value

result
A matrix even if type 0 (scalar-by-scalar).
type
The type of derivative: 0,1,...,5

Details

Error or misleading result may occur if arugments are not properly supplied. Some examples:
  • Variables in the functions are not the same: If Y=c(function(x,y,z){x^2+y^2*z}, function(x,z,y){sqrt(x*y*z)},function(x,y,z){x^y*log(z)}), the second function as the order of the variables as x,z,y.
  • The number of evaluation points and the number of variables not match: If Y=matrix(function(x){x^3},function(x){x^2},function(x){x},function(x){1},ncol=2) and X0=c(1,2), this is a matrix-by-scalar derivative (type 5) but X0 here is a vector.
  • The order of the variables and the order of the evaluation points: If Y=function(a,b,c,d){a*b+c/d} and X0=matrix(1:4,ncol=2), then the derivative is evaluated at a=1, b=2, c=3, d=4 respectively.

See Also

derv

Examples

Run this code
#Type-0 : scalar-by-scalar
X<-3
Y<-function(x){x^3}
matrix.derv(Y,X)
#Type-1 (gradient): scalar-by-vector
X<-c(1,2,0.4)
Y<-function(a,b,c){a^2+b*c}
matrix.derv(Y,X)
#Type-2 : vector-by-scalar
X<-10
Y<-c(function(a){sin(a)},function(a){cos(a)*a^2})
matrix.derv(Y,X)
#Type-3 (Jacobian) : vector-by-vector 
X<-c(1,3,5)
Y<-c(function(a,b,c){a^2+c/b},function(a,b,c){a+b/c})
matrix.derv(Y,X)
#Type-4 : scalar-by-matrix
X<-matrix(1:9,ncol=3)
Y<-function(a,b,c,x,y,z,s,t,r){a*b+c-x*y*z*(s+t+r)}
matrix.derv(Y,X)
#Type-5 : matrix-by-scalar
X<-2
Y<-matrix(c(function(a){a^3},function(a){a^2},function(a){a},function(a){1}),ncol=2)
matrix.derv(Y,X)

Run the code above in your browser using DataLab