Re: Suggestions for translating a Do[] loop ...
- To: mathgroup at smc.vnet.net
- Subject: [mg69800] Re: [mg69773] Suggestions for translating a Do[] loop ...
- From: "Adriano Pascoletti" <pascolet at dimi.uniud.it>
- Date: Sat, 23 Sep 2006 04:45:03 -0400 (EDT)
David,
consider two lists
In[1]:=
n = 10; A = Array[Subscript[a, #1] & , {n - 1}];
B = Array[Subscript[b, #1] & , {n - 1}];
the list r is computed by
In[3]:=
r = Reverse[FoldList[#2[[1]]*((#2[[2]] + #1)/
(1 + #2[[2]] + #1)) & , 0,
Transpose[{Reverse[A], Reverse[B]}]]];
In order to verify the result let
In[4]:=
f = Function[i, A[[i]]*((B[[i]] + r[[i + 1]])/
(1 + B[[i]] + r[[i + 1]]))];
and compute
In[5]:=
Function[i, f[i] - r[[i]]] /@ Range[n - 1]
Out[5]=
{0,0,0,0,0,0,0,0,0}
Hope it helps
Adriano Pascoletti
David Annetts wrote ..
> Hi,
>
> Given that a, b & r are lists of the same length, can anyone suggest a
> translation of the following Do[] loop to something functional? It looks
> like an ideal application for Fold, but for me, the loop going backwards
> is
> really obscuring things.
>
> The loop is
>
> Do[
> r[[j]] = a[[j]] * (b[[j]] + r[[j + 1]]) / (1 + b[[j]] * r[[j
> + 1]]),
> {j, Length@a - 1, 1, -1}
> ];
>
> with r[[Length@a]] = 0.
>
> Many thanks,
>
> Dave.
>