Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Created April 30, 2013 02:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tristanwietsma/5486332 to your computer and use it in GitHub Desktop.
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
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
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
)
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