smish.dev
implicit_function_derivatives

Say we have some system of equations f(x,y)=0, where f and y each have n components and x has m components. If we were given values for x, we could imagine solving f(x,y)=0 for y, which lets us think of y as an implicit function of x.

As a matter of notation, let's express this implicit function as y:=y(x).

How do we compute the gradient and Hessian of the implicit function y(x)?

To start, we can differentiate the system of equations to obtain

f(x,y)=0⟹df=0

Then, with a little bit of algebra we can manipulate this to get an expression for dydx

0=dfi0=∂fi∂xkdxk+∂fi∂yjdyj∂fi∂yjdyj=−∂fi∂xkdxk∂fi∂yjdyjdxk=−∂fi∂xk∂yl∂fi∂fi∂yjdyjdxk=−∂yl∂fi∂fi∂xk,∂yi∂fj:=[∂f∂y]ij−1δljdyjdxk=−∂yl∂fi∂fi∂xkdyldxk=−∂yl∂fi∂fi∂xkdyldxk=−∂yl∂fi∂fi∂xk

We can follow a similar process to get the Hessian, but it is more involved. Start by differentiating our gradient expression above

d2yldxkdxm=ddxm(dyldxk)=−ddxm(∂yl∂fi∂fi∂xk)=−ddxm(∂yl∂fi)∂fi∂xk−∂yl∂fiddxm(∂fi∂xk)=−(−∂yl∂fpddxm(∂fp∂yq)∂yq∂fi)∂fi∂xk−∂yl∂fi(∂2fi∂xk∂xm+∂2fi∂xk∂yjdyjdxm)=∂yl∂fp(∂2fp∂yq∂xm+∂2fp∂yq∂yjdyjdxm)∂yq∂fi∂fi∂xk−∂yl∂fi(∂2fi∂xk∂xm+∂2fi∂xk∂yjdyjdxm)=−∂yl∂fp(∂2fp∂yq∂xm+∂2fp∂yq∂yjdyjdxm)dyqdxk−∂yl∂fi(∂2fi∂xk∂xm+∂2fi∂xk∂yjdyjdxm)d2yldxkdxm=−∂yl∂fp(∂2fp∂xk∂xm+∂2fp∂xk∂yjdyjdxm+∂2fp∂yq∂xmdyqdxk+∂2fp∂yq∂yjdyqdxkdyjdxm)d2yldxkdxm=−∂yl∂fp(∂2fp∂xk∂xm+∂2fp∂xk∂yjdyjdxm+∂2fp∂yq∂xmdyqdxk+∂2fp∂yq∂yjdyqdxkdyjdxm)

Here is a mathematica notebook verifying the correctness of these expressions for scalar and vector-valued functions.