Re:programming competition Correction
- To: mathgroup at smc.vnet.net
- Subject: [mg5031] Re:[mg4904] programming competition Correction
- From: espen.haslund at fys.uio.no (Espen Haslund)
- Date: Sat, 19 Oct 1996 16:40:34 -0400
- Organization: Universitet i Oslo
- Sender: owner-wri-mathgroup at wolfram.com
In article <53a6b6$oa6 at dragonfly.wolfram.com>, hay at haystack.demon.co.uk says... > >Please change line 6 in the code that I gave from > If[n<2, {#1}, >to If[n<1, {#1}, > >giving > >linearInterpolate2[ li_List, m_ ] := > Block[{}, > Append[ > Join@@Partition[ li, 2,1, > ( n=Floor[Sqrt[(#2-#1).(#2-#1)//N]/m]; > If[n<1, > {#1}, > Table[#1 + k #2, {k,0,n}]&[#1,(#2-#1)/(n+1)], > > ] > )& > ], > Last[li] > ] > ]; > >(the error crept in because of changing from Ceiling to Floor) > >I time it (and check it!) against the fastest code in Xah Lee's >posting "[mg4900] Re: programming competition", due to >espen.haslund at fys.uio.no (Espen Haslund): > .... >Allan Hayes >hay at haystack.demon.co.uk >URL:haystack.demon.co.uk > > > Hi Allan; I have made some improvements on my function, and it now runs as fast as yours on the random data, and a bit faster in some other cases. Note: I think you should have a "n" in your Block (i.e. Block[{n}, ) also note that changing from Ceiling to Floor make a small difference in special cases (i.e. when the distance between points is exactly equal to maxLength) linearInterpolateEH3[ li_List, maxLength_ ]:= Module[{p1, d, n}, Append[Join @@ Table[ d = N[li[[i+1]] - (p1 = li[[i]])]; n = Ceiling[Sqrt[#.#]&[d]/maxLength]; If[n > 1, Table[p1 + d*j,{j,0,1-.5/n,1./n}] , {p1} ], {i,Length[li]-1}], Last[li] ] ] li = Table[{Random[], Random[]},{1000}]; tim1 = Map[First, { Timing[linearInterpolateEH3[li, .1] ], Timing[linearInterpolate2[li, .1] ]}]; li = Table[{i,i},{i,100}]; tim2 = Map[First, { Timing[linearInterpolateEH3[li, 0.02] ], Timing[linearInterpolate2[li, 0.02] ]}]; li = Table[{i,i},{i,3000}]; tim3 = Map[First, { Timing[linearInterpolateEH3[li, 2] ], Timing[linearInterpolate2[li, 2] ]}]; res = {{"linearInterpolateEH3", "linearInterpolate2"}, tim1, tim2, tim3} //TableForm (* Out[2]//TableForm= linearInterpolateEH3 linearInterpolate2 15.16 Second 15.77 Second 12.3 Second 18.18 Second 12.58 Second 14.06 Second *) -Espen