# SMatrix static slice

**URL:** <https://discourse.julialang.org/t/smatrix-static-slice/107223>\
**Category:** General Usage\
**Tags:** question, staticarrays\
**Created:** [December 6, 2023, 3:58pm UTC](https://discourse.julialang.org/t/smatrix-static-slice/107223 "2023-12-06T15:58:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 6, 2023, 3:58pm UTC](https://discourse.julialang.org/t/smatrix-static-slice/107223/1 "2023-12-06T15:58:43Z")

</div>

Is `StaticArrays.SUnitRange` (which is internal, cf [StaticArrays.jl#474](https://github.com/JuliaArrays/StaticArrays.jl/pull/474)) the only way to get static slices for dimensions known at compile time?

MWE:

```julia
julia> using StaticArrays

julia> A = one(SMatrix{4,4})
4×4 SMatrix{4, 4, Float64, 16} with indices SOneTo(4)×SOneTo(4):
 1.0 0.0 0.0 0.0
 0.0 1.0 0.0 0.0
 0.0 0.0 1.0 0.0
 0.0 0.0 0.0 1.0

julia> s = StaticArrays.SUnitRange(1,2)
SUnitRange(1,2)

julia> A[s, s]
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 1.0 0.0
 0.0 1.0

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 6, 2023, 4:06pm UTC](https://discourse.julialang.org/t/smatrix-static-slice/107223/2 "2023-12-06T16:06:22Z")

</div>

Not always optimal, but one option you have is to just make `SVector`s of the range elements:

```julia-repl
julia> srange(start, stop) = SVector(ntuple(i -> i+start-1, stop-start+1));

julia> s = srange(1, 2)
2-element SVector{2, Int64} with indices SOneTo(2):
 1
 2

julia> A[s, s]
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 1.0 0.0
 0.0 1.0

```

This is usually a bad idea, but for the sorts of small sizes typical to StaticArrays.jl, it might be fine.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [December 6, 2023, 4:14pm UTC](https://discourse.julialang.org/t/smatrix-static-slice/107223/3 "2023-12-06T16:14:42Z")

</div>

You may use a `SizedArray` wrapper at present:

```julia
julia> s = SizedVector{2}(1:2)
2-element SizedVector{2, Int64, UnitRange{Int64}} with indices SOneTo(2):
 1
 2

julia> A[s,s]
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 1.0 0.0
 0.0 1.0

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 6, 2023, 6:17pm UTC](https://discourse.julialang.org/t/smatrix-static-slice/107223/4 "2023-12-06T18:17:27Z")

</div>

Is there something wrong with this solution (I’m asking because that’s what I would have done)?

```julia
julia> function test(a, ::Val{N}) where {N}
           b = SMatrix{N,N}(@view(a[1:N, 1:N]))
           return b
       end
test (generic function with 2 methods)

julia> @btime test($A, Val(2))
  1.858 ns (0 allocations: 0 bytes)
2×2 SMatrix{2, 2, Float64, 4} with indices SOneTo(2)×SOneTo(2):
 1.0 0.0
 0.0 1.0

julia> @btime test($A, Val(3))
  2.121 ns (0 allocations: 0 bytes)
3×3 SMatrix{3, 3, Float64, 9} with indices SOneTo(3)×SOneTo(3):
 1.0 0.0 0.0
 0.0 1.0 0.0
 0.0 0.0 1.0

```
