Skip to content

Instantly share code, notes, and snippets.

@pwl
Created October 13, 2009 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwl/209373 to your computer and use it in GitHub Desktop.
Save pwl/209373 to your computer and use it in GitHub Desktop.
Clear[Grad, Jac, InvJacGrad, ChangeVariable];
Grad[f_, args_] := Module[{IdM},
IdM = IdentityMatrix[Length[args]];
Evaluate[Derivative[Sequence @@ #][f]] @@ args & /@ IdM
]
Jac[yOf_, args_] := Module[{IdM},
IdM = IdentityMatrix[Length[args]];
Transpose[
Outer[Derivative[Sequence @@ #][#2] @@ args &, IdM, yOf, 1]]
]
InvJacGrad[f_, xOf_, args_, l_] := Module[{ytemp},
If[Length[l] == 0, f,
ytemp = Unique[args];
Function[
Evaluate[ytemp],
Evaluate[(
Transpose[Inverse[Jac[xOf, ytemp]]].Grad[
InvJacGrad[f, xOf, ytemp, Rest[l]],
ytemp](*Inverse Jacobian used!*)
)[[First[l]]]
]]
]
]
ChangeVariable[f_, args_, xOf_, newArgs_, g_] := Module[{},
{Derivative[l__][f] @@ args :> Module[{k = List[l]},
InvJacGrad[g, xOf, newArgs,
Flatten[ConstantArray[#, k[[#]]] &
/@ Range[Length[k]]]] @@ newArgs
], f @@ args -> g @@ newArgs}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment