8
$\begingroup$

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.

$\endgroup$
  • $\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 Feb 28 at 15:28
  • 2
    $\begingroup$ f @@@ Transpose[{x, y}]. Look up Apply. The @@@ is a shortcut for "Apply at level 1". $\endgroup$ – MarcoB Feb 28 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 Feb 28 at 15:40
  • $\begingroup$ @TimWagner Thank you. That works! $\endgroup$ – Nohora Alejandra Hernndez Cepe Feb 28 at 19:03
11
$\begingroup$
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]
Thread[foo[x,y]]

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

2. Apply + Thread

ClearAll[foo]
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:

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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