codegen[HESSIAN] - compute the HESSIAN matrix of a Maple procedure
|
Calling Sequence
|
|
HESSIAN(F)
HESSIAN(F, X)
HESSIAN(F, X, ...)
|
|
Parameters
|
|
F
|
-
|
Maple procedure
|
X
|
-
|
list of symbols (parameters of F)
|
|
|
|
|
Description
|
|
•
|
The first argument F is a Maple procedure which computes a function of x1,x2,...,xn. The HESSIAN command outputs a new procedure H, which when executed at given values for x1,x2,...,xn, returns a matrix of the second partial derivatives of H w.r.t. x1,...,xn at the given values. For example, given
|
F := proc(x, y) local t; t := exp(-x); y*t + t end proc
|
|
|
|
The output of H := HESSIAN(F); is the procedure
|
H := proc(x, y) local grd1, grd2, df, grd, df1, t, dfr0;
|
t := exp(-x);
|
df1 := y + 1;
|
grd1 := - t*df1;
|
grd2 := t;
|
df := array(1 .. 4);
|
dfr0 := array(1 .. 4);
|
df[3] := 1;
|
df[2] := - df[3]*t;
|
df[1] := - df[3]*df1;
|
dfr0[4] := 1;
|
dfr0[1] := dfr0[4];
|
grd := array(1 .. 2, 1 .. 2);
|
grd[1, 1] := - df[1]*exp(-x);
|
grd[1, 2] := df[2];
|
grd[2, 1] := - dfr0[1]*exp(-x);
|
grd[2, 2] := 0;
|
return grd
|
end proc
|
|
|
|
The H procedure can be optimized by optimize(H). When H is called with inputs , it outputs the matrix
|
•
|
The code in H is constructed by applying the GRADIENT command to F twice. The GRADIENT command uses automatic differentiation. This often leads to a more efficient computation than symbolic differentiation, that is, what you would obtain from using linalg[hessian]. See codegen[GRADIENT for further details on automatic differentiation. The remaining arguments to HESSIAN are optional, they are described below.
|
•
|
By default, HESSIAN computes the partial derivatives of F w.r.t. all the parameters present in F. The optional argument X, a list of symbols, may be used to specify which parameters to take the derivative w.r.t.
|
•
|
Two algorithms are supported, the so-called forward and reverse modes. By default, HESSIAN tries to use the reverse mode since it usually leads to a more efficient code. If it is unable to use the reverse mode, the forward mode is used. The user may specify which algorithm is to be used by giving the optional argument mode=forward or mode=reverse.
|
•
|
The matrix of partial derivatives is, by default, returned as an array. The optional argument result_type=list, result_type=array, or result_type=seq specifies that the matrix of derivatives returned by H is to be a Maple list, array, and sequence respectively.
|
•
|
The command with(codegen,HESSIAN) allows the use of the abbreviated form of this command.
|
|
|
Examples
|
|
>
|
|
>
|
F := proc(x,y) local t; t := x*y; x+t-y*t; end proc;
|
| (1) |
>
|
|
| (2) |
>
|
|
| (3) |
>
|
|
| (4) |
>
|
|
| (5) |
>
|
|
| (6) |
>
|
|
| (7) |
This example we compute the Hessian w.r.t. the first two parameters phi and omega only. Since the torus program returns a vector of values, the result is of dimension 3.
>
|
torus := proc(phi,omega,R,r) local x,y,z;
x := cos(phi)*(R+r*cos(omega));
y := sin(phi)*(R+r*cos(omega));
z := r*sin(omega);
[x,y,z]
end proc:
|
>
|
|
| (8) |
>
|
|
| (9) |
|
|