6
$\begingroup$

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?

$\endgroup$

1 Answer 1

3
$\begingroup$
Clear[f];
tabrhs={SparseArray[{1,1}->Sin[x]],SparseArray[{1,1}->Cos[x]]};
MapIndexed[(f[First[#2]][t_]:=SparseArray[ArrayRules[#1]/.x->t])&,tabrhs];
f[1][y]//Normal

{{Sin[y]}}

Update:

Clear[f];
tabrhs={SparseArray[{1,1}->Sin[x]],SparseArray[{1,1}->Cos[x]]};
MapIndexed[(f[First[#2]]=SparseArray@*(Function@@{x,ArrayRules@#1}))&,tabrhs];
f[1][y] // Normal

{{Sin[y]}}

Update 2:

Clear[f];
tabrhs={SparseArray[{1,1}->Sin[x]],SparseArray[{_,_}->0,{1,1}]};
MapIndexed[(f[First[#2]]=SparseArray@@#&@*(Function@@{x,{ArrayRules@#1,Dimensions@#1}}))&,tabrhs];
f[1][y]//Normal
f[2][y]//Normal

{{Sin[y]}}

{{0}}

$\endgroup$
7
  • $\begingroup$ That works. Follow up: is there a way to make that work with a Set (=) instead of SetDelayed (:=) ? $\endgroup$
    – jrekier
    Feb 21 at 21:42
  • $\begingroup$ Also, I'm actually not a fan of the replace rule, as this does not work out of the box in an external package where the t would be interpreted as package'Private't, and not amenable to patterns. $\endgroup$
    – jrekier
    Feb 21 at 22:13
  • 1
    $\begingroup$ @jrekier Updated. $\endgroup$
    – Karl
    Feb 22 at 1:04
  • $\begingroup$ Yup, that works. Thanks! $\endgroup$
    – jrekier
    Feb 22 at 1:32
  • 1
    $\begingroup$ @jrekier Updated 2. $\endgroup$
    – Karl
    Feb 22 at 23:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.