JuliaDiff / DiffRules.jl

A simple shared suite of common derivative definitions
Other
74 stars 38 forks source link

rule for GSL functions #56

Closed j-fu closed 3 years ago

j-fu commented 3 years ago

Hi, I am trying to add

@define_diffrule GSL.sf_fermi_dirac_half(x)          = :(GSL.sf_fermi_dirac_mhalf($x))

as a diffrule. To make this work I need to import GSL to ForwardDiff, creating a dependency on GSL which likely should be avoided. It indeed seems to work well.

Is there another way to proceed here ?

j-fu commented 3 years ago

Ok, looking into dual.jl in ForwardDiff gives the idea to just define:

function GSL.sf_fermi_dirac_half(d::ForwardDiff.Dual{T}) where T
    x=ForwardDiff.value(d)
    val=GSL.sf_fermi_dirac_half(x)
    deriv=GSL.sf_fermi_dirac_mhalf(x)
    ForwardDiff.Dual{T}(val, deriv * ForwardDiff.partials(d))
end

This works for my purpose, and it is easier than I thought...