Compute the Hessian of the function.
H = diffcode_hessian(f,x)
a function or a list, the function to differentiate.
a n-by-1 matrix of doubles, real, the point where to compute the derivatives
a n-ny-n-by-m matrix of doubles, the exact Hessian. The matrix H(:,:,k)
is the Hessian of fk
, for k=1,2,...,m
.
Computes the exact Hessian matrix of the function. The algorithm uses an exact differentiation method.
If f
is a compiled function, then diffcode_hessian
cannot differentiate f
.
The function must have header
y = f( x )
where
x is a n-by-1 matrix of doubles,
y is a m-by-1 matrix of doubles.
It might happen that the function requires additionnal
arguments to be evaluated.
In this case, we can use the following feature.
The function f
can also be the list (func,a1,a2,...)
where, func
, the first element in the list,
must be a function with header
y = func ( x , a1 , a2 , ... ).
In this case, the input arguments a1, a2, ...
will be automatically be appended at the
end of the calling sequence of func
.
The diffcode_hessian
function calls the function
f
several times in order to compute its
derivatives.
The total function evaluations is n*(n+1)/2
times, where n
is the size of x
.
// The function to differentiate function y=f(x) f1 = sin(x(1)*x(2))+exp(x(2)*x(3)+x(1)) f2 = sum(x.^3) y=[f1;f2] endfunction // The exact Hessian function H=exactH(x) H1(1,1) = -sin(x(1)*x(2))*x(2)^2+exp(x(2)*x(3)+x(1)) H1(1,2) = cos(x(1)*x(2)) - sin(x(1)*x(2))*x(2)*x(1)+exp(x(2)*x(3)+x(1))*x(3) H1(1,3) = exp(x(2)*x(3)+x(1))*x(2) H1(2,1) = H1(1,2) H1(2,2) = -sin(x(1)*x(2))*x(1)^2+exp(x(2)*x(3)+x(1))*x(3)^2 H1(2,3) = exp(x(2)*x(3)+x(1))+exp(x(2)*x(3)+x(1))*x(3)*x(2) H1(3,1) = H1(1,3) H1(3,2) = H1(2,3) H1(3,3) = exp(x(2)*x(3)+x(1))*x(2)^2 // H2(1,1) = 6*x(1) H2(1,2) = 0 H2(1,3) = 0 H2(2,1) = H2(1,2) H2(2,2) = 6*x(2) H2(2,3) = 0 H2(3,1) = H2(1,3) H2(3,2) = H2(2,3) H2(3,3) = 6*x(3) // H(:,:,1) = H1 H(:,:,2) = H2 endfunction // Compute the exact Hessian x=[1;2;3]; H=diffcode_hessian(f,x) Hexact = exactH(x) and(H==Hexact) // Passing extra parameters function y=G(x, p) f1 = sin(x(1)*x(2)*p)+exp(x(2)*x(3)+x(1)) f2 = sum(x.^3) y=[f1; f2] endfunction p=1; h=1e-3; H=diffcode_hessian(list(G,p),x) | ![]() | ![]() |