I'm trying to evaluate a function, that depends on two variables, in a specific set of values. I will write an example, but my system is more complicated.

f[x_,y_]:= x^2+y;

x and y must be valued for the following list of data:


The thing is that I need to evaluate f[x,y] for each pair of coordinates [x,y]:


with the help of a loop, for example Do. This, because my system is more complicated and I can not do it manually (It will take me a lot of time). I was trying to use the loop Do with the iterator Thread, nonetheless I don´t know if this is possible .

I'm new using Mathematica. I really don't understand why this is happening. Some of you guys can help me? Thank you.

  • $\begingroup$ If you only need to input pairs where the x and y values have the same index position in the list, you could use Table[f[x[[i]], y[[i]]], {i, 1, Length[x]}]. (This wouldn't iterate over all possible ordered pairs of elements from the lists x and y.) $\endgroup$
    – Tim Wagner
    Commented Feb 28, 2020 at 15:28
  • 2
    $\begingroup$ f @@@ Transpose[{x, y}]. Look up Apply. The @@@ is a shortcut for "Apply at level 1". $\endgroup$
    – MarcoB
    Commented Feb 28, 2020 at 15:37
  • 1
    $\begingroup$ just use f[x,y] , that is, x = {1, 2, 3, 4}; y = {0, 1, 2, 5};f[x, y] (because f as defined is a Listable function). For an arbitrary function g[u,v] defined for ordered pair {u,v} you can give it the attribute Listable using SetAttributes[g, Listable] to make it automatically thread over its arguments so that for lists ul and vl of the same length, you can simply use g[ul, vl]. $\endgroup$
    – kglr
    Commented Feb 28, 2020 at 15:40
  • $\begingroup$ @TimWagner Thank you. That works! $\endgroup$ Commented Feb 28, 2020 at 19:03

1 Answer 1

x = {1, 2, 3, 4};
y = {0, 1, 2, 5};

Listable >> Properties and Relations:

"A function implemented in terms of a listable operation may not need the Listable attribute:"

Since f is defined in terms of Listable operations (Plus and Power) it automatically threads over list arguments. So you can simply use f[x,y] for list inputs x and y:

f[x_,y_] := x^2+y

{1, 5, 11, 21}

In general, you can use

1. Thread


{foo[1, 0], foo[2, 1], foo[3, 2], foo[4, 5]}

2. Apply + Thread

foo @@@ Thread[{x, y}]

{foo[1, 0], foo[2, 1], foo[3, 2], foo[4, 5]}

3. MapThread

MapThread[foo, {x, y}]

{foo[1, 0], foo[2, 1], foo[3, 2], foo[4, 5]}

4. SetAttributes + Listable:

SetAttributes[foo, Listable]
foo[x, y]

{foo[1, 0], foo[2, 1], foo[3, 2], foo[4, 5]}

  • $\begingroup$ Very educational answer. It is interesting but not intuitive that f is listable, yet as you say, it works. I've tried Attributes[f] and "?f" to see what it says. How would we know that f is listable? $\endgroup$
    – Mark R
    Commented Feb 29, 2020 at 5:47
  • 1
    $\begingroup$ @MarkR, although f does not have any attributes it is effectively Listable since FullForm[f[x,y] is Plus[Power[x, 2], y] and both Power and Plus are Listable. $\endgroup$
    – kglr
    Commented Feb 29, 2020 at 18:30
  • 1
    $\begingroup$ Nice answer. MapThread is the most obvious one to understand. The others are a bit non-obvious. $\endgroup$
    – MikeY
    Commented Feb 29, 2020 at 22:47
  • $\begingroup$ Thanks for letting me know why it is listable - this makes complete sense. $\endgroup$
    – Mark R
    Commented Mar 1, 2020 at 0:56

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.