# Which matrix types are defined?

**URL:** <https://discourse.julialang.org/t/which-matrix-types-are-defined/98224>\
**Category:** General Usage\
**Tags:** question, linearalgebra\
**Created:** [May 3, 2023, 12:22am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224 "2023-05-03T00:22:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 3, 2023, 12:22am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/1 "2023-05-03T00:22:42Z")

</div>

Base Julia knows at least three types of vectors:

```julia
julia> x=rand(3)
3-element Vector{Float64}:
 0.9378913600546382
 0.04174077280647481
 0.7608954281716017

julia> x'
1×3 adjoint(::Vector{Float64}) with eltype Float64:
 0.937891 0.0417408 0.760895

julia> transpose(x)
1×3 transpose(::Vector{Float64}) with eltype Float64:
 0.937891 0.0417408 0.760895

```

What are commonly used types of matrices?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 3, 2023, 1:16am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/2 "2023-05-03T01:16:59Z")

</div>

If you are asking to type function arguments, don’t worry: type them as `AbstractMatrix{T}`. This will be sufficiently flexible.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 3, 2023, 1:21am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/3 "2023-05-03T01:21:33Z")

</div>

I don’t worry, I just want to explain the advantages of Julia to a colleague that uses Matlab, and I think one of the advantages is that you can write functions that are optimized for curtain matrix types, e.g. symmetric matrices, but I did not find a list of types that are defined…

EDIT: OK, found it: [Linear Algebra · The Julia Language](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/)

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 3, 2023, 1:32am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/4 "2023-05-03T01:32:11Z")

</div>

Maybe also useful?

```julia
julia> subtypes(AbstractMatrix)
28-element Vector{Any}:
 AbstractSlices{T, 2} where T
 Base.ReinterpretArray{T, 2, S} where {T, S}
 Base.ReshapedArray{T, 2} where T
 Base.SCartesianIndices2
 BitMatrix (alias for BitArray{2})
 CartesianIndices{2, R} where R<:Tuple{OrdinalRange{Int64, Int64}, OrdinalRange{Int64, Int64}}
 Core.Compiler.BitArray{2}
 Core.Compiler.LinearIndices{2, R} where R<:Tuple{Core.Compiler.AbstractUnitRange{Int64}, Core.Compiler.AbstractUnitRange{Int64}}
 DenseMatrix (alias for DenseArray{T, 2} where T)
 LinearAlgebra.AbstractQ
 LinearAlgebra.AbstractTriangular
 LinearAlgebra.Adjoint
 LinearAlgebra.Bidiagonal
 ⋮
 LinearAlgebra.LQPackedQ
 LinearAlgebra.SymTridiagonal
 LinearAlgebra.Symmetric
 LinearAlgebra.Transpose
 LinearAlgebra.Tridiagonal
 LinearAlgebra.UpperHessenberg
 LinearIndices{2, R} where R<:Tuple{AbstractUnitRange{Int64}, AbstractUnitRange{Int64}}
 PermutedDimsArray{T, 2} where T
 AbstractSparseMatrix (alias for SparseArrays.AbstractSparseArray{Tv, Ti, 2} where {Tv, Ti})
 SparseArrays.CHOLMOD.FactorComponent
 SparseArrays.ReadOnly{T, 2, V} where {T, V<:AbstractMatrix{T}}
 SubArray{T, 2} where T
 Test.GenericArray{T, 2} where T

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 3, 2023, 1:33am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/5 "2023-05-03T01:33:11Z")

</div>

I am pretty sure Matlab has specialized matrices too.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 3, 2023, 2:04am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/6 "2023-05-03T02:04:07Z")

</div>

Well, not really. While you can define classes in Matlab, matrices are not classes but a special build-in type.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 3, 2023, 2:05am UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/7 "2023-05-03T02:05:04Z")

</div>

Yes, you are right: the specializations are built-in types.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [May 3, 2023, 4:40pm UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/8 "2023-05-03T16:40:39Z")

</div>

You might also want to point out that many packages have already implemented special matrix types. Here are just a few for example…

```julia
julia> using LinearAlgebra

julia> old_types = subtypes(AbstractMatrix); length(old_types)
28

julia> using BlockArrays, InfiniteArrays, StaticArrays, ToeplitzMatrices

julia> foreach(t -> t ∉ old_types && println(t), subtypes(AbstractMatrix))
AbstractToeplitz
ArrayLayouts.LayoutMatrix
BlockArrays.BlockIndexRange{2, R} where R<:Tuple{AbstractUnitRange{Int64}, AbstractUnitRange{Int64}}
BlockArrays.BlocksView{S, 2, T, B} where {T<:(AbstractMatrix), S, B<:AbstractMatrix{S}}
BlockRange{2, R} where R<:Tuple{AbstractUnitRange{Int64}, AbstractUnitRange{Int64}}
FillArrays.AbstractFill{T, 2} where T
FillArrays.OneElement{T, 2} where T
FillArrays.RectDiagonal
Hankel
InfiniteArrays.ReshapedArray{T, 2} where T
StaticArrays.TrivialView{A, T, 2} where {A, T}
StaticArray{S, T, 2} where {S<:Tuple, T}

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 3, 2023, 5:03pm UTC](https://discourse.julialang.org/t/which-matrix-types-are-defined/98224/9 "2023-05-03T17:03:42Z")

</div>

And I think you cannot specialize your own code on the inbuilt Matlab specialized matrices when writing your own Matlab functions. At least that’s my understanding…
