Skip to content

Commit 03003a4

Browse files
Add support for complex weights
1 parent f983afd commit 03003a4

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

numpy/core/src/multiarray/compiled_base.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
195195
Py_DECREF(lst);
196196
}
197197
else {
198-
wts = (PyArrayObject *)PyArray_ContiguousFromAny(
199-
weight, NPY_DOUBLE, 1, 1);
198+
wts = (PyArrayObject *)PyArray_FromAny(
199+
weight, NULL, 1, 1, NPY_ARRAY_DEFAULT, NULL);
200200
if (wts == NULL) {
201201
goto fail;
202202
}
@@ -206,16 +206,36 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args,
206206
"The weights and list don't have the same length.");
207207
goto fail;
208208
}
209-
ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_DOUBLE, 0);
210-
if (ans == NULL) {
209+
if (PyArray_ISFLOAT(wts)) {
210+
ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_DOUBLE, 0);
211+
if (ans == NULL) {
212+
goto fail;
213+
}
214+
dans = (double *)PyArray_DATA(ans);
215+
NPY_BEGIN_ALLOW_THREADS;
216+
for (i = 0; i < len; i++) {
217+
dans[numbers[i]] += weights[i];
218+
}
219+
NPY_END_ALLOW_THREADS;
220+
} else if (PyArray_ISCOMPLEX(wts)) {
221+
ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_CDOUBLE, 0);
222+
if (ans == NULL) {
223+
goto fail;
224+
}
225+
dans = (double *)PyArray_DATA(ans);
226+
NPY_BEGIN_ALLOW_THREADS;
227+
for (i = 0; i < len; i++) {
228+
/* Add real parts */
229+
dans[2 * numbers[i]] += weights[2 * i];
230+
/* Add complex parts */
231+
dans[2 * numbers[i] + 1] += weights[2 * i + 1];
232+
}
233+
NPY_END_ALLOW_THREADS;
234+
} else {
235+
PyErr_SetString(PyExc_TypeError,
236+
"The weights array must only contain floats or complex numbers.");
211237
goto fail;
212238
}
213-
dans = (double *)PyArray_DATA(ans);
214-
NPY_BEGIN_ALLOW_THREADS;
215-
for (i = 0; i < len; i++) {
216-
dans[numbers[i]] += weights[i];
217-
}
218-
NPY_END_ALLOW_THREADS;
219239
Py_DECREF(lst);
220240
Py_DECREF(wts);
221241
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy