LinearAlgebra.jl
LinearAlgebra.jl copied to clipboard
Constants Zeros and Ones that behave somewhat like I
In scientific papers using matrices, you'll often see notation where block matrices consist of blocks such as A (a full matrix), but also I (an appropriately sized identity matrix), 0 and 1 (appropriately sized blocks of all zeros or all ones).
Julia (in particular the LinearAlgebra standard library) has a nice constant, I, that creates an implicit identity matrix, so we can do things like:
D = [A I; I B] * C
and Julia will magically do the right thing. This makes it really easy to transcribe matrix algebra using the identity matrix into code, and makes it easy for scientists reading the code to understand what it does. And of course it is performant because the code understands what the identity is, so doesn't waste time doing unnecessary multiplications by 0 and 1.
It would be really great to have additional constants, Zeros and Ones, which represent blocks of zeros and ones respectively, so we can similarly write:
D = [A Zeros; Ones B] * C
and again Julia will magically do the right thing, again not wasting time with unnecessary multiplications. This way transcribing matrix algebra from papers becomes even easier.
Of course, I is always assumed to be a square matrix, whereas 0 and 1 are not, so sometimes the dimensionality of the block cannot be inferred. In cases such as:
C = [A Zeros; Ones B]
C = A * [B; Ones]
it can be inferred. However, in cases such as:
C = [A Zeros]
C = [A Ones] * [B; Ones]
one of the dimensions of each block cannot be inferred. In this case, a compile-time or run-time block size parameter can be specified for the unknown dimension, e.g. Ones{1} or Ones(1). This could default to one, such that [A; Ones] would implicitly concatenate a row of ones to the bottom of A.