2
$\begingroup$

I have a set of points {x,y} given by the following:

LIST1 = {{2, 10}, {4, 12}, {6, 17}, {8, 20}, {10, 25}, {12, 23}, {14, 
32}, {16, 34}, {16, 33}, {18, 56}, {20, 43}, {22, 67}, {24, 55}}; 

I have a function defined as MYFUN[x_,y_]=2y^2 - 4x +1. I want to compute the value of this fucntion for every pair of (x,y) given in LIST1 and form the LIST2 = {x,MYFUN[x,y]}. Is there a quick way of doing this? I tried by putting each point in the function, but it takes lot of time.

MYFUN = 2 (12  )^2 - 4 (4)  + 1


 LIST2 = {{2, 193}, {4, 
    273}, {6,}, {8,}, {10,}, {12,}, {14,}, {16,}, {18,}, {20,}, \
{22,}, {24,}};

I don't know why I gave this question a horrible title before " Parameter as a variable".

$\endgroup$
4
  • 2
    $\begingroup$ Have a look at this tutorial about defining function and this tutorial about applying functions to stuff. Both of them should be really easy to find in the documentation as well. $\endgroup$
    – Sascha
    Sep 20, 2017 at 6:59
  • $\begingroup$ You can do it like this: myFun[x_, y_] := 2 y^2 - 4 x + 1; Transpose@{list1[[;; , 1]], myFun @@@ list1}. Usually, user defined functions and variables start with a lower-case letter to differentiate from the built-in functions. $\endgroup$ Sep 20, 2017 at 7:05
  • $\begingroup$ @Sascha, thanks. I just wanted to know whether I could get my LIST2 by some quick way. $\endgroup$
    – Jee
    Sep 20, 2017 at 7:05
  • $\begingroup$ Easiest to understand is maybe define your function to receive a {x, y} tuple as in myFun[{x_, y_}] := 2 y^2 - 4 x + 1 and then map this function to your list of tuples via Map[myFun]@LIST1. If you also want every x together with the result you can wrap this in another function and map the resulting function: Map[Function[p, {First[p], myFun[p]}]]@LIST1 $\endgroup$
    – Sascha
    Sep 20, 2017 at 7:23

4 Answers 4

4
$\begingroup$
MYFUN = {#, 2 #2^2 - 4 # + 1} &;
MYFUN @@@ LIST1

{{2, 193}, {4, 273}, {6, 555}, {8, 769}, {10, 1211}, {12, 1011}, {14, 1993}, {16, 2249}, {16, 2115}, {18, 6201}, {20, 3619}, {22, 8891}, {24, 5955}}

a @@@ b means Apply[a, b, {1}]. For large lists, this will be more efficient:

Transpose[MYFUN @@ Transpose[LIST1]]
$\endgroup$
4
$\begingroup$

For better or worse, Map is my default for anything over a list. Define

LIST1 = {{2, 10}, {4, 12}, {6, 17}, {8, 20}, {10, 25}, {12, 23}, {14, 
    32}, {16, 34}, {16, 33}, {18, 56}, {20, 43}, {22, 67}, {24, 55}};
MYFUN[x_, y_] = 2 y^2 - 4 x + 1

Then using Map (/@) and Apply (@@)

{#[[1]], MYFUN @@ ##} & /@ LIST1

{{2, 193}, {4, 273}, {6, 555}, {8, 769}, {10, 1211}, {12, 1011}, {14, 1993}, {16, 2249}, {16, 2115}, {18, 6201}, {20, 3619}, {22, 8891}, {24, 5955}}

$\endgroup$
2
$\begingroup$
 MYFUN[x_, y_] = 2 y^2 - 4 x + 1  
{#, MYFUN[##]} & @@@ LIST1
$\endgroup$
1
$\begingroup$

You should avoid uppercase variables and function names, see this answer.

Also, look at the documentation for Thread as an alternative to the other solutions.

list1 = {{2, 10}, {4, 12}, {6, 17}, {8, 20}, {10, 25}, {12, 23}, {14, 
    32}, {16, 34}, {16, 33}, {18, 56}, {20, 43}, {22, 67}, {24, 55}};

ClearAll[myfunc];
myfunc[{x_, y_}] := {x, 2 y^2 - 4 x + 1}

Thread@myfunc@list1
(* {{2, 193}, {4, 273}, {6, 555}, {8, 769}, {10, 1211}, {12, 
  1011}, {14, 1993}, {16, 2249}, {16, 2115}, {18, 6201}, {20, 
  3619}, {22, 8891}, {24, 5955}} *)
$\endgroup$

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