# How can I implement a vector of StaticArrays as a variable in MTK?

**URL:** https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit, staticarrays, mtk
**Created:** [January 9, 2026, 11:24am UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962 "2026-01-09T11:24:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [January 9, 2026, 11:24am UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/1 "2026-01-09T11:24:15Z")

</div>

Hi everyone. After trying for weeks I would like to ask the community if what I’m trying to do is possible or whether I have arrived to a dead end.

I’ve reduced it to a MWE. The system I want to solve in ModelingToolkit has two array variables, `r` and `H`. Each element of both arrays is a `SMatrix` from the `StaticArrays` package. First I define some vectors and initialize `r` to the value `r0`:

```julia-auto
using ModelingToolkit, StaticArrays

@independent_variables x
D = Differential(x)

# Define some vectors and matrices
n = 5
ys = @SVector [0.26, 1.41, 3.6, 7.09, 12.64]
u = @SMatrix [0.93 0.31; -0.14 + 0.02im 0.81]

# Initialize variable
r0 = [SHermitianCompact{2, ComplexF64, 3}([exp(-ys[i]), 0, exp(-ys[i])]) for i in eachindex(ys)]

vars = @variables begin
    r(x)[1:n] = r0
    H(x)[1:n]
end

```

Next, I define a function called `Hvar`. This function takes several parameters, makes basic operations with matrices and returns the value of what the `H` variable should have. Notice that I want element-wise multiplication in the return value. I want each `SMatrix` of `H` be multiplied by each `SMatrix` of `r`.

```julia-auto
# This computes the value of H as a function of other parameters
function Hvar(r, x, ys, U)
    H = [x^2 * U / ys[i] for i in eachindex(ys)]

    return H .* r
end

eqs = [
    H ~ Hvar(r, x, ys, u)
    D(r) ~ H
]

_sys = System(eqs, x, vars, []; name = :foo)
sys = mtkcompile(_sys)

```

When I try to compile the system with `mtkcompile` I get the following error:

```julia-auto
ERROR: TypeError: in typeassert, expected Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}}, got a value of type SizedVector{5, SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}, Vector{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}}}

```

I’ve tried converting the variables to floats with the `.value` field and then create a `Num` array back but it doesn’t work either.

Is there a way to define a function that takes a vector of `SMatrix` and other symbolic variables as input, do some work and return another vector of `SMatrix`? Note that my functions are general and outside of ModelingToolkit, they work without problems normal Julia arrays of with StaticArrays.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 9, 2026, 1:13pm UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/2 "2026-01-09T13:13:40Z")

</div>

Did you try using `@register_symbolic`, see: [Function Registration and Tracing · Symbolics.jl](https://symbolics.juliasymbolics.org/dev/manual/functions/#Symbolics.@register_symbolic) ?

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [January 9, 2026, 2:38pm UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/3 "2026-01-09T14:38:14Z")

</div>

Yes, I tried with either `@register_symbolic` or `@register_array_symbolic` and the error persisted.

For example:

```julia-auto
@register_array_symbolic Hvar(r::AbstractVector, x, ys, U) begin
    size = size(r)
    eltype=eltype(r)
end

```

produces

```julia-auto
ERROR: TypeError: in typeassert, expected Array{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}}, got a value of type SizedVector{5, SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}, Vector{SymbolicUtils.BasicSymbolicImpl.var"typeof(BasicSymbolicImpl)"{SymReal}}}

```

---

<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: [January 11, 2026, 1:34am UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/4 "2026-01-11T01:34:23Z")

</div>

ModelingToolkit takes the semantics of the equations but not the implementation. In other words, it has the ability (in some branches coming soon) to use static arrays as needed for internal array operations when it makes sense, so it does not have a representation to force it

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [January 11, 2026, 11:40am UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/5 "2026-01-11T11:40:22Z")

</div>

I see, thanks! Should I wait for that to be implemented (if it won’t take long) or rewrite my problem to find a compatible way for now?

---

<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: [January 11, 2026, 12:37pm UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/6 "2026-01-11T12:37:51Z")

</div>

There has never been a way for you to give this information to the compiler reliably

---

<div class="post-metadata">

### Author: ![pmc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pmc4/32/211593_2.png) [@pmc4](https://discourse.julialang.org/u/pmc4)
#### Post date: [January 11, 2026, 5:13pm UTC](https://discourse.julialang.org/t/how-can-i-implement-a-vector-of-staticarrays-as-a-variable-in-mtk/134962/7 "2026-01-11T17:13:01Z")

</div>

Thanks, Chris! Maybe I can divide that submodel into more smaller models and connect them later somehow.
