I have defined a function $f$ from number to number. I want to tell Mathematica to apply that function to each entry of a high dimensional array $A$, to get a new array $f(A)$ in the same form of $A$. Mathematica automatically does that for simple functions like sine, powers, logarithms, etc. How do I do it for my defined functions?

  • $\begingroup$ You may want to look at Map[f, expr, levelspec] as in the documentation. $\endgroup$ – dr.blochwave Oct 6 '15 at 9:05
  • 1
    $\begingroup$ Also, lookup Listable. $\endgroup$ – yohbs Oct 6 '15 at 9:07
  • $\begingroup$ @yohbs Listable is exactly what I needed! Thanks. $\endgroup$ – geodude Oct 6 '15 at 9:11

Set the attribute Listable on the function.

SetAttributes[f, Listable]

Then it will auto-thread over arrays:

f[{1, 2, 3}]
(* {f[1], f[2], f[3]} *)

Sin, Cos, etc. also have this attribute:

(* {Listable, NumericFunction, Protected} *)

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