# SVector from concat of subvectors

**URL:** <https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 24, 2024, 8:35pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215 "2024-01-24T20:35:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [January 24, 2024, 8:35pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/1 "2024-01-24T20:35:21Z")

</div>

Greetings.

I am seeking to replace

```julia
# generates local stiffness matrix 
function genLocStiffMat(element::Element)
    h = element.area
    e1 = element.e1
    e2 = element.e2
    Iloc = SVector(e1, e1, e2, e2) 
    Jloc = SVector(e1, e2, e1, e2) 
    Aloc = SVector(1/h, -1/h, -1/h, 1/h) 
    return Iloc, Jloc, Aloc
end

```

by a more elegant version that works for larger sets of e\_i values.

The version

```julia
function genLocStiffMat2(element::Element)
    h = element.area
    v = SVector(element.e1, element.e2)
    Iloc = SVector{4}([v[i] for j=1:2, i=1:2]) 
    Jloc = SVector{4}([v[i] for i=1:2, j=1:2])
    Aloc = SVector(1/h, -1/h, -1/h, 1/h) 
    return Iloc, Jloc, Aloc
end

```

allocates memory. Advice is appreciated.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 24, 2024, 9:01pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/2 "2024-01-24T21:01:42Z")

</div>

try using generator instead […]

```julia
function genLocStiffMat2(element)
    h = element.area
    v = SVector(element.e1, element.e2)
    Iloc = SVector{4}(v[i] for j=1:2, i=1:2) 
    Jloc = SVector{4}(v[i] for i=1:2, j=1:2)
    Aloc = SVector(1/h, -1/h, -1/h, 1/h) 
    return Iloc, Jloc, Aloc
end

```

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [January 24, 2024, 9:03pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/3 "2024-01-24T21:03:23Z")

</div>

I don’t have access to running Julia at the moment, but the key is that the statements `[v[i] for j=1:2, i=1:2]` and `[v[i] for i=1:2, j=1:2]` both allocate a 2x2 matrix. I think what you want is

```julia
Iloc = SVector{4}(v[i] for j=1:2 for i=1:2)

```

though I may have gotten my is and js backwards.

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [January 24, 2024, 9:07pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/4 "2024-01-24T21:07:17Z")

</div>

Hurray! What is “generator” is this context?

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [January 24, 2024, 9:09pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/5 "2024-01-24T21:09:54Z")

</div>

Yup! That does the trick. I would like to understand the underlying principle. Why does […] allocate memory? How is it avoided by the suggestion you provide? Sincere thanks.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 24, 2024, 9:10pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/6 "2024-01-24T21:10:06Z")

</div>

Comprehensions can also be written without the enclosing square brackets, producing an object known as a [Generator](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions-1)

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [January 24, 2024, 9:11pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/7 "2024-01-24T21:11:12Z")

</div>

Hurray again! Thx for bearing with me.

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [January 24, 2024, 9:14pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/8 "2024-01-24T21:14:31Z")

</div>

`[...]` is julias syntax for creating an `Array`, which is always (or close enough to always, there may be some exceptions, I’m not an expert) heap allocated (reported as allocations by `@time`). Thus, your original expression first created an `Array` and then `SVector` used this allocated `Array` to construct the `SVector`.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [January 24, 2024, 9:18pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/9 "2024-01-24T21:18:51Z")

</div>

Playing in the REPL should be helpful:

```julia-repl
julia> [i for j=1:2, i=1:2]
2×2 Matrix{Int64}:
 1 2
 1 2

julia> isbits(ans)
false

julia> (i for j=1:2, i=1:2)
Base.Generator{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}, var"#3#4"}(var"#3#4"(), Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:2, 1:2)))

julia> isbits(ans)
true

```

I’d also note that `Iterators.map` is an alternative to using the generator syntax sugar:

```julia-repl
julia> a = (i for j=1:2, i=1:2)
Base.Generator{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}, var"#1#2"}(var"#1#2"(), Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:2, 1:2)))

julia> b = Iterators.map(last, Iterators.product(1:2, 1:2))
Base.Generator{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}, typeof(last)}(last, Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:2, 1:2)))

julia> a == b
false

julia> collect(a) == collect(b)
true

```

(TBH I’m not sure why `a == b` returns `false`? EDIT: `Base.Generator` doesn’t overload `==`, so they get compared by `===`. Bug? Reported an issue on the Github: [equality for iterators · Issue #53046 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/53046))

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 24, 2024, 10:21pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/10 "2024-01-24T22:21:46Z")

</div>

a shape ALMOST the same as `a=...`

```julia
julia> i=1:2
1:2

julia> j=1:2
1:2

julia> c=Base.Generator((j,i)->i,Base.product(j,i))
Base.Generator{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}, var"#41#42"}(var"#41#42"(), 
Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:2, 1:2)))

julia> a = (i for j=1:2, i=1:2)
Base.Generator{Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}, var"#43#44"}(var"#43#44"(), 
Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:2, 1:2)))

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 24, 2024, 11:28pm UTC](https://discourse.julialang.org/t/svector-from-concat-of-subvectors/109215/11 "2024-01-24T23:28:05Z")

</div>

There is also the `@SVector` convenience macro:

```julia
Iloc = @SVector [e1, e1, e2, e2]
Jloc = @SVector [e1, e2, e1, e2]
Aloc = @SVector [1/h, -1/h, -1/h, 1/h]

```

which doesn’t allocate and looks quite clear.
