How do I create efficient representations like the uniformScaling type for non identity matrices?
for example how would I create the matrices
X=[0 1;1 0]
Z=[1 0;0 -1]
as well as [1 0;0 0] and [0 0;0 1]
with similar space and computational efficiency
(note that the examples given are 2x2 matrices but they are used in kron multiplications which creates very large data structures
would each one of the special matrices require a special type with corresponding functions?
Iād appreciate example code for one of the aforementioned matrices
Use a SparseMatrixCSC (in the SparseArrays standard library). e.g. sparse([1 0;0 -1]). This good for storing matrices that are very sparse (mostly zero) and is completely generic (can handle any sparsity pattern), but for very special sparsity patterns (e.g. diagonal like I) it is suboptimal.
Define your own AbstractMatrix subtype, similar to how UniformScaling is implemented. This in theory allows you maximum efficiency (you can specialize every operation for your specific structure), but is a lot more work (especially if you want to support Kronecker products, which will require a implementing special matrix type for a Kronecker product of your little matrices).
If you take the Kronecker product (kron) of SparseMatrixCSC matrices, the result is a SparseMatrixCSC ā so, if the result is sparse (mostly zero) it will be stored efficiently and support efficient computations.
For example, if you set:
using SparseArrays
X = sparse([0 1;1 0])
Z = sparse([1 0;0 -1])
then X ā Z ā X ā Z ā X ā X ā Z ā X ā Z ā X is a 1024Ć1024 sparse matrix with only 1024 nonzero entries (one per row), and this is pretty efficient to work with.