Skip to main content
1 of 3
jrekier
  • 525
  • 2
  • 10

MapIndexed on a table of Sparsearray structures produces different output in Mathematica 12 and 14.0

Here is a snippet I use to dynamically define a bunch of functions f[i][x_]:

Clear[f];
tabrhs = {SparseArray[{1, 1} -> Sin[x]], SparseArray[{1, 1} -> Cos[x]]};
MapIndexed[(f[First[#2]][x_] := #1) &, tabrhs];

In Mathematica 12, this produces the desired output:

f[1][z] (* SparseArray[{1,1} -> Sin[z]] *)
f[2][z] (* SparseArray[{1,1} -> Cos[z]] *)

In Mathematica 14.0, this produces instead:

f[1][z] (* SparseArray[{1,1} -> Sin[x]] *)
f[2][z] (* SparseArray[{1,1} -> Cos[x]] *) (* notice the 'x' instead of 'z' *)

Note that if I don't use SparseArray structures, and instead use the simpler:

Clear[f];
tabrhs = {Sin[x], Cos[x]};
MapIndexed[(f[First[#2]][x_] := #1) &, tabrhs];

The result is consistent in both versions:

f[1][z] (* Sin[z] *)
f[2][z] (* Cos[z] *)

I do care for the SparseArray structure, though. Any idea?

jrekier
  • 525
  • 2
  • 10