`@variables` and `@parameters` macros not working on arrays

I have Julia installed on two different computers and the code executes differently on each one despite having the same versions of VSCode, Julia, and installed packages (everything is the most recent release version).

The basic example from the Package Readme should work like this:

julia> @variables x[1:3];
julia> x
3-element Array{Operation,1}:

However, using one of the computers, it returns:

julia> @variables x[1:3];
julia> x

where x is a 1-element Vector{Symbolics.Arr{Num, 1}}. Both are using Windows 10 and I’m using VS Code to run the script. The same thing happens for the @variables macro.

I understand that this is quite a vague question, I’m just not sure where I should be looking to find out what the problem is? Any pointers would be appreciated.


You could check

using Symbolics
@macroexpand @variables x[1:3]


That’s an old version of the documentation. The current docs are: Home · ModelingToolkit.jl

It does this and then x seems to not exist:

julia> using Symbolics
julia> @macroexpand @variables x[1:3]
    x = (identity)((Symbolics.wrap)((SymbolicUtils.setmetadata)((SymbolicUtils.setmetadata)((Sym){Array{Real, (length)((1:3,))}}(:x), Symbolics.ArrayShapeCtx, (1:3,)), Symbolics.VariableSource, (:variables, :x))))
julia> x
ERROR: UndefVarError: x not defined

Thanks for the link! I haven’t managed to find the relevant part in it (so far), do have any ideas?

Yes, @macroexpand only shows you what the macro is going to expand to without executing it.

I thought maybe you see differences then on the two systems involved…

1 Like

I see, sorry, I’m new at this!

I will have access to the other one again on Monday, I’ll run this then and see

Your second option is expected. If you see the first, it’s because you installed an old version. If you’re getting different things on different computers, it’s because you’ve installed different versions of the packages.

Check which version you installed using import Pkg; Pkg.status()

1 Like

On the second system, we get:

julia> using Symbolics

julia> @macroexpand @variables x[1:3]
    x = (identity)(map(Iterators.product(1:3)) do ind
                #= C:\Users\mholmes\.julia\packages\Symbolics\sITWZ\src\variable.jl:227 =#
                (Num)((SymbolicUtils.Sym){Real}(:x, ind...))

I see two possibilities: either your packages are different (see @odow’s remark regarding Pkg.status()) or something external is influencing the behavior of @variables. I tried to look into the source of Symbolics.jl but that is way over my head.

I also see

    x = (identity)((Symbolics.wrap)((SymbolicUtils.setmetadata)((SymbolicUtils.setmetadata)((SymbolicUtils.Sym){Array{Real, (length)((1:3,))}}(:x), Symbolics.ArrayShapeCtx, (1:3,)), Symbolics.VariableSource, (:variables, :x))))       

so the first seems to be the better version. Question to others: is there a way to find out where a macro is defined (similar to methods)?

Edit: one more hint that your second system uses an outdated version of Symbolics

Omitting some irrelevant packages from the output, Pkg.status() gives:

julia> Pkg.status()
      Status `C:\Users\mholmes\.julia\environments\v1.7\Project.toml`
  [961ee093] ModelingToolkit v5.16.0
  [d1185830] SymbolicUtils v0.11.0
  [0c5d862f] Symbolics v0.1.32

which, indeed, are outdated: current github versions seem to be v8.6.0, v0.19.7, and v4.4.1 respectively. However, using Pkg.update() doesn’t update anything - it seems to think that it’s up to date.

Is there a way that I can force it? Maybe an uninstall and reinstall? I don’t think it is a Julia version issue as I am using the most recent release:

julia> VERSION

Finally, do you have an idea why this newer version is this way? I rather liked having subscripts next to the state variables, differentiating between x₁, x₂, etc. The current version seems to be less featureful.

There is probably an older package installed holding the update back. You might want to try a new local environment?

1 Like

This did the trick! I now get the same result on both systems and I have Symbolics v4.4.1 when I run Pkg.status() in this new environment.

So, to prevent this happening again, I should try and find other installed package(s) to see what’s holding this system back? Or is it standard practice to use different environments for different projects to ensure that installed packages don’t cause problems with each other?

I’d say so.

OK, thanks for your help @goerch @odow