# Priority of nested function in Mathematica

I am facing a small problem with the nested function in Mathematica. Please take a look at the attached Code below.

T2 = 2/3;
VR = 0.6;
VDD = 3;
zb = 1+T2;

Nest[(Piecewise[{1+T1+(T1/T2)*(1-#), 0 <= # < zb}, {# - T2 , zb < # <= VDD/VR}}]) &, Range [ 1,3,1], 1]


I got the output:

{1+T1,1-T1/2,1-2 T1}    0<={1,2,3}<5/3
5/3                     5/3<={1,2,3}<=5.
0                       True


I want the Piecewise function takes the highest priority before Nest function. This means depending on the value of parameter (x or #), the Piecewise function decides which "piecewise" will be used.

Piecewise function:

= 1+T1+(T1/T2)*(1-x) if 0 < x < zb


or

= 1-T2               if zb < x < VDD/VR


For example: zb = 1+T2 (zb is the boundary condition) => with T2 = 2/3 we have zb = 5/3. So if x = 1, The output of piecewise function will be 1+T1+(T1/T2)*(1-x). After that, the Nest function will be implemented continuously with x = 1, 2, 3 and FINAL output will be a vector (with T1 is the parameter).

Also, I would like to give another example with the expression inside the Nest function is T1*x*(1-x) and its output what i want with my Piecewise function

Nest[T1 #(1-#)&,Range[0.01,0.03,0.01],1]


And the output:

{0.0099 T1,0.0196 T1,0.0291 T1}


BRs,

Ha

• @NguyenHa maybe this then: Nest[ Map[Piecewise[ {{1 + T1 + (T1/T2)*(1 - #), 0 <= # < zb}, {# - T2, zb < # <= VDD/VR}} ] &], Range[1, 3, 1], 1] but T1 needs to be defined. – Kuba Nov 22 '17 at 19:30
• @Kuba Thank you so much for your comment. It works perfectly now. Really appreciate your help. BRs - Ha – Nguyen Ha Nov 22 '17 at 20:49

Nest[f, arg, n] applies f to arg

Nest[f, {a1, a2, a3}, 2] (* f[f[{a1, a2, a3}]] *)


but from your description it is clear that you want to map f /@ arg, so one step is missing.

Quick fix is to add Map, in operator form:

Nest[Map[f], {a1, a2, a3}, 2] (*{f[f[a1]], f[f[a2]], f[f[a3]]}*)


or classical:

Nest[Map[f, #] &, {a1, a2, a3}, 2]
(*don't forget & for your f if f is explicitly written with slots # e.g.:*)

Nest[Map[#^2 &(*!*), #] &(*!*), {a1, a2, a3}, 2]


sometimes Map is not needed because certain functions automatically thread over Lists, e.g. Plus/Times/Power etc. So the above example can be written:

Nest[#^2 &, {a1, a2, a3}, 2]


but Piecewise is not doing this so you need Map.

p.s. another way is to make your function listable:

Nest[Function[a, Panel[a], Listable], {a1, a2, a3}, 2]


The last one is just a teaser and not as general as initial examples. I'm encouraging you to do help lookup or search around to learn more.