2010-09-06 22:15:53 +08:00
|
|
|
"""
|
|
|
|
|
Small collection of auxiliary functions that operate on arrays
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
cimport numpy as np
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2010-11-05 21:54:09 +08:00
|
|
|
cimport cython
|
2010-09-06 22:15:53 +08:00
|
|
|
|
2013-01-03 21:58:16 +08:00
|
|
|
from libc.float cimport DBL_MAX, FLT_MAX
|
|
|
|
|
|
|
|
|
|
cdef extern from "src/cholesky_delete.h":
|
|
|
|
|
int cholesky_delete_dbl(int m, int n, double *L, int go_out)
|
|
|
|
|
int cholesky_delete_flt(int m, int n, float *L, int go_out)
|
2010-11-05 21:54:09 +08:00
|
|
|
|
2010-09-06 22:15:53 +08:00
|
|
|
ctypedef np.float64_t DOUBLE
|
|
|
|
|
|
2010-09-15 04:02:44 +08:00
|
|
|
|
2014-03-05 21:09:53 +08:00
|
|
|
np.import_array()
|
|
|
|
|
|
|
|
|
|
|
2010-11-05 21:54:09 +08:00
|
|
|
def min_pos(np.ndarray X):
|
|
|
|
|
"""
|
2012-10-31 16:05:35 +08:00
|
|
|
Find the minimum value of an array over positive values
|
2010-11-05 21:54:09 +08:00
|
|
|
|
|
|
|
|
Returns a huge value if none of the values are positive
|
|
|
|
|
"""
|
|
|
|
|
if X.dtype.name == 'float32':
|
|
|
|
|
return _float_min_pos(<float *> X.data, X.size)
|
|
|
|
|
elif X.dtype.name == 'float64':
|
|
|
|
|
return _double_min_pos(<double *> X.data, X.size)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError('Unsupported dtype for array X')
|
|
|
|
|
|
|
|
|
|
|
2012-10-31 16:05:35 +08:00
|
|
|
cdef float _float_min_pos(float *X, Py_ssize_t size):
|
2010-11-05 21:54:09 +08:00
|
|
|
cdef Py_ssize_t i
|
|
|
|
|
cdef float min_val = DBL_MAX
|
|
|
|
|
for i in range(size):
|
2015-11-02 04:59:38 +08:00
|
|
|
if 0. < X[i] < min_val:
|
2010-11-05 21:54:09 +08:00
|
|
|
min_val = X[i]
|
|
|
|
|
return min_val
|
|
|
|
|
|
|
|
|
|
|
2012-10-31 16:05:35 +08:00
|
|
|
cdef double _double_min_pos(double *X, Py_ssize_t size):
|
2010-11-05 21:54:09 +08:00
|
|
|
cdef Py_ssize_t i
|
|
|
|
|
cdef np.float64_t min_val = FLT_MAX
|
|
|
|
|
for i in range(size):
|
2015-11-02 04:59:38 +08:00
|
|
|
if 0. < X[i] < min_val:
|
2010-11-05 21:54:09 +08:00
|
|
|
min_val = X[i]
|
|
|
|
|
return min_val
|
2010-09-15 04:02:44 +08:00
|
|
|
|
2012-10-31 16:05:35 +08:00
|
|
|
|
2013-01-03 21:58:16 +08:00
|
|
|
# we should be using np.npy_intp or Py_ssize_t for indices, but BLAS wants int
|
2012-10-31 16:05:35 +08:00
|
|
|
def cholesky_delete(np.ndarray L, int go_out):
|
2010-09-15 04:45:56 +08:00
|
|
|
cdef int n = <int> L.shape[0]
|
2013-01-03 21:58:16 +08:00
|
|
|
cdef int m = <int> L.strides[0]
|
2010-11-22 21:56:37 +08:00
|
|
|
|
|
|
|
|
if L.dtype.name == 'float64':
|
2013-01-03 21:58:16 +08:00
|
|
|
cholesky_delete_dbl(m / sizeof(double), n, <double *> L.data, go_out)
|
2010-11-22 21:56:37 +08:00
|
|
|
elif L.dtype.name == 'float32':
|
2013-01-03 21:58:16 +08:00
|
|
|
cholesky_delete_flt(m / sizeof(float), n, <float *> L.data, go_out)
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError("unsupported dtype %r." % L.dtype)
|