[PYTHON MATRIX-SIG] Evaluation release of expression compiler

Hinsen Konrad hinsenk@ere.umontreal.ca
Fri, 19 Jan 1996 16:25:24 -0500

For those of you who still don't know what to do during the weekend, I
offer a nice toy. In the next message, I will send the source code for
the very first incomplete, non-optimized version of my numerical
expression compiler. It is a single C extension module called
"numexprmodule.c". For now, both this module and the module "cmath"
must be statically linked with the interpreter, since numexpr calls
functions from cmath.

Here's an example for an application:

from numexpr import *
from omath import *

def f(x):
    return sin(x*x)+0.5*sqrt(x/7)*cos(2/x)*log(x+exp(-x))-sqrt(2)*tanh(x)

f_compiled = f(FloatVariable())

print f(5)
print f_compiled(5)

The module "numexpr" exports three functions: IntegerVariable,
FloatVariable, and ComplexVariable. Each of these takes an
optional argument (default is zero) that indicates
the position of this variable in the argument list of the
compiled expression. So if you want a compiled expression
for "x+y", write

  f = FloatVariable(0)+FloatVariable(1)

The range of mathematical functions is the same as for cmath. All
arithmetic operators are supported, with exception of exponentiation,
which with the current coercion scheme cannot be implemented
efficiently, so I decided to wait a bit. With that restriction, all
functions that contain no conditional or loop statements can be

Speed: First tests have shown that the asymptotic speed (i.e.  for
infinitely long functions) relative to uncompiled functions is about
40. For one-line expressions such as the one in the example above,
expect a factor of 7 (which shows that the overhead if the interpreter
is still substantial). Note that the expressions are not optimized
in any way. If you compile something like
    temp = sin(x+y)
    f = temp*temp,
then sin(x+y) will actually be calculated twice at execution time.  So
elimination of common subexpressions is the very least amount of
optimization that will have to be done.

To do:
- Control structures
- Optimization
- Array expressions

I'd appreciate any comments whatsoever.

Konrad Hinsen                     | E-Mail: hinsenk@ere.umontreal.ca
Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. Centre-Ville     | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org