# Vcat broadcast to all columns?

**URL:** <https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178>\
**Category:** General Usage\
**Tags:** broadcasting, staticarrays\
**Created:** [August 23, 2022, 8:51am UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178 "2022-08-23T08:51:36Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 24, 2022, 11:27am UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/42 "2022-08-24T11:27:57Z")

</div>

An intermediate, and decent, solution could be if you implement your own `scalar_vcat` function that lets you write

```julia
V1 = scalar_vcat(V, 1)

```

That would be clear, and also optimal, performance-wise. Rolling your own specialized solutions instead of relying on built-in funcionality is quite idiomatic in Julia.

> [@user664303](#):
>
> I think expanding singleton dimensions is actually really obvious and intuitive

Sure, and this is common in Julia. But it is always enabled through explicit dot-broadcasting, so that should probably be part of a built-in solution.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 24, 2022, 11:29am UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/43 "2022-08-24T11:29:56Z")

</div>

Yes, in the short term, by the way, it would be easy to put up a “ScalarCat” package with all that functionality and be happy, anyway these packages are necessary to test any functionality before proposing something definitive to Base.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 24, 2022, 11:36am UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/44 "2022-08-24T11:36:05Z")

</div>

@DNF note also that the fact that `vcat` is slow for multidimensional matrices is not a fundamental limitation of the column-major representation in Julia. It is feasible to implement matrices efficiently with extra space row-wise to allow resizing and pushing: [Restriction to the last dimension · Issue #3 · JuliaArrays/ElasticArrays.jl · GitHub](https://github.com/JuliaArrays/ElasticArrays.jl/issues/3#issuecomment-576800333)

This performance limitation could be lifted, in principle, if that was implemented. Maybe someday it will, so it in this sense it is correct to _not_ limit the expressiveness of the language for that performance reason.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 24, 2022, 11:43am UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/45 "2022-08-24T11:43:56Z")

</div>

> [@lmiq](#):
>
> Maybe someday it will, so it in this sense it is correct to _not_ limit the expressiveness of the language for that performance reason.

Yes, but I don’t think I was the one arguing for that.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [August 24, 2022, 11:54am UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/46 "2022-08-24T11:54:41Z")

</div>

> [@jishnub](#):
>
> That being said, I am all for such a syntax, and for easier block concatenation that reads like `[A 0; 0 B]` instead of `cat(A, B, dims=(1,2))`

`[A 0; 0 B]` seems like neat syntax, especially as one can already do `[A I; I B]` when using the LinearAlgebra package. Maybe adding special objects (sorry, I have no idea what `I` is here) `Zeros` and `Ones` to LinearAlgebra, which behave like `I`, would be a better solution for supporting scientific notation.

In the case of `[A Ones]` it’s not clear how wide the `Ones` array is, but with `[A Ones] * B` it is clear how wide it needs to be. I’m not sure if the second case can be made to work, though. In the first case, there could be an additional compile-time or run-time parameter (`Ones{1}` or `Ones(n)`) that defaults to 1. In this case, I’d get what I need by simply writing `[x; Ones]`.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [August 24, 2022, 12:10pm UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/47 "2022-08-24T12:10:57Z")

</div>

> [@DNF](#):
>
> If this requires a nonobvious notation, with limited gain, I’m not sure it’s worth it.

From what I understand, the primary gain is that the same syntax `[A; 1]` may work for both `Array`s and `StaticArray`s (while preserving static size information), and not so much about performance.

In this context, it’s a shame that concatenation uses sizes instead of axes. Ideally, something like this should preserve static size information:

```julia
julia> a = SMatrix{2,2,Int,4}(1:4)
2×2 SMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1 3
 2 4

julia> Ones((SOneTo(1),axes(a,2))) |> axes
(SOneTo(1), SOneTo(2))

julia> [a; Ones((SOneTo(1),axes(a,2)))]
3×2 Matrix{Float64}:
 1.0 3.0
 2.0 4.0
 1.0 1.0

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [August 24, 2022, 1:57pm UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/48 "2022-08-24T13:57:30Z")

</div>

> [@user664303](#):
>
> 1. I don’t see that it’s mixing two functionalities. I see it as changing the functionality. A bit like when MATLAB changed the functionality of `.*` from element-wise multiplication to singleton dimension expanding, element-wise multiplication. I note that plenty of Julia functionality already expands singleton dimensions by default anyway, e.g. `.*`. That isn’t considered mixing two functionalities. You suggest something quite different with resize. You cannot set the padding value at present, and it would not allow concatenation with longer arrays, such as [1, 0].

The two functionalities I was referring to are concatenation and single dimension expansion, others in this thread have also noted this as an issue. My idea with `resize` is to do something like:

```julia
x = rand(2, 1000)
resize(x, size(x,1)+1, size(x,2))
foreach(i->x[end,i]=1, 1:size(x,2))

```

or alternatively:

```julia
x = rand(2, 1000)
resize(x, size(x,1)+1, size(x,2); initval_for_new_elements=1)

```

but as I said, Stefan Karpinsky was not keen on a change like this so it is probably not the right solution to promote.

I do feel that your `Zeros` and `Ones` idea will fit into Julias syntax. I am not a big fan of `I` without an explicit size specification, but now that it is part of Julia, then having some similar “constants” would make it less of an outsider.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 24, 2022, 2:11pm UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/49 "2022-08-24T14:11:34Z")

</div>

Efficiencies apart, could be something like this:

```julia
julia> struct ConstRow{T}
               val::T
       end

julia> 

julia> import Base: vcat, hcat

julia> vcat(m, c::ConstRow) = vcat(m, [c.val for i in axes(m,1)]')
vcat (generic function with 29 methods)

julia> m = zeros(3,3)
3×3 Matrix{Float64}:
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0

julia> vcat(m, ConstRow(1))
4×3 Matrix{Float64}:
 0.0 0.0 0.0
 0.0 0.0 0.0
 0.0 0.0 0.0
 1.0 1.0 1.0

```

That actually looks neat, IMO.

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [August 24, 2022, 2:58pm UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/50 "2022-08-24T14:58:50Z")

</div>

Shouldn’t it be `axes(m,2)` so that it works on 3x6 matrices for example?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 24, 2022, 3:32pm UTC](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178/51 "2022-08-24T15:32:33Z")

</div>

Yes, it should.

[Previous page](https://discourse.julialang.org/t/vcat-broadcast-to-all-columns/86178.md?page=2)
