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

Arithmetic operations are ambiguous when combining Num:s and ForwardDiff #1032

Closed
asterycs opened this issue Jan 2, 2024 · 2 comments
Closed

Comments

@asterycs
Copy link
Contributor

asterycs commented Jan 2, 2024

Hi!

Arithmetic operations appear to be ambiguous when combining Num:s with ForwardDiff. A minimal example is below (originally reported here):

using Symbolics
using ForwardDiff

@variables a, x

function f(x)
    a*x
end

ForwardDiff.derivative(f, x)

which yields the following error:

ERROR: MethodError: *(::Num, ::ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Num}, Num, 1}) is ambiguous.

Candidates:
  *(x::Real, y::ForwardDiff.Dual{Ty}) where Ty
    @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/dual.jl:145
  *(a::Num, b::Real)
    @ Symbolics ~/.julia/packages/SymbolicUtils/r1pzW/src/methods.jl:73
  *(a::Num, b::Number)
    @ Symbolics ~/.julia/packages/SymbolicUtils/r1pzW/src/methods.jl:75

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

Stacktrace:
 [1] f(x::ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Num}, Num, 1})
   @ Main ./REPL[4]:2
 [2] derivative(f::typeof(f), x::Num)
   @ ForwardDiff ~/.julia/packages/ForwardDiff/PcZ48/src/derivative.jl:14
 [3] top-level scope
   @ REPL[5]:1

I'm trying to come up with a fix for this, but my understanding of Symbolics is still superficial. Would adding the (::Num, ::ForwardDiff.Dual{Ty}) methods to Symbolics similarly as here be a reasonable approach?

@ChrisRackauckas
Copy link
Member

Yes. You'd have to copy the ForwardDiff DiffRules loop and add new methods for each operation.

@asterycs
Copy link
Contributor Author

Closed by #1036

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants