Defining and using mathematical functions in cython

685 views Asked by At

I am new to python and struggling to get cython to speed up some of my calculations. I have tried every possible way i can think of but it seems that i don't yet understand something so i fail miserably to this. I have these files: The first one called Myfunc.pyx:

import math
from sympy import Symbol
x = Symbol('x')


wi=10

cdef float MyList(float a,float b):
  return [(xa,ya,wi+a.subs(x,xa)*math.sin(xa)*b.subs(x,xa)*math.exp(1/ya**2)) for xa in range(1,5001,10) for ya in range(-2501,2501,10)]

The setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules=[
    Extension("Myfunc",
              ["Myfunc.pyx"],
              libraries=["m"]) # Unix-like specific
]

setup(
  name = "Demos",
  cmdclass = {"build_ext": build_ext},
  ext_modules = ext_modules
)

finally the python script called calc.py which i run to get the list i want, called "MyList1":

import Myfunc
from sympy import Symbol
import math

x = Symbol('x')


a1=(10*x)**(0.5)
b1=(20*x)**(0.5)
wi=10


MyList1=Myfunc.MyList(a1,b1)
print MyList1

Now some clarifications: 1) x,y are used in previous scripts purely as symbols to derive formulas. I use the a.subs(x,xa) and b.subs(x,xa) (see the first file Myfunc.pyx) to get rid of the x and replace it with xa to finally get numeric and not symbolic solutions. 2) I think that on the top of the third file (calc.py) i should not use import Myfunc but something else i don't get. What i think i should do: run the setup.py and then since there are 2 new files created the Myfunc.c and the Myfunc.so i just run the calc.py file and supposedly i will have my numbers faster tahn in pure python. What must be done: I finally have no idea... What i want if possible: The exp() and a.subs(x,xa) and b.subs(x,xa) i think are slowing down my calculations. I try to convert this piece of code from pure python to "cython" (i think conversion is not a good word to describe this). The tutorials in the official site are a bit incrompehensible for me so i always fail to achieve this. Excuse my english because my hovercraft is full of eels

2

There are 2 answers

3
Davoud Taghawi-Nejad On BEST ANSWER

It is not possible to combine sympy with cython. Cython speeds up python by translating cdef classes and declared variables into C and compiling it. sympy.Symbol or any other sympy function has no C++ equivalent it can thus not be compiled into C, hence no speed ups.

More concretely. When you declare a with float a, it is a C double not a sympy type. it therefore has no .subs method. (and it can't have)

If I understand what you are working on numpy is a much better solution than cython. Essentially numpy.arrays are C data types. When you perform calculations with them it happens at C++ speed. It is documented how to use numpy with sympy. If for any reason you can not achieve what you intend with numpy(unlikely) numpy still works well with cython.

0
MRocklin On

There are a number of ways to start with a symbolic expression in SymPy and end up with fast numeric code.

  • You could look into SymPy's code generation

  • You could look into the lamdify function