diff --git a/src/rules.jl b/src/rules.jl index e2dd233..c32e6dd 100644 --- a/src/rules.jl +++ b/src/rules.jl @@ -96,6 +96,8 @@ _abs_deriv(x) = signbit(x) ? -one(x) : one(x) # trinary # #---------# +@define_diffrule Base.hypot(x, y, z) = :( $x / hypot($x, $y, $z) ), :( $y / hypot($x, $y, $z) ), :( $z / hypot($x, $y, $z) ) + #= @define_diffrule Base.muladd(x, y, z) = :($y), :($x), :(one($z)) diff --git a/test/runtests.jl b/test/runtests.jl index 92b0866..5f91905 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -88,23 +88,32 @@ 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 = rand($T), rand($T), rand($T) + dx, dy, dz = $(derivs[1]), $(derivs[2]), $(derivs[3]) + if !(isnan(dx)) + @test dx ≈ finitediff(z -> $M.$f(z, 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(z -> $M.$f(foo, z, 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