There is a system option that controls how to build sparse arrays when a position is assigned more than one value. The default setting is First
, meaning that all remaining positions are discarded.
SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> First]
SparseArray[{{1, 2}, {1, 2}, {2, 2}} -> {2, 3, 4}] // Normal
(* {{0, 2}, {0, 4}} *)
SetSystemOptions["SparseArrayOptions" -> "TreatRepeatedEntries" -> Total]
SparseArray[{{1, 2}, {1, 2}, {2, 2}} -> {2, 3, 4}] // Normal
(* {{0, 5}, {0, 4}} *)
What is the complete list of valid values?
One may naïvely think that the value should be a function that will be applied to the list of values, such as First
or Total
(both of which work). But this isn't quite the case, as Mean
fails. We must use Mean[{##}]&
instead. Some numerical values like 0
, 1
or 2
also work.
I am implementing a function which has a combiner function argument which basically does the same thing as TreatRepeatedEntries
, and I would like to dispatch to TreatRepeatedEntries
for performance reasons. I am not fully comfortable doing this because I do not have a full understanding of which values are allowed.