Gradients, Jacobians, Hessians, and higher order derivatives

John Fredsted's picture

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];
    

    <br />
Array([diff(f(x, y, z), x), diff(f(x, y, z), y), diff(f(x, y, z), z)])<br />

  • 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];
    

    <br />
Array([diff(f(x, y, z), x), diff(f(x, y, z), y), diff(f(x, y, z), z)])<br />
<br />
Array([diff(g(x, y, z), x), diff(g(x, y, z), y), diff(g(x, y, z), z)])<br />

  • Jacobian for two functions of two variables
    multiDiff(Array([f(x,y),g(x,y)]),[x,y],1);
    

    <br />
Array([[diff(f(x, y), x), diff(f(x, y), y)], [diff(g(x, y), x), diff(g(x, y), y)]])<br />

  • Hessian for one function of two variables
    multiDiff(Array([f(x,y)]),[x,y],2)[1];
    

    <br />
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))]])<br />

  • 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];
    

    <br />
diff(f(x, y), `$`(x, 3))<br />
, <br />
diff(f(x, y), `$`(x, 2), y)<br />
, <br />
diff(f(x, y), x, `$`(y, 2))<br />
, <br />
diff(f(x, y), `$`(y, 3))<br />

Comments

Thank

Thank Professor Robert Israel and Professor John Fredsted.
It has helped,
Best Regards,
Rajiv

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}