@@ -9,26 +9,21 @@ implementation_specific_values = [
9
9
# for the float64 case as to still be able to expose the original
10
10
# float64 implementation under the same API, namely `DistanceMetric`.
11
11
#
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
13
13
# case to remove ambiguity and use `DistanceMetric32`, which is not
14
14
# publicly exposed.
15
15
#
16
16
# The metric mapping is adapted accordingly to route to the correct
17
17
# implementations.
18
18
#
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')
25
21
]
26
22
27
23
}}
28
- cimport numpy as cnp
29
24
from libc.math cimport sqrt, exp
30
25
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
32
27
33
28
{{for name_suffix, INPUT_DTYPE_t, INPUT_DTYPE in implementation_specific_values}}
34
29
@@ -37,37 +32,37 @@ from ..utils._typedefs cimport DTYPE_t, ITYPE_t, SPARSE_INDEX_TYPE_t
37
32
#
38
33
# We use these for the default (euclidean) case so that they can be
39
34
# 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}}(
41
36
const {{INPUT_DTYPE_t}}* x1,
42
37
const {{INPUT_DTYPE_t}}* x2,
43
- ITYPE_t size,
38
+ intp_t size,
44
39
) 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
47
42
for j in range(size):
48
- tmp = <DTYPE_t > (x1[j] - x2[j])
43
+ tmp = <float64_t > (x1[j] - x2[j])
49
44
d += tmp * tmp
50
45
return sqrt(d)
51
46
52
47
53
- cdef inline DTYPE_t euclidean_rdist{{name_suffix}}(
48
+ cdef inline float64_t euclidean_rdist{{name_suffix}}(
54
49
const {{INPUT_DTYPE_t}}* x1,
55
50
const {{INPUT_DTYPE_t}}* x2,
56
- ITYPE_t size,
51
+ intp_t size,
57
52
) 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
60
55
for j in range(size):
61
- tmp = <DTYPE_t >(x1[j] - x2[j])
56
+ tmp = <float64_t >(x1[j] - x2[j])
62
57
d += tmp * tmp
63
58
return d
64
59
65
60
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:
67
62
return dist * dist
68
63
69
64
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:
71
66
return sqrt(dist)
72
67
73
68
@@ -78,89 +73,89 @@ cdef class DistanceMetric{{name_suffix}}:
78
73
# we must define them here so that cython's limited polymorphism will work.
79
74
# Because we don't expect to instantiate a lot of these objects, the
80
75
# 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
85
80
cdef object func
86
81
cdef object kwargs
87
82
88
- cdef DTYPE_t dist(
83
+ cdef float64_t dist(
89
84
self,
90
85
const {{INPUT_DTYPE_t}}* x1,
91
86
const {{INPUT_DTYPE_t}}* x2,
92
- ITYPE_t size,
87
+ intp_t size,
93
88
) except -1 nogil
94
89
95
- cdef DTYPE_t rdist(
90
+ cdef float64_t rdist(
96
91
self,
97
92
const {{INPUT_DTYPE_t}}* x1,
98
93
const {{INPUT_DTYPE_t}}* x2,
99
- ITYPE_t size,
94
+ intp_t size,
100
95
) except -1 nogil
101
96
102
- cdef DTYPE_t dist_csr(
97
+ cdef float64_t dist_csr(
103
98
self,
104
99
const {{INPUT_DTYPE_t}}* x1_data,
105
- const SPARSE_INDEX_TYPE_t [:] x1_indices,
100
+ const int32_t [:] x1_indices,
106
101
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,
113
108
) except -1 nogil
114
109
115
- cdef DTYPE_t rdist_csr(
110
+ cdef float64_t rdist_csr(
116
111
self,
117
112
const {{INPUT_DTYPE_t}}* x1_data,
118
- const SPARSE_INDEX_TYPE_t [:] x1_indices,
113
+ const int32_t [:] x1_indices,
119
114
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,
126
121
) except -1 nogil
127
122
128
123
cdef int pdist(
129
124
self,
130
125
const {{INPUT_DTYPE_t}}[:, ::1] X,
131
- DTYPE_t [:, ::1] D,
126
+ float64_t [:, ::1] D,
132
127
) except -1
133
128
134
129
cdef int cdist(
135
130
self,
136
131
const {{INPUT_DTYPE_t}}[:, ::1] X,
137
132
const {{INPUT_DTYPE_t}}[:, ::1] Y,
138
- DTYPE_t [:, ::1] D,
133
+ float64_t [:, ::1] D,
139
134
) except -1
140
135
141
136
cdef int pdist_csr(
142
137
self,
143
138
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,
148
143
) except -1 nogil
149
144
150
145
cdef int cdist_csr(
151
146
self,
152
147
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,
155
150
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,
160
155
) except -1 nogil
161
156
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
163
158
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
165
160
166
161
{{endfor}}
0 commit comments