7

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

Sjoerd Smit
  • 23,370
  • 46
  • 75
  • 2
    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. – Henrik Schumacher Dec 02 '21 at 10:26
  • 5
    In version 11.3.0 we get {1, 1, 1, 1, 1.}. – kglr Dec 02 '21 at 10:38
  • 3
    @kglr Thanks for pointing that out. I think that's sufficient reason to file this as a bug. – Sjoerd Smit Dec 02 '21 at 10:57
  • 2
    @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. – Sjoerd Smit Dec 02 '21 at 10:59
  • 1
    @SjoerdSmit Yes, you are right. But, e.g., Normal[SparseArray[{{1} -> 1, {5} -> 1.}, Automatic, 1.]] returns {1., 1., 1., 1., 1.} (version 12.0) and I find it quite convenient. – Henrik Schumacher Dec 02 '21 at 11:42
  • 3
    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. – Henrik Schumacher Dec 02 '21 at 11:43
  • 1
    @HenrikSchumacher I just got word back from the developers that this is as-designed. – Sjoerd Smit Dec 09 '21 at 09:12
  • 1
    Weird. Thanks anyways! – Henrik Schumacher Dec 09 '21 at 09:13
  • 2
    Well, "as-designed" doesn't necessarily mean "properly designed". – rhermans Dec 09 '21 at 09:14
  • @rhermans Couldn't agree more. – Sjoerd Smit Dec 09 '21 at 09:17
  • @SjoerdSmit, would Around[1,$MachineEpsilon] be an alternative to 1.0 for your application? – rhermans Dec 09 '21 at 09:39

0 Answers0