Re: Derivative of a function with multiple variables
- To: mathgroup at smc.vnet.net
- Subject: [mg69510] Re: Derivative of a function with multiple variables
- From: "J Siehler" <jsiehler at gmail.com>
- Date: Thu, 14 Sep 2006 06:56:29 -0400 (EDT)
- References: <ee8grk$itd$1@smc.vnet.net>
Adel Elsabbagh wrote:
> Assume I have f = f[x,y,z]
> I would like to construct a simple function g[f[x,y,z]] that will
> generate the Hessian. i.e.
Here is a solution that will work with any number of variables. I
struggled with this problem when I was first learning Mathematica; I
don't think it's easy to do it correctly and cleanly, though I can hope
some clever reply to this thread will teach me better.
Hess[f_][x__] := Table[
(Derivative @@ MapAt[(# + 1) &, 0 & /@ Range[Length[{x}]], {{i},
{j}}])[f][x],
{i, Length[{x}]}, {j, Length[{x}]}]
For example, try:
g[x_, y_] := 3x^2*y - y^3;
Hess[g][x, y]
Hess[g][1, 2]
and
g[x_, y_, z_] := z*x^2/(1 + y^2)
Hess[g][x, y, z]
Hess[g][1, 2, 3]
You might find this useful, too:
Grad[f_][x__] := Table[(Derivative @@ RotateRight[PadLeft[{1},
Length[{x}]], i])[f][x],{i, Length[{x}]}]