@@ -9,26 +9,21 @@ implementation_specific_values = [
99 # for the float64 case as to still be able to expose the original
1010 # float64 implementation under the same API, namely `DistanceMetric`.
1111 #
12- # On the other hand, '32' bit is used for `name_suffix` for the float32
12+ # On the other hand, '32' is used for `name_suffix` for the float32
1313 # case to remove ambiguity and use `DistanceMetric32`, which is not
1414 # publicly exposed.
1515 #
1616 # The metric mapping is adapted accordingly to route to the correct
1717 # implementations.
1818 #
19- # We also use 64bit types as defined in `sklearn.utils._typedefs`
20- # to maintain backward compatibility at the symbol level for extra
21- # safety.
22- #
23- ('', 'DTYPE_t', 'DTYPE'),
24- ('32', 'cnp.float32_t', 'np.float32')
19+ ('', 'float64_t', 'np.float64'),
20+ ('32', 'float32_t', 'np.float32')
2521]
2622
2723}}
28- cimport numpy as cnp
2924from libc.math cimport sqrt, exp
3025
31- from ..utils._typedefs cimport DTYPE_t, ITYPE_t, SPARSE_INDEX_TYPE_t
26+ from ..utils._typedefs cimport float64_t, float32_t, int32_t, intp_t
3227
3328{{for name_suffix, INPUT_DTYPE_t, INPUT_DTYPE in implementation_specific_values}}
3429
@@ -37,37 +32,37 @@ from ..utils._typedefs cimport DTYPE_t, ITYPE_t, SPARSE_INDEX_TYPE_t
3732#
3833# We use these for the default (euclidean) case so that they can be
3934# inlined. This leads to faster computation for the most common case
40- cdef inline DTYPE_t euclidean_dist{{name_suffix}}(
35+ cdef inline float64_t euclidean_dist{{name_suffix}}(
4136 const {{INPUT_DTYPE_t}}* x1,
4237 const {{INPUT_DTYPE_t}}* x2,
43- ITYPE_t size,
38+ intp_t size,
4439) except -1 nogil:
45- cdef DTYPE_t tmp, d=0
46- cdef cnp. intp_t j
40+ cdef float64_t tmp, d=0
41+ cdef intp_t j
4742 for j in range(size):
48- tmp = <DTYPE_t > (x1[j] - x2[j])
43+ tmp = <float64_t > (x1[j] - x2[j])
4944 d += tmp * tmp
5045 return sqrt(d)
5146
5247
53- cdef inline DTYPE_t euclidean_rdist{{name_suffix}}(
48+ cdef inline float64_t euclidean_rdist{{name_suffix}}(
5449 const {{INPUT_DTYPE_t}}* x1,
5550 const {{INPUT_DTYPE_t}}* x2,
56- ITYPE_t size,
51+ intp_t size,
5752) except -1 nogil:
58- cdef DTYPE_t tmp, d=0
59- cdef cnp. intp_t j
53+ cdef float64_t tmp, d=0
54+ cdef intp_t j
6055 for j in range(size):
61- tmp = <DTYPE_t >(x1[j] - x2[j])
56+ tmp = <float64_t >(x1[j] - x2[j])
6257 d += tmp * tmp
6358 return d
6459
6560
66- cdef inline DTYPE_t euclidean_dist_to_rdist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil:
61+ cdef inline float64_t euclidean_dist_to_rdist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil:
6762 return dist * dist
6863
6964
70- cdef inline DTYPE_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil:
65+ cdef inline float64_t euclidean_rdist_to_dist{{name_suffix}}(const {{INPUT_DTYPE_t}} dist) except -1 nogil:
7166 return sqrt(dist)
7267
7368
@@ -78,89 +73,89 @@ cdef class DistanceMetric{{name_suffix}}:
7873 # we must define them here so that cython's limited polymorphism will work.
7974 # Because we don't expect to instantiate a lot of these objects, the
8075 # extra memory overhead of this setup should not be an issue.
81- cdef DTYPE_t p
82- cdef const DTYPE_t [::1] vec
83- cdef const DTYPE_t [:, ::1] mat
84- cdef ITYPE_t size
76+ cdef float64_t p
77+ cdef const float64_t [::1] vec
78+ cdef const float64_t [:, ::1] mat
79+ cdef intp_t size
8580 cdef object func
8681 cdef object kwargs
8782
88- cdef DTYPE_t dist(
83+ cdef float64_t dist(
8984 self,
9085 const {{INPUT_DTYPE_t}}* x1,
9186 const {{INPUT_DTYPE_t}}* x2,
92- ITYPE_t size,
87+ intp_t size,
9388 ) except -1 nogil
9489
95- cdef DTYPE_t rdist(
90+ cdef float64_t rdist(
9691 self,
9792 const {{INPUT_DTYPE_t}}* x1,
9893 const {{INPUT_DTYPE_t}}* x2,
99- ITYPE_t size,
94+ intp_t size,
10095 ) except -1 nogil
10196
102- cdef DTYPE_t dist_csr(
97+ cdef float64_t dist_csr(
10398 self,
10499 const {{INPUT_DTYPE_t}}* x1_data,
105- const SPARSE_INDEX_TYPE_t [:] x1_indices,
100+ const int32_t [:] x1_indices,
106101 const {{INPUT_DTYPE_t}}* x2_data,
107- const SPARSE_INDEX_TYPE_t [:] x2_indices,
108- const SPARSE_INDEX_TYPE_t x1_start,
109- const SPARSE_INDEX_TYPE_t x1_end,
110- const SPARSE_INDEX_TYPE_t x2_start,
111- const SPARSE_INDEX_TYPE_t x2_end,
112- const ITYPE_t size,
102+ const int32_t [:] x2_indices,
103+ const int32_t x1_start,
104+ const int32_t x1_end,
105+ const int32_t x2_start,
106+ const int32_t x2_end,
107+ const intp_t size,
113108 ) except -1 nogil
114109
115- cdef DTYPE_t rdist_csr(
110+ cdef float64_t rdist_csr(
116111 self,
117112 const {{INPUT_DTYPE_t}}* x1_data,
118- const SPARSE_INDEX_TYPE_t [:] x1_indices,
113+ const int32_t [:] x1_indices,
119114 const {{INPUT_DTYPE_t}}* x2_data,
120- const SPARSE_INDEX_TYPE_t [:] x2_indices,
121- const SPARSE_INDEX_TYPE_t x1_start,
122- const SPARSE_INDEX_TYPE_t x1_end,
123- const SPARSE_INDEX_TYPE_t x2_start,
124- const SPARSE_INDEX_TYPE_t x2_end,
125- const ITYPE_t size,
115+ const int32_t [:] x2_indices,
116+ const int32_t x1_start,
117+ const int32_t x1_end,
118+ const int32_t x2_start,
119+ const int32_t x2_end,
120+ const intp_t size,
126121 ) except -1 nogil
127122
128123 cdef int pdist(
129124 self,
130125 const {{INPUT_DTYPE_t}}[:, ::1] X,
131- DTYPE_t [:, ::1] D,
126+ float64_t [:, ::1] D,
132127 ) except -1
133128
134129 cdef int cdist(
135130 self,
136131 const {{INPUT_DTYPE_t}}[:, ::1] X,
137132 const {{INPUT_DTYPE_t}}[:, ::1] Y,
138- DTYPE_t [:, ::1] D,
133+ float64_t [:, ::1] D,
139134 ) except -1
140135
141136 cdef int pdist_csr(
142137 self,
143138 const {{INPUT_DTYPE_t}}* x1_data,
144- const SPARSE_INDEX_TYPE_t [:] x1_indices,
145- const SPARSE_INDEX_TYPE_t [:] x1_indptr,
146- const ITYPE_t size,
147- DTYPE_t [:, ::1] D,
139+ const int32_t [:] x1_indices,
140+ const int32_t [:] x1_indptr,
141+ const intp_t size,
142+ float64_t [:, ::1] D,
148143 ) except -1 nogil
149144
150145 cdef int cdist_csr(
151146 self,
152147 const {{INPUT_DTYPE_t}}* x1_data,
153- const SPARSE_INDEX_TYPE_t [:] x1_indices,
154- const SPARSE_INDEX_TYPE_t [:] x1_indptr,
148+ const int32_t [:] x1_indices,
149+ const int32_t [:] x1_indptr,
155150 const {{INPUT_DTYPE_t}}* x2_data,
156- const SPARSE_INDEX_TYPE_t [:] x2_indices,
157- const SPARSE_INDEX_TYPE_t [:] x2_indptr,
158- const ITYPE_t size,
159- DTYPE_t [:, ::1] D,
151+ const int32_t [:] x2_indices,
152+ const int32_t [:] x2_indptr,
153+ const intp_t size,
154+ float64_t [:, ::1] D,
160155 ) except -1 nogil
161156
162- cdef DTYPE_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil
157+ cdef float64_t _rdist_to_dist(self, {{INPUT_DTYPE_t}} rdist) except -1 nogil
163158
164- cdef DTYPE_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil
159+ cdef float64_t _dist_to_rdist(self, {{INPUT_DTYPE_t}} dist) except -1 nogil
165160
166161{{endfor}}
0 commit comments