Message Boards Message Boards

0
|
4351 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Fast emptiness test on sparse arrays

Posted 8 years ago

Dear Wolframers, do you know if there is some efficient way to test that the only entries in a sparse array are set to the default element, without having recourse to the conversion of the sparse array into a list of array rules like so :

emptyQ[sparseArray_] := Length[ArrayRules[sparseArray]] == 1

Thanks for your help

POSTED BY: Fabien Todescato
4 Replies
Posted 8 years ago

Very nice, Ilian. Perhaps this could be noted in the properties section of SparseArray documentation?

POSTED BY: David Keith

Another way would be

sparseArray["Density"] === 0.

See also What are SparseArray Properties? How and when should they be used?.

POSTED BY: Ilian Gachevski

Nice hidden functionality; is this in the documentation and is this planned to change? Or is it used throughout the system already?

POSTED BY: Sander Huisman

I would guess this to be most efficient way to do it currently without using hidden function. If you do:

sparseArray // FullForm 

You will see the input, but you can't access it directly using Part as a sparseArray is an atom (AtomQ). You therefore have to use functions like ArrayRules to get 'deeper' information from sparse arrays...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract