Hi all,
I was unable to create a type stable static matrix from an Array{T,2}
How make it type stable?
Tanks!

using StaticArrays
function Tmaker(kdim::IT) where IT<:Integer
T=reshape([isodd(x÷(2^y)) for y in 0:(kdim-1) for x in 0:(2^kdim-1)],:,kdim)
TT=SMatrix{size(T)...}(T)
end
@code_warntype Tmaker(3)

That’s interesting, I tried exactly the same value type trick, but tested with kdim=3 and saw that it wasn’t type-stable, because SMatrix{2^kdim, kdim} has a calculation:

@code_warntype Tmaker(Val(3))

Somehow 2^2 is caught as a special case that can be inferred?

Ah, the ^2 is special cased. Can hack around it with:

julia> Base.@pure mypow(x, y) = x^y
mypow (generic function with 1 method)
julia> function Tmaker(::Val{kdim}) where {kdim}
T=reshape([isodd(x÷(2^y)) for y in 0:(kdim-1) for x in 0:(2^kdim-1)],:,kdim)
TT=SMatrix{mypow(kdim, 2), kdim}(T)
end
Tmaker (generic function with 2 methods)
julia> @code_warntype Tmaker(Val(3))
Body::SArray{Tuple{9,3},Bool,2,27}

Nice. I’m terrified of @pure but was cobbling this together:

@generated twopow(::Val{n}) where n = 2^n
function Tmaker(valk::Val{kdim}) where {kdim}
M=reshape([isodd(x÷(2^y)) for y in 0:(kdim-1) for x in 0:(2^kdim-1)],:,kdim)
SMatrix{twopow(valk), kdim}(M)
end
@code_warntype Tmaker(Val(3))