7
$\begingroup$

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

$\endgroup$
11
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Dec 2, 2021 at 10:26
  • 5
    $\begingroup$ In version 11.3.0 we get {1, 1, 1, 1, 1.}. $\endgroup$
    – kglr
    Commented Dec 2, 2021 at 10:38
  • 3
    $\begingroup$ @kglr Thanks for pointing that out. I think that's sufficient reason to file this as a bug. $\endgroup$ Commented Dec 2, 2021 at 10:57
  • 2
    $\begingroup$ @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. $\endgroup$ Commented Dec 2, 2021 at 10:59
  • 3
    $\begingroup$ 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. $\endgroup$ Commented Dec 2, 2021 at 11:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.