[Cython] Lisp inspired transforms
Martin C. Martin
martin at martincmartin.com
Sun Apr 6 14:46:56 CEST 2008
Hi all,
I've been doing some thinking and prototyping of a transform system
inspired by Common Lisp macros. You can see the results as the newest CEP:
http://wiki.cython.org/enhancements/metaprogramming
Briefly, it allows you to define a transform in the Cython source code.
The transform runs at compile time, and takes the *parse trees* of its
arguments. In the examples, I define a simple symbolic differentiator
"deriv" which means you can write:
def eggs(a, b):
return deriv(5*a+b, a)
and it is translated at compile time to:
def eggs(a, b):
return (((5 * 1) + (0 * a)) + 0)
You can use this, for example, with numerical optimization techniques
like Newton's method. An example which differentiates 5 * x**3 - 10 and
finds it's root is given on the above wiki page, here's the output:
>>> TestTrans.myfunct(10.0)
(4990.0, 1500.0)
>>> TestTrans.newtons(TestTrans.myfunct, 10.0)
f( 10.0 ) = 4990.0 , f'( 10.0 ) = 1500.0
f( 6.67333333333 ) = 1475.93037185 , f'( 6.67333333333 ) = 668.000666667
f( 4.46385893383 ) = 434.735082042 , f'( 4.46385893383 ) = 298.890548717
f( 3.00936301916 ) = 126.267956666 , f'( 3.00936301916 ) = 135.843986716
f( 2.07985587115 ) = 34.9852072625 , f'( 2.07985587115 ) = 64.8870066716
f( 1.54068464016 ) = 8.28568621842 , f'( 1.54068464016 ) = 35.6056374064
f( 1.3079774954 ) = 1.18847303518 , f'( 1.3079774954 ) = 25.6620769269
f( 1.26166506953 ) = 0.0415843883547 , f'( 1.26166506953 ) = 23.8769812152
f( 1.25992345957 ) = 5.73769235945e-05 , f'( 1.25992345957 ) = 23.8111068596
f( 1.2599210499 ) = 1.09734443754e-10 , f'( 1.2599210499 ) = 23.8110157797
f( 1.25992104989 ) = 0.0 , f'( 1.25992104989 ) = 23.8110157795
1.2599210498948732
It's also useful for moving computation from run time to compile time,
and makes it easy for the programmer to specify what should happen where.
There are many other use cases as well.
I'm attaching a patch that implements the examples. It's very
proof-of-concept, which means its really rough. :) I also implemented
the bar minimum to get the examples working, but it gives you an idea.
Best,
Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CythonMetaprogramming.tar.gz
Type: application/gzip
Size: 8137 bytes
Desc: not available
Url : http://codespeak.net/pipermail/cython-dev/attachments/20080406/de466ac3/attachment.bin
More information about the Cython-dev
mailing list