Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

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},

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







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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.