Flatten a tuple of Operations/Operation arrays

I’m using the @variables macro of the ModelingToolkit module:

(edit) n = 2
vars = @variables x y z[1:n]

which produces (x, y, Operation[z₁, z₂]).

I’m now using this tuple as parameter in the constructor of Contractor (IntervalConstraintProgramming.jl) that expects a tuple of Operations.

How can I flatten (x, y, Operation[z₁, z₂]) to (x, y, z₁, z₂)?
I’ve tried vcat, reduce(vcat), collect(flatten), without success.


Your code yields

julia> vars = @variables x y z[1:n]
(x, y, Num[z₁, z₂, z₃, z₄, z₅, z₆, z₇, z₈, z₉, z₁₀, z₁₁, z₁₂, z₁₃, z₁₄, z₁₅, z₁₆, z₁₇, z₁₈, z₁₉, z₂₀])

Did you mean n=2?

julia> n=2

julia> vars = @variables x y z[1:n]
(x, y, Num[z₁, z₂])

Also vcat works for me:

julia> vcat(vars...)
4-element Vector{Num}:

If everything fails, do it manually :smiley:

julia> x, y, ops = vars
(x, y, Num[z₁, z₂])

julia> newtuple = (x, y, ops...)
(x, y, z₁, z₂)
1 Like

Oh yeah my bad, I actually tried n = 2 but wanted to show that z can be large.
It works perfectly, thanks :slight_smile: Hadn’t tried with the splat.

1 Like