Mathematica:Iteration of pairs in a list

  • Context: Mathematica 
  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    List
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 4K views
Sarah rob
Messages
16
Reaction score
0
If I want to put these two functions
j[x_] := 2 x
k[x_] := 3 x
in pairs e.g. for the 1 st 4 terms of the functons i done
Table[{j[x], k[x]}, {x, 1, 4}]
to get
{{2, 3}, {4, 6}, {6, 9}, {8, 12}}
However i want to run this for several interations so that I get
{{2, 3}, {4, 6}, {6, 9}, {8, 12}}
{{4, 9}, {8, 18}, {12, 27}, {16, 32}}
{{8, 27}, {16, 54}, {24, 81}, {32, 96}}
etc ...
I am trying to use NestList but it doesn' t work with Table, is there
another way other than Table I can combine the functions
 
Physics news on Phys.org
The obstacle is that the first time you go through the iteration, you feed j[x] and k[x] the same number. The subsequent times you must feed them the different elements in a list of pairs.

An ugly way is something like
Code:
In[1]:= j[x_]:=2 x
        k[x_]:=3 x
In[3]:= iter1=Table[{j[x],k[x]},{x,1,4}]
Out[3]= {{2,3},{4,6},{6,9},{8,12}}
In[4]:= iter2=Table[{j[#1],k[#2]}&@@x, {x,iter1}]
Out[4]= {{4,9},{8,18},{12,27},{16,36}}
In[5]:= iter3=Table[{j[#1],k[#2]}&@@x, {x,iter2}]
Out[5]= {{8,27},{16,54},{24,81},{32,108}}

If you just want to create the lists that you want, then maybe something like
Code:
In[6]:= Table[{2^n i,3^n i},{n,0,3},{i,1,4}]//Grid
Out[6]= {1,1}	{2,2}	{3,3}	{4,4}
        {2,3}	{4,6}	{6,9}	{8,12}
        {4,9}	{8,18}	{12,27}	{16,36}
        {8,27}	{16,54}	{24,81}	{32,108}

The same output can be created using NestList, e.g.
Code:
NestList[{j@#1,k@#2}&@@@#&,{Range[1,4],Range[1,4]}\[Transpose],3]//Grid
or
Code:
NestList[{2,3}#&/@#&,{Range[1,4],Range[1,4]}\[Transpose],3]//Grid
etc... none of the NestList solutions I came up with were particularly satisfactory.
 
Thanks for that.
Using one of the codes you suggested, is there a way to implement it with initial conditions, so starting with pairs of nuumbers and applying a function to all the 1st elements and another function to and the 2nd elements in the list.
 
Let's say I want twice the first element and half the second.

t = Table[{2^n i, 3^n i}, {n, 0, 3}, {i, 1, 4}];
funpair[list_]:={2list[[1]], list[[2]]/2};
Table[t[[j, i]]//funpair, {j, 1, 4}, {i, 1, 4}]

generates:

{
{{2, 1/2}, {4, 1}, {6, 3/2}, {8, 2}},
{{4, 3/2}, {8, 3}, {12, 9/2}, {16, 6}},
{{8, 9/2}, {16, 9}, {24, 27/2}, {32, 18}},
{{16, 27/2}, {32, 27}, {48, 81/2}, {64, 54}}
}