# Derivative of linear function

**URL:** <https://discourse.julialang.org/t/derivative-of-linear-function/68607>\
**Category:** New to Julia\
**Created:** [September 22, 2021, 10:24pm UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607 "2021-09-22T22:24:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Afaria1971](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@Afaria1971](https://discourse.julialang.org/u/Afaria1971)\
**Post date:** [September 22, 2021, 10:24pm UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607/1 "2021-09-22T22:24:10Z")

</div>

How to balance the variants type in the simple case below:

function dev(X::Number)  
return diff(X)  
end

if Julia derivatives X with respect to X returns 1.0?

ERROR: LoadError: MethodError: no method matching diff(::Float64)  
Closest candidates are:  
diff(!Matched::AbstractRange{T}; dims) where T at multidimensional.jl:851  
diff(!Matched::SparseArrays.AbstractSparseMatrixCSC; dims) at /buildworker/worker/package\_linux64/build/usr/share/julia/stdlib/v1.5/SparseArrays/src/linalg.jl:1068  
diff(!Matched::AbstractArray{T,1} where T) at multidimensional.jl:809

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 22, 2021, 10:29pm UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607/2 "2021-09-22T22:29:02Z")

</div>

[`diff`](https://docs.julialang.org/en/v1/base/arrays/#Base.diff) computes the difference between elements in a vector, e.g.

```julia
julia> x = [1, 2, 5];

julia> diff(x)
2-element Vector{Int64}:
 1
 3

```

This operation is not defined on a single number, thats why you get a [`MethodError`](https://docs.julialang.org/en/v1/base/base/#Core.MethodError).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 23, 2021, 10:07am UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607/3 "2021-09-23T10:07:16Z")

</div>

FWIW, for “numerical” derivatives, you can use ForwardDiff.jl.

```julia
using ForwardDiff
import ForwardDiff.derivative
f(x) = x

julia> derivative(f, 2.0)
1.0

```

For symbolic derivatives, use Symbolics.jl.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2021, 11:44am UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607/4 "2021-09-23T11:44:56Z")

</div>

> [@rafael.guerra](#):
>
> FWIW, for numerical derivatives, you can use ForwardDiff.jl.

I would say that ForwardDiff computes the exact symbolic derivative, though you can only directly access the result evaluated using floating-point arithmetic. If you look at the assembly code the compiler generates from ForwardDiff, it contains the exact symbolic derivative formula; for example, here it is computing that the derivative of x^2 is 2x:

```julia
julia> f(x) = x^2

julia> @code_llvm f(3.0)
define double @julia_f_202(double %0) {
    %1 = fmul double %0, %0
  ret double %1
}

julia> @code_llvm ForwardDiff.derivative(f, 3.0)
     %1 = fmul double %0, 2.000000e+00
  ret double %1
}

```

Whereas [“numerical derivatives”](https://en.wikipedia.org/wiki/Numerical_differentiation) typically refer to approximate derivative formulas obtained from a sequence of function values only, such as those computed by [FiniteDifferences.jl](https://github.com/JuliaDiff/FiniteDifferences.jl).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 23, 2021, 12:14pm UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607/5 "2021-09-23T12:14:34Z")

</div>

Thank you for the insight. According to this Wikipedia [article](https://en.wikipedia.org/wiki/Automatic_differentiation), automatic differentiation as used in `ForwardDiff` is distinct from [symbolic differentiation](https://en.wikipedia.org/wiki/Symbolic_differentiation) and [numerical differentiation](https://en.wikipedia.org/wiki/Numerical_differentiation).

Btw, why the right-limit is output for the following derivative at zero?

```julia
using ForwardDiff
import ForwardDiff.derivative
f(x) = abs(x)

julia> derivative(f, 0)
1.0

```

And interestingly the left-limit can be obtained too:

```julia
julia> derivative(f, -0.0)
-1.0

```

While strictly speaking, the derivative at 0 does not exist.

Thank you.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 23, 2021, 3:42pm UTC](https://discourse.julialang.org/t/derivative-of-linear-function/68607/6 "2021-09-23T15:42:16Z")

</div>

Most automatic differentiation systems take the point of view that they can essentially return anything at a non-differentiable point, and should prefer to return a value that is useful. There’s currently a ChainRulesCore.jl pull request to document this convention, here is a preview of the proposed documentation.

[https://juliadiff.org/ChainRulesCore.jl/previews/PR419/nondiff\_points.html](https://juliadiff.org/ChainRulesCore.jl/previews/PR419/nondiff_points.html)
