2
$\begingroup$

I am interested in save a list (the size of the list is two) of numbers in the sparse matrix, but this is not supported. Maybe you familiar with some workarounds or any suggestion on another data structure?

example = 
 SparseArray[{{1, 1} -> {2, 3}, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 
    4}]
$\endgroup$
  • $\begingroup$ is "{2, 3}" (instead of {2, 3}) acceptable? $\endgroup$ – kglr Aug 2 '18 at 10:24
  • $\begingroup$ .. or FromDigits[{2, 3}, x]? $\endgroup$ – kglr Aug 2 '18 at 10:30
  • $\begingroup$ I want to do computation after the definition. So saving the data as String may slowing the calculation. $\endgroup$ – Kiril Danilchenko Aug 2 '18 at 10:59
  • 1
    $\begingroup$ how about using a 3D array instead of 2D: rules = {{1, 1} -> {2, 3}, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4};SparseArray[ Flatten[rules /. Rule[a_, b_] :> Thread[(Append[a, #] & /@ Range[Length@If[Head[b] === List, b, {b}]]) -> b]]]? $\endgroup$ – kglr Aug 2 '18 at 11:04
  • $\begingroup$ I suspect that you want to use SparseArray for something for which it was not made for. If you tell us what you actually want to do, then, maybe, we can tell you how to do it with SparseArray or another suitable data type. $\endgroup$ – Henrik Schumacher Aug 2 '18 at 18:08
2
$\begingroup$

This is a hacky workaround.

X = {{1, 1} -> 2, {1, 1} -> 3, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}
A = With[{spopt = SystemOptions["SparseArrayOptions"]},
   Internal`WithLocalSettings[
    SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> List}],
    SparseArray[X, {3, 3}, 0],
    SetSystemOptions[spopt]
    ]
   ];
Normal[A]

{{{2, 3}, 0, 4}, {0, 2, 0}, {0, 0, 3}}

Usually, one uses this with "TreatRepeatedEntries" -> Total for additive assembly. The default value of "TreatRepeatedEntries" is First.

Overall, Mathematica is quite inconsistent with the handling of sparse array entries with head List. Any other (symbolic) head work seems to work without any problems. So if you do not rely on packed arrays as entries, you can also use this:

example = SparseArray[{{1, 1} -> list[2, 3], {2, 2} -> 2, {3, 3} -> 
    3, {1, 3} -> 4}]
$\endgroup$
  • $\begingroup$ Thank you @Henrik. I a little bit confused by the following fact when I update the sparse matrix (which I created according to your advice) I get List. A[[1, 3]] = {1, 55};Head@A $\endgroup$ – Kiril Danilchenko Aug 2 '18 at 12:06
  • 1
    $\begingroup$ @KirilDanilchenko That's one of things I meant with "inconsistent". Notice that most routines for (and actually also the data structure of) SparseArray are optimized for matrices with entries only in one of the following data types: Real, Integer, or Complex. Everything beyond that can be considered as luxury, in particular since most other programming languages do not support anything beyond. Have also a look at Association: It provides more flexibility in many ways, but it is also less performant for several tasks. $\endgroup$ – Henrik Schumacher Aug 2 '18 at 17:58

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.