# Type instability with StaticArrays

**URL:** https://discourse.julialang.org/t/type-instability-with-staticarrays/20084
**Category:** New to Julia
**Tags:** question
**Created:** [January 25, 2019, 11:32am UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084 "2019-01-25T11:32:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bachrathyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bachrathyd/32/7545_2.png) [@bachrathyd](https://discourse.julialang.org/u/bachrathyd)
#### Post date: [January 25, 2019, 11:32am UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/1 "2019-01-25T11:32:01Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [January 25, 2019, 11:38am UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/2 "2019-01-25T11:38:53Z")

</div>

> [@bachrathyd](#):
>
> I was unable to create a type stable static matrix from an Array{T,2}

Yes, you are unable to do that. `Array{T,2}` has insufficient type information to be able to specify a fully typed static array at compile time.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 25, 2019, 11:39am UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/3 "2019-01-25T11:39:36Z")

</div>

This is inherently type unstable in that the type of the output depends on the value of the input.

You can give the input as a type and do something like

```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{2^kdim, kdim}(T)
end

```

```julia
julia> @code_warntype Tmaker(Val(2))
Body::SArray{Tuple{4,2},Bool,2,8}

```

or you just work around the type instability using a function barrier (see performance tips in the julia manual).

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [January 25, 2019, 11:53am UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/4 "2019-01-25T11:53:07Z")

</div>

> [@kristoffer.carlsson](#):
>
> julia\> @code\_warntype Tmaker(Val(2)) Body::SArray{Tuple{4,2},Bool,2,8}

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:

```julia
@code_warntype Tmaker(Val(3))

```

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

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 25, 2019, 12:01pm UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/5 "2019-01-25T12:01:41Z")

</div>

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

```julia
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}

```

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [January 25, 2019, 12:16pm UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/6 "2019-01-25T12:16:21Z")

</div>

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

```julia
@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))

```

---

<div class="post-metadata">

### Author: ![bachrathyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bachrathyd/32/7545_2.png) [@bachrathyd](https://discourse.julialang.org/u/bachrathyd)
#### Post date: [January 25, 2019, 1:27pm UTC](https://discourse.julialang.org/t/type-instability-with-staticarrays/20084/7 "2019-01-25T13:27:28Z")

</div>

Thank you for your fast help!
