Guest User

calc density cython

a guest
Jan 14th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import numpy as np
  2. cimport numpy as np
  3. cimport cython
  4.  
  5. FTYPE = np.float
  6. ctypedef np.float32_t FTYPE_t
  7.  
  8. @cython.boundscheck(False)
  9. cdef np.ndarray[np.int32_t, ndim=1] _calc_density(np.ndarray[FTYPE_t, ndim=2] normals, float r):
  10.     cdef np.ndarray[np.int32_t, ndim=1] density = np.zeros(normals.shape[0], dtype=np.int32)
  11.     cdef Py_ssize_t i, j, t
  12.     cdef float s, distance
  13.     s = r**2
  14.     t = normals.shape[0]
  15.     print "Hi"
  16.     for i in range(t):
  17.         density[i] = 0
  18.         for j in range(i):
  19.             print i, j
  20.             distance = (normals[i, 0] - normals[j, 0])**2 \
  21.                     + (normals[i, 1] - normals[j, 1])**2 \
  22.                     + (normals[i, 2] - normals[j, 2])**2
  23.             if  distance <= s:
  24.                 density[i] += 1
  25.                 density[j] += 1
  26.     return density
  27.  
  28. def calc_density(normals, r):
  29.     return _calc_density(normals, r)
Advertisement
Add Comment
Please, Sign In to add comment