Skip to content

Instantly share code, notes, and snippets.

@mattjj
Last active August 29, 2015 13:57
Show Gist options
  • Save mattjj/9692956 to your computer and use it in GitHub Desktop.
Save mattjj/9692956 to your computer and use it in GitHub Desktop.
#include "bar.h"
void blah(int sz, double *x) {
for (int i=0; i<sz; i++) {
x[i] = (double) 2*i;
}
}
void blah(int sz, double *x);
# distutils: extra_compile_args = -O3 -w
# distutils: sources = bar.c
# cython: boundscheck = False
# cython: initializedcheck=False
# cython: nonecheck = False
# cython: wraparound = False
import numpy as np
cimport numpy as np
### interfacing external c code
# add any c files to the distutils sources directive at the top
cdef extern from "bar.h":
void blah(int sz, double *x)
def blargh(double[:] x):
blah(x.shape[0], &x[0])
### writing cython
def foo(double[:,::1] x):
cdef int i, j
cdef double[:,::1] y = np.arange(100.).reshape((10,10))
z = [a**2 for a in range(10)]
for i in range(x.shape[0]):
for j in range(x.shape[1]):
x[i,j] = i*j
return np.asarray(x), np.asarray(y), z
from distutils.core import setup
from Cython.Build import cythonize
import numpy
setup(
ext_modules=cythonize('**/*.pyx'),
include_dirs=[numpy.get_include(),],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment