Is it possible in Mathematica to replace the part of the name of the function to give the same arguments to another function? In fact, I have a lot of functions with the following name pattern:


and I want to replace the number part to substitute x,y, and z in all of the functions.

I do not want to give these numbers as normal arguments due to clarity.

As far as I know from other languages I should do something like StringReplace but is there any better way to do this?

  • 1
    $\begingroup$ Names["Global`function$" ~~ DigitCharacter ..] /. fn_String :> Symbol[fn][x, y, z]? $\endgroup$ Dec 3 '21 at 18:40
  • 5
    $\begingroup$ You can always define functions like function[1][x, y, z], function[2][x, y, z], etc. $\endgroup$
    – yarchik
    Dec 3 '21 at 19:09
  • $\begingroup$ If you have a number like 6423 you can get the function name with something like template = StringTemplate["function$``"]; Symbol[template[6423]] $\endgroup$
    – Jason B.
    Dec 3 '21 at 20:34

The following example should get you there. Just change the regular expression and the evaluation point.

Clear[f1, f2, f3]
f1[x_, y_, z_] := x*y*z
f2[x_, y_, z_] := x*y*z^2
f3[x_, y_, z_] := x*y*z^3
funcs = ToExpression /@ Names[RegularExpression["f[0-9]"]] (* the functions *)
Through[funcs[1, 2, 3]]  (* evaluate at a common point *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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