MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_Where

Function PyArray_Where

numpy/core/src/multiarray/multiarraymodule.c:3368–3550  ·  view source on GitHub ↗

NUMPY_API * Where */

Source from the content-addressed store, hash-verified

3366 * Where
3367 */
3368NPY_NO_EXPORT PyObject *
3369PyArray_Where(PyObject *condition, PyObject *x, PyObject *y)
3370{
3371 PyArrayObject *arr, *ax, *ay;
3372 PyObject *ret = NULL;
3373
3374 arr = (PyArrayObject *)PyArray_FROM_O(condition);
3375 if (arr == NULL) {
3376 return NULL;
3377 }
3378 if ((x == NULL) && (y == NULL)) {
3379 ret = PyArray_Nonzero(arr);
3380 Py_DECREF(arr);
3381 return ret;
3382 }
3383 if ((x == NULL) || (y == NULL)) {
3384 Py_DECREF(arr);
3385 PyErr_SetString(PyExc_ValueError,
3386 "either both or neither of x and y should be given");
3387 return NULL;
3388 }
3389
3390 NPY_cast_info cast_info = {.func = NULL};
3391
3392 ax = (PyArrayObject*)PyArray_FROM_O(x);
3393 ay = (PyArrayObject*)PyArray_FROM_O(y);
3394 if (ax == NULL || ay == NULL) {
3395 goto fail;
3396 }
3397 else {
3398 npy_uint32 flags = NPY_ITER_EXTERNAL_LOOP | NPY_ITER_BUFFERED |
3399 NPY_ITER_REFS_OK | NPY_ITER_ZEROSIZE_OK;
3400 PyArrayObject * op_in[4] = {
3401 NULL, arr, ax, ay
3402 };
3403 npy_uint32 op_flags[4] = {
3404 NPY_ITER_WRITEONLY | NPY_ITER_ALLOCATE | NPY_ITER_NO_SUBTYPE,
3405 NPY_ITER_READONLY,
3406 NPY_ITER_READONLY | NPY_ITER_ALIGNED,
3407 NPY_ITER_READONLY | NPY_ITER_ALIGNED
3408 };
3409 PyArray_Descr * common_dt = PyArray_ResultType(2, &op_in[0] + 2,
3410 0, NULL);
3411 PyArray_Descr * op_dt[4] = {common_dt, PyArray_DescrFromType(NPY_BOOL),
3412 common_dt, common_dt};
3413 NpyIter * iter;
3414 NPY_BEGIN_THREADS_DEF;
3415
3416 if (common_dt == NULL || op_dt[1] == NULL) {
3417 Py_XDECREF(op_dt[1]);
3418 Py_XDECREF(common_dt);
3419 goto fail;
3420 }
3421 iter = NpyIter_MultiNew(4, op_in, flags,
3422 NPY_KEEPORDER, NPY_UNSAFE_CASTING,
3423 op_flags, op_dt);
3424 Py_DECREF(op_dt[1]);
3425 Py_DECREF(common_dt);

Callers 1

array_whereFunction · 0.85

Calls 13

PyArray_NonzeroFunction · 0.85
PyArray_ResultTypeFunction · 0.85
NpyIter_MultiNewFunction · 0.85
NpyIter_GetOperandArrayFunction · 0.85
NpyIter_GetTransferFlagsFunction · 0.85
NpyIter_GetIterSizeFunction · 0.85
NpyIter_GetDataPtrArrayFunction · 0.85
NPY_cast_info_xfreeFunction · 0.85
NpyIter_DeallocateFunction · 0.85

Tested by

no test coverage detected