In the sparse matrix, we have a list of properties: specified elements, dimension, default, and density. I am interested in getting this data without calculating them again. For an example, the following sparse matrix has four specified elements.
s = SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]
I can calculate the number of specified elements of s
by
Length@s["NonzeroValues"]
but I look for an option to get this information from s
properties
Length@s["NonzeroValues"]
not an acceptable solution? I'm afraid it is not clear what fundamentally is the question you are asking. $\endgroup$ – rhermans Jul 29 '18 at 11:25s well-formatted (e.g., if
SparseArraySparseArraySortedQ[s]
returnsTrue
),s["RowPointers"][[-1]]
is a second possibility. ButLength[s["NonzeroValues"]]
is much safer. $\endgroup$ – Henrik Schumacher Jul 29 '18 at 13:28Length
won't compute anything.s["NonzeroValues"]
is a basic constitutent of a sparse array. Within the internal data type for sparse arrays (MSparseSparseArray
),s["NonzeroValues"]
is stored as a dense array (MTensor
). The length (and dimensions) of a dense array are also stored in eachMTensor
object, soLength
just reads off a value of a field of theMTensor
. $\endgroup$ – Henrik Schumacher Jul 29 '18 at 13:39