Symbolics.jl icon indicating copy to clipboard operation
Symbolics.jl copied to clipboard

Type ambiguity in isequal(Num, ForwardDiff.Dual)

Open hersle opened this issue 1 year ago • 2 comments

I want to fix https://github.com/SciML/ModelingToolkit.jl/issues/2997, and this seems to be the root cause:

using Symbolics, ForwardDiff, Test

@variables x
xfunc(xval) = isequal(x, xval) ? xval : xval

@test ForwardDiff.derivative(xfunc, 0.0) == 1.0
Error During Test at REPL[5]:1
  Test threw exception
  Expression: ForwardDiff.derivative(xfunc, 0.0) == 1.0
  MethodError: isequal(::Num, ::ForwardDiff.Dual{ForwardDiff.Tag{typeof(xfunc), Float64}, Float64, 1}) is ambiguous.
  
  Candidates:
    isequal(a::Num, b::Number)
      @ Symbolics C:\Users\herma\.julia\dev\Symbolics\src\num.jl:132
    isequal(x::Real, y::ForwardDiff.Dual{Ty}) where Ty
      @ ForwardDiff C:\Users\herma\.julia\packages\ForwardDiff\PcZ48\src\dual.jl:145
  
  Possible fix, define
    isequal(::Num, ::ForwardDiff.Dual{Ty}) where Ty
  
  Stacktrace:
   [1] xfunc(xval::ForwardDiff.Dual{ForwardDiff.Tag{typeof(xfunc), Float64}, Float64, 1})
     @ Main .\REPL[4]:1
   [2] derivative(f::typeof(xfunc), x::Float64)
     @ ForwardDiff C:\Users\herma\.julia\packages\ForwardDiff\PcZ48\src\derivative.jl:14
   [3] macro expansion
     @ C:\Users\herma\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\share\julia\stdlib\v1.10\Test\src\Test.jl:669 [inlined] 
   [4] top-level scope
     @ REPL[5]:1
ERROR: There was an error during testing

hersle avatar Aug 29 '24 09:08 hersle

  Possible fix, define
    isequal(::Num, ::ForwardDiff.Dual{Ty}) where Ty

from the error message could be an option.

bowenszhu avatar Aug 29 '24 09:08 bowenszhu

Yep, thanks! I am preparing a PR now.

hersle avatar Aug 29 '24 10:08 hersle

This was fixed by https://github.com/JuliaSymbolics/Symbolics.jl/pull/1247.

hersle avatar Aug 30 '24 10:08 hersle