# Symbolics expand derivative throws an error

**URL:** <https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868>\
**Category:** New to Julia\
**Tags:** symbolics\
**Created:** [December 15, 2024, 5:34pm UTC](https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868 "2024-12-15T17:34:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)\
**Post date:** [December 15, 2024, 5:34pm UTC](https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868/1 "2024-12-15T17:34:22Z")

</div>

How do I fix the following piece of code?. This seems like an issue to me, however I’m somewhat new to Julia and I’m not sure if i’m missing something or if this is a real ‘bug’

```julia
@variables r
g = Symbolics.variable("f", T=Symbolics.FnType)(Symbolics.variable("x"), r)
@variables η($g)
expand_derivatives(Differential(x)(η))

```

Gives (Julia 1.11.2):

```julia
ERROR: MethodError: no method matching *(::SymbolicUtils.BasicSymbolic{Real}, ::SymbolicUtils.BasicSymbolic{Any})
The function `*` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:596
  *(::ChainRulesCore.NotImplemented, ::Any)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/6Pucz/src/tangent_arithmetic.jl:37
  *(::Any, ::Differential)
   @ Symbolics ~/.julia/packages/Symbolics/YbNrd/src/diff.jl:52
  ...

Stacktrace:
  [1] (::Symbolics.var"#272#274"{Differential})(a::SymbolicUtils.BasicSymbolic{Any})
    @ Symbolics ~/.julia/packages/Symbolics/YbNrd/src/diff.jl:205
  [2] MappingRF
    @ ./reduce.jl:100 [inlined]
  [3] _foldl_impl(op::Base.MappingRF{Symbolics.var"#272#274"{…}, Base.BottomRF{…}}, init::Int64, itr::Vector{Any})
    @ Base ./reduce.jl:58
  [4] foldl_impl
    @ ./reduce.jl:48 [inlined]
  [5] mapfoldl_impl(f::Symbolics.var"#272#274"{Differential}, op::typeof(Base.add_sum), nt::Int64, itr::Vector{Any})
    @ Base ./reduce.jl:44
  [6] _mapreduce_dim(f::Function, op::Function, nt::Int64, A::Vector{Any}, ::Colon)
    @ Base ./reducedim.jl:334
  [7] mapreduce
    @ ./reducedim.jl:329 [inlined]
  [8] _sum
    @ ./reducedim.jl:987 [inlined]
  [9] #sum#934
    @ ./reducedim.jl:983 [inlined]
 [10] expand_derivatives(O::SymbolicUtils.BasicSymbolic{Real}, simplify::Bool; occurrences::Nothing)
    @ Symbolics ~/.julia/packages/Symbolics/YbNrd/src/diff.jl:204
 [11] expand_derivatives(n::Num, simplify::Bool; occurrences::Nothing)
    @ Symbolics ~/.julia/packages/Symbolics/YbNrd/src/diff.jl:299
 [12] expand_derivatives
    @ ~/.julia/packages/Symbolics/YbNrd/src/diff.jl:298 [inlined]
 [13] expand_derivatives(n::Num)
    @ Symbolics ~/.julia/packages/Symbolics/YbNrd/src/diff.jl:298
 [14] top-level scope
    @ REPL[88]:1

```

I know I could just do it with`@variables x r η(x, r)`, however in my use case i don’t have explicit access to the symbol of the input variable (for example, it’s a PDE that was parsed from the user and i need to create new symbolic functions whose input matches those symbolic variables given at runtime by the user).

---

<div class="post-metadata">

**Author:** ![Fe-r-oz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fe-r-oz/32/216415_2.png) [@Fe-r-oz](https://discourse.julialang.org/u/Fe-r-oz)\
**Post date:** [December 15, 2024, 6:04pm UTC](https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868/2 "2024-12-15T18:04:55Z")

</div>

> [@elvispy](#):
>
> How do I fix the following piece of code?.

```julia
julia> using Symbolics

julia> @variables x r;

julia> η = Symbolics.variable("η", T=Symbolics.FnType)(x, r);

julia> expr = Differential(x)(η);

julia> expand_derivatives(expr)
Differential(x)(η(x, r))

```

Does it meet your objective?

---

<div class="post-metadata">

**Author:** ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)\
**Post date:** [December 15, 2024, 6:42pm UTC](https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868/3 "2024-12-15T18:42:13Z")

</div>

No, this does not work for me because I don’t have access to the variables x and r, as I just mentioned in my comment at the end of my post. Thanks for your help, regardless. I appreciate the time to read my question.

---

<div class="post-metadata">

**Author:** ![elvispy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elvispy/32/45602_2.png) [@elvispy](https://discourse.julialang.org/u/elvispy)\
**Post date:** [December 15, 2024, 6:44pm UTC](https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868/4 "2024-12-15T18:44:59Z")

</div>

I figured it out. I need to interpolate the variable at runtime:

```julia
function createSymFunction(x::Num, r::Num)
    @variables f($x, $r)
    return f
end

```

This way, the user can generate symbolic functions of variables that are defined at runtime.

---

<div class="post-metadata">

**Author:** ![Fe-r-oz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fe-r-oz/32/216415_2.png) [@Fe-r-oz](https://discourse.julialang.org/u/Fe-r-oz)\
**Post date:** [December 15, 2024, 6:52pm UTC](https://discourse.julialang.org/t/symbolics-expand-derivative-throws-an-error/123868/5 "2024-12-15T18:52:00Z")

</div>

Thanks for your explanation. I am glad you figured it out!
