Another optimization request :-)
andrew at acooke.org
Thu Feb 12 11:53:47 CET 2009
> To be honest, this is not my code and I'm new to python. It's part of
> the open source project NetworkX, but I'm using this one call
> extensively. I'm also not that familiar with the math behind the
> physics. I'll read the documents and see if I can figure it
> out. :-) Thank you for the links and suggestions. I really need to
> get this code performing at peak levels.
the maths is quite simple really, if you know any physics. it imagines
that a spring connects each point, and then works out what the total
effect of all the springs is. that gives the net "push" on each point.
it then goes through and moves each point a small amount in the direction
of the push (there's a "max" that makes no sense physically but probably
gives better numeric stability).
once they have moved, their positions have changed, so everything needs to
be calculated again, hence the iteration.
terry's suggestion was that, rather than working particle by particle, if
you can generalise the code to use matrices then you can use numpy. that
would mean more of the calculation is done using C rather than Python, and
so would be a big speed-up.
that means that you need to rewrite the program so that you don't have the
for-loops in Python (for u.. and for v...). instead you need to call
numpy to do array calculations.
that is going to give you much more speedup than what i recommended
(modifying the integration step at the end).
to be honest, although this is not hard if you are used to this kind of
thing, if the above isn't obvious it is quite a job. you would probably
be better looking for a different library. unfortunately i don't know of
one (i looked for exactly this a month or two ago and concluded i would
have to write my own; i didn't have time so i used a simpler layout). i'm
really busy, or i would do this for you, because it's interesting and
useful. but the code you have looks "amateur" - it wasn't written by
someone who does numerical work for a living. that doesn't mean it's not
correct or that the original author was stupid, but it does mean that to
make it efficient means looking at the problem in a different way.
there are certainly (very good) java libraries that do this. could you
use jpython and call out to one of those? if so, that's probably your
More information about the Python-list