JuliaDiff / JuliaDiff/ForwardDiff.jl

Symbolics.jl compatibility

Open
#674 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example using ForwardDiff.derivative, Symbolics.jl, Num, and ForwardDiff.Dual, then inspect the conflicting arithmetic methods shown in the error from ForwardDiff and SymbolicUtils. Determine whether the compatibility change belongs in ForwardDiff.jl or Symbolics.jl, and verify that the basic arithmetic operations no longer produce an ambiguous-method error.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.