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:
multiDiff(Array([f(x,y,z)]),[x,y,z],1)[1];
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];
![<br />
Array([diff(f(x, y, z), x), diff(f(x, y, z), y), diff(f(x, y, z), z)])<br />](http://mapleoracles.maplesoft.com:8080/maplenet/primes/2ce5ca51a41fa8f941cb25d70e1db576.gif)
multiDiff(Array([f(x,y),g(x,y)]),[x,y],1);
multiDiff(Array([f(x,y)]),[x,y],2)[1];
multiDiff(Array([f(x,y),g(x,y)]),[x,y],2)[1]; multiDiff(Array([f(x,y),g(x,y)]),[x,y],2)[2];
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];
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];
,
,
,
Comments
Thank
Thank Professor Robert Israel and Professor John Fredsted.
It has helped,
Best Regards,
Rajiv