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

**URL:** <https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252>\
**Category:** Modelling & Simulations\
**Tags:** question, versions, modelingtoolkit\
**Created:** [April 9, 2022, 9:06am UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252 "2022-04-09T09:06:47Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 9, 2022, 9:06am UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/1 "2022-04-09T09:06:47Z")

</div>

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](https://docs.juliahub.com/ModelingToolkit/Qmdqu/2.0.0/) should work like this:

```julia
julia> @variables x[1:3];
julia> x
3-element Array{Operation,1}:
 x₁()
 x₂()
 x₃()

```

However, using one of the computers, it returns:

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

```

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.

Thanks

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 9, 2022, 9:28am UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/2 "2022-04-09T09:28:29Z")

</div>

> [@markjholmes](#):
>
> `@variables x[1:3];`

You could check

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

```

then?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 9, 2022, 10:03am UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/3 "2022-04-09T10:03:26Z")

</div>

That’s an old version of the documentation. The current docs are: [Home · ModelingToolkit.jl](https://mtk.sciml.ai/stable/)

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 9, 2022, 9:22pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/4 "2022-04-09T21:22:07Z")

</div>

It does this and then `x` seems to not exist:

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

```

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 9, 2022, 9:34pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/5 "2022-04-09T21:34:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 9, 2022, 10:06pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/6 "2022-04-09T22:06:59Z")

</div>

> [@markjholmes](#):
>
> It does this and then `x` seems to not exist:

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

> [@markjholmes](#):
>
> 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)

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

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 9, 2022, 10:20pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/7 "2022-04-09T22:20:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 9, 2022, 11:26pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/8 "2022-04-09T23:26:08Z")

</div>

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()`

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 11, 2022, 11:48am UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/9 "2022-04-11T11:48:33Z")

</div>

On the second system, we get:

```julia
julia> using Symbolics

julia> @macroexpand @variables x[1:3]
quote
    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...))
            end)
    [x]
end

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 11, 2022, 12:01pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/10 "2022-04-11T12:01:02Z")

</div>

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

```julia
quote
    x = (identity)((Symbolics.wrap)((SymbolicUtils.setmetadata)((SymbolicUtils.setmetadata)((SymbolicUtils.Sym){Array{Real, (length)((1:3,))}}(:x), Symbolics.ArrayShapeCtx, (1:3,)), Symbolics.VariableSource, (:variables, :x))))       
    [x]
end

```

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](https://github.com/JuliaSymbolics/Symbolics.jl/blob/b7cae533e0a2aae8d7e227b2e0db4e25e6cb8d09/src/variable.jl#L227) that your second system uses an outdated version of `Symbolics`

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 11, 2022, 12:53pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/11 "2022-04-11T12:53:54Z")

</div>

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

```julia
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`](https://github.com/SciML/ModelingToolkit.jl), [`v0.19.7`](https://github.com/JuliaSymbolics/SymbolicUtils.jl), and [`v4.4.1`](https://github.com/JuliaSymbolics/Symbolics.jl) 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
julia> VERSION
v"1.7.2"

```

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.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 11, 2022, 1:01pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/12 "2022-04-11T13:01:23Z")

</div>

> [@markjholmes](#):
>
> However, using `Pkg.update()` doesn’t update anything - it seems to think that it’s up to date

There is probably an older package installed holding the update back. You might want to try a new [local environment](https://pkgdocs.julialang.org/v1/environments/)?

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 11, 2022, 1:23pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/13 "2022-04-11T13:23:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 11, 2022, 1:25pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/14 "2022-04-11T13:25:19Z")

</div>

> [@markjholmes](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![markjholmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markjholmes/32/34139_2.png) [@markjholmes](https://discourse.julialang.org/u/markjholmes)\
**Post date:** [April 11, 2022, 1:51pm UTC](https://discourse.julialang.org/t/variables-and-parameters-macros-not-working-on-arrays/79252/15 "2022-04-11T13:51:30Z")

</div>

OK, thanks for your help @goerch @odow
