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

Function array_setscalar

numpy/core/src/multiarray/methods.c:768–849  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

766}
767
768static PyObject *
769array_setscalar(PyArrayObject *self, PyObject *args)
770{
771 npy_intp multi_index[NPY_MAXDIMS];
772 int n = PyTuple_GET_SIZE(args) - 1;
773 int idim, ndim = PyArray_NDIM(self);
774 PyObject *obj;
775
776 if (n < 0) {
777 PyErr_SetString(PyExc_ValueError,
778 "itemset must have at least one argument");
779 return NULL;
780 }
781 if (PyArray_FailUnlessWriteable(self, "assignment destination") < 0) {
782 return NULL;
783 }
784
785 obj = PyTuple_GET_ITEM(args, n);
786
787 /* If there is a tuple as a single argument, treat it as the argument */
788 if (n == 1 && PyTuple_Check(PyTuple_GET_ITEM(args, 0))) {
789 args = PyTuple_GET_ITEM(args, 0);
790 n = PyTuple_GET_SIZE(args);
791 }
792
793 if (n == 0) {
794 if (PyArray_SIZE(self) == 1) {
795 for (idim = 0; idim < ndim; ++idim) {
796 multi_index[idim] = 0;
797 }
798 }
799 else {
800 PyErr_SetString(PyExc_ValueError,
801 "can only convert an array of size 1 to a Python scalar");
802 return NULL;
803 }
804 }
805 /* Special case of C-order flat indexing... :| */
806 else if (n == 1 && ndim != 1) {
807 npy_intp *shape = PyArray_SHAPE(self);
808 npy_intp value, size = PyArray_SIZE(self);
809
810 value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, 0));
811 if (error_converting(value)) {
812 return NULL;
813 }
814
815 if (check_and_adjust_index(&value, size, -1, NULL) < 0) {
816 return NULL;
817 }
818
819 /* Convert the flat index into a multi-index */
820 for (idim = ndim-1; idim >= 0; --idim) {
821 multi_index[idim] = value % shape[idim];
822 value /= shape[idim];
823 }
824 }
825 /* A multi-index tuple */

Callers

nothing calls this directly

Calls 6

PyArray_NDIMFunction · 0.85
PyArray_SHAPEFunction · 0.85
PyArray_PyIntAsIntpFunction · 0.85
check_and_adjust_indexFunction · 0.85

Tested by

no test coverage detected