How to programmatically create symbolic variable names in Symbolics.jl

Essentially, I am looping over indices and I need to use those indices to create symbolic variable names.

A simplified version of what I am trying to do as shown using SymPy:

vars=[]
for i=1:5
   for j=2:7
       push!(vars,symbols("x$i$j"))
   end
end

In actuality, the indices are all coming from a list so it is not straightforward to program using for loops.

The only method for defining symbolic variables I can find for Symbolics.jl is

@variables x1

Does anyone have suggestions for how I can incorporate the x$i symbolic variable construction in Symbolics? Thank you.

@variables $x

Can you elaborate? Is x supposed to be assigned as something beforehand?

yes it’s interpolating in a name.

I’ve tried different formulations but can’t seem to figure out what the requirements on x are. Can you show an example of how x is defined before @variables $x?

x = :myvar

I have struggled to add a time dependency in this way. Is this supported?


julia> @variables t x(t)
2-element Vector{Num}:
    t
 x(t)

julia> ẋ = Symbol(:Δ, Symbol(Symbolics.value(x)))
Symbol("Δx(t)")

julia> @variables $ẋ
1-element Vector{Num}:
 var"Δx(t)"

julia> Δx
ERROR: UndefVarError: `Δx` not defined

I can accomplish this by leaving out the time dependency, and adding it back later…

julia> ẋ = :Δx
:Δx

julia> @eval @variables $(ẋ)(t)
1-element Vector{Num}:
 Δx(t)

julia> Δx
Δx(t)

Oh, that’s not intended. Open an issue.