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.


The complete list is (V11.2)

  1. First or symbolic 0
  2. Total or symbolic 1
  3. List or symbolic 2
  4. Anything else

For anything else:

  "SparseArrayOptions" -> "TreatRepeatedEntries" -> Blah];
SparseArray[{{1, 2}, {1, 2}, {2, 2}} -> {2, 3, 4}] // Normal

{{0, Blah[2, 3]}, {0, 4}}

The reason these are undocumented is that it is not clear how these should interact with pattern sparse arrays.

  • $\begingroup$ You worked a lot with LibraryLink. Is it at all reasonable to use Shared passing and modify the pass-in arguments as a way to return multiple results? It seems like a dangerous thing to do. So many things can go wrong. It is not clear if this is one of the intended uses of Shared passing. There is a similar example for MSparseArray in the LibraryLink examples directory, but I haven't found such a use of MTensors. $\endgroup$ – Szabolcs Jan 26 '18 at 16:35
  • $\begingroup$ @Szabolcs, my understanding of shared is to save the memory of duplicating the tensor. $\endgroup$ – user21 Jan 31 '18 at 18:39
  • $\begingroup$ "Constant" passing also avoids duplicating the tensor. I assumed that the main difference between "Constant" and "Shared" was that the latter allowed modifications (and of course also that the tensor wouldn't be freed by the kernel until the library disowns it). But using in-place modification for returning values seems to require such great care that I am not at all confident using it. $\endgroup$ – Szabolcs Jan 31 '18 at 19:51
  • $\begingroup$ @Szabolcs, the details of all this seem hidden behind the haze of past times. I am not sure I remember enough details to give sound advice here. Sorry. $\endgroup$ – user21 Jan 31 '18 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.