Re: Re: programming competition
- To: mathgroup at smc.vnet.net
- Subject: [mg4904] Re: [mg4866] Re: programming competition
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Fri, 4 Oct 1996 00:17:40 -0400
- Sender: owner-wri-mathgroup at wolfram.com
rhall2 at umbc.edu (hall robert) [mg4866] Re: programming competition Bob, considers Xah Lee's problem (at [1] below), and introduces a solution (at[2]) with the remark >With Lichtblau's and Hayes' admonitions to use procedural >programming to solve procedural problems ringing freshly in my ears, >I offer the following: Still, my preference is definitely for functional and list processing styles and I strongly recommend them (see Experiments in Efficient Programming, The Mathematica Journal, 5, No 1, 24-31, 1995.). Here is another solution to Xah's problem linearInterpolate2[ li_List, m_ ] := Block[{n}, Append[ Join@@Partition[ li, 2,1, ( n=Floor[Sqrt[(#2-#1).(#2-#1)//N]/m]; If[n<2, {#1}, Table[#1 + k #2, {k,0,n}]&[#1,(#2-#1)/(n+1)] ] )& ], Last[li] ] ]; (*check*) linearInterpolate2[{{0, 0}, {1, 1}, {1, 1}, {6/5, 1}, {7/5, 1}, {8/5, 1}, {9/5, 1}, {2, 1}, {2, 1}, {3, 3}}, .3 ] {{0, 0}, {0.2, 0.2}, {0.4, 0.4}, {0.6, 0.6}, {0.8, 0.8}, {1., 1.}, {1., 1.}, {1.2, 1.}, {1.4, 1.}, {1.6, 1.}, {1.8, 1.}, {2., 1.}, {2., 1.}, {2.125, 1.25}, {2.25, 1.5}, {2.375, 1.75}, {2.5, 2.}, {2.625, 2.25}, {2.75, 2.5}, {2.875, 2.75}, {3, 3}} (*timings*) li2 = Table[Random[], {1000},{2}]; linearInterpolate2[ li2, .3 ];//Timing {6.91667 Second, Null} insertPoints[ li2, .3 ];//Timing (*Robert Hall*) {9.66667 Second, Null} Allan Hayes hay at haystack.demon.co.uk ************************************************* [1] Problem Suppose you have a ordered list {p1, p2, ... , pn} where points has the form {x,y}. You want to modify the list so any neighboring points will have a distance less or equal to a given value maxLength. You do this by adding points in between points. For example, suppose p3 and p4 has length greater than maxLength. Your new list should then be {p1, p2, p3, newP1, newP2,..., newPm, p4, p5, ... , pn} where newP1, ...newPm lies on the line p3 p4. [2] Robert Hall's solution insertPoints[points_List, maxDistance_] := Module[ {vector, distance}, Append[ Flatten[ Table[ If[ vector = points[[pointIndex]] - points[[pointIndex + 1]] ; distance = N[Sqrt[vector . vector]]; distance > maxDistance, (points[[pointIndex]] - #)& /@ Table[ multiplier * vector / distance, { multiplier, 0, distance, maxDistance } ], {points[[pointIndex]]} ], {pointIndex, Length[points] - 1} ], 1 ], Last[points] ] ] ==== [MESSAGE SEPARATOR] ====