1
$\begingroup$

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?

$\endgroup$

closed as off-topic by yohbs, Kuba, gpap, MarcoB, dr.blochwave Oct 6 '15 at 12:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – yohbs, Kuba, gpap, MarcoB, dr.blochwave
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\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
1
$\begingroup$

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:

Attributes[Sin]
(* {Listable, NumericFunction, Protected} *)
$\endgroup$

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