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

HappyQuokka
Sun, 02 Jul 2023 22:04:21 +0000
General Usage

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.

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))

[-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.

2 posts - 2 participants