# uniformScaling non identity matrices

**URL:** <https://discourse.julialang.org/t/uniformscaling-non-identity-matrices/35532>\
**Category:** General Usage\
**Created:** [March 4, 2020, 6:07pm UTC](https://discourse.julialang.org/t/uniformscaling-non-identity-matrices/35532 "2020-03-04T18:07:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [March 4, 2020, 6:07pm UTC](https://discourse.julialang.org/t/uniformscaling-non-identity-matrices/35532/1 "2020-03-04T18:07:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 4, 2020, 6:26pm UTC](https://discourse.julialang.org/t/uniformscaling-non-identity-matrices/35532/2 "2020-03-04T18:26:53Z")

</div>

> [@hros](#):
>
> How do I create efficient representations like the uniformScaling type for 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](https://github.com/JuliaLang/julia/blob/780bbe6c2d2940a3f0e3414f78f73e5875e81fd2/stdlib/LinearAlgebra/src/uniformscaling.jl). 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).

> [@hros](#):
>
> note that the examples given are 2x2 matrices but they are used in `kron` multiplications which creates very large data structures

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 4, 2020, 6:29pm UTC](https://discourse.julialang.org/t/uniformscaling-non-identity-matrices/35532/3 "2020-03-04T18:29:57Z")

</div>

See [Quantum Ising Phase Transition](https://juliaphysics.github.io/PhysicsTutorials.jl/tutorials/general/quantum_ising/quantum_ising.html) for a physics example utilizing what Steven said.
