3
$\begingroup$

I have a very large number of functions defined in the following manner:

f[1] = Function[{x}, x^2];

f[2] = Function[{x}, x^3];

I would like to make all of these Listable. Is there any simple way of doing this? I have tried:

Attributes[f[2]] = {Listable};

and

SetAttributes[f[2],Listable];

both of which failed miserably!

Thanks!

$\endgroup$
6
  • 3
    $\begingroup$ AppendTo[f[1], Listable]? $\endgroup$
    – Carl Woll
    Commented May 3, 2017 at 23:13
  • 2
    $\begingroup$ Your functions (at least those you have as your examples) are already Listable, because Power operation maintains listability. Moreover, if you give them explicit Listable attribute, you will only make matters worse. Read this. $\endgroup$ Commented May 3, 2017 at 23:14
  • $\begingroup$ My actual functions are way more complicated, this was just an example to see if this is possible. $\endgroup$
    – user12588
    Commented May 3, 2017 at 23:17
  • $\begingroup$ Try this f[1] = Function[{x}, x^2]; f[2] = Function[{x}, x^3]; SetAttributes[f, Listable]; f[{1, 2}] but I doubt this is what you really want to do. $\endgroup$
    – Bill
    Commented May 3, 2017 at 23:18
  • 7
    $\begingroup$ What @CarlWoll suggested may be a bit hard to understand, but he meant this: f[1] = Function[{x}, x^2, Listable]. Pure functions can have a subset of symbol attributes (including Listable), and then you specify them as a third optional argument to Function. Still, read the link I provided, to not run into nasty performance surprises. Because adding Listable attribute won't make the performance better, but can easily make it worse, so adding it for performance reasons doesn't make much sense, and can be counterproductive. $\endgroup$ Commented May 3, 2017 at 23:20

0

Browse other questions tagged or ask your own question.