RE: Apply and Heads option
- To: mathgroup at smc.vnet.net
- Subject: [mg35238] RE: [mg35213] Apply and Heads option
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Wed, 3 Jul 2002 05:14:47 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
> -----Original Message----- > From: Wolf, Hartmut To: mathgroup at smc.vnet.net > Sent: Tuesday, July 02, 2002 11:51 AM > Subject: [mg35238] RE: [mg35213] Apply and Heads option > > > > > -----Original Message----- > > From: Ersek, Ted R [mailto:ErsekTR at navair.navy.mil] To: mathgroup at smc.vnet.net > > Sent: Tuesday, July 02, 2002 8:13 AM > > Subject: [mg35238] [mg35213] Apply and Heads option > > > > > > Hello mathgroup, > > --------- > > > > Apply has the Heads option. > > Can somebody give an example where > > Apply[f, expr, Heads->True] gives a different result than > > Apply[f, expr, Heads->False]. > > > > OR where > > > > Apply[f, expr, levelspec, Heads->True] gives a different > > result than > > Apply[f, expr, levelspec, Heads->False]. > > > > ---------- > > Thanks, > > Ted Ersek > > > > > > Ted, > > indeed, there is a difference, see e.g. > > In[100]:= > Outer[Apply[ > f,{h, h[x], h[x][h[x]], h[x][h[x][h[x]]], > h[x][h[x][h[x][h[x]]]]},{#1}, > Heads -> #2]&, Range[2,4], {True,False}]//TableForm > Out[100]//TableForm= > h h > h[x] h[x] > f[x][f[x]] h[x][f[x]] > f[x][f[h[x]]] h[x][f[h[x]]] > f[x][f[h[x][h[x]]]] h[x][f[h[x][h[x]]]] > > h h > h[x] h[x] > h[x][h[x]] h[x][h[x]] > h[x][f[x][f[x]]] h[x][h[x][f[x]]] > h[x][f[x][f[h[x]]]] h[x][h[x][f[h[x]]]] > > h h > h[x] h[x] > h[x][h[x]] h[x][h[x]] > h[x][h[x][h[x]]] h[x][h[x][h[x]]] > h[x][h[x][f[x][f[x]]]] h[x][h[x][h[x][f[x]]]] > > or > > In[106]:= Apply[f, h[x][h[x][h[x][h[x]]]], 3, Heads -> True] > Out[106]= f[x][f[f[f[x]]]] > > In[107]:= Apply[f, h[x][h[x][h[x][h[x]]]], 3, Heads -> False] > Out[107]= h[x][f[f[f[x]]]] > > > As far as I can see, > > Apply[f, expr, levelspec, headsopt] > > is the same as > > Replace[expr, _[s___] :> f[s], levelspec, headsopt] > > > > In[147]:= > Table[Replace[h[x][h[x][h[x][h[x]]]], _[s___] -> f[s], {l, ll}, > Heads -> (h == 1)] == > Apply[f, h[x][h[x][h[x][h[x]]]], {l, ll}, Heads -> (h == 1)], > {l, 0, 3}, {ll, l, 3}, {h, 0, 1}] > Out[147]= > {{{True, True}, {True, True}, {True, True}, {True, True}}, > {{True, True}, {True, True}, {True, True}}, > {{True, True}, {True, True}}, > {{True, True}}} > > > -- > Hartmut > some more illustrative examples: In[54]:= expr = ReplaceRepeated[h[x], e_ -> e[e], MaxIterations -> 4] >From In[54]:= ReplaceRepeated::"rrlim": "Exiting after h[x] scanned 4 times." Out[54]= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]]] levels {5},...,{0} Heads -> True, quite interesting... In[60]:= Scan[Print[Apply[f, expr, {#}, Heads -> True]] &, Range[5, 0, -1]] >From In[60]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]]] >From In[60]:= f[x][f[x]][f[x][f[x]]][f[x][f[x]][f[x][f[x]]]][ f[x][f[x]][f[x][f[x]]][f[x][f[x]][f[x][f[x]]]]] >From In[60]:= f[h[x]][f[h[x]]][f[h[x]][f[h[x]]]][f[h[x]][f[h[x]]][f[h[x]][f[h[x]]]]] >From In[60]:= f[h[x][h[x]]][f[h[x][h[x]]]][f[h[x][h[x]]][f[h[x][h[x]]]]] >From In[60]:= f[h[x][h[x]][h[x][h[x]]]][f[h[x][h[x]][h[x][h[x]]]]] >From In[60]:= f[h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]]] levels {4,4},...,{0,4} Heads -> True In[61]:= Scan[Print[Apply[f, expr, {#, 4}, Heads -> True]] &, Range[4, 0, -1]] >From In[61]:= f[x][f[x]][f[x][f[x]]][f[x][f[x]][f[x][f[x]]]][ f[x][f[x]][f[x][f[x]]][f[x][f[x]][f[x][f[x]]]]] >From In[61]:= f[f[x]][f[f[x]]][f[f[x]][f[f[x]]]][f[f[x]][f[f[x]]][f[f[x]][f[f[x]]]]] >From In[61]:= f[f[f[x]]][f[f[f[x]]]][f[f[f[x]]][f[f[f[x]]]]] >From In[61]:= f[f[f[f[x]]]][f[f[f[f[x]]]]] >From In[61]:= f[f[f[f[f[x]]]]] levels {5},...,{0} Heads -> False, comparatively dull... In[62]:= Scan[Print[Apply[f, expr, {#}, Heads -> False]] &, Range[5, 0, -1]] >From In[62]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]]] >From In[62]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][f[x]]]]] >From In[62]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][f[h[x]]]]] >From In[62]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][f[h[x][h[x]]]]] >From In[62]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][f[h[x][h[x]][h[x][h[x]]]]] >From In[62]:= f[h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]]] levels {4,4},...,{0,4} Heads -> False In[63]:= Scan[Print[Apply[f, expr, {#, 4}, Heads -> False]] &, Range[4, 0, -1]] >From In[63]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][f[x]]]]] >From In[63]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][h[x][h[x]][f[f[x]]]]] >From In[63]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][ h[x][h[x]][h[x][h[x]]][f[f[f[x]]]]] >From In[63]:= h[x][h[x]][h[x][h[x]]][h[x][h[x]][h[x][h[x]]]][f[f[f[f[x]]]]] >From In[63]:= f[f[f[f[f[x]]]]] -- Hartmut