@@ -9,8 +9,7 @@ from libc.math cimport exp, fabs, log
9
9
from numpy.math cimport EULER
10
10
11
11
12
- def mean_change (cnp.ndarray[ndim = 1 , dtype = floating] arr_1,
13
- cnp.ndarray[ndim = 1 , dtype = floating] arr_2):
12
+ def mean_change (const floating[:] arr_1 , const floating[:] arr_2 ):
14
13
""" Calculate the mean difference between two arrays.
15
14
16
15
Equivalent to np.abs(arr_1 - arr2).mean().
@@ -28,9 +27,11 @@ def mean_change(cnp.ndarray[ndim=1, dtype=floating] arr_1,
28
27
return total / size
29
28
30
29
31
- def _dirichlet_expectation_1d (cnp.ndarray[ndim = 1 , dtype = floating] doc_topic,
32
- floating doc_topic_prior ,
33
- cnp.ndarray[ndim = 1 , dtype = floating] out):
30
+ def _dirichlet_expectation_1d (
31
+ floating[:] doc_topic ,
32
+ floating doc_topic_prior ,
33
+ floating[:] out
34
+ ):
34
35
""" Dirichlet expectation for a single sample:
35
36
exp(E[log(theta)]) for theta ~ Dir(doc_topic)
36
37
after adding doc_topic_prior to doc_topic, in-place.
@@ -56,7 +57,7 @@ def _dirichlet_expectation_1d(cnp.ndarray[ndim=1, dtype=floating] doc_topic,
56
57
out[i] = exp(psi(doc_topic[i]) - psi_total)
57
58
58
59
59
- def _dirichlet_expectation_2d (cnp.ndarray[ ndim = 2 , dtype = floating ] arr):
60
+ def _dirichlet_expectation_2d (const floating[:, : ] arr ):
60
61
""" Dirichlet expectation for multiple samples:
61
62
E[log(theta)] for theta ~ Dir(arr).
62
63
@@ -66,7 +67,7 @@ def _dirichlet_expectation_2d(cnp.ndarray[ndim=2, dtype=floating] arr):
66
67
the exp and doesn't add in the prior.
67
68
"""
68
69
cdef floating row_total, psi_row_total
69
- cdef cnp.ndarray[ndim = 2 , dtype = floating ] d_exp
70
+ cdef floating[:, : ] d_exp
70
71
cdef cnp.npy_intp i, j, n_rows, n_cols
71
72
72
73
n_rows = arr.shape[0 ]
@@ -82,7 +83,7 @@ def _dirichlet_expectation_2d(cnp.ndarray[ndim=2, dtype=floating] arr):
82
83
for j in range (n_cols):
83
84
d_exp[i, j] = psi(arr[i, j]) - psi_row_total
84
85
85
- return d_exp
86
+ return d_exp.base
86
87
87
88
88
89
# Psi function for positive arguments. Optimized for speed, not accuracy.
@@ -107,4 +108,4 @@ cdef floating psi(floating x) nogil:
107
108
result += log(x) - .5 * r
108
109
r = r * r
109
110
result -= r * ((1. / 12. ) - r * ((1. / 120. ) - r * (1. / 252. )))
110
- return result;
111
+ return result
0 commit comments