# Directional derivative with automatic differentiation

**URL:** <https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305>\
**Category:** General Usage\
**Tags:** forwarddiff\
**Created:** [June 2, 2021, 10:10pm UTC](https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305 "2021-06-02T22:10:06Z")\
**Posts on this page:** 5\
**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:** [June 2, 2021, 10:10pm UTC](https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305/1 "2021-06-02T22:10:06Z")

</div>

Hello,

I would like to compute the directional derivative of a vector-valued function f at a point x along the direction v, i.e. \nabla f(x) \cdot v using ForwardDiff.jl.  
From what I understood, it is possible to compute this with a single Dual vector whose field `value` is to x and whose `dual` values are set to v.

Can someone walk me through this implementation with Dual numbers? I am getting the good result, but I still end up summing the entries of the Jacobian (hence forming the Jacobian =( ). What is a better way to implement this directional derivative?

```julia
using ForwardDiff
import ForwardDiff: seed!

function f(x)
    @show "call"
    return [2.0*x[1] + x[2]^3 + x[3]^2; 3.0*x[2] + x[3]^2]  
end

x = rand(10)
v = rand(10)
y = zero(x)

cfg = ForwardDiff.GradientConfig(f, x)
xdual = cfg.duals
# Create seeds along the direction v
seeds = ntuple(j -> ForwardDiff.Partials(ntuple(i-> i==j ? v[i] : 0.0, N)), N)

seed!(xdual, x, seeds)

ydual = f(xdual)

Jv = ForwardDiff.jacobian(f, x)*v

@show norm(Jv[1] - sum(ydual[1].partials))
@show norm(Jv[2] - sum(ydual[2].partials))

```

---

<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:** [June 2, 2021, 11:21pm UTC](https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305/2 "2021-06-02T23:21:42Z")

</div>

See [SparseDiffTools.jl/jaches\_products.jl at master · JuliaDiff/SparseDiffTools.jl · GitHub](https://github.com/JuliaDiff/SparseDiffTools.jl/blob/master/src/differentiation/jaches_products.jl#L4-L10)

---

<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:** [June 3, 2021, 12:42am UTC](https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305/3 "2021-06-03T00:42:23Z")

</div>

Thank you Chris for your answer!

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 3, 2021, 3:54am UTC](https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305/4 "2021-06-03T03:54:20Z")

</div>

The directional derivative at `x` along `d` is also just:

```julia
ForwardDiff.derivative(t -> f(x + t * d), 0.0)

```

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [June 4, 2021, 1:48am UTC](https://discourse.julialang.org/t/directional-derivative-with-automatic-differentiation/62305/5 "2021-06-04T01:48:49Z")

</div>

Also, FWIW: [Directional derivatives · Issue #428 · JuliaDiff/ForwardDiff.jl · GitHub](https://github.com/JuliaDiff/ForwardDiff.jl/issues/428#issuecomment-567707210)
