# Using ForwardDiff and Symbolics, with arithmetic operations

**URL:** https://discourse.julialang.org/t/using-forwarddiff-and-symbolics-with-arithmetic-operations/106530
**Category:** General Usage
**Tags:** question, package, forwarddiff, symbolics
**Created:** [November 21, 2023, 2:14pm UTC](https://discourse.julialang.org/t/using-forwarddiff-and-symbolics-with-arithmetic-operations/106530 "2023-11-21T14:14:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![polhager](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/polhager/32/37340_2.png) [@polhager](https://discourse.julialang.org/u/polhager)
#### Post date: [November 21, 2023, 2:14pm UTC](https://discourse.julialang.org/t/using-forwarddiff-and-symbolics-with-arithmetic-operations/106530/1 "2023-11-21T14:14:15Z")

</div>

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 `Num`s. MWE:

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

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

julia> ForwardDiff.derivative(f, x)
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 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?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 21, 2023, 3:35pm UTC](https://discourse.julialang.org/t/using-forwarddiff-and-symbolics-with-arithmetic-operations/106530/2 "2023-11-21T15:35:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![asterycs](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@asterycs](https://discourse.julialang.org/u/asterycs)
#### Post date: [January 2, 2024, 9:32am UTC](https://discourse.julialang.org/t/using-forwarddiff-and-symbolics-with-arithmetic-operations/106530/3 "2024-01-02T09:32:49Z")

</div>

Hi, I ran into this problem as well and opened an issue [here](https://github.com/JuliaSymbolics/Symbolics.jl/issues/1032).
