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: List programming, speed improvement
Replies: 0

 Dr Reinhard Simonovits Posts: 9 Registered: 4/17/06
List programming, speed improvement
Posted: Apr 17, 2006 3:31 AM

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