Using ForwardDiff and Symbolics, with arithmetic operations

I have a somewhat complicated function I’d like to find the analytical jacobian for. When using ForwardDiff and Symbolics and defining a function where both the input variables and certain parameters are symbolic, I get an error saying it doesn’t know how to multiply a Num with a Dual containing Nums. MWE:

julia> using Symbolics, ForwardDiff
julia> @variables a, x;
julia> f = x -> a*x;

julia> Differential(x)(f(x)) |> expand_derivatives # Expected result from ForwardDiff

julia> ForwardDiff.derivative(f, x)
ERROR: MethodError: *(::Num, ::ForwardDiff.Dual{ForwardDiff.Tag{var"#5#6", Num}, Num, 1}) is ambiguous.

  *(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

 [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 happen with all basic arithmetic operations. I feel like there’s something fundamental here that I don’t understand. Are the packages not supposed to be usable together this way?

In theory this can be fixed. It’s worth an issue.

Hi, I ran into this problem as well and opened an issue here.