# Is there a way to make SparseArray distinguish between background values of different precision that are numerically equal?

To illustrate what I mean, consider the following:

SparseArray[{{1} -> 1, {5} -> 1.}, Automatic, 0] // Normal

{1, 0, 0, 0, 1.}

As you can see, the distinction between 1 and 1. is preserved. Now consider:

SparseArray[{{1} -> 1, {5} -> 1.}, Automatic, 1] // Normal

{1, 1, 1, 1, 1}

In this situation, the 1. has been absorbed into the background value 1 and the distinction disappears.

There seems to be one way to do what I want, which is this:

one = 1.;
SparseArray[Unevaluated @ {{1} -> 1, {5} -> one}, Automatic, 1] // Normal

{1, 1, 1, 1, 1.}

However, this makes the list of rules very tricky to manipulate, since the one can evaluate away very easily if you're not careful about holding the evaluation. Is there a better way to do this?

# Edit

Just to be clear: I'm not interested in generating the list {1, 1, 1, 1, 1.} by itself. What I'm interested in, is having a SparseArray object that holds a background value of 1 and has 1. among it's specified elements.

Also: these evaluations were done on V12.3

• Interesting. I am pretty sure that this is not how it is supposed to work. Casting from integers to doubles should be allowed, but not vice versa. Maybe this is something that you should discuss with the Wolfram Technical Support. Commented Dec 2, 2021 at 10:26
• In version 11.3.0 we get {1, 1, 1, 1, 1.}.
– kglr
Commented Dec 2, 2021 at 10:38
• @kglr Thanks for pointing that out. I think that's sufficient reason to file this as a bug. Commented Dec 2, 2021 at 10:57
• @HenrikSchumacher I'm not sure I actually agree that automatic casting should be allowed at all. It breaks the fundamental symmetry between dense and sparse arrays. At the very least it should be configurable, if you ask me. Commented Dec 2, 2021 at 10:59
• Btw., in version 12.0 SparseArray[{{1} -> 1, {5} -> 1.}, Automatic, 1] // Normal evaluates to {1, 1, 1, 1, 1}, too. So together with kglr's comment we have quite exactly pinned down when this behavior was changed. Commented Dec 2, 2021 at 11:43