# Autodiff: calculate just the diagonal of the Hessian

**URL:** https://discourse.julialang.org/t/autodiff-calculate-just-the-diagonal-of-the-hessian/22893
**Category:** General Usage
**Tags:** question
**Created:** [April 7, 2019, 11:17pm UTC](https://discourse.julialang.org/t/autodiff-calculate-just-the-diagonal-of-the-hessian/22893 "2019-04-07T23:17:25Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [April 8, 2019, 4:30am UTC](https://discourse.julialang.org/t/autodiff-calculate-just-the-diagonal-of-the-hessian/22893/5 "2019-04-08T04:30:18Z")

</div>

Awesome, thank you–that’s good enough for my purposes now, I’m just working through [this paper](http://www.statslab.cam.ac.uk/~rjs57/RSS/0708/Rue08.pdf) and coding parts of it up to make sure I understand it. NB, you’ve got a misplaced `end` (should be right after the `return z`).

I’d been trying something similar to your answer, but setting each element of `xx` to `w` in-place, then setting it back to its original value before moving to the next element. Which avoids the allocations from `vcat`, but causes `ForwardDiff` to choke…I think basically the same issue as [here](https://discourse.julialang.org/t/nested-forwarddiff-jacobian-calls-with-inplace-function/21232/3). So it probably _can_ be optimized, but you know what they say about premature optimization!

---

_[View the full topic](https://discourse.julialang.org/t/autodiff-calculate-just-the-diagonal-of-the-hessian/22893)._
