From 03003a44cac03eaee89ab7c87daa3b8dbb6659bb Mon Sep 17 00:00:00 2001 From: Alex Gregory Date: Sat, 22 Apr 2023 15:48:02 +0100 Subject: [PATCH 1/5] Add support for complex weights --- numpy/core/src/multiarray/compiled_base.c | 40 +++++++++++++++++------ 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index 22f2547ada6f..aa94e0a1a7a1 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -195,8 +195,8 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, Py_DECREF(lst); } else { - wts = (PyArrayObject *)PyArray_ContiguousFromAny( - weight, NPY_DOUBLE, 1, 1); + wts = (PyArrayObject *)PyArray_FromAny( + weight, NULL, 1, 1, NPY_ARRAY_DEFAULT, NULL); if (wts == NULL) { goto fail; } @@ -206,16 +206,36 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, "The weights and list don't have the same length."); goto fail; } - ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_DOUBLE, 0); - if (ans == NULL) { + if (PyArray_ISFLOAT(wts)) { + ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_DOUBLE, 0); + if (ans == NULL) { + goto fail; + } + dans = (double *)PyArray_DATA(ans); + NPY_BEGIN_ALLOW_THREADS; + for (i = 0; i < len; i++) { + dans[numbers[i]] += weights[i]; + } + NPY_END_ALLOW_THREADS; + } else if (PyArray_ISCOMPLEX(wts)) { + ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_CDOUBLE, 0); + if (ans == NULL) { + goto fail; + } + dans = (double *)PyArray_DATA(ans); + NPY_BEGIN_ALLOW_THREADS; + for (i = 0; i < len; i++) { + /* Add real parts */ + dans[2 * numbers[i]] += weights[2 * i]; + /* Add complex parts */ + dans[2 * numbers[i] + 1] += weights[2 * i + 1]; + } + NPY_END_ALLOW_THREADS; + } else { + PyErr_SetString(PyExc_TypeError, + "The weights array must only contain floats or complex numbers."); goto fail; } - dans = (double *)PyArray_DATA(ans); - NPY_BEGIN_ALLOW_THREADS; - for (i = 0; i < len; i++) { - dans[numbers[i]] += weights[i]; - } - NPY_END_ALLOW_THREADS; Py_DECREF(lst); Py_DECREF(wts); } From 66ae571d6b50e4ea12ffdc23ccc8132f45a3baf1 Mon Sep 17 00:00:00 2001 From: Alex Gregory Date: Fri, 28 Apr 2023 18:25:51 +0100 Subject: [PATCH 2/5] Add support for integers --- numpy/core/src/multiarray/compiled_base.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index aa94e0a1a7a1..c2b28f57d65a 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -115,7 +115,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, { PyObject *list = NULL, *weight = Py_None, *mlength = NULL; PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL; - npy_intp *numbers, *ians, len, mx, mn, ans_size; + npy_intp *numbers, *ians, *iweights, len, mx, mn, ans_size; npy_intp minlength = 0; npy_intp i; double *weights , *dans; @@ -200,13 +200,25 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, if (wts == NULL) { goto fail; } - weights = (double *)PyArray_DATA(wts); if (PyArray_SIZE(wts) != len) { PyErr_SetString(PyExc_ValueError, "The weights and list don't have the same length."); goto fail; } - if (PyArray_ISFLOAT(wts)) { + if (PyArray_ISINTEGER(wts)) { + iweights = (npy_intp *)PyArray_DATA(wts); + ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_INTP, 0); + if (ans == NULL) { + goto fail; + } + ians = (npy_intp *)PyArray_DATA(ans); + NPY_BEGIN_ALLOW_THREADS; + for (i = 0; i < len; i++) { + ians[numbers[i]] += iweights[i]; + } + NPY_END_ALLOW_THREADS; + } else if (PyArray_ISFLOAT(wts)) { + weights = (double *)PyArray_DATA(wts); ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_DOUBLE, 0); if (ans == NULL) { goto fail; From c9ef986a5b56ca38f9cf7f7460081afa78b78150 Mon Sep 17 00:00:00 2001 From: Alex Gregory Date: Fri, 28 Apr 2023 21:16:32 +0100 Subject: [PATCH 3/5] Switch to unsigned integers for the weights --- numpy/core/src/multiarray/compiled_base.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index c2b28f57d65a..1ba4d32e8ae5 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -115,9 +115,10 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, { PyObject *list = NULL, *weight = Py_None, *mlength = NULL; PyArrayObject *lst = NULL, *ans = NULL, *wts = NULL; - npy_intp *numbers, *ians, *iweights, len, mx, mn, ans_size; + npy_intp *numbers, len, mx, mn, ans_size; npy_intp minlength = 0; npy_intp i; + npy_uintp *ians, *iweights; double *weights , *dans; NPY_PREPARE_ARGPARSER; @@ -206,12 +207,12 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, goto fail; } if (PyArray_ISINTEGER(wts)) { - iweights = (npy_intp *)PyArray_DATA(wts); - ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_INTP, 0); + iweights = (npy_uintp *)PyArray_DATA(wts); + ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_UINTP, 0); if (ans == NULL) { goto fail; } - ians = (npy_intp *)PyArray_DATA(ans); + ians = (npy_uintp *)PyArray_DATA(ans); NPY_BEGIN_ALLOW_THREADS; for (i = 0; i < len; i++) { ians[numbers[i]] += iweights[i]; From 2cfe3a7307929389ee313a4232f0a2d95bc1b4d9 Mon Sep 17 00:00:00 2001 From: Alex Gregory Date: Fri, 28 Apr 2023 22:00:04 +0100 Subject: [PATCH 4/5] Change to npy_uintp to reflect type at initialization --- numpy/core/src/multiarray/compiled_base.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index 1ba4d32e8ae5..1e61a8862625 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -184,11 +184,11 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, } } if (weight == Py_None) { - ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_INTP, 0); + ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_UINTP, 0); if (ans == NULL) { goto fail; } - ians = (npy_intp *)PyArray_DATA(ans); + ians = (npy_uintp *)PyArray_DATA(ans); NPY_BEGIN_ALLOW_THREADS; for (i = 0; i < len; i++) ians[numbers[i]] += 1; From 6ee969cd9345c825948c2c78cd5078a3fbfebc86 Mon Sep 17 00:00:00 2001 From: Alex Gregory Date: Fri, 28 Apr 2023 22:05:08 +0100 Subject: [PATCH 5/5] Initialise weights for the complex case --- numpy/core/src/multiarray/compiled_base.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numpy/core/src/multiarray/compiled_base.c b/numpy/core/src/multiarray/compiled_base.c index 1e61a8862625..e775db18b48e 100644 --- a/numpy/core/src/multiarray/compiled_base.c +++ b/numpy/core/src/multiarray/compiled_base.c @@ -119,7 +119,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, npy_intp minlength = 0; npy_intp i; npy_uintp *ians, *iweights; - double *weights , *dans; + double *weights = NULL, *dans; NPY_PREPARE_ARGPARSER; if (npy_parse_arguments("bincount", args, len_args, kwnames, @@ -231,6 +231,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *const *args, } NPY_END_ALLOW_THREADS; } else if (PyArray_ISCOMPLEX(wts)) { + weights = (double *)PyArray_DATA(wts); ans = (PyArrayObject *)PyArray_ZEROS(1, &ans_size, NPY_CDOUBLE, 0); if (ans == NULL) { goto fail; 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