# Possible bug: SparseArray Part assignment loses default value?

fixed in 10.0.2

This sure looks like a bug to me (Mathematica 10.0.0, MacOS):

(Local) In[345]:= s = SparseArray[{}, {20}, 1]

(Local) Out[345]= SparseArray[< 0 >, {20}, 1]

(Local) In[346]:= s // Normal

(Local) Out[346]= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

(Local) In[347]:= s[[All]] = 1; s

(Local) Out[347]= SparseArray[< 0 >, {20}]

(Local) In[348]:= s // Normal

(Local) Out[348]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}


Apparently when I assigned 1 to all elements of s, it correctly realized that everything was still equal to the default, so left everything unspecified. But it incorrectly lost the 1 default value and changed the array to have the default default value of 0.

This is a toy example, but it comes out of a more complicated example in which it is causing me a serious problem. I don't know the full scope of the problem, but it is not necessary to assign to the entire array for the default value to be lost. I guess the lesson is that non-zero defaults are fragile.

Is this really a bug, or am I missing something obvious?

• on version 9, I get same thing. I think it has to do with the way you build it in first place. Compare to data = Table[1, {20}];s = SparseArray[data]; s[[All]] = 1; s // Normal and now it is ok. – Nasser Jul 17 '14 at 20:05
• Yes, but in that case the default value is 0 from the start. Compare to data = Table[1, {20}]; s = SparseArray[data, Automatic, 1]; s[[All]] = 1; s // Normal, where the SparseArray is created with default 1. The same problem occurs. – Leon Avery Jul 17 '14 at 20:16
• Looks like a bug to me – Rojo Jul 17 '14 at 20:24
• That is a bug and I filed it as such. Thanks for bringing it up. – user21 Jul 18 '14 at 6:59
• As a side note, once can use s["Background"] to get access to the background element. – user21 Jul 18 '14 at 7:03

## 1 Answer

Fixed in 10.0.2, windows 7, 64 bits

• Awesome! Thanks. :-) – Leon Avery Dec 11 '14 at 16:38