# Finding Derivative of 2 arrays of numbers Using Zygote

**URL:** https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822
**Category:** General Usage
**Tags:** question, diffeq
**Created:** [July 6, 2022, 7:18am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822 "2022-07-06T07:18:42Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 6, 2022, 7:18am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/1 "2022-07-06T07:18:42Z")

</div>

Hello,

Please I am working on a physical system, and after running simulations I have 2 arrays of variables like this:

D = [0.01, 0.02, …, 0.05]  
m = [1, 2, …, 5]

Please how do I find the derivative (∂D/∂m) using Zygote, given only the above 2 arrays of values. I have checked the docs and I do not understand it. I do not have a function, so I am thinking Zygote could compute the derivative directly.

Thank you in anticipation of your help.

Best,

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 6, 2022, 7:56am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/2 "2022-07-06T07:56:39Z")

</div>

Hello @alegeaa and welcome!  
If you don’t have a function, you cannot compute a derivative. Indeed, the arrays `D` and `m` might have been generated by an arbitrary process we know nothing about.  
However, what I mean by “function” is quite fuzzy, and it could encompass physical simulators. Do you have access to one or not at all?

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 9, 2022, 12:25am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/3 "2022-07-09T00:25:07Z")

</div>

Hello @gdalle thank YOU FOR YOUR REPLY. I have access to all, i.e. both D and m arrays. I wrote a function to compute the derivative numerically by this formula, ∂D/∂m = [D(m1+delta/2) - D(m1-delta/2)]/ delta. However, I would like to use Zygote for this. Any directions please?

PS: sorry for the late reply.

---

<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: [July 9, 2022, 4:24am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/4 "2022-07-09T04:24:05Z")

</div>

You can create a function which takes in 2 values and “completes” D and m from that, and then calls your inner function.

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 10, 2022, 9:39am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/5 "2022-07-10T09:39:41Z")

</div>

Thank you Chris, sadly I don’t understand what you mean by “completes”. Could you elaborate please?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 10, 2022, 2:03pm UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/6 "2022-07-10T14:03:35Z")

</div>

Ok, so for instance if you have a function `compute_D` available, then you can use Zygote as follows:

```julia
using Zygote
D = compute_D(m)
JacD = Zygote.jacobian(compute_D, m)[1]

```

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 11, 2022, 12:15am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/7 "2022-07-11T00:15:56Z")

</div>

I have a function that uses the D and m arrays to compute the differentials for corresponding D and m pairs in the arrays. The function looks like this:

```julia
function calc_differentials(delta, diameters, masses)
    diffs = []
    for i in range(1, length(diameters))
        diff = ((diameters[i] * (masses[i] + delta / 2)) - (diameters[i] * (masses[i] - delta / 2))) / delta
        append!(diffs, diff)
    end;
    return diffs
end;

```

So I understand, you are implying that I do sth like:

```julia
for i in range(1, length(diameters))
        JacD = Zygote.jacobian(D[i], m[I])[1]
end

```

Is this correct?  
Thanks

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 11, 2022, 5:19am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/8 "2022-07-11T05:19:23Z")

</div>

OK, so I think I’m starting to understand your problem. The formula you’re using only makes sense when D is a _function_ that you apply to m and not just a _value_.

To clarify, let me change notations, and suppose you want to compute the derivative of f: x \longmapsto y. The method of [finite differences](https://en.wikipedia.org/wiki/Finite_difference#Relation_with_derivatives) tells you

f'(x) = \frac{f(x + \delta / 2) - f(x - \delta / 2)}{\delta}

Instead, what you have been computing is the following quantity, which does not tell you anything useful:

\frac{y \times (x + \delta / 2) - y \times (x - \delta / 2)}{\delta}

One reason is that without the function f, you really cannot say anything about the relation between x and y. For instance, consider x = 0 and y = 0:

- This could be the result of f\_1(x) = x, in which case f\_1'(x) = 1
- This could be the result of f\_2(x) = -x, in which case f\_2'(x) = -1

The same goes if you have several input-ouput pairs (x\_i, y\_i). You need to have access to the function to differentiate it, whether you use finite differences, symbolic differentiation or automatic differentiation (with Zygote or ForwardDiff)

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 11, 2022, 5:23am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/9 "2022-07-11T05:23:47Z")

</div>

So my message above tells you why your function `calc_differentials` is not correct. You need access to the function `compute_D` if you want to retrieve any useful derivative information.  
Supposing you do, then you would compute the derivative at a point `m` using the following syntax:

```julia
i = 3
Zygote.gradient(compute_D, m[i])[1]

```

I suggested `jacobian` because I thought `compute_D` took a vector as input and returned another vector. If the output is a real number, `gradient` is the way to go.

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 11, 2022, 5:54am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/10 "2022-07-11T05:54:17Z")

</div>

Thank you so much. I understand the solution now. However, my `compute_D` function returns an array of all the D’s in the simulation. Also, the function has 4 arguments, can I do sth like this:

```julia
i = 3
Zygote.gradient(compute_D(arg1, arg2, arg3, arg4), m[i])[1]

```

or it doesn’t change, i.e,

```julia
i = 3
Zygote.gradient(compute_D, m[i])[1]

```

Thank you, and sorry for my endless questions.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 11, 2022, 6:36am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/11 "2022-07-11T06:36:33Z")

</div>

It’s always a good idea to refer to the [package documentation](https://fluxml.ai/Zygote.jl/latest/) for questions about syntax. In this case, if you have a function f(x, y, z), the call

```julia
gs = Zygote.gradient(f, x, y, z)

```

returns a tuple whose first element is the gradient wrt x, second element wrt y, etc.

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 11, 2022, 6:56am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/12 "2022-07-11T06:56:49Z")

</div>

thanks

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 11, 2022, 7:34am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/13 "2022-07-11T07:34:43Z")

</div>

> [@gdalle](#):
>
> ```julia
> gs = Zygote.gradient(f, x, y, z)
> 
> ```

It returns this error:

MethodError: no method matching zero(::Type{Any})  
Closest candidates are:…

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 11, 2022, 10:35am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/14 "2022-07-11T10:35:18Z")

</div>

That is really hard to debug without a MWE

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 11, 2022, 1:21pm UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/15 "2022-07-11T13:21:48Z")

</div>

@alegeaa reading this guide is probably a good idea if you want further help 🙃

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![alegeaa](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alegeaa](https://discourse.julialang.org/u/alegeaa)
#### Post date: [July 12, 2022, 4:36am UTC](https://discourse.julialang.org/t/finding-derivative-of-2-arrays-of-numbers-using-zygote/83822/16 "2022-07-12T04:36:49Z")

</div>

> [@gdalle](#):
>
> `Zygote.jacobian(compute_D, m)[1]`

Thank you. I will.
