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

Function PyArray_Round

numpy/core/src/multiarray/calculation.c:557–711  ·  view source on GitHub ↗

NUMPY_API * Round */

Source from the content-addressed store, hash-verified

555 * Round
556 */
557NPY_NO_EXPORT PyObject *
558PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out)
559{
560 PyObject *f, *ret = NULL, *tmp, *op1, *op2;
561 int ret_int=0;
562 PyArray_Descr *my_descr;
563 if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) {
564 PyErr_SetString(PyExc_ValueError,
565 "invalid output shape");
566 return NULL;
567 }
568 if (PyArray_ISCOMPLEX(a)) {
569 PyObject *part;
570 PyObject *round_part;
571 PyObject *arr;
572 int res;
573
574 if (out) {
575 arr = (PyObject *)out;
576 Py_INCREF(arr);
577 }
578 else {
579 arr = PyArray_Copy(a);
580 if (arr == NULL) {
581 return NULL;
582 }
583 }
584
585 /* arr.real = a.real.round(decimals) */
586 part = PyObject_GetAttrString((PyObject *)a, "real");
587 if (part == NULL) {
588 Py_DECREF(arr);
589 return NULL;
590 }
591 part = PyArray_EnsureAnyArray(part);
592 round_part = PyArray_Round((PyArrayObject *)part,
593 decimals, NULL);
594 Py_DECREF(part);
595 if (round_part == NULL) {
596 Py_DECREF(arr);
597 return NULL;
598 }
599 res = PyObject_SetAttrString(arr, "real", round_part);
600 Py_DECREF(round_part);
601 if (res < 0) {
602 Py_DECREF(arr);
603 return NULL;
604 }
605
606 /* arr.imag = a.imag.round(decimals) */
607 part = PyObject_GetAttrString((PyObject *)a, "imag");
608 if (part == NULL) {
609 Py_DECREF(arr);
610 return NULL;
611 }
612 part = PyArray_EnsureAnyArray(part);
613 round_part = PyArray_Round((PyArrayObject *)part,
614 decimals, NULL);

Callers 1

array_roundFunction · 0.85

Calls 8

PyArray_EnsureAnyArrayFunction · 0.85
PyArray_AssignArrayFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_EmptyFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
power_of_tenFunction · 0.85
PyArray_CastToTypeFunction · 0.85

Tested by

no test coverage detected