1
$\begingroup$

If I have defined functions as "A1", "A2" ecc. then I have many function as A+number, can I define a For cycle to call these functions (they have all the same kind of arguments) once?

Something like

For[i<64, i == 0, i++, A"i"[]]

I don't know if it is possible.

$\endgroup$
3
  • $\begingroup$ Table[ToExpression @ StringTemplate["A``[]"] @ i, {i, 64}]. In general take a look at: 783 $\endgroup$
    – Kuba
    Commented Jan 8, 2018 at 15:32
  • 2
    $\begingroup$ Why not define these functions as A[1], A[2] etc.? $\endgroup$
    – evanb
    Commented Jan 8, 2018 at 15:40
  • $\begingroup$ Thank you both, I have defined these functions as A[1] ecc... Now I am satisfied! $\endgroup$ Commented Jan 8, 2018 at 16:06

1 Answer 1

3
$\begingroup$

You can use Symbol function. For example:

For[i = 0, i < 64, i++, Symbol[StringTemplate["A``"]@i][]]

Perhaps it will be faster than ToExpression. Also you can use auxiliary function:

A[i_] := A[i] = Symbol@StringTemplate["A``"]@i

For[i = 0, i < 64, i++, A[i][]]

Previously calculated A[i_] results (symbols) are "cached" by A[i_] := A[i]=....

IMHO best way is to define array (or SparseArray) of functions. Then you can call functions directly by index without extra symbol lookup.

$\endgroup$

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.