Skip to content
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: 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.17.0-dev"
version = "1.17.0"

[deps]
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
Expand Down
38 changes: 19 additions & 19 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@define_diffrule Base.abs2(x) = :( $x + $x )
@define_diffrule Base.inv(x) = :( -abs2(inv($x)) )
@define_diffrule Base.log(x) = :( inv($x) )
@define_diffrule Base.log10(x) = :( inv($x) / $logten )
@define_diffrule Base.log2(x) = :( inv($x) / $logtwo )
@define_diffrule Base.log10(x) = :( inv($logten * $x) )
@define_diffrule Base.log2(x) = :( inv($logtwo * $x) )
@define_diffrule Base.log1p(x) = :( inv($x + 1) )
@define_diffrule Base.exp(x) = :( exp($x) )
@define_diffrule Base.exp2(x) = :( exp2($x) * $logtwo )
Expand All @@ -33,30 +33,30 @@
@define_diffrule Base.cotd(x) = :( - deg2rad(1 + cotd($x)^2) )
@define_diffrule Base.sinpi(x) = :( π * cospi($x) )
@define_diffrule Base.cospi(x) = :( -(π * sinpi($x)) )
@define_diffrule Base.asin(x) = :( inv(sqrt(1 - $x^2)) )
@define_diffrule Base.acos(x) = :( -inv(sqrt(1 - $x^2)) )
@define_diffrule Base.asin(x) = :( inv(sqrt((1 - $x) * (1 + $x))) )
@define_diffrule Base.acos(x) = :( -inv(sqrt((1 - $x) * (1 + $x))) )
@define_diffrule Base.atan(x) = :( inv(1 + $x^2) )
@define_diffrule Base.asec(x) = :( inv(abs($x) * sqrt($x^2 - 1)) )
@define_diffrule Base.acsc(x) = :( -inv(abs($x) * sqrt($x^2 - 1)) )
@define_diffrule Base.asec(x) = :( inv(abs($x) * sqrt(($x - 1) * ($x + 1))) )
@define_diffrule Base.acsc(x) = :( -inv(abs($x) * sqrt(($x - 1) * ($x + 1))) )
@define_diffrule Base.acot(x) = :( -inv(1 + $x^2) )
@define_diffrule Base.asind(x) = :( inv(deg2rad(sqrt(1 - $x^2))) )
@define_diffrule Base.acosd(x) = :( -inv(deg2rad(sqrt(1 - $x^2))) )
@define_diffrule Base.asind(x) = :( inv(deg2rad(sqrt((1 - $x) * (1 + $x)))) )
@define_diffrule Base.acosd(x) = :( -inv(deg2rad(sqrt((1 - $x) * (1 + $x)))) )
@define_diffrule Base.atand(x) = :( inv(deg2rad(1 + $x^2)) )
@define_diffrule Base.asecd(x) = :( inv(deg2rad(abs($x) * sqrt($x^2 - 1))) )
@define_diffrule Base.acscd(x) = :( -inv(deg2rad(abs($x) * sqrt($x^2 - 1))) )
@define_diffrule Base.asecd(x) = :( inv(deg2rad(abs($x) * sqrt(($x - 1) * ($x + 1)))) )
@define_diffrule Base.acscd(x) = :( -inv(deg2rad(abs($x) * sqrt(($x - 1) * ($x + 1)))) )
@define_diffrule Base.acotd(x) = :( -inv(deg2rad(1 + $x^2)) )
@define_diffrule Base.sinh(x) = :( cosh($x) )
@define_diffrule Base.cosh(x) = :( sinh($x) )
@define_diffrule Base.tanh(x) = :( 1 - tanh($x)^2 )
@define_diffrule Base.tanh(x) = :( sech($x)^2 )
@define_diffrule Base.sech(x) = :( -tanh($x) * sech($x) )
@define_diffrule Base.csch(x) = :( -coth($x) * csch($x) )
@define_diffrule Base.coth(x) = :( -(csch($x)^2) )
@define_diffrule Base.asinh(x) = :( inv(sqrt($x^2 + 1)) )
@define_diffrule Base.acosh(x) = :( inv(sqrt($x^2 - 1)) )
@define_diffrule Base.atanh(x) = :( inv(1 - $x^2) )
@define_diffrule Base.asech(x) = :( -inv($x * sqrt(1 - $x^2)) )
@define_diffrule Base.acosh(x) = :( inv(sqrt($x - 1) * sqrt($x + 1)) )
@define_diffrule Base.atanh(x) = :( inv((1 - $x) * (1 + $x)) )
@define_diffrule Base.asech(x) = :( -inv($x * sqrt((1 - $x) * (1 + $x))) )
@define_diffrule Base.acsch(x) = :( -inv(abs($x) * sqrt(1 + $x^2)) )
@define_diffrule Base.acoth(x) = :( inv(1 - $x^2) )
@define_diffrule Base.acoth(x) = :( inv((1 - $x) * (1 + $x)) )
@define_diffrule Base.sinc(x) = :( cosc($x) )
@define_diffrule Base.deg2rad(x) = :( deg2rad(one($x)) )
@define_diffrule Base.mod2pi(x) = :( isinteger($x / $twoπ) ? oftype(float($x), NaN) : one(float($x)) )
Expand Down Expand Up @@ -265,10 +265,10 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x)
@define_diffrule NaNMath.sin(x) = :( NaNMath.cos($x) )
@define_diffrule NaNMath.cos(x) = :( -NaNMath.sin($x) )
@define_diffrule NaNMath.tan(x) = :( 1 + NaNMath.pow(NaNMath.tan($x), 2) )
@define_diffrule NaNMath.asin(x) = :( inv(NaNMath.sqrt(1 - NaNMath.pow($x, 2))) )
@define_diffrule NaNMath.acos(x) = :( -inv(NaNMath.sqrt(1 - NaNMath.pow($x, 2))) )
@define_diffrule NaNMath.acosh(x) = :( inv(NaNMath.sqrt(NaNMath.pow($x, 2) - 1)) )
@define_diffrule NaNMath.atanh(x) = :( inv(1 - NaNMath.pow($x, 2)) )
@define_diffrule NaNMath.asin(x) = :( inv(NaNMath.sqrt((1 - $x) * (1 + $x))) )
@define_diffrule NaNMath.acos(x) = :( -inv(NaNMath.sqrt((1 - $x) * (1 + $x))) )
@define_diffrule NaNMath.acosh(x) = :( inv(NaNMath.sqrt($x - 1) * NaNMath.sqrt($x + 1)) )
@define_diffrule NaNMath.atanh(x) = :( inv((1 - $x) * (1 + $x)) )
@define_diffrule NaNMath.log(x) = :( inv($x) )
@define_diffrule NaNMath.log2(x) = :( inv($logtwo * $x) )
@define_diffrule NaNMath.log10(x) = :( inv($logten * $x) )
Expand Down
39 changes: 39 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,45 @@ for xytype in [:Float32, :Float64, :BigFloat]
end
end

# Rules must not suffer from catastrophic cancellation, e.g., `1 - x^2` near `|x| = 1` or
# `1 - tanh(x)^2` for large `|x|` (#102): compare with the rule evaluated in high precision
below_one(T) = (1 - 4 * eps(T), -(1 - 64 * eps(T))) # 1 ± x for |x| < 1 near 1
above_one(T) = (1 + 4 * eps(T), -(1 + 64 * eps(T))) # x ± 1 for |x| > 1 near 1
acosh_xs(T) = (abs.(above_one(T))..., 2 * sqrt(floatmax(T))) # x^2 overflows for the last
@testset "accuracy: ($M, $f)" for (M, f, xs) in [
(:Base, :asin, below_one),
(:Base, :acos, below_one),
(:Base, :asind, below_one),
(:Base, :acosd, below_one),
(:Base, :atanh, below_one),
(:Base, :asech, T -> abs.(below_one(T))),
(:Base, :acoth, above_one),
(:Base, :acosh, acosh_xs),
(:Base, :asec, above_one),
(:Base, :acsc, above_one),
(:Base, :asecd, above_one),
(:Base, :acscd, above_one),
(:Base, :tanh, T -> T.((0.5, -5, 10, -20, 30, 300))),
(:NaNMath, :asin, below_one),
(:NaNMath, :acos, below_one),
(:NaNMath, :atanh, below_one),
(:NaNMath, :acosh, acosh_xs),
]
deriv = DiffRules.diffrule(M, f, :x)
@eval let
for T in (Float32, Float64), x in $xs(T)
# `x` is exact in `BigFloat`, so this is the derivative at `x` up to ~1e-70
expected = setprecision(BigFloat, 256) do
let x = big(x)
T($deriv)
end
end
@test $deriv isa T
@test $deriv ≈ expected rtol = 8 * eps(T)
end
end
end

end

@testset "diffrules" begin
Expand Down
Loading