# Calculating Third (and Higher) Derivatives: ReverseDiff gives 0s while ForwardDiff works

**URL:** https://discourse.julialang.org/t/calculating-third-and-higher-derivatives-reversediff-gives-0s-while-forwarddiff-works/101095
**Category:** General Usage
**Tags:** forwarddiff, reversediff
**Created:** [July 2, 2023, 10:04pm UTC](https://discourse.julialang.org/t/calculating-third-and-higher-derivatives-reversediff-gives-0s-while-forwarddiff-works/101095 "2023-07-02T22:04:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![HappyQuokka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/happyquokka/32/51571_2.png) [@HappyQuokka](https://discourse.julialang.org/u/HappyQuokka)
#### Post date: [July 2, 2023, 10:04pm UTC](https://discourse.julialang.org/t/calculating-third-and-higher-derivatives-reversediff-gives-0s-while-forwarddiff-works/101095/1 "2023-07-02T22:04:21Z")

</div>

I am trying to calculate the third derivative (tensor). ForwardDiff seems to work but is incredibly slow, also for lower orders. ReverseDiff, while fast for lower derivatives, doesn’t seem to work for third derivatives or higher. Below is a simple example.

```julia
using ForwardDiff
using ReverseDiff

x = [1. 3.]
f = z -> cos(z[1])*cos(z[2])
println(ForwardDiff.jacobian(xh -> ForwardDiff.hessian(f, xh), x))
println(ReverseDiff.jacobian(xh -> ReverseDiff.hessian(f, xh), x))

```

```julia
[-0.833049961066805 0.07624746575887673; 0.07624746575887673 -0.833049961066805; 0.07624746575887673 -0.833049961066805; -0.833049961066805 0.07624746575887673]
[0.0 0.0; 0.0 0.0; 0.0 0.0; 0.0 0.0]

```

I will try TaylorSeries next, as suggested in another post for higher order derivative computation. In the meantime, I wanted to confirm wether I am using ReverseDiff incorrectly or if there is another issue.

---

<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: [July 3, 2023, 2:14am UTC](https://discourse.julialang.org/t/calculating-third-and-higher-derivatives-reversediff-gives-0s-while-forwarddiff-works/101095/2 "2023-07-03T02:14:33Z")

</div>

I would try [TaylorDiff](https://github.com/JuliaDiff/TaylorDiff.jl), which is designed for this kind of problem.

I would also suggest `x = [1, 3]` rather than `[1 3]` (that is, use a 1d array, a vector, not a row vector).
