# Evaluation, gradient and Hessian of a scalar function for multiple values using ForwardDiff.jl

**URL:** <https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641>\
**Category:** Numerics\
**Created:** [July 6, 2020, 9:24pm UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641 "2020-07-06T21:24:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [July 6, 2020, 9:24pm UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641/1 "2020-07-06T21:24:56Z")

</div>

Hello,

I would like to evaluate a scalar-function f: R \xrightarrow{} R as well as its derivative and hessian at around 200 values using ForwardDiff.jl

To speed-up the computations, is it possible to precompute an object like the graph of the function and apply it to the different values? From my understanding, it would be a waste of time to recompute the same thing multiple times.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 6, 2020, 9:42pm UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641/2 "2020-07-06T21:42:46Z")

</div>

An equivalent to what you’re talking about already happens automagically. ForwardDiff works by calling your code with a type (`dual`) that causes your code to also compute derivatives. As such your code will run with a new type, so compilation occurs to optimize your code.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [July 6, 2020, 10:04pm UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641/3 "2020-07-06T22:04:07Z")

</div>

Thank you for your answer. Ok, so the following code will use the optimized code for `f`?

```julia
using ForwardDiff
using BenchmarkTools

function timing()
x = randn(200)
g(x) = (x^4-5x^3*x^2+1)*exp(-x^2/2)
@btime ForwardDiff.derivative.(g, x)
end

```

How do I get the evaluation and the hessian of the function at the same time?

To compute the second derivative of a scalar function, is there a cleaner way than using nested ForwardDiff.derivative calls ?

```julia
dg = zeros(200)
map(xi->ForwardDiff.derivative(z->ForwardDiff.derivative(g, z), xi),x)

```

---

<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:** [July 6, 2020, 11:10pm UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641/4 "2020-07-06T23:10:05Z")

</div>

Check DiffResults.jl, sounds like is what you want

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [July 6, 2020, 11:30pm UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641/5 "2020-07-06T23:30:34Z")

</div>

> [@longemen3000](#):
>
> DiffResults.jl

The problem is that DiffResults.jl computes simultaneously f(x), \nabla f(x), H(f(x)) but solely for the same vector, not for repeated use of the same function f for different values.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [July 7, 2020, 1:36am UTC](https://discourse.julialang.org/t/evaluation-gradient-and-hessian-of-a-scalar-function-for-multiple-values-using-forwarddiff-jl/42641/6 "2020-07-07T01:36:25Z")

</div>

Calculating at a different point requires everything to be recalculated. As @Oscar_Smith said, once you call ForwardDiff once, the code should have been compiled for your particular function, and the compiled version will be used for all the evaluations.
