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

Advanced Search

Back to Topic List Back to Topic List  
Dr Reinhard Simonovits

Posts: 9
Registered: 4/17/06
List programming, speed improvement
Posted: Apr 17, 2006 3:31 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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@uni-graz.at
*********************************************




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.