Mathematica:Iteration of pairs in a list

  • Context: Mathematica 
  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    List
Click For Summary

Discussion Overview

The discussion revolves around iterating pairs of values generated by two functions in Mathematica. Participants explore methods to apply these functions over multiple iterations, including the use of NestList and Table, while also considering initial conditions and transformations on the pairs.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant demonstrates how to generate pairs using the functions j[x_] and k[x_] with Table for the first four terms.
  • Another participant points out the challenge of feeding different elements from a list of pairs into the functions during subsequent iterations.
  • Multiple methods are suggested for generating the desired output, including using NestList and alternative approaches with Table.
  • A participant asks about implementing initial conditions to modify the first and second elements of the pairs differently.
  • Another participant provides a specific example of applying a transformation to the pairs, doubling the first element and halving the second, and shares the resulting output.

Areas of Agreement / Disagreement

Participants present various methods and approaches without reaching a consensus on a single solution. The discussion remains open with multiple competing views on how to achieve the desired iterations and transformations.

Contextual Notes

Some methods rely on specific assumptions about the structure of the input lists and the behavior of the functions, which may not be universally applicable. The effectiveness of different approaches may depend on the specific requirements of the iteration process.

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}}
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K