# Implementation of an implace Hessian of a vector-valued function?

**URL:** https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720
**Category:** General Usage
**Tags:** forward
**Created:** [January 24, 2020, 1:16am UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720 "2020-01-24T01:16:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [January 24, 2020, 1:16am UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720/1 "2020-01-24T01:16:51Z")

</div>

HI, i’m looking to implement an implace hessian of a vector-valued function, for example:

```julia
f(x) = [x[1],x[2],sin(x[1]),sum(x)]

```

[The ForwardDiff Documentation](http://www.juliadiff.org/ForwardDiff.jl/stable/user/advanced/#Hessian-of-a-vector-valued-function-1) gives an out of place example (slightly wrong, as `size(f(x)) !=size(x)` in the general case:

```julia
julia> function vector_hessian(f, x)
       n = length(x)
       out = ForwardDiff.jacobian(x -> ForwardDiff.jacobian(f, x), x)
       return reshape(out, n, n, n)
   end

```

the problem arrives when you use a inplace version of the same function:

```julia
function f!(res,x) 
res .= [x[1],x[2],sin(x[1]),sum(x)]
end

```

the problem with nested inplace jacobians is that the result of the intermediate jacobian is a dual, and its very difficult to create a cache storage.  
Anybody has encountered something like that? how can i create an efficient cache for this type of function?

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [January 24, 2020, 3:13am UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720/2 "2020-01-24T03:13:46Z")

</div>

See [Nested ForwardDiff.jacobian calls with inplace function - #3 by tkoolen](https://discourse.julialang.org/t/nested-forwarddiff-jacobian-calls-with-inplace-function/21232/3) for one solution.

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [January 25, 2020, 5:04am UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720/3 "2020-01-25T05:04:05Z")

</div>

Although I didn’t use the same method, the idea is here, to pass a implace Jacobian to another implace Jacobian, the cache needs to be build dinamically, and the inner jacobian should be passed as an out of place cached function.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 25, 2020, 5:49am UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720/4 "2020-01-25T05:49:43Z")

</div>

And for reference, FiniteDiff.jl has a fully cached version of Hessian computations through finite differencing:

> **[GitHub - JuliaDiff/FiniteDiff.jl: Fast non-allocating calculations of...](https://github.com/JuliaDiff/FiniteDiff.jl#hessians)**
>
> Fast non-allocating calculations of gradients, Jacobians, and Hessians with sparsity support - GitHub - JuliaDiff/FiniteDiff.jl: Fast non-allocating calculations of gradients, Jacobians, and Hessia...

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [January 25, 2020, 6:01am UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720/5 "2020-01-25T06:01:50Z")

</div>

Nice! I also need to implement this on FiniteDiff (as an user selected option)

---

<div class="post-metadata">

### Author: ![ferrolho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferrolho/32/213665_2.png) [@ferrolho](https://discourse.julialang.org/u/ferrolho)
#### Post date: [February 1, 2021, 4:45pm UTC](https://discourse.julialang.org/t/implementation-of-an-implace-hessian-of-a-vector-valued-function/33720/6 "2021-02-01T16:45:05Z")

</div>

But the Hessian computations in FiniteDiff.jl do no support vector-valued functions, right?
