From e0bde4a08a5253c90d7e142b07724273908d008d Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 29 Aug 2024 11:46:55 +0200 Subject: [PATCH 1/3] Resolve isequal(Num, ForwardDiff.Dual) ambiguity --- ext/SymbolicsForwardDiffExt.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ext/SymbolicsForwardDiffExt.jl b/ext/SymbolicsForwardDiffExt.jl index 480fc83f5..9882bec12 100644 --- a/ext/SymbolicsForwardDiffExt.jl +++ b/ext/SymbolicsForwardDiffExt.jl @@ -103,6 +103,25 @@ function binary_dual_definition(M, f, Ts) return expr end +##################### +# Generic Functions # +##################### + +# Predicates # +#------------# + +for pred in [:isequal, :(==)] + @eval begin + @define_binary_dual_op( + Base.$(pred), + $(pred)(value(x), value(y)) && $(pred)(partials(x), partials(y)), + $(pred)(value(x), y) && iszero(partials(x)), + $(pred)(x, value(y)) && iszero(partials(y)), + $AMBIGUOUS_TYPES + ) + end +end + ################################### # General Mathematical Operations # ################################### From 3664e2091dd369c99dd7f4550cbe4d4744da4dd3 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 29 Aug 2024 12:03:30 +0200 Subject: [PATCH 2/3] Test isequal(Num, ForwardDiff.Dual) ambiguity --- test/forwarddiff_symbolic_dual_ops.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/forwarddiff_symbolic_dual_ops.jl b/test/forwarddiff_symbolic_dual_ops.jl index 1b89c79e6..d5e3d3f47 100644 --- a/test/forwarddiff_symbolic_dual_ops.jl +++ b/test/forwarddiff_symbolic_dual_ops.jl @@ -107,3 +107,10 @@ for f ∈ (hypot, muladd) end # fma is not defined for Symbolics.Num + +# https://github.com/JuliaSymbolics/Symbolics.jl/issues/1246 +@testset "isequal type ambiguity" begin + @variables x + xfunc(xval) = isequal(x, xval) ? xval : xval + @test ForwardDiff.derivative(xfunc, 0.0) == 1.0 +end From f0bcc47f9411b7d5aed72cd85db679b4557bc488 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 29 Aug 2024 15:43:08 +0200 Subject: [PATCH 3/3] Improve test --- test/forwarddiff_symbolic_dual_ops.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/forwarddiff_symbolic_dual_ops.jl b/test/forwarddiff_symbolic_dual_ops.jl index d5e3d3f47..e132f9a50 100644 --- a/test/forwarddiff_symbolic_dual_ops.jl +++ b/test/forwarddiff_symbolic_dual_ops.jl @@ -110,7 +110,7 @@ end # https://github.com/JuliaSymbolics/Symbolics.jl/issues/1246 @testset "isequal type ambiguity" begin - @variables x - xfunc(xval) = isequal(x, xval) ? xval : xval - @test ForwardDiff.derivative(xfunc, 0.0) == 1.0 + @variables z + y(x) = isequal(z, x) ? 0 : x + @test ForwardDiff.derivative(y, 0) == 1 # expect ∂(x)/∂x end