# How many fillable elements in an AbstractMatrix?

**URL:** https://discourse.julialang.org/t/how-many-fillable-elements-in-an-abstractmatrix/96174
**Category:** General Usage
**Created:** [March 16, 2023, 11:05am UTC](https://discourse.julialang.org/t/how-many-fillable-elements-in-an-abstractmatrix/96174 "2023-03-16T11:05:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pgoulart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgoulart/32/17514_2.png) [@pgoulart](https://discourse.julialang.org/u/pgoulart)
#### Post date: [March 16, 2023, 11:05am UTC](https://discourse.julialang.org/t/how-many-fillable-elements-in-an-abstractmatrix/96174/1 "2023-03-16T11:05:43Z")

</div>

It seems like it should be possible to count the number of “fillable” entries in a matrix, but I can’t work out how to do so for any AbstractMatrix.

What I want:

Some function `foo` that returns:

- `nnz(A)` when A is SparseMatrixCSC.
- `length(A.diag))` if A is Diagonal.
- `prod(size(A))` if A is a Matrix.

I still want to count zeros as long as they are structural zeros, i.e. actually stored somewhere. So I should get :

`foo(Diagonal(zeros(3))) == 3`  
`foo(sparse(Diagonal(zeros(3)))) == 0`  
`foo(zeros(3,3)) == 9`

Does such a thing exist already?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [March 16, 2023, 5:49pm UTC](https://discourse.julialang.org/t/how-many-fillable-elements-in-an-abstractmatrix/96174/2 "2023-03-16T17:49:38Z")

</div>

Nothing like this exists. There aren’t many situations where this is a useful quantity to know. You’ll have to define it yourself for your types of interest if you really need it.

It’s tricky because you’d think that you could do things like  
`foo(x::LowerTriangular) = div(size(x,1)^2 + size(x,1), 2)`,  
but what about `LowerTriangular{T,<:Diagonal}`? What about `LowerTriangular{T, SparseMatrixCSC{T,Int}}` and `LowerTriangular{T, Diagonal{T, SparseVector{T,Int}}}`? How about `UpperTriangular{T, UnitLowerTriangular{T, Matrix{T}}}`? And it gets even harder when you start looking at `SubArray` (ie, `view`) wrappers. This question is staggeringly complicated when you consider nested types like these.

---

<div class="post-metadata">

### Author: ![pgoulart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgoulart/32/17514_2.png) [@pgoulart](https://discourse.julialang.org/u/pgoulart)
#### Post date: [March 16, 2023, 11:08pm UTC](https://discourse.julialang.org/t/how-many-fillable-elements-in-an-abstractmatrix/96174/3 "2023-03-16T23:08:13Z")

</div>

Yes, I see what you mean and it is not obvious at all what should be expected in some cases. Many thanks.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 16, 2023, 11:30pm UTC](https://discourse.julialang.org/t/how-many-fillable-elements-in-an-abstractmatrix/96174/4 "2023-03-16T23:30:51Z")

</div>

> [@pgoulart](#):
>
> `prod(size(A))` if A is a Matrix.

BTW, for this you can just use `length(A)`, that returns the total number of elements.
