The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Re:[mg4904] programming competition Correction
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Allan Hayes

Posts: 1,508
Registered: 12/6/04
Re:[mg4904] programming competition Correction
Posted: Oct 7, 1996 2:01 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Please change line 6 in the code that I gave from
If[n<2, {#1},
to If[n<1, {#1},


linearInterpolate2[ li_List, m_ ] :=
Join@@Partition[ li, 2,1,
( n=Floor[Sqrt[(#2-#1).(#2-#1)//N]/m];
Table[#1 + k #2, {k,0,n}]&[#1,(#2-#1)/(n+1)],


(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):

linearInterpolateEH2[ li_List, maxLength_ ]:=
Module[{p1, p2, n},
Append[Join @@ Table[{p1, p2} = li[[{i, i+1}]];
n = Max[Ceiling[Sqrt[#.#]&[N[p2-p1]]/maxLength],1];
{i,1,Length[li]-1}], Last[li] ]

li = Table[{Random[], Random[]}, {1000}];
maxLength = .1;

a4 = Timing@ linearInterpolateEH2[ li, maxLength];
a5 = Timing@ linearInterpolate2[ li, maxLength];

First /@ {a4,a5}

SameQ@@ Last /@ SetPrecision[{a4,a5},5]

{21.3 Second, 14.8333 Second}


Allan Hayes

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.