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