uniformScaling non identity matrices

Two options:

  1. 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.
  2. 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.

3 Likes