# Automatic Differentiation over Vectors

**URL:** <https://discourse.julialang.org/t/automatic-differentiation-over-vectors/21603>\
**Category:** Machine Learning\
**Created:** [March 7, 2019, 9:10pm UTC](https://discourse.julialang.org/t/automatic-differentiation-over-vectors/21603 "2019-03-07T21:10:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benkj](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@benkj](https://discourse.julialang.org/u/benkj)\
**Post date:** [March 7, 2019, 9:10pm UTC](https://discourse.julialang.org/t/automatic-differentiation-over-vectors/21603/1 "2019-03-07T21:10:09Z")

</div>

Consider the following expression `c=exp(A*t)*b` where `A` is a matrix, `b` and `c` are vectors and `t` is a scalar. I’m trying to teach ForwardDiff to calculate the derivative of `c` with respect to `t`, which is nothing but `A*c`. This is my attempt so far

```julia
import ForwardDiff: derivative, Dual, value, partials
import ExponentialUtilities: expv
using SparseArrays

function expv(t::Dual, A, v::Vector; kw...) 
	w = expv(value(t), A, v; kw...)
	return Dual(w, (A*w) .* partials(t))
end

A = sparse([0 1.; 1. 0])
v = [1., 0]

f(t) = expv(t, A, v)

derivative(f, 1.0) 

```

However, I get the following error, which makes me think that this is not possible at all. Is this the case? Is there any other automatic differentiation tool for doing this?

```julia
ERROR: ArgumentError: Cannot create a dual over scalar type Array{Float64,1}. If the type behaves
as a scalar, define FowardDiff.can_dual.

```

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [March 7, 2019, 11:30pm UTC](https://discourse.julialang.org/t/automatic-differentiation-over-vectors/21603/2 "2019-03-07T23:30:01Z")

</div>

I haven’t tried this, but I believe `ForwardDiff.Dual` needs to be the elements of the array `w`. It may be as simple as `Dual.(w, …)`. (But it may also just work without any definitions?)

The various reverse-diff packages (Flux, Zygote, Yota) all have ways to define custom gradients, but the reverse one (I guess `dot(Δ,W*c)`?)
