Re: What is happening here?

*To*: mathgroup at smc.vnet.net*Subject*: [mg22115] Re: [mg22107] What is happening here?*From*: Hartmut Wolf <hwolf at debis.com>*Date*: Wed, 16 Feb 2000 02:34:31 -0500 (EST)*Organization*: debis Systemhaus*References*: <200002140703.CAA12418@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Jordan Rosenthal schrieb: > > Hi all, > > I finally had a chance to try and compare all the different methods people > sent me for computing the matrix > > ( 1 0 0 ) > ( 2 1 0 ) > ( 3 2 1 ) > ( 0 3 2 ) > ( 0 0 3 ) > > from the vector {1,2,3} (using much larger vectors). I am running into a > strange problem that I can't explain. > > I started by creating a function for each contributer. Here is an example > for two of the defintions (picked arbitrarily for this example because they > were first and last in the alphabet): > > ------------------------------------------------------- > f["Paul Abbot"][v_?VectorQ] := Module[ > {n = Length[v]}, > NestList[RotateRight, > Reverse[PadRight[PadLeft[v, 2n - 1], 3n - 2]], 2n - 2][[All, > Range[2n - 1, 3n - 2]]] > ] > > f["Hartmut Wolf"][v_?VectorQ] := With[{r = Length[v] - 1}, > Transpose[NestList[RotateRight, Join[v, Table[0, {r}]], r]]] > ------------------------------------------------------- > > Running the following code (based on a suggestion from Hartmut Wolf) works > great: > > ------------------------------------------------------- > vLarge = Range[400]; > > List @@ (Part[#, 1, 1] &) /@ > Timing /@ Through[ Hold[f["Paul Abbot"], f["Hartmut Wolf"]][vLarge]] > > {0.49 Null, 0.27 Null} > ------------------------------------------------------- > > This works great; it gives me a list of timings for the two methods. But > because I have a large list I wanted to instead run code like this (where > authornames would be a larger vector): > > ------------------------------------------------------- > authornames = {"Paul Abbot", "Hartmut Wolf"}; > > List @@ (Part[#, 1, 1] &) /@ > Timing /@ Through[ FullForm[Hold @@ (f /@ authornames)][vLarge]] > > {0. Null} > ------------------------------------------------------- > > but this doesn't work the same way. So I tried to compare the difference > between the two versions by checking the difference between the code I > replaced and my code. Shown below, the two expressions have the same > FullForms and Attributes: > > ------------------------------------------------------- > expr1 = Hold[ f["Paul Abbot"], f["Hartmut Wolf"] ]; > expr2 = Hold @@ (f /@ authornames); > > FullForm[expr1]==FullForm[expr2] > True > > Attributes[expr1]==Attributes[expr2] > True > > expr1 == expr2 > True > ------------------------------------------------------- > > If the two expression have the same FullForm and Attributes, why do I not > get the same results if I substitute one for the other? > > Humbly perplexed, > > Jordan Hi Jordan, first let me give you the solution: {authornames, List @@ (Part[#, 1, 1] &) /@ Timing /@ Through[(Hold @@ f /@ authornames)[vLarge]]} // Transpose // TableForm Out[..]//TableForm= Paul Abbot 0.311 Hartmut Wolf 0.19 So you see that you nearly got it. However you spoiled it through the unneccessary introduction of FullForm. To understand what happend compare: Through[(Hold @@ f /@ authornames)[actual]] Hold[f["Paul Abbot"][actual], f["Hartmut Wolf"][actual]] ...with Through[FullForm[Hold @@ f /@ authornames][actual]] Out[..]//FullForm= Hold[f["Paul Abbot"], f["Hartmut Wolf"]][actual] Now FullForm is "invisible", so let's replace it with g: Through[g[Hold @@ f /@ authornames][actual]] g[Hold[f["Paul Abbot"], f["Hartmut Wolf"]][actual]] You have to look very close at the last result: Through applies the (first) arguments of g to the (final) argument (here: 'actual'), and then wraps that again with g. But the first argument of g -- there is only one here -- was Hold[f[...],f[...]] So here FullForm acts like any function, you only can't see it in the output, i.e. you don't see the head and you don't see the brackets around its arguments. Now you'll find in Help: * FullForm acts as a "wrapper", which affects printing, but not evaluation. The last subordinate clause seems not to be valid here. You can try to repair that by defining an upvalue for FullForm. But only to run into the next problem with Timing. There defining an upvalue won't help, and you'r left with the execution time for "FullForm" which is 0. Kind regards, Hartmut

**References**:**What is happening here?***From:*"Jordan Rosenthal" <jr@ece.gatech.edu>