# Type instability in array concatenation

**URL:** <https://discourse.julialang.org/t/type-instability-in-array-concatenation/32951>\
**Category:** Performance\
**Created:** [January 4, 2020, 12:59am UTC](https://discourse.julialang.org/t/type-instability-in-array-concatenation/32951 "2020-01-04T00:59:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fleecy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fleecy/32/11210_2.png) [@fleecy](https://discourse.julialang.org/u/fleecy)\
**Post date:** [January 4, 2020, 12:59am UTC](https://discourse.julialang.org/t/type-instability-in-array-concatenation/32951/1 "2020-01-04T00:59:02Z")

</div>

I have some simple code to make a left-quaternion multiplication matrix:

```julia
function L_q(q::Vector{T}) where {T<:Real}
    return [q[1] -q[2:4]'; q[2:4] q[1] * I + SkewSymmetricMatrix(q[2:4])]
end

```

But the problem is that the hvcat operation makes the return type unstable. Here’s the output from running

```julia
function L_q(q::Vector{T}) where {T<:Real}
    return [q[1] -q[2:4]'; q[2:4] q[1] * I + SkewSymmetricMatrix(q[2:4])]
end
function SkewSymmetricMatrix(q::Vector{T}) where {T<:Real}
    return [T(0) -q[3] q[2]; q[3] T(0) -q[1]; -q[2] q[1] T(0)]
end

q = [1.0, 0.0, 0.0, 0.0]
@code_warntype L_q(q)

```

The output:

```julia
Variables
  #self#::Core.Compiler.Const(L_q, false)
  q::Array{Float64,1}

Body::Any
1 ─ %1 = Core.tuple(2, 2)::Core.Compiler.Const((2, 2), false)
│ %2 = Base.getindex(q, 1)::Float64
│ %3 = (2:4)::Core.Compiler.Const(2:4, false)
│ %4 = Base.getindex(q, %3)::Array{Float64,1}
│ %5 = Base.adjoint(%4)::Adjoint{Float64,Array{Float64,1}}
│ %6 = -%5::Adjoint{Float64,Array{Float64,1}}
│ %7 = (2:4)::Core.Compiler.Const(2:4, false)
│ %8 = Base.getindex(q, %7)::Array{Float64,1}
│ %9 = Base.getindex(q, 1)::Float64
│ %10 = (%9 * Main.I)::UniformScaling{Float64}
│ %11 = (2:4)::Core.Compiler.Const(2:4, false)
│ %12 = Base.getindex(q, %11)::Array{Float64,1}
│ %13 = Main.SkewSymmetricMatrix(%12)::Array{Float64,2}
│ %14 = (%10 + %13)::Array{Float64,2}
│ %15 = Base.hvcat(%1, %2, %6, %8, %14)::Any
└── return %15

```

There’s an `::Any` on line %15. Is the problem because it’s concatenating an adjoint with other arrays? I read that I could use copy(…) but just doing `copy(q[2:4]')` didn’t work.

Would appreciate any ideas to fix this. Thanks!

---

<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:** [January 4, 2020, 8:18am UTC](https://discourse.julialang.org/t/type-instability-in-array-concatenation/32951/2 "2020-01-04T08:18:37Z")

</div>

Please provide an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 4, 2020, 9:59am UTC](https://discourse.julialang.org/t/type-instability-in-array-concatenation/32951/3 "2020-01-04T09:59:54Z")

</div>

Last time I looked at the implementation of `hvcat` (mind you, it was years ago, so things may have changed) it was wonderfully general but inherently type unstable. A better chance for type stability would be to use a typed `hvcat`,

```julia
function L_q(q::Vector{T}) where {T<:Real}
    return T[q[1] -q[2:4]'; q[2:4] q[1] * I + SkewSymmetricMatrix(q[2:4])]
end

```

but it doesn’t seem to be enough. Adding a return type declaration does help with the return type though:

```julia
function L_q(q::Vector{T})::Matrix{T} where {T<:Real}
    return T[q[1] -q[2:4]'; q[2:4] q[1] * I + SkewSymmetricMatrix(q[2:4])]
end

```

---

<div class="post-metadata">

**Author:** ![fleecy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fleecy/32/11210_2.png) [@fleecy](https://discourse.julialang.org/u/fleecy)\
**Post date:** [January 4, 2020, 10:09am UTC](https://discourse.julialang.org/t/type-instability-in-array-concatenation/32951/5 "2020-01-04T10:09:21Z")

</div>

Thanks for the suggestion! I wonder why typed\_hvcat doesn’t fix the issue. Is there documentation for this function anywhere?
