When two SparseArrays are added together and new zero values are created, these new zero values are reported as "NonzeroValues". Example, produced with Mathematica version 10.2:
tst = SparseArray[{1, 0, 1, 0, 1}] - SparseArray[{1, 0, 1, 0, 1}];
tst["NonzeroValues"]
{0, 0, 0}
tst["NonzeroPositions"]
{{1}, {3}, {5}}
It appears that SparseArrays constructed in this way can become "polluted" with lots of false non-zeros. Is there a way to get Mathematica to quickly compact such a SparseArray and strip out the introduced zeros? In my application, I produce large sparse vectors through many such additions, and I need to quickly identify the positions of nonzero entries.
Edit: My application is similar to RowReduce. I have a large sparse matrix of mostly zeros and ones, and I am implementing pivoting, with selection rules based on the number of nonzero elements in the rows and columns. After a pivot, the number of nonzero elements will change for many of the rows of the matrix. My matrices have hundreds of rows and columns, with densities of around 1%.
r = SparseArray[p + q];directly; I just wanted to demonstrate that the new zeroes do get recognized. – J. M.'s missing motivation Oct 17 '15 at 02:52{SparseArray[{1, 0, 1, 0}], SparseArray[{0, 0, 1, 1}]}is certainly possible. After doing your row operations, you can then applySparseArray[]to the whole thing. – J. M.'s missing motivation Oct 17 '15 at 03:36SparseArrayhad is just one possible user-interface for compacting the sparse array, and it's not clear at all how this is implemented internally. It could, in principle, be as efficient as possible. – Szabolcs Oct 17 '15 at 07:46SparseArray[{1., 0., 1., 0., 1.}] - SparseArray[{1., 0., 1., 0., 1.}]zero threshold would be sufficient:0.`is zero, otherwise it isn't. – Alexey Popkov Oct 17 '15 at 08:15SparseArray: it would be logical to determine such things only when it is necessary, not on every stage of computation withSparseArrays. That is the reason for my question: is it "by design" and the user have to applySparseArrayto the final result before using things like"NonzeroPositions"or is it a bug? Earlier I thought that"NonzeroPositions"aren't stored and calculated on the fly. – Alexey Popkov Oct 17 '15 at 08:22"Nonzero*"are undocumented, thus should be immune from prosecution. :) – Silvia Oct 17 '15 at 13:23tst = SparseArray[{1, 0, 1, 0, 1}] - SparseArray[{1, 0, 1, 0, 1}]; newTst = SparseArray[tst];tst["NonzeroValues"] {0,0,0}newTst["NonzeroValues"] {}– dcutrell Oct 18 '15 at 17:19tst = SparseArray[SparseArray[{1, 0, 1, 0, 1}] - SparseArray[{1, 0, 1, 0, 1}]];), but is your memory really that taxed for you not to want to maintain two copies? – J. M.'s missing motivation Oct 18 '15 at 17:22sa = SparseArray[sa];, then there will only be one copy left. – Szabolcs Oct 18 '15 at 17:23MSparseArray_resetImplicitValueson it, the return. But once again: 1. What evidence do you have to show that this is not what happens when you dosa = SparseArray[sa]? 2. Are you sure that the recomputing can at all be done without a temporary internal copy? If yes, explain why. Based on my limited familiarity with the SparseArray internal structure, ... – Szabolcs Oct 18 '15 at 17:47