JuliaMath / Calculus.jl

Calculus functions in Julia
Other
278 stars 76 forks source link

analytic Jacobian #94

Open bjarthur opened 8 years ago

bjarthur commented 8 years ago

i wrote counterparts to the existing finite_difference_jacobian for the case where the function of interest is analytically differentiable. happy to submit a PR if there is interest. would just need to know the best place to put it.

function analytic_jacobian{T<:Number}(fdot::Vector{Function}, x::Vector{T})
    f_x = fdot[1](x)
    J = Array(Float64,length(f_x),length(x))
    J[:,1] = f_x
    for i = 2:length(fdot)
        J[:,i] = fdot[i](x)
    end
    J
end

"""
    `jacobian(fdot::Vector{Function}) -> g(x::Vector)`

Given a function `f` whose partial derivatives are `fdot`, return a function
`g(x)` which itself returns the Jacobian of `f` at `x`.
"""
function jacobian(fdot::Vector{Function})
    g(x::Vector) = analytic_jacobian(fdot, x)
    return g
end
mlubin commented 8 years ago

I'm very confused. What does this do? Why do you need a function to compute the jacobian if you already have functions to evaluate derivatives?

bjarthur commented 8 years ago

this packages up the output of the derivatives into a single function which returns the Jacobian matrix. since it uses the same interface as it's finite difference counterpart, it's easy to switch between the two. for example, the code structure between the analytic and finite-difference versions of levenberg-marquardt least squares fit can be nearly identical with the addition of these two functions.