# How can I use a loop with an iterator in order to evaluate a function?

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:

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


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

{{1,0},{2,1},{3,2},{4,5}}


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.

• 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.) – Tim Wagner Feb 28 at 15:28
• f @@@ Transpose[{x, y}]. Look up Apply. The @@@ is a shortcut for "Apply at level 1". – MarcoB Feb 28 at 15:37
• 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]. – kglr Feb 28 at 15:40
• @TimWagner Thank you. That works! – Nohora Alejandra Hernndez Cepe Feb 28 at 19:03

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
f[x,y]


{1, 5, 11, 21}

In general, you can use

### 1. Thread

ClearAll[foo]


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

### 2. Apply + Thread

ClearAll[foo]


{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:

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


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

• 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? – Mark R Feb 29 at 5:47
• @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. – kglr Feb 29 at 18:30
• Nice answer. MapThread is the most obvious one to understand. The others are a bit non-obvious. – MikeY Feb 29 at 22:47
• Thanks for letting me know why it is listable - this makes complete sense. – Mark R Mar 1 at 0:56