Symbolics expand derivative throws an error

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’

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

Gives (Julia 1.11.2):

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).

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?

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.

1 Like

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

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.

1 Like

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