Jacobians

The supertype Jacobian comprises different ways of taking Jacobians:

We first start by showing JacobianAutodiff:

# the input and output dimensions of this function are the same
F(y::AbstractArray, x::AbstractArray, params) = y .= tanh.(x)
dim = 3
x = rand(dim)
jac = JacobianAutodiff{eltype(x)}(F, dim)
JacobianAutodiff{Float64, typeof(Main.F), ForwardDiff.JacobianConfig{Nothing, Float64, 3, Tuple{Vector{ForwardDiff.Dual{Nothing, Float64, 3}}, Vector{ForwardDiff.Dual{Nothing, Float64, 3}}}}, Vector{Float64}}(Main.F, ForwardDiff.JacobianConfig{Nothing, Float64, 3, Tuple{Vector{ForwardDiff.Dual{Nothing, Float64, 3}}, Vector{ForwardDiff.Dual{Nothing, Float64, 3}}}}((Partials(1.0, 0.0, 0.0), Partials(0.0, 1.0, 0.0), Partials(0.0, 0.0, 1.0)), (ForwardDiff.Dual{Nothing, Float64, 3}[Dual{Nothing}(6.91009385161446e-310,6.9100938512765e-310,6.91009385127257e-310,6.91009385126466e-310), Dual{Nothing}(6.9100938512607e-310,6.91009385125636e-310,6.91009385128047e-310,6.91009385124055e-310), Dual{Nothing}(6.91009385123225e-310,6.9100938512275e-310,6.91009385120854e-310,6.91009385122277e-310)], ForwardDiff.Dual{Nothing, Float64, 3}[Dual{Nothing}(6.9100938516864e-310,6.91009385250773e-310,6.91009385301484e-310,6.91009385300417e-310), Dual{Nothing}(6.9100938705423e-310,6.91009387346284e-310,6.9100938259037e-310,6.9100938546769e-310), Dual{Nothing}(6.9100938538856e-310,6.91009386427954e-310,6.9100938734723e-310,6.9100938734415e-310)])), [0.0, 0.0, 0.0])

Instead of calling JacobianAutodiff(f, x) we can equivalently do:

jac = Jacobian{eltype(x)}(F, dim; mode = :autodiff)
JacobianAutodiff{Float64, typeof(Main.F), ForwardDiff.JacobianConfig{Nothing, Float64, 3, Tuple{Vector{ForwardDiff.Dual{Nothing, Float64, 3}}, Vector{ForwardDiff.Dual{Nothing, Float64, 3}}}}, Vector{Float64}}(Main.F, ForwardDiff.JacobianConfig{Nothing, Float64, 3, Tuple{Vector{ForwardDiff.Dual{Nothing, Float64, 3}}, Vector{ForwardDiff.Dual{Nothing, Float64, 3}}}}((Partials(1.0, 0.0, 0.0), Partials(0.0, 1.0, 0.0), Partials(0.0, 0.0, 1.0)), (ForwardDiff.Dual{Nothing, Float64, 3}[Dual{Nothing}(6.9100938516864e-310,6.91009385250773e-310,6.91009385301484e-310,6.91009385300417e-310), Dual{Nothing}(6.9100938705423e-310,6.91009387346284e-310,6.9100938259037e-310,6.9100938546769e-310), Dual{Nothing}(6.9100938538856e-310,6.91009386427954e-310,6.9100938734723e-310,6.9100938734415e-310)], ForwardDiff.Dual{Nothing, Float64, 3}[Dual{Nothing}(6.91009385161446e-310,6.9100938512765e-310,6.91009385127257e-310,6.91009385126466e-310), Dual{Nothing}(6.9100938512607e-310,6.91009385125636e-310,6.91009385128047e-310,6.91009385124055e-310), Dual{Nothing}(6.91009385123225e-310,6.9100938512275e-310,6.91009385120854e-310,6.91009385122277e-310)])), [0.0, 0.0, 0.0])

When calling an instance of Jacobian we can use the function [compute_jacobian!]:

params = nothing
j = zeros(dim, dim)
compute_jacobian!(j, x, jac, params)
3×3 Matrix{Float64}:
 0.770907  0.0       0.0
 0.0       0.721643  0.0
 0.0       0.0       0.493302

This is equivalent to calling:

jac(j, x, params)
3×3 Matrix{Float64}:
 0.770907  0.0       0.0
 0.0       0.721643  0.0
 0.0       0.0       0.493302