forked from scipy/scipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_convex_analysis.pxd
47 lines (41 loc) · 1.07 KB
/
_convex_analysis.pxd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
cdef extern from "numpy/npy_math.h":
double inf "NPY_INFINITY"
from libc.math cimport log, sqrt, fabs
cdef inline double entr(double x) nogil:
if x > 0:
return -x * log(x)
elif x == 0:
return 0
else:
return -inf
cdef inline double kl_div(double x, double y) nogil:
if x > 0 and y > 0:
return x * log(x / y) - x + y
elif x == 0 and y >= 0:
return y
else:
return inf
cdef inline double rel_entr(double x, double y) nogil:
if x > 0 and y > 0:
return x * log(x / y)
elif x == 0 and y >= 0:
return 0
else:
return inf
cdef inline double huber(double delta, double r) nogil:
if delta < 0:
return inf
elif fabs(r) <= delta:
return 0.5 * r * r;
else:
return delta * (fabs(r) - 0.5 * delta);
cdef inline double pseudo_huber(double delta, double r) nogil:
cdef double u, v
if delta < 0:
return inf
elif delta == 0 or r == 0:
return 0
else:
u = delta
v = r / delta
return u*u*(sqrt(1 + v*v) - 1)