5
$\begingroup$

The @ and /@ functions do the same thing:

Factorial @ List[1, 2, 3, 4, 5, 6]
{1, 2, 6, 24, 120, 720}

Factorial /@ List[1, 2, 3, 4, 5, 6]
{1, 2, 6, 24, 120, 720}

What is the / for? I've looked all over and can't find any explanation for it.

Edit:

Using f gives the same result:

f[x_] := x + 1

f @ List[1, 2, 3, 4, 5, 6]
{2, 3, 4, 5, 6, 7}

f /@ List[1, 2, 3, 4, 5, 6]
{2, 3, 4, 5, 6, 7}
$\endgroup$
5
  • 3
    $\begingroup$ The /@ is called Map, it happens to do the same thing as @ in this case because Factorial automatically maps over lists. Try the same thing with f instead of Factorial and you’ll see a difference. $\endgroup$ Oct 19 at 2:36
  • 2
    $\begingroup$ Attributes[Factorial] ,and see Listable . $\endgroup$
    – cvgmt
    Oct 19 at 2:38
  • $\begingroup$ See mathematica.stackexchange.com/a/25616/4999 $\endgroup$
    – Michael E2
    Oct 19 at 2:41
  • $\begingroup$ Tanner - Sqrt does the same thing. Is f limited to certain types of functions? $\endgroup$ Oct 19 at 2:55
  • $\begingroup$ Michael E2 - that doesn't help in this case because the explanation about Listable is missing. $\endgroup$ Oct 19 at 3:17
10
$\begingroup$

The difference is that for an arbitrary function f,

  • f @ {a,b,c,...} yields f[{a,b,c,...}];
  • f /@ {a,b,c,...} yields {f[a], f[b], f[c], ...}.

The two input forms are equivalent for Factorial because it's what's called a Listable function. For a listable function f, the input f[{a,b,c,...}] automatically evaluates to {f[a], f[b], f[c], ...}. So f @ {a,b,c,...}, in the end, gets you the same thing as f /@ {a,b,c,...}.

To see the difference, look at (for example) the function Max instead. When Max is provided with one or more numbers as input, it returns the largest of those numbers. When it is provided with one or more lists, it returns the largest value in any of the lists. So:

  • Max @ {1,2,3,4,5,6} is equivalent to Max[{1,2,3,4,5,6}]. Since the argument is a list, Mathematica returns the largest value in the list, 6.
  • Max /@ {1,2,3,4,5,6} is equivalent to {Max[1], Max[2], ...}. In each instance, Max is provided with a single number, and so returns that number. So the output is {1,2,3,4,5,6}.
$\endgroup$
2
  • $\begingroup$ That's the best explanation I've found anywhere, and I've looked. The key is to know that you have to look at the docs for Listable functions, and newcomers to Mathematica won't know that. Unfortunately book authors don't help in that regard. $\endgroup$ Oct 19 at 3:12
  • 3
    $\begingroup$ Read Leonid shifrin's book. It's free and is a fantastic resource. mathprogramming-intro.org $\endgroup$
    – Lou
    Oct 19 at 5:10

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