List programming, speed improvement

*To*: mathgroup at smc.vnet.net*Subject*: [mg65801] List programming, speed improvement*From*: Dr Reinhard Simonovits <Reinhard.Simonovits at uni-graz.at>*Date*: Mon, 17 Apr 2006 02:28:57 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

Hi, soon I have to give a Mathematica presentation. There I would like to demonstrate various programming techniques. One of the problems is: Given a list of pairs of any reals, convert it to Graphics primitives in the following way: (* I just choose 1,2,3,4,5,6 *) { {1.,2.}, {3.,4.}, {5.,6.} } -> {Line[{{1., 0}, {1., 2.}}], Line[{{3., 0}, {3., 4.}}], Line[{{5., 0}, {5., 6.}}]} I have 3 solutions following pattern matching, pure functions, only List commands. 1) Can anyone show me a quicker solutions, including Compile? It must run under Mathematica 4.0, the participants must work with it for other problems. 2) But I could also demonstrate a solution under Mathematica 5.2, just to show the version improvements. So actually I need a 4.0 and 5.2 version. I think only List commands better programmed must be a hit. data = Table[{Random[Real, {1, 10}], Random[Integer, {1, 10}]}, {100000}]; Here are my solutions: solution1 = data /. a : {_, _} :> Line[{{a[[1]], 0}, a }]; // Timing {0.469 Second, Null} solution2 = Line[{ {#1, 0}, {#1, #2}}] & @@@ data; // Timing {0.344 Second, Null} solution3 = ( {xcoord, ycoord} = Transpose[ data]; Line /@ Partition[ Transpose[{Transpose[ {xcoord, xcoord}] // Flatten, Transpose[{ Table[0, {Length@xcoord}], ycoord}] // Flatten} ], 2]); // Timing {0.531 Second, Null} solution1 == solution2 == solution3 True In solution3 {{1, 2}, {3, 4}, {5, 6}} -> {{1, 0}, {1, 2}, {3, 0}, {3, 4}, {5, 0}, {5, 6}} was created first. -Reinhard -- ******************************************** Dr. Reinhard Simonovits Handelsakademie | Karl Franzens University Math Department | Inst. for Mathematics Grazbachgasse 71 | Heinrichstrasse 36 A-8010 Graz, Austria Email: Reinhard.Simonovits at uni-graz.at *********************************************