# Checking for various Matrix types

**URL:** <https://discourse.julialang.org/t/checking-for-various-matrix-types/62425>\
**Category:** New to Julia\
**Created:** [June 5, 2021, 11:07am UTC](https://discourse.julialang.org/t/checking-for-various-matrix-types/62425 "2021-06-05T11:07:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 5, 2021, 11:07am UTC](https://discourse.julialang.org/t/checking-for-various-matrix-types/62425/1 "2021-06-05T11:07:46Z")

</div>

I have a few related questions about checking for various matrix types to get Boolean values. Any help would be appreciated.  
Q1) Suppose I have a function `function f( ..., A ,...)` where the type of A is unspecified. Inside the body of the function, I want to check whether A is a Matrix. But `typeof(A) == AbstractMatrix` does not work, for example

```julia
julia> A = rand(3,3);
julia> typeof(A)==AbstractMatrix
false

```

Q2) I also want to check for various formats, such as Diagonal or upper Triangular. But attempts similar to the following do not work.

```julia
julia> B = LowerTriangular(A)
3×3 LowerTriangular{Float64,Array{Float64,2}}:
 0.660393 ⋅ ⋅
 0.00534751 0.697988 ⋅
 0.660567 0.967677 0.0742839
julia> typeof(B) == LowerTriangular
false

```

I have purposefully not mentioned the storage type, as it might not be known apriori in a code.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 5, 2021, 12:00pm UTC](https://discourse.julialang.org/t/checking-for-various-matrix-types/62425/2 "2021-06-05T12:00:28Z")

</div>

Somehting like that:

```julia
julia> typeof(A) <: AbstractMatrix
true

julia> typeof(B) <: LowerTriangular
true

help?> <:
search: <:

  <:(T1, T2)

  Subtype operator: returns true if and only if all values of type T1 are also of type T2.

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 5, 2021, 12:00pm UTC](https://discourse.julialang.org/t/checking-for-various-matrix-types/62425/3 "2021-06-05T12:00:35Z")

</div>

You need to check for subtypes:

```julia
julia> rand(2,2) isa AbstractMatrix
true

julia> typeof(rand(2,2)) <: AbstractMatrix
true

```

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [June 5, 2021, 2:54pm UTC](https://discourse.julialang.org/t/checking-for-various-matrix-types/62425/4 "2021-06-05T14:54:05Z")

</div>

If you’re envisioning the function looking something like

```julia
function matrixstuff(..., A, ...)
  if A isa LowerTriangular

  elseif A isa UpperTriangular

  elseif A isa ...

  ...

  else
    # generic thing for AbstractMatrix or whatever
  end
end

```

then you might also consider using dispatch, which is a nice code pattern:

```julia
function matrixstuff(..., A::LowerTriangular, ...)
  ... # now you can be sure that A is a LowerTriangular
end

function matrixstuff(..., A::UpperTriangular, ...)
  ... #ditto here for UpperTriangular
end

```

Organizing your code like that may have some nice benefits—the dispatch will automatically use the most specialized version of `matrixstuff` possible for the inputs, so it ostensibly takes at least a little pressure off of you to get type hierarchies right. It also is arguably cleaner in the sense that if you accidentally put an `if A isa AbstractMatrix` or something at the top of your long ifelse in the first form of the function, then you’d actually have unreachable branches, because many more specialized matrix types are subtypes of `AbstractMatrix`. That certainly doesn’t make anything wrong and maybe dispatching like this isn’t right for your problem. Just food for thought.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [June 5, 2021, 3:36pm UTC](https://discourse.julialang.org/t/checking-for-various-matrix-types/62425/5 "2021-06-05T15:36:53Z")

</div>

@oheil @hendri54 @cgeoga Thanks a lot ! This is exactly what I was looking for.
