Hi,
I have observed some very strange behaviour using Symbolics.jl that I’m trying to understand. The behaviour of expressions in a dictionary, one which I might pass to Symbolics.substitute()
, changes after I println()
the dictionary. I have created a MWE that demonstrates the behaviour that is loosely based on my desired application (Reynolds decomposition of state variables):
using Symbolics
function ReynoldsDecompose(StateVars)
DecompDict = Dict()
for Var = StateVars
SymRep = Symbolics.tosymbol(Var, escape = false)
# Generate variables for the mean and disturbance parts
DecompVars = eval(Meta.parse("@variables $(SymRep)_m $(SymRep)_p"))
DecompDict[Var] = (DecompVars[1] + DecompVars[2]).val
end
return DecompDict()
end
function InspectRDDict(ReynoldsDecompDict)
for key = keys(ReynoldsDecompDict)
println(ReynoldsDecompDict[key].arguments) # Any[]
println(ReynoldsDecompDict[key]) # $key_m + $key_p
println(ReynoldsDecompDict[key].arguments) # Any[$key_m, $key_p]
end
end
@variables u v
InspectRDDict(ReynoldsDecompose([u, v]))
The act of printing the value seems to change the value, which sounds absurd so I must be missing something here. I suspect I am not understanding the scoping of Symbolics variables, because the same does not occur if I execute the for loop within the global scope, rather than in the function scope.
Can anyone explain what exactly I’m missing here?