Cython experiment with SymPy

6 views
Skip to first unread message

Ondrej Certik

unread,
Nov 30, 2007, 10:40:00 AM11/30/07
to sy...@googlegroups.com
Hi,

I just tried this experiment:

http://permalink.gmane.org/gmane.comp.python.cython.devel/311

It's a Cython code, which gets compiled to pure C, compiled with gcc,
this produces a clean *.so file, which you just import in python.
It creates a user defined class, which SymPy knows how to work with
(it expands it, calculates a limit, etc.) and it works.

I am not sure it can speed SymPy up, because most of the time is spent
by creating new classes, etc. But this should allow us to
easily and nicely integrate any pure C solution, that we might come with.

Ondrej

kent-and

unread,
Dec 1, 2007, 5:34:09 PM12/1/07
to sympy

Hi,

I thought I could mention the module we have created for inlining C/C+
+ code
in Python.

You can use it like this:

from instant import inline

add_func = inline("double add(double a, double b){ return a+b; }")

print "The sum of 3 and 4.5 is ", add_func(3, 4.5)


We combine this with the code-generation utilites in GiNaC/Swinginac
to make sort of "Just-in-time compilation"
of finite element python code. This is why I was curious about C code
generation in sympy.

It is a very small module created on top of SWIG. You can find it at
www.fenics.org/instant.

Kent

Ondrej Certik

unread,
Dec 1, 2007, 5:47:14 PM12/1/07
to sy...@googlegroups.com
Hi Kent,

> I thought I could mention the module we have created for inlining C/C+
> + code
> in Python.
>
> You can use it like this:
>
> from instant import inline
>
> add_func = inline("double add(double a, double b){ return a+b; }")
>
> print "The sum of 3 and 4.5 is ", add_func(3, 4.5)
>
>
> We combine this with the code-generation utilites in GiNaC/Swinginac
> to make sort of "Just-in-time compilation"
> of finite element python code. This is why I was curious about C code
> generation in sympy.
>
> It is a very small module created on top of SWIG. You can find it at
> www.fenics.org/instant.

Thanks for sharing this. One problem with SWIG is that the final
bingings are very bloated - there is the .so module and then there is
a python module on top of that. And classes are emulated in that
python module. Cython, on the other hand, creates just one nice C
file, which you compile to .so and that's it. So it's much faster and
very easy to debug - you just look into the generated C file to see
what's really happening (it's very readable).

Is there some example of your finite element code? I'd like to look at
that how you use instant in there.

Ondrej

Reply all
Reply to author
Forward
0 new messages