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

Resolve isequal(Num, ForwardDiff.Dual) ambiguity #1247

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions ext/SymbolicsForwardDiffExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
###################################
Expand Down
7 changes: 7 additions & 0 deletions test/forwarddiff_symbolic_dual_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xfunc(xval) = isequal(x, xval) ? xval : xval
xfunc(xval) = isequal(x, xval) ? x : xval

I don't quite understand this test? In both cases the derivative is respect to the value, so even if isequal is wrong this would pass. It needs to have a "bad" branch that would be wrong/error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I agree. I was very focused on simply ensuring it does not crash. I will improve it a bit and add a bad branch.

@test ForwardDiff.derivative(xfunc, 0.0) == 1.0
end
Loading