Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DiffRules"
uuid = "b552c78f-8df3-52c6-915a-8e097449b14b"
version = "1.13.0"
version = "1.14.0"

[deps]
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
Expand Down
6 changes: 1 addition & 5 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,10 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x)
# trinary #
#---------#

#=

@define_diffrule Base.muladd(x, y, z) = :($y), :($x), :(one($z))
@define_diffrule Base.fma(x, y, z) = :($y), :($x), :(one($z))

@define_diffrule Base.ifelse(p, x, y) = false, :($p), :(!$p)

=#
@define_diffrule Base.ifelse(p, x, y) = NaN, :($p), :(!$p)

####################
# SpecialFunctions #
Expand Down
48 changes: 28 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,29 @@ non_diffeable_arg_functions = [(:Base, :rem2pi, 2), (:Base, :ldexp, 2), (:Base,
end
end
elseif arity == 3
#=
@test DiffRules.hasdiffrule(M, f, 3)
derivs = DiffRules.diffrule(M, f, :foo, :bar, :goo)
@eval begin
foo, bar, goo = randn(3)
dx, dy, dz = $(derivs[1]), $(derivs[2]), $(derivs[3])
if !(isnan(dx))
@test isapprox(dx, finitediff(x -> $M.$f(x, bar, goo), foo), rtol=0.05)
end
if !(isnan(dy))
@test isapprox(dy, finitediff(y -> $M.$f(foo, y, goo), bar), rtol=0.05)
end
if !(isnan(dz))
@test isapprox(dz, finitediff(z -> $M.$f(foo, bar, z), goo), rtol=0.05)
let
foo, bar, goo = randn($T, 3)
dx, dy, dz = $(derivs[1]), $(derivs[2]), $(derivs[3])
if !isnan(dx)
@test dx ≈ finitediff(x -> $M.$f(x, bar, goo), foo) rtol=1e-2
# Check type, if applicable.
@test promote_type(typeof(real(dx)), $T) === $T
end
if !isnan(dy)
@test dy ≈ finitediff(y -> $M.$f(foo, y, goo), bar) rtol=1e-2
# Check type, if applicable.
@test promote_type(typeof(real(dy)), $T) === $T
end
if !isnan(dz)
@test dz ≈ finitediff(z -> $M.$f(foo, bar, z), goo) rtol=1e-2
# Check type, if applicable.
@test promote_type(typeof(real(dz)), $T) === $T
end
end
end
=#
end
end
end
Expand Down Expand Up @@ -201,16 +207,18 @@ end
end

# Test ifelse separately as first argument is boolean
#=
@test DiffRules.hasdiffrule(:Base, :ifelse, 3)
derivs = DiffRules.diffrule(:Base, :ifelse, :foo, :bar, :goo)
for cond in [true, false]
for cond in (true, false)
@eval begin
foo = $cond
bar, gee = randn(2)
dx, dy, dz = $(derivs[1]), $(derivs[2]), $(derivs[3])
@test isapprox(dy, finitediff(y -> ifelse(foo, y, goo), bar), rtol=0.05)
@test isapprox(dz, finitediff(z -> ifelse(foo, bar, z), goo), rtol=0.05)
let
foo = $cond
bar, goo = randn(2)
dx, dy, dz = $(derivs[1]), $(derivs[2]), $(derivs[3])
@test isnan(dx)
@test dy ≈ finitediff(y -> ifelse(foo, y, goo), bar) rtol=1e-2 atol=1e-9
@test dz ≈ finitediff(z -> ifelse(foo, bar, z), goo) rtol=1e-2 atol=1e-9
end
end
end
=#