Search All of the Math Forum:

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

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

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

 Allan Hayes Posts: 1,508 Registered: 12/6/04
Re:[mg4904] programming competition Correction
Posted: Oct 7, 1996 2:01 AM

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@fys.uio.no (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];
Table[p1+(p2-p1)j,{j,0,1-.5/n,1./n}],
{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}

True

Allan Hayes
hay@haystack.demon.co.uk
URL:haystack.demon.co.uk