[Numpy-discussion] Efficient operator overloading
Wed Apr 18 17:31:44 CDT 2007
On Apr 18, 2007, at 6:31 PM, Timothy Hochberg wrote:
> On 4/18/07, Sturla Molden <email@example.com> wrote:
> I'd really like to see a Python extension library do this one day. It
> would be very cool and (almost) as efficient as plain Fortran - though
> not quite, we would still get some small temporary objects created.
> that is a sacrifice I am willing to pay to use Python. We would gain
> some efficacy over Fortran by postponing indefinitely evaluation of
> computations that are not needed, when this is not known at compile
> Any comments?
> I periodically wonder if something like this could be built on top
> of numexpr without too much pain. Simply build up the expression on
> operations and then evaluate when data is requested. You'd need
> some sort of Jekyl & Hyde matrix that spent the first part of it's
> life unevaluated and then converted itself to something very much
> like an array when it evaluated itself. If might end up being
> tricky to make sure that everything got transparently evaluated at
> the right time though.
It seems that we have developed something like this for the fipy code
(http://www.ctcms.nist.gov/fipy). We refer to
it as "lazy evaluation". Essentially, objects called "Variable"s hold
numpy arrays. They behave
as ordinary numpy arrays, but are only evaluated when the information
is requested. e.g.
>>> from fipy import *
>>> v0 = Variable(numerix.array((0,1,2)))
>>> v1 = Variable(numerix.array((2,3,4)))
>>> v2 = v0 * v1 + v0 * v1
((Variable(value = array([0, 1, 2])) * Variable(value = array([3,
4, 5]))) + (Variable(value = array([0, 1, 2])) * Variable(value =
array([3, 4, 5]))))
>>> print v2
[ 0 8 20]
By passing an '--inline' flag at run time we can choose to evaluate
this expression with weave. Variable's have a _getCstring() method
that's used to pass to weave when inlining along with other information.
>>> print v2._getCstring()
'((var00(i) * var01(i)) + (var10(i) * var11(i)))'
Actually, we could and should have been using numexpr to do this. I
think numexpr would substantially clean up
the code we've developed. Anyway, you can browse variable.py at
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion