# 9.2. Special Matrices#

using LinearAlgebra


The 2D arrays seen so far have been general, that is, that can be used to represent arbitrary matrices. Many applications lead to matrices with special structures or properties, and Julia defines a number of specialized matrix representations for this. The main reason for using these is to obtain better performance (which can make a very big difference), but they are also useful to e.g. ensure correctness of codes by enforcing known properties.

Type

Description

Symmetric

Symmetric matrix

Hermitian

Hermitian matrix

UpperTriangular

Upper triangular matrix

UnitUpperTriangular

Upper triangular matrix with unit diagonal

LowerTriangular

Lower triangular matrix

UnitLowerTriangular

Lower triangular matrix with unit diagonal

Tridiagonal

Tridiagonal matrix

SymTridiagonal

Symmetric tridiagonal matrix

Bidiagonal

Upper/lower bidiagonal matrix

Diagonal

Diagonal matrix

UniformScaling

Uniform scaling operator

For example, if you know that your matrix is both symmetric and tridiagonal, you can use the SymTridiagonal type. The example below shows how to generate a famous matrix which is very common in applications:

T = SymTridiagonal(2ones(5), -ones(4))

5×5 SymTridiagonal{Float64, Vector{Float64}}:
2.0  -1.0    ⋅     ⋅     ⋅
-1.0   2.0  -1.0    ⋅     ⋅
⋅   -1.0   2.0  -1.0    ⋅
⋅     ⋅   -1.0   2.0  -1.0
⋅     ⋅     ⋅   -1.0   2.0


The matrix operations defined above will work just as before on these specialized types, but likely be much more efficient. For example:

T * randn(5)     # Matrix-vector multiplication
T^3              # Matrix cube

5×5 Matrix{Float64}:
14.0  -14.0    6.0   -1.0    0.0
-14.0   20.0  -15.0    6.0   -1.0
6.0  -15.0   20.0  -15.0    6.0
-1.0    6.0  -15.0   20.0  -14.0
0.0   -1.0    6.0  -14.0   14.0


## 9.2.1. The identity matrix#

The identity matrix $$I$$ is so commonly used, that it has a special syntax which supports some additional performance improvements. In its simplest form, you can simply use the I operator and it will behave as expected:

T + 2I     # OK, since T is 5-by-5

5×5 SymTridiagonal{Float64, Vector{Float64}}:
4.0  -1.0    ⋅     ⋅     ⋅
-1.0   4.0  -1.0    ⋅     ⋅
⋅   -1.0   4.0  -1.0    ⋅
⋅     ⋅   -1.0   4.0  -1.0
⋅     ⋅     ⋅   -1.0   4.0


If you want to actually create an identity matrix, you have to specify the element type and the dimensions:

I4 = Matrix{Float64}(I, 4, 4)

4×4 Matrix{Float64}:
1.0  0.0  0.0  0.0
0.0  1.0  0.0  0.0
0.0  0.0  1.0  0.0
0.0  0.0  0.0  1.0


but this is often not necessary since the I operator can be used in expressions, as shown above.