Is spzeros(m,n) empty?

Based on my understanding, spzeros(m,n) would create a sparse matrix of zeros with size m*n. I checked and found that calling isempty with a matrix like this would return false. What I do not understand is that, based on this link, https://docs.julialang.org/en/v1/stdlib/SparseArrays/index.html,
spzeros(m,n) is empty? …

isempty is meant to tell you if a collection has any elements in it (docs). A sparse matrix is an array, which Julia sees as a collection too, just like a vector v = [0.0, 1.0] is both a 1-dimensional array and a collection of two things (namely 0.0 and 1.0). So spzeros(m,n) creates an n\times m sparse matrix which has n*m elements – and so it’s not empty-- the elements just all happen to be zero.

2 Likes
help?> isempty
search: isempty

  isempty(collection) -> Bool

  Determine whether a collection is empty (has no elements).

A spare matrix of all zeros has elements (all zeros) as long as its size is bigger than (0,0). This is also consistent with how normal arrays behave.

2 Likes

The function nnz may be what you want. For a sparse matrix A that does not store any nonzero element, there exists nnz(A) == 0

1 Like