Skip to main content
edited title
Link
user64494
  • 29.1k
  • 4
  • 29
  • 56

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

fixed grammar
Source Link
user444
  • 2.7k
  • 2
  • 7
  • 30

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'tdo not 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?

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?

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 do not 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?

Source Link
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?