Skip to main content
deleted 11 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

You can use SparseArray setting the system sub-option "TreatRepeatedEntries" to TotalPlus:

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> (Total[{##}] &)Plus}];
sa = SparseArray[{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1}];
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> First];First}];

Thread[sa@"NonzeroPositions" -> sa@ "NonzeroValues"]

{{1, 2} -> -2, {1, 1} -> 1, {1, 6} -> 1}

You can use SparseArray setting the system sub-option "TreatRepeatedEntries" to Total:

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> (Total[{##}] &)}];
sa = SparseArray[{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1}];
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> First];

Thread[sa@"NonzeroPositions" -> sa@ "NonzeroValues"]

{{1, 2} -> -2, {1, 1} -> 1, {1, 6} -> 1}

You can use SparseArray setting the system sub-option "TreatRepeatedEntries" to Plus:

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> Plus}];
sa = SparseArray[{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1}];
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> First}];

Thread[sa@"NonzeroPositions" -> sa@ "NonzeroValues"]

{{1, 2} -> -2, {1, 1} -> 1, {1, 6} -> 1}

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

You can use SparseArray setting the system sub-option "TreatRepeatedEntries" to Total:

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> (Total[{##}] &)}];
sa = SparseArray[{{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1}];
SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> First];

Thread[sa@"NonzeroPositions" -> sa@ "NonzeroValues"]

{{1, 2} -> -2, {1, 1} -> 1, {1, 6} -> 1}