4
$\begingroup$

I have a list of points $$L = \{\{x_1,y_1\}, \{x_2,y_2\}, \{x_3,y_3\},\cdots\{x_n,y_n\}\}$$ and I want to create a list $$L1 = \{f(x_1,y_1), f(x_2,y_2), \cdots, f(x_n,y_n)\},$$ where $f$ is some function. Let us take the example $f(x,y) = x^i y^j.$

Is there a simple direct method? In my example, would it be possible to get the list $\{x_1^iy_1^j, x_2^iy_2^j,\cdots x_n^iy_n^j\}$ directly without defining $f(x,y) = x^i y^j$?

This might be a trivial question, but not for people who used to use Python.

$\endgroup$
4
  • $\begingroup$ If alist = {{x1, y1}, {x2, y2}, {x3, y3}};, then f @@@ alist will do this. Or the more verbose way is to do: Map[Apply@f, alist, {1}]. $\endgroup$
    – Syed
    Feb 1, 2023 at 15:03
  • 2
    $\begingroup$ f /@ points if f takes a list e.g f[{x,y}], otherwise just f@@@points. This is all covered in the documentation so I'm voting to close. If you want to also pass an index, then use MapIndexed. $\endgroup$
    – flinty
    Feb 1, 2023 at 15:11
  • 1
    $\begingroup$ #1^i #2^j & @@@ {{x1, y1}, {x2, y2}, {x3, y3}} $\endgroup$
    – Syed
    Feb 1, 2023 at 15:21
  • $\begingroup$ I’m voting to close this question because the OP needs to spend time learning the tool and show minimal knowledge of Mathematica before posting questions. I also wish the OP good luck. $\endgroup$
    – Syed
    Feb 1, 2023 at 15:23

2 Answers 2

6
$\begingroup$
Clear["Global`*"]

Format indexed variables as subscripts

(Format[#[n_]] := Subscript[#, n]) & /@ {x, y};

pts = Array[{x[#], y[#]} &, 5]

enter image description here

Map

f /@ pts

enter image description here

ex1 = #[[1]]^i*#[[2]]^j & /@ pts

enter image description here

MapApply

f @@@ pts

enter image description here

ex2 = #1^i*#2^j & @@@ pts

enter image description here

ex1 === ex2

(* True *)
$\endgroup$
1
  • $\begingroup$ Thank you. This is informative. $\endgroup$
    – A. PI
    Feb 1, 2023 at 21:43
4
$\begingroup$

Another approach (which I learned from this post by bmf) is to use ArrayReduce

m = Array[{Subscript[x, #],Subscript[y,#]} &, 5]
{{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4 }, {x5, y5}}
ArrayReduce[f, m, 2]
{f[{x1, y1}], f[{x2, y2}], f[{x3, y3}], f[{x4, y4}], f[{x5, y6}]} 
ArrayReduce[Times@@{#[[1]]^i,#[[2]]^j}&, m,2]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Thank you. I liked this approach too. $\endgroup$
    – A. PI
    Feb 1, 2023 at 21:44
  • 1
    $\begingroup$ (+1) I am very glad to see that ArrayReduce is gaining popularity! $\endgroup$
    – bmf
    Feb 2, 2023 at 0:24

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