Mathematica Mathematica Beginner question: Nested loops

AI Thread Summary
The discussion revolves around using Wolfram Mathematica for a sequence of calculations involving arrays. The user, Daniele, seeks help in executing a series of operations that derive new values from initial arrays using loop controls, which they find unintuitive. A contributor suggests using recursive functions instead of loops, providing a step-by-step breakdown of the calculations and introducing a "helper function" to facilitate the process. This function calculates new arrays based on previous values, and the use of NestList is recommended to generate multiple iterations efficiently. Another participant questions the necessity of the helper function, suggesting that direct recursive definitions might be simpler. The conversation highlights the flexibility of Mathematica, noting that there are numerous ways to approach problem-solving within the software, each with its own advantages and complexities. Daniele expresses gratitude for the insights and plans to apply the solutions discussed.
fleuredelis
Messages
3
Reaction score
0
Hello,

I'm quite new to Wolfram Mathematica and do like the elegance of some parts of the language. However, I think that loop controls are quite different and not very intuitive.

Here is a simplified version of what I'm trying to do (now of course done manually):

k[1] = {200, 300, 400, 500}
u[1] = k[1]*1/k[1][[4]]
k[2] = k[1]*u[1]
u[2] = k[2]*1/k[2][[4]]
k[3] = k[2]*u[2]

Basically, there is are some starting value and then a control is derived from that, which, in the next steps creates a new set of values. Problem is that I haven't figured out how to execute the code in sequence with commands like Table or Do.

Any help is highly appreciated!

Thanks a lot already,

Daniele
 
Physics news on Phys.org
Looks like you should use recursive functions, not looping.
 
Very good that you included an example. I hope I understand what you want. If you had included the numeric values for u[2] and k[3] I would have been more certain I understood.

First try this step by step so you can verify this is what you really meant

In[1]:= k1={200,300,400,500};u1=k1*1/k1[[4]]
Out[1]= {2/5, 3/5, 4/5, 1}

In[2]:= k2=k1*u1
Out[2]= {80,180,320,500}

In[3]:= u2=k2*1/k2[[4]]
Out[3]= {4/25, 9/25, 16/25, 1}

In[4]:= k3=k2*u2
Out[4]= {64/5, 324/5, 1024/5, 500}

Now, since you didn't show the actual output, are these what you really want?

If so then let's create a small "helper function" and see if it can find the next u and k

In[5]:= f[{k_,u_}]:={k*k/k[[4]],k/k[[4]]}

In[6]:= f[{{200,300,400,500},{}}]
Out[6]= {{80, 180, 320, 500}, {2/5, 3/5, 4/5, 1}}

In[7]:= f[%]
Out[7]= {{64/5, 324/5, 1024/5, 500}, {4/25, 9/25, 16/25, 1}}

Is the helper function correctly calculating both u and k correctly for each step?
If so then we can use NestList to find these all at once.

In[9]:= result = NestList[f,{{200,300,400,500},{}},3]

Out[9]= {{{200, 300, 400, 500}, {}}, {{80, 180, 320, 500}, {2/5, 3/5, 4/5, 1}}, {{64/5, 324/5, 1024/5, 500}, {4/25, 9/25, 16/25, 1}}, {{1024/3125, 26244/3125, 262144/3125, 500}, {16/625, 81/625, 256/625, 1}}}

You can then use, for example, result[[3,1]] and result[[3,2]] to extract a k or u.
Or perhaps you would like to group the k's together and the u's together.

In[10]:= Transpose[result]
Out[10]= {{{200, 300, 400, 500}, {80, 180, 320, 500}, {64/5, 324/5, 1024/5, 500}, {1024/3125, 26244/3125, 262144/3125, 500}},
{{}, {2/5, 3/5, 4/5, 1}, {4/25, 9/25, 16/25, 1}, {16/625, 81/625, 256/625, 1}}}
 
Bill, a thousand thanks for your detailed and very kind answer! I'll look into the exact application to my problem tomorrow morning, but it looks like the "helper function" in combination with NestList is really what I was missing.

Many thanks again and sorry for bothering you!

Cheers, Daniele
 
I don't understand the advantage of the helper function. I would just implement k and u directly as recursive functions:

k[1] := {200, 300, 400, 500}
u[n_] := k[n]*1/k[n][[4]]
k[n_] := k[n - 1]*u[n - 1]
 
DaleSpam said:
I don't understand the advantage of the helper function.

My understanding from the original description of the problem was that what was desired was table, of unknown size, of these values. So I tried to construct the easiest to understand f that could be handed to NestList to return the desired table and by changing a single number you could ask NestList to return 3 or 1000 of these. The original description was not explicitly clear how the results needed to be organized and that makes it a little difficult to decide how best to try to answer the question. If what was desired was just a single value then what I did was certainly not the simplest way to get there.

As always, particularly in Mathematica, there are at least a dozen ways of doing anything, half of them most people would never think of and some of those nearly incomprehensible.
 
Indeed, the problem is a lot larger than just three iterations. NestList didn't work directly for the problem at hand, but I found another solution that is a lot faster than my previous work around. As has been said there are dozens of ways to solve a problem ;-)

Many thanks again to all of you!
 

Similar threads

Replies
6
Views
4K
Replies
3
Views
2K
Replies
5
Views
2K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
2
Views
6K
Back
Top