StaticArrays.jl icon indicating copy to clipboard operation
StaticArrays.jl copied to clipboard

Can a static array be repeated into a static matrix using a similar repeat function in Julia?

Open Sonric opened this issue 4 years ago • 3 comments

My input is an array, and I want to use the static array to extend it into a large matrix to reduce the calculation time. However, after using the repeat() function in Julia, it changes back to the usual dynamic matrix, and there is no similar function after checking the document, is there any way to achieve this function?

Svals = @SVector [i for i = 1:1000]
repeat(Svals,1,50)

And I have tried the following code, but it didn't work.

SMatrix{length(vals),50}(repeat(vals,1,50))

Thanks! Luo

Sonric avatar Oct 15 '21 07:10 Sonric

repeat(Svals,1,50) returning a dynamic array is actually quite reasonable because for arrays with more than roughly a few hundred elements static arrays are almost always slower than dynamic ones. SMatrix{length(Svals),50}(repeat(Svals,1,50)) sort of works but takes a long time to even print for the first time.

mateuszbaran avatar Oct 15 '21 07:10 mateuszbaran

Thank you! Luo

Sonric avatar Oct 15 '21 08:10 Sonric

Disregarding that it would not be a good idea to work with such large matrices, a simple implementation of this could e.g. be:

function repeat(v::SVector{N,T}, vM::Val{M}) where {N,M,T}
   SMatrix{N,M,T,N*M}(ntuple(i -> v[mod1(i, N)], Val(N*M)))
end

But if a type-stable repeat should be added, it should probably be able to take more general array and dimension arguments.

thchr avatar Oct 17 '21 00:10 thchr