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

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> A = zeros( 2, 0 )
2×0 Matrix{Float64}

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

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

Zero matrices are the conventional outputs of multiplying empty matrices. 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, exits) will have an inferred “return” type of the bottom/empty type Union{}, which has no values and subtypes every other type.

5 Likes

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> A = zeros(2, 0); B = zeros(0, 0); A * B
2×0 Matrix{Float64}
3 Likes

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

I couldn’t find a practical use for multiplying empty matrices, and according to this blogpost (Empty Matrices in MATLAB – Nick Higham), 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.

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.

2 Likes

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]

1 Like

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 .

2 Likes

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]?

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).

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.

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.

2 Likes