# Matrix with zero columns multiplied by vector with zero elements yields vector

**URL:** <https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518>\
**Category:** General Usage\
**Tags:** matrices\
**Created:** [August 10, 2025, 10:45pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518 "2025-08-10T22:45:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [August 10, 2025, 10:45pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/1 "2025-08-10T22:45:21Z")

</div>

What is the reason behind this behavior? Type stability?

I had expected this to either yield a two by zero matrix or throw an error.

```julia-auto
julia> A = zeros( 2, 0 )
2×0 Matrix{Float64}

julia> B = zeros( 0, )
Float64[]

julia> A * B
2-element Vector{Float64}:
 0.0
 0.0

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 10, 2025, 11:23pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/2 "2025-08-10T23:23:39Z")

</div>

Zero matrices are the conventional outputs of [multiplying empty matrices](https://en.wikipedia.org/wiki/Matrix_(mathematics)#Empty_matrix). The column-major `Vector` with length N is treated as a column vector corresponding to a Nx1 matrix. Multiplying a MxN matrix by a Nx1 matrix is expected to make a Mx1 matrix, and given matrix-vector multiplication, the output is the corresponding column vector with length M. Each element of the output is the dot product of a row and column, both of length N. In your case, N=0 and M=2, and the dot products of empty sequences are 0s. I don’t know of a practical use for empty matrices, but the math is consistent.

Generally, throwing an error for some inputs won’t affect type stability because if it doesn’t return, then it doesn’t contribute another return type. At an extreme, a method that is inferred to never return (e.g. throws error, `exit`s) will have an inferred “return” type of the bottom/empty type `Union{}`, which has no values and subtypes every other type.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [August 10, 2025, 11:56pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/3 "2025-08-10T23:56:17Z")

</div>

I see no problem here. If you multiply a real n\times m matrix by a vector in \mathbb{R}^m, you get a vector in \mathbb{R}^n. You are looking at the case m=0, n=2. (Note that your `B` is a vector!)

If you make `B` a 0\times 0 matrix, then you indeed get a 2\times 0 matrix:

```julia-auto
julia> A = zeros(2, 0); B = zeros(0, 0); A * B
2×0 Matrix{Float64}

```

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [August 11, 2025, 1:26am UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/4 "2025-08-11T01:26:39Z")

</div>

It isn’t a problem. It’s in fact convenient to me.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 11, 2025, 1:35am UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/5 "2025-08-11T01:35:31Z")

</div>

I couldn’t find a practical use for multiplying empty matrices, and according to this blogpost ([Empty Matrices in MATLAB – Nick Higham](https://nhigham.com/2016/03/15/empty-matrices-in-matlab/)), it may only exist to implement instead of work around an edge case caused by the usage of empty vectors (or empty matrices in MATLAB where dedicated scalars and vectors don’t exist) in initialization or concatenation.

---

<div class="post-metadata">

**Author:** ![Joris\_Pinkse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joris_pinkse/32/216398_2.png) [@Joris\_Pinkse](https://discourse.julialang.org/u/Joris_Pinkse)\
**Post date:** [August 11, 2025, 1:59am UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/6 "2025-08-11T01:59:07Z")

</div>

Yeah, that’s exactly it. In my case the null space of a matrix is part of an additive contribution to a larger object. In Julia, the null space can be a matrix with one dimension zero. Instead of having to test for the dimension being zero as a special case, it is convenient if that contribution is automatically equal to a vector of zeros.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [August 11, 2025, 2:53pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/7 "2025-08-11T14:53:21Z")

</div>

Empty matrices are a perfectly normal mathematical object. Almost no one teaches them, but we should all have learned about them when we studied linear algebra, because they are necessary to make the whole theory work. Consider the function such that f(x) = 0 for each x\in \mathbb{R}^2. This is a perfectly fine linear map from \mathbb{R}^2 to the trivial vector space \{0\}, right? So there must be a matrix associated to it, and this matrix is necessarily `zeros(0,2)`. [EDIT: fixed dimension order, as noted in the next message]

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [August 11, 2025, 8:35pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/8 "2025-08-11T20:35:39Z")

</div>

> [@fph](#):
>
> this matrix is necessarily `zeros(2,0)`

If evaluating a linear map `A` on a vector `v` corresponds to matrix-vector multiplication `A * v`, then the zero map from \mathbb{R}^2 to \mathbb{R}^0 is represented by `zeros(0,2)`. In contrast, `zeros(2,0)` is the (zero) map from \mathbb{R}^0 to \mathbb{R}^2 .

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 12, 2025, 12:54am UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/9 "2025-08-12T00:54:16Z")

</div>

> [@fph](#):
>
> Consider the function such that f(x) = 0 for each x\in \mathbb{R}^2.

Wouldn’t that be a map from \mathbb{R}^2 to \mathbb{R}^1 e.g. 0a+0b=0 meaning `[0 0]*[a, b] == [0]`?

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [August 12, 2025, 1:15am UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/10 "2025-08-12T01:15:51Z")

</div>

> [@Benny](#):
>
> Wouldn’t that be a map from \mathbb{R}^2 to \mathbb{R}^1

For any m and n there is a zero map \mathbb{R}^m\to\mathbb{R}^n because any vector space has a zero vector (generically written as “0”). In a 0-dimensional vector space, there are no other vectors. In other words, a 0-dimensional vector space is a single point (the origin).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [August 12, 2025, 2:16am UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/11 "2025-08-12T02:16:30Z")

</div>

That part I get (I think). I’m just reading f(x) = 0 as a system of 1 linear equation with 2 variables, which in my understanding is represented by a 1x2 matrix mapping \mathbb{R}^2 to \mathbb{R}^1. And isn’t the element of \mathbb{R}^0 an empty (column) vector like `B`? Not great at math so I might be misremembering stuff.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [August 12, 2025, 12:29pm UTC](https://discourse.julialang.org/t/matrix-with-zero-columns-multiplied-by-vector-with-zero-elements-yields-vector/131518/12 "2025-08-12T12:29:06Z")

</div>

The “0” in “f(x) = 0” stands for the zero vector of whichever vector space you map to. If you map to \mathbb{R}^0, then the zero vector is indeed the empty vector because it has no coordinates.
