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

Symbolics.jl compatibility #674

Open
polhager opened this issue Nov 22, 2023 · 1 comment
Open

Symbolics.jl compatibility #674

polhager opened this issue Nov 22, 2023 · 1 comment

Comments

@polhager
Copy link

polhager commented Nov 22, 2023

Arithmetic operations between Num and Dual based on Num is not clearly defined. The basic usecase is this:

using ForwardDiff, Symbolics.jl

@variables a, x
f = x -> a*x
Differential(x)(f(x)) |> expand_derivatives # This works, and gives what I would expect from ForwardDiff.derivative
ForwardDiff.derivative(f, x) # This returns an error

The error given by the last line is this

ERROR: MethodError: *(::Num, ::ForwardDiff.Dual{ForwardDiff.Tag{var"#5#6", 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/ssQsQ/src/methods.jl:73
  *(a::Num, b::Number)
    @ Symbolics ~/.julia/packages/SymbolicUtils/ssQsQ/src/methods.jl:75

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

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

This seems to be the case for (at least) all basic arithmetic operations. Not sure if this should be fixed here or in Symbolics.jl.

@KristofferC
Copy link
Collaborator

ForwardDiff has a custom number type D and claims that d::D + x should be evaluated as d + D(x).
Symbolics has a custom number type N and claims that n::N + x should be evaluated as n + N(x).

The question now is, what should d + n evaluate to? Without cooperation between ForwardDiff and Symbolics, there is no way to tell. Could maybe be added as an extension?

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