Skip to content

Instantly share code, notes, and snippets.

@ruoyu0088
Created March 31, 2015 00:12
Show Gist options
  • Save ruoyu0088/9989ab8c9bd919e7610c to your computer and use it in GitHub Desktop.
Save ruoyu0088/9989ab8c9bd919e7610c to your computer and use it in GitHub Desktop.
%%cython
#cython: boundscheck=False, wraparound=False
import numpy as np
cimport numpy as np
from cpython cimport array
def sum_array(array.array arr):
cdef int i
cdef double s = 0
for i in range(len(arr)):
s += arr.data.as_doubles[i]
return s
def where_1d(x):
cdef array.array[int] pos = array.array("i")
cdef int i
cdef unsigned char[::1] xview = x.view("uint8")
for i in range(xview.shape[0]):
if xview[i]:
pos.append(i)
#array.extend_buffer(pos, <char*>(&i), 1)
return pos
def in_circle(double[:, :] points, double cx, double cy, double r):
cdef array.array[double] res = array.array("d")
cdef double r2 = r * r
cdef double p[2]
cdef int i
for i in range(points.shape[0]):
p[0] = points[i, 0]
p[1] = points[i, 1]
if (p[0] - cx)**2 + (p[1] - cy)**2 < r2:
array.extend_buffer(res, <char*>p, 2)
return np.frombuffer(res, np.double).copy().reshape(-1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment