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 diffrules for log1pmx and logmxp1 #82

Merged
merged 8 commits into from
Apr 28, 2022
Merged
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: 2 additions & 0 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x)
@define_diffrule LogExpFunctions.log1mexp(x) = :(-exp($x - LogExpFunctions.log1mexp($x)))
@define_diffrule LogExpFunctions.log2mexp(x) = :(-exp($x - LogExpFunctions.log2mexp($x)))
@define_diffrule LogExpFunctions.logexpm1(x) = :(exp($x - LogExpFunctions.logexpm1($x)))
@define_diffrule LogExpFunctions.log1pmx(x) = :(-$x / (1 + $x))
@define_diffrule LogExpFunctions.logmxp1(x) = :((1 - $x) / $x)

# binary
@define_diffrule LogExpFunctions.xlogy(x, y) = :(log($y)), :($x / $y)
Expand Down
10 changes: 8 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ non_diffeable_arg_functions = [(:Base, :rem2pi, 2), (:Base, :ldexp, 2), (:Base,
goo = if $(f in (:asec, :acsc, :asecd, :acscd, :acosh, :acoth))
# avoid singularities with finite differencing
rand($T) + $T(1.5)
elseif $(f in (:log, :airyaix, :airyaiprimex))
elseif $(f in (:log, :airyaix, :airyaiprimex, :logmxp1))
# avoid singularities with finite differencing
rand($T) + $T(0.5)
elseif $(f === :log1mexp)
Expand All @@ -40,7 +40,13 @@ non_diffeable_arg_functions = [(:Base, :rem2pi, 2), (:Base, :ldexp, 2), (:Base,
# We're happy with types with the correct promotion behavior, e.g.
# it's fine to return `1` as a derivative despite input being `Float64`.
@test promote_type(typeof($deriv), $T) === $T
@test $deriv ≈ finitediff($M.$f, goo) rtol=1e-2 atol=1e-3
if $(f in (:log1pmx, :logmxp1)) && $T == Float32
# These two functions currently don't have fallbacks for `Real`
# arguments, nor optimized implementations for `Float32`
@test_throws MethodError finitediff($M.$f, goo)
else
@test $deriv ≈ finitediff($M.$f, goo) rtol=1e-2 atol=1e-3
simsurace marked this conversation as resolved.
Show resolved Hide resolved
end
# test for 2pi functions
if $(f === :mod2pi)
goo = 4 * pi
Expand Down