# ModelingToolkit: Different ways to access array of variables

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-different-ways-to-access-array-of-variables/59939>\
**Category:** General Usage\
**Tags:** modelingtoolkit\
**Created:** [April 24, 2021, 5:44pm UTC](https://discourse.julialang.org/t/modelingtoolkit-different-ways-to-access-array-of-variables/59939 "2021-04-24T17:44:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jairorua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jairorua/32/24109_2.png) [@jairorua](https://discourse.julialang.org/u/jairorua)\
**Post date:** [April 24, 2021, 5:44pm UTC](https://discourse.julialang.org/t/modelingtoolkit-different-ways-to-access-array-of-variables/59939/1 "2021-04-24T17:44:01Z")

</div>

Hello,

Let’s consider a general function that aims at being a reusable pin/connector of arrays of variables.

```julia
using ModelingToolkit

function arrayConnection(;name, discretizations = 4)

	@parameters t
	@variables x[1:discretizations](t)

	ODESystem(Equation[], t, x, []; name=name)

end

```

Define two instances of this general connection:

```julia
@named connection1 = arrayConnection()
@named connection2 = arrayConnection()

```

Because of the issue mentioned in [https://github.com/SciML/ModelingToolkit.jl/issues/908](https://github.com/SciML/ModelingToolkit.jl/issues/908), it is not possible to access the variables directly with their name. One needs to use either unicode variables or states. However, the output is different. For instance,

```julia
eqs1 = [connection1.states[1] ~ connection2.states[1]]

```

leads to:

```julia
1-element Array{Equation,1}:
 x₁(t) ~ x₁(t)

```

which is a trivial solution, while:

```julia
eqs2 = [connection1.x₁ ~ connection2.x₁]

```

generates the equality one could expect:

```julia
1-element Array{Equation,1}:
 connection1₊x₁(t) ~ connection2₊x₁(t)

```

At this point, I have two questions:

1. I guess the answer is yes, but just to confirm. Are these two equations actually different within ModelingToolkit, i.e. eqs1 != eqs2?

2. Assume one wants a general function to connect two, or more, of these connections, e.g.:

```julia
function connect(ps...)

	discretizations = Int(length(ps[1].states) )

	equations = [
		         [0 ~ sum(p -> p.states[i], ps) for i in 1:discretizations]
		        ]

	return equations

end

```

This code would lead to trivial equalities as those as in eqs1, which do not add any information to the system and do not actually connect both connectors. Is there a way to loop over the unicode variables so the subscript is `i` and the connections resembles that in eqs2?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 29, 2021, 3:19pm UTC](https://discourse.julialang.org/t/modelingtoolkit-different-ways-to-access-array-of-variables/59939/2 "2021-04-29T15:19:14Z")

</div>

> [@jairorua](#):
>
> I guess the answer is yes, but just to confirm. Are these two equations actually different within ModelingToolkit, i.e. eqs1 != eqs2?

yes, one is namespaced while the other isn’t.

> [@jairorua](#):
>
> Is there a way to loop over the unicode variables so the subscript is `i` and the connections resembles that in eqs2?

Array variables are being overhauled.

> <https://github.com/JuliaSymbolics/SymbolicUtils.jl/pull/123>
>
> builds on #215

> <https://github.com/JuliaSymbolics/Symbolics.jl/pull/197>
>
> Moving this from SymbolicUtils.jl
> 
> \`ArrayOp\` is the new term type which denote…s a symbolic array object. Alternatively you can use \`Term{\<:AbstractArray}\` with shape stored in \`ArrayShapeCtx\`. \`shape\` on either will return a tuple of index ranges or the \`Unknown()\` object to denote it's not known. Array introspection such as \`eltype\`, \`ndims\`, \`size\`, \`length\` are defined on both.
> 
> Operations such as \`broadcast\`, \`map\`, \`mapreduce\`, \`\*\` return \`ArrayOp\` objects which will automatically know the output eltype and size (if input size is known) by propagating the inputs' attributes. \`ArrayOp\` also has a \`.term\` field which is optional, but stores the operation which resulted in the \`ArrayOp\`. E.g. \`A\*b\`
> 
> This is the first part of the PR, it tentatively needs the following before merging. Differentiation will be done in a later PR.
> 
> TODO
> \- \[x\] \`@variables\` for Arrays.
> \- \[x\] Codegen
> \- \[x\] Fuzz test \`adjoint\` and \`\*\`
> \- \[\] Fuzz test \`mapreduce\`
> \- \[\] Fuzz test \`broadcast\`
> \- \[\] Fuzz test with codegen and numerical input
> \- \[\] Add LinearAlgebra methods
> \- \[x\] Wrapper type for Arrays
> \- \[\] Documentation

> <https://github.com/SciML/ModelingToolkit.jl/pull/989>
>
> Running MTK off of https://github.com/JuliaSymbolics/Symbolics.jl/pull/197

This whole issue will go away.

---

<div class="post-metadata">

**Author:** ![jairorua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jairorua/32/24109_2.png) [@jairorua](https://discourse.julialang.org/u/jairorua)\
**Post date:** [July 17, 2021, 9:06am UTC](https://discourse.julialang.org/t/modelingtoolkit-different-ways-to-access-array-of-variables/59939/3 "2021-07-17T09:06:20Z")

</div>

I see that these branches are merged now @ChrisRackauckas.

You also mentioned in this link, [Sub plus (₊) instead of dot in the dot notation in equations displayed by ModelingToolkit - #4 by zdenek\_hurak](https://discourse.julialang.org/t/sub-plus-instead-of-dot-in-the-dot-notation-in-equations-displayed-by-modelingtoolkit/61113/4), that these changed were going to be released ASAP. Are these changes already out?

Is there a way in Julia to get notifications of releases/changes in particular packages?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 17, 2021, 10:57am UTC](https://discourse.julialang.org/t/modelingtoolkit-different-ways-to-access-array-of-variables/59939/4 "2021-07-17T10:57:46Z")

</div>

Yes, the symbolic array changes are already out in the wild.
