# ForwardDiff: How to get the derivative part of a dual number

**URL:** <https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [January 19, 2020, 10:21pm UTC](https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560 "2020-01-19T22:21:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [January 19, 2020, 10:21pm UTC](https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560/1 "2020-01-19T22:21:11Z")

</div>

How do I get the derivative part _b_ (or whatever it is called) of a [dual number](https://en.wikipedia.org/wiki/Dual_number) given by a+b\epsilon, \quad \epsilon^2=0, defined using [ForwardDiff](https://github.com/JuliaDiff/ForwardDiff.jl) package?

In particular, with the dual number 3+1\epsilon given by

```julia
julia> using ForwardDiff

julia> a = ForwardDiff.Dual(3.0,1.0)
Dual{Nothing}(3.0,1.0)

```

the real part can be obtained as

```julia
julia> a.value
3.0

```

but accessing the derivative part does not work using the dot notation

```julia
julia> a.partials
1-element ForwardDiff.Partials{1,Float64}:
Error showing value of type ForwardDiff.Partials{1,Float64}:
ERROR: MethodError: no method matching getindex(::Tuple{Float64}, ::Int64, ::Int64)
Closest candidates are:
  getindex(::Tuple, ::Int64) at tuple.jl:24
  getindex(::Tuple, ::Real) at tuple.jl:25
  getindex(::Tuple, ::AbstractUnitRange{#s66} where #s66<:Real) at range.jl:290
  ...
Stacktrace:
 [1] getindex at /home/hurak/.julia/packages/ForwardDiff/vt5F1/src/partials.jl:23 [inlined]
 [2] isassigned(::ForwardDiff.Partials{1,Float64}, ::Int64, ::Int64) at ./abstractarray.jl:405
 [3] alignment(::IOContext{REPL.Terminals.TTYTerminal}, ::ForwardDiff.Partials{1,Float64}, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Int64, ::Int64, ::Int64) at ./arrayshow.jl:67
 [4] print_matrix(::IOContext{REPL.Terminals.TTYTerminal}, ::ForwardDiff.Partials{1,Float64}, ::String, ::String, ::String, ::String, ::String, ::String, ::Int64, ::Int64) at ./arrayshow.jl:186
 [5] print_matrix at ./arrayshow.jl:159 [inlined]
 [6] print_array at ./arrayshow.jl:308 [inlined]
 [7] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::ForwardDiff.Partials{1,Float64}) at ./arrayshow.jl:345
 [8] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:132
 [9] display(::REPL.REPLDisplay, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:136
 [10] display(::Any) at ./multimedia.jl:323
 [11] #invokelatest#1 at ./essentials.jl:709 [inlined]
 [12] invokelatest at ./essentials.jl:708 [inlined]
 [13] print_response(::IO, ::Any, ::Bool, ::Bool, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:156
 [14] print_response(::REPL.AbstractREPL, ::Any, ::Bool, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:141
 [15] (::REPL.var"#do_respond#38"{Bool,REPL.var"#48#57"{REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:719
 [16] #invokelatest#1 at ./essentials.jl:709 [inlined]
 [17] invokelatest at ./essentials.jl:708 [inlined]
 [18] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/LineEdit.jl:2306
 [19] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:1045
 [20] run_repl(::REPL.AbstractREPL, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:201
 [21] (::Base.var"#770#772"{Bool,Bool,Bool,Bool})(::Module) at ./client.jl:382
 [22] #invokelatest#1 at ./essentials.jl:709 [inlined]
 [23] invokelatest at ./essentials.jl:708 [inlined]
 [24] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at ./client.jl:366
 [25] exec_options(::Base.JLOptions) at ./client.jl:304
 [26] _start() at ./client.jl:460

```

Apparently, it is just a matter of printing the output, because

```julia
julia> show(a.partials)
Partials(1.0,)

```

---

<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:** [January 20, 2020, 3:06am UTC](https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560/2 "2020-01-20T03:06:46Z")

</div>

`ForwardDiff.partials(x)`

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [January 20, 2020, 4:00am UTC](https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560/3 "2020-01-20T04:00:24Z")

</div>

`@edit ForwardDiff.partials(x)` shows

`@inline partials(d::Dual) = d.partials`

So it still throws an error at the REPL. Either way, throwing an error instead of doing anything else is a bug.

`x.partials.values` does work at the REPL

---

<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:** [January 20, 2020, 4:07am UTC](https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560/4 "2020-01-20T04:07:21Z")

</div>

Yeah, that printing should get fixed. I’m surprised I don’t remember running into that 🤷‍♂️, but using the accessor functions `ForwardDiff.value` and `ForwardDiff.partials` is how it’s supposed to be done.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 20, 2020, 10:00am UTC](https://discourse.julialang.org/t/forwarddiff-how-to-get-the-derivative-part-of-a-dual-number/33560/5 "2020-01-20T10:00:20Z")

</div>

I had a fix lying around that I had forgotten to upstream, see [Restrict `getindex` for partials to a single Int by timholy · Pull Request #437 · JuliaDiff/ForwardDiff.jl · GitHub](https://github.com/JuliaDiff/ForwardDiff.jl/pull/437)
