# How to do partial derivatives?

**URL:** https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869
**Category:** New to Julia
**Created:** [January 21, 2019, 1:05pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869 "2019-01-21T13:05:15Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Lucy929](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Lucy929](https://discourse.julialang.org/u/Lucy929)
#### Post date: [January 21, 2019, 1:05pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/1 "2019-01-21T13:05:15Z")

</div>

I’m totally new in Julia. Now, I’m working on derivative. I found that ‘2nd derivatives’ (ex. function derivative\_sec\_fd(f,x)), but still couldn’t find how to do partial derivative…  
Could you tell me which command should I use?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 21, 2019, 1:18pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/2 "2019-01-21T13:18:20Z")

</div>

Do you try to do numeric, automatic or symbolic differentiation?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 21, 2019, 1:30pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/3 "2019-01-21T13:30:49Z")

</div>

Related (maybe xref?): [Partial derivatives in Julia - Stack Overflow](https://stackoverflow.com/questions/54277219/partial-derivatives-in-julia)

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 21, 2019, 1:37pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/4 "2019-01-21T13:37:23Z")

</div>

You could use automatic differentiation to calculate partial derivatives of a function

```julia
julia> using ForwardDiff

julia> f(x) = 2*x[2]^2+x[1]^2 # some function
f (generic function with 2 methods)

julia> g = x -> ForwardDiff.gradient(f, x); # g is now a function representing the gradient of f

julia> g([1,2]) # evaluate the partial derivatives (gradient) at some point x
2-element Array{Int64,1}:
 2
 8

```

---

<div class="post-metadata">

### Author: ![Lucy929](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@Lucy929](https://discourse.julialang.org/u/Lucy929)
#### Post date: [January 22, 2019, 6:19pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/5 "2019-01-22T18:19:04Z")

</div>

Thank you for all of replies. I’ll try to do it !!

---

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [October 19, 2020, 12:56am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/6 "2020-10-19T00:56:27Z")

</div>

Is there anything wrong with doing it this way?

```julia
julia> using ForwardDiff

julia> f(x, y) = 2y^2 + x^2
f (generic function with 1 method)

julia> ∂f_∂x(x, y) = ForwardDiff.derivative(x -> f(x, y), x)
∂f_∂x (generic function with 1 method)

julia> ∂f_∂y(x, y) = ForwardDiff.derivative(y -> f(x, y), y)
∂f_∂y (generic function with 1 method)

julia> ∂f_∂x(1, 2)
2

julia> ∂f_∂y(1, 2)
8

```

Just thought I’d ask to check if it’s unhealthy to define partial derivatives like so.

---

<div class="post-metadata">

### Author: ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)
#### Post date: [October 19, 2020, 1:01am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/7 "2020-10-19T01:01:11Z")

</div>

I think it’s fine. You’re formally introducing `y` via closure, which is usually pretty efficient in Julia.

Disclaimer: I do this myself for some of my codes.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 19, 2020, 6:32am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/8 "2020-10-19T06:32:09Z")

</div>

> [@kapple](#):
>
> Is there anything wrong with doing it this way?

No particularly, but if you need both partial derivatives anyway, just define a function on a vector and use `ForwardDiff.gradient`.

---

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [October 19, 2020, 6:44am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/9 "2020-10-19T06:44:30Z")

</div>

This is true, but given `f(x, y)`,

```julia
g(x) = f(x[1], x[2])
∇g(x) = ForwardDiff.gradient(g, x)
∂f_∂x(x, y) = ∇g([x, y])[1]
∂f_∂y(x, y) = ∇g([x, y])[2]

```

is noticeably longer and somewhat less elegant than

```julia
∂f_∂x(x, y) = ForwardDiff.derivative(x -> f(x, y), x)
∂f_∂y(x, y) = ForwardDiff.derivative(y -> f(x, y), y)

```

in my opinion.

I’ve needed second derivatives too, so the latter method is much less verbose (and accomplishes the same thing, AFAIU).

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [October 19, 2020, 7:29am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/10 "2020-10-19T07:29:02Z")

</div>

Some one in slack (sorry, I forgot whom) gave me this snippet

```julia
using ForwardDiff: derivative

partiali(f, x, i) = ForwardDiff.partials(f([Dual{}(x[j], 1.0*(i==j)) for j in eachindex(x)]))[]

struct ∂{k, F} <: Function
    f::F
    ∂{k}(f::F) where {k, F} = new{k, F}(f)
end
function (pd::∂{k})(args...) where {k}
    N = length(args)
    derivative(argk -> f(ntuple(i -> i == k ? argk : args[i], Val(N))...), args[k])
end
f(x, y, z, w) = x + 2y + 3z + 4w 
∂{3}(f)(1,1,1,1)

```

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [March 5, 2021, 12:08am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/11 "2021-03-05T00:08:17Z")

</div>

Addendum via @cscherrer

```julia
julia> using StructArrays

julia> using ForwardDiff

julia> function partiali(n,i)
           ith = zeros(n)
           ith[i] += 1
           function (f,x)
               sa = StructArray{ForwardDiff.Dual{}}((x, ith))
               return f(sa)
           end
       end
partiali (generic function with 2 methods)

julia> f(x) = sum(x -> x^2, x)
f (generic function with 1 method)

julia> x = randn(1000);

julia> ∂₅₀ = partiali(1000,50);

julia> x[50]
2.1129129387789667

julia> @btime $f($x)
  82.987 ns (0 allocations: 0 bytes)
1000.4848272576928

julia> @btime $∂₅₀($f,$x)
  167.038 ns (0 allocations: 0 bytes)
Dual{Nothing}(1000.4848272576928,4.225825877557933)

```

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [March 5, 2021, 12:15am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/12 "2021-03-05T00:15:30Z")

</div>

Thanks @mschauer 🙂

This is the approach I’d go with if you just need one partial at a time, say for coordinate descent or Gibbs sampling. A big advantage of doing it this way is that by using a StructArray you can use the original data with no new allocations.

---

<div class="post-metadata">

### Author: ![kylebeggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylebeggs/32/43348_2.png) [@kylebeggs](https://discourse.julialang.org/u/kylebeggs)
#### Post date: [October 7, 2022, 2:51am UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/13 "2022-10-07T02:51:11Z")

</div>

Hi @cscherrer , can you explain the unnamed function inside `partiali()`?

---

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [October 7, 2022, 5:23pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/14 "2022-10-07T17:23:54Z")

</div>

Say you have a function like

```julia
julia> f(x, y, z) = x^2 * y - x * y * z
f (generic function with 1 method)

```

At the point `(2,3,4)`, this is

```julia
julia> f(2,3,4)
-12

```

If you wanted \frac{\partial f}{\partial y} at this point, you could get this by replacing the values with `ForwardDiff.Dual`s:

```julia
julia> using ForwardDiff: Dual

julia> f(Dual{}(2,0), Dual{}(3,1), Dual{}(4,0))
Dual{Nothing}(-12,-4)

```

If the function instead takes an array, like

```julia
julia> function g(arr)
       x,y,z = arr
       x^2 * y - x * y * z
       end
g (generic function with 1 method)

```

then we could write an input as e.g.

```julia
julia> [Dual{}(2,0), Dual{}(3,1), Dual{}(4,0)]
3-element Vector{Dual{Nothing, Int64, 1}}:
 Dual{Nothing}(2,0)
 Dual{Nothing}(3,1)
 Dual{Nothing}(4,0)

```

To save typing, we could instead write this as

```julia
julia> Dual{}.([2,3,4],[0,1,0])
3-element Vector{Dual{Nothing, Int64, 1}}:
 Dual{Nothing}(2,0)
 Dual{Nothing}(3,1)
 Dual{Nothing}(4,0)

```

Now, notice that `[2,3,4]` is just the point we want to evaluate at, and `[0,1,0]` is a “one-hot-encoding” of the variable we want the partial with respect to. The latter is `ith` in @mschauer’s post above.

Finally, even if `x` and `ith` are given, building the `Dual` vector in this way requires allocation:

```julia
julia> x = [2,3,4]
3-element Vector{Int64}:
 2
 3
 4

julia> ith = [0,1,0]
3-element Vector{Int64}:
 0
 1
 0

julia> @allocated Dual{}.(x, ith)
176

```

The idea behind the `StructArray` is that it lets you avoid this allocation. There are a few other packages that would work in the same way - you could instead use MappedArrays or LazyArrays, and the effect would be the same.

---

<div class="post-metadata">

### Author: ![kylebeggs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylebeggs/32/43348_2.png) [@kylebeggs](https://discourse.julialang.org/u/kylebeggs)
#### Post date: [October 7, 2022, 7:16pm UTC](https://discourse.julialang.org/t/how-to-do-partial-derivatives/19869/15 "2022-10-07T19:16:41Z")

</div>

Thank you, thats a great explanation!! Seems like something the package could implement, but i suppose they don’t want to bloat it. But then again, implementing this is not perfectly strait forward to the novice user…
