StaticArrays.jl
StaticArrays.jl copied to clipboard
Can a static array be repeated into a static matrix using a similar repeat function in Julia?
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
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.
Thank you! Luo
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.