Skip to content

Instantly share code, notes, and snippets.

@mikaem
Last active January 26, 2016 15:47
Show Gist options
  • Save mikaem/f7fe4b94517a3f40f640 to your computer and use it in GitHub Desktop.
Save mikaem/f7fe4b94517a3f40f640 to your computer and use it in GitHub Desktop.
# memview_bench_v6.pyx
import numpy as np
cimport numpy as np
from libc.math cimport sqrt
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
def pairwise(np.ndarray[double, ndim=2, mode='c'] X not None):
cdef np.intp_t i, j, k, n_samples, n_dim
cdef double tmp, d
n_samples = X.shape[0]
n_dim = X.shape[1]
cdef np.ndarray[double, ndim=2, mode='c'] D = np.empty((n_samples,
n_samples))
for i in xrange(n_samples):
for j in xrange(n_samples):
d = 0.0
for k in xrange(n_dim):
tmp = X[i, k] - X[j, k]
d += tmp*tmp
D[i, j] = sqrt(d)
return D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment