Created
April 30, 2013 02:51
-
-
Save tristanwietsma/5486332 to your computer and use it in GitHub Desktop.
Cython example To compile into a shared library, execute:
python setup.py build_ext --inplace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdef extern from "math.h": | |
double sin(double) | |
cdef double f(double x) except *: | |
return sin(x**2) | |
def integrate(double a, double b, int N): | |
cdef int i | |
cdef double s, dx | |
s = 0 | |
dx = (b-a)/N | |
for i in range(N): | |
s += f(a + i*dx) | |
return s*dx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Distutils import build_ext | |
ext_modules = [Extension("helloworld", ["helloworld.pyx"])] | |
setup( | |
name = 'Cython Example', | |
cmdclass = {'build_ext': build_ext}, | |
ext_modules = ext_modules | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import helloworld | |
print helloworld.integrate(0,1,1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment