Skip to content

Instantly share code, notes, and snippets.

@fabianp
Created July 19, 2011 18:55
Show Gist options
  • Save fabianp/1093404 to your computer and use it in GitHub Desktop.
Save fabianp/1093404 to your computer and use it in GitHub Desktop.
check an array for finite values
cimport numpy as np
import numpy as np
np.import_array()
def chkfinite_double(np.ndarray X):
cdef int i, length
cdef np.PyArrayObject val
cdef np.flatiter iter
length = np.PyArray_SIZE(X)
iter = np.PyArray_IterNew(X)
while np.PyArray_ITER_NOTDONE(iter):
val = np.PyArray_GETITEM(X, np.PyArray_ITER_DATA(iter))
if not npy_isfinite(val):
raise ValueError('Array cannot contain Inf or NaN')
np.PyArray_ITER_NEXT(iter)
return True
import pyximport
pyximport.install()
import gistfile1.pyx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment