:

Gradients, Jacobians, Hessians, and higher order derivatives

Inspired by the post "finding the Hessian (third order tensor) and higher order derivatives", I have written the following function which treats gradients, Jacobians, Hessians, and higher order derivatives in a unified way, implementing tensors as multidimensional Arrays:
```multiDiff := proc(expr::Array(algebraic),vars::list(symbol),n::posint)
local i,result;
if n > 1 then result := multiDiff(multiDiff(expr,vars,n - 1),vars,1)
else
result := Array(ArrayDims(expr),1..nops(vars),order = C_order);
for i from 1 to nops(vars) do
ArrayTools:-Copy(
map(diff,Array(expr,order = C_order),vars[i]),
result,i-1,nops(vars)
)
end do
end if;
result
end proc:
```
Below follows several examples:
• Gradient of one function of three variables
```multiDiff(Array([f(x,y,z)]),[x,y,z],1)[1];
```
Array([diff(f(x, y, z), x), diff(f(x, y, z), y), diff(f(x, y, z), z)])
• Gradients of two functions of three variables
```multiDiff(Array([f(x,y,z),g(x,y,z)]),[x,y,z],1)[1];
multiDiff(Array([f(x,y,z),g(x,y,z)]),[x,y,z],1)[2];
```
Array([diff(f(x, y, z), x), diff(f(x, y, z), y), diff(f(x, y, z), z)]) Array([diff(g(x, y, z), x), diff(g(x, y, z), y), diff(g(x, y, z), z)])
• Jacobian for two functions of two variables
```multiDiff(Array([f(x,y),g(x,y)]),[x,y],1);
```
Array([[diff(f(x, y), x), diff(f(x, y), y)], [diff(g(x, y), x), diff(g(x, y), y)]])
• Hessian for one function of two variables
```multiDiff(Array([f(x,y)]),[x,y],2)[1];
```
Array([[diff(f(x, y), `\$`(x, 2)), diff(f(x, y), x, y)], [diff(f(x, y), x, y), diff(f(x, y), `\$`(y, 2))]])
• Hessians for two functions (in a 1D-Array) of two variables
```multiDiff(Array([f(x,y),g(x,y)]),[x,y],2)[1];
multiDiff(Array([f(x,y),g(x,y)]),[x,y],2)[2];
```
• Hessians for four functions (in a 2D-Array) of two variables
```multiDiff(Array([[f(x,y),g(x,y)],[k(x,y),l(x,y)]]),[x,y],2)[1,1],
multiDiff(Array([[f(x,y),g(x,y)],[k(x,y),l(x,y)]]),[x,y],2)[1,2];
multiDiff(Array([[f(x,y),g(x,y)],[k(x,y),l(x,y)]]),[x,y],2)[2,1],
multiDiff(Array([[f(x,y),g(x,y)],[k(x,y),l(x,y)]]),[x,y],2)[2,2];
```
• Third order derivatives of one function of two variables
```multiDiff(Array([f(x,y)]),[x,y],3)[1][1,1,1],
multiDiff(Array([f(x,y)]),[x,y],3)[1][1,1,2],
multiDiff(Array([f(x,y)]),[x,y],3)[1][1,2,2],
multiDiff(Array([f(x,y)]),[x,y],3)[1][2,2,2];
```
diff(f(x, y), `\$`(x, 3)) , diff(f(x, y), `\$`(x, 2), y) , diff(f(x, y), x, `\$`(y, 2)) , diff(f(x, y), `\$`(y, 3))

﻿