# AutoDiff of function inverse?

**URL:** <https://discourse.julialang.org/t/autodiff-of-function-inverse/75868>\
**Category:** Modelling & Simulations\
**Tags:** autodiff, inverse\
**Created:** [February 5, 2022, 5:35pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868 "2022-02-05T17:35:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [February 5, 2022, 5:35pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868/1 "2022-02-05T17:35:32Z")

</div>

I am using a time-dependent invertible map to define a moving geometry: `ξ=f(x,t)` where `ξ,x` have the same dimension. For example, this might be a time-varying shift and rotation `ξ=R(θ(t))*(x-x₀(t))`, or it might be something a little more strange, like a traveling wave deformation.

The velocity of the geometry is then `ẋ=ḟ⁻¹(ξ,t)=ḟ⁻¹(f(x,t),t)`. Is it possible automatically compute this function for a given invertible `f`? I could ask the user to specify `f⁻¹` and then use autodiff, but I feel like the whole thing should be easy enough to do using some form of reverse-diff… But I’m not sure.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [February 5, 2022, 6:05pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868/2 "2022-02-05T18:05:50Z")

</div>

Just as comment, there was a similar question in the past

> [@Automatic inverse of a function?](https://discourse.julialang.org/t/automatic-inverse-of-a-function/53342/3):
>
> I hear you, yes. subpieces of the algorithm might not be invertibles. But what if the global algorithm is, indeed, invertible ? I mean, f(a,b) = (a+b, exp(a+2b)) is invertible, and you could get more creative.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 5, 2022, 6:28pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868/3 "2022-02-05T18:28:18Z")

</div>

If you have the Jacobian of f, then you can just invert this matrix to get the Jacobian of the inverse function (if it exists locally).

(This was in the second problem set of our [matrix calculus course](https://github.com/mitmath/matrixcalc).)

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 5, 2022, 6:34pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868/4 "2022-02-05T18:34:07Z")

</div>

Treating `ξ` and `t` as independent variables and `x` as a dependent variable and using the implicit function theorem, you get:

```julia
f(x,t) - ξ = 0
∂f/∂t + ∂f/∂x * dx/dt = 0
dx/dt = -∂f/∂x \ ∂f/∂t

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 5, 2022, 6:39pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868/5 "2022-02-05T18:39:33Z")

</div>

If you want to automate this using AD and make it all Zygote compatible, check my fresh differentiable implicit function implementation here [https://github.com/JuliaNonconvex/NonconvexUtils.jl#hack-5-implicitfunction](https://github.com/JuliaNonconvex/NonconvexUtils.jl#hack-5-implicitfunction). (but that might be overkill for your case)

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [February 5, 2022, 6:55pm UTC](https://discourse.julialang.org/t/autodiff-of-function-inverse/75868/6 "2022-02-05T18:55:24Z")

</div>

I was sooooo close. But I’m glad I asked since now I’m aware of your nice package for implicit functions.
