Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add div methods for Dual #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/forwarddiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ for PT in (Particles, StaticParticles)
Dual{T}($PT(value(d) .- p.particles), ntuple(i->$PT(0p.particles .+ partials(d)[i]) ,length(partials(d))))
end

function Base.:(/)(p::$PT, d::Dual{T}) where {T}
Dual{T}($PT(p.particles ./ value(d)), ntuple(i->$PT(0p.particles ./ partials(d)[i]) ,length(partials(d))))
end
function Base.:(/)(d::Dual{T}, p::$PT) where {T}
Dual{T}($PT(value(d) ./ p.particles), ntuple(i->$PT(0p.particles .+ partials(d)[i]) ,length(partials(d))))
end

function Base.promote_rule(::Type{ForwardDiff.Dual{T,V,NP}}, ::Type{$PT{S, N}}) where {T, V, NP, S, N}
VS = promote_type(V,S)
Dual{T, $PT{VS, N}, NP}
Expand Down
6 changes: 5 additions & 1 deletion test/test_forwarddiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const FD = ForwardDiff
# In the cost function below, we ensure that $cx+dy > 10 \; ∀ \; c,d ∈ P$ by looking at the worst case
function cost(params)
x,y = params
-(3x+2y) + 10000sum(params .< 0) + 10000*(pmaximum(c*x+d*y) > 10)
-(3x+2y) + 10000sum(params .< 0) + 10000*(pmaximum(c*x+d*y) > 10) + (x/3)*(3/x) - 1
end

params = [1., 2] # Initial guess
Expand Down Expand Up @@ -55,4 +55,8 @@ const FD = ForwardDiff
@test pmean(pmean(r[2])) != ref[2]

unsafe_comparisons(false)

x = paramsp[1]
@test FD.derivative(x -> x/2, x) == 1/2
@test FD.derivative(x -> 2/x, x) ≈ -2/x^2
end