static int arrayOK(PyObject *o) { PyArrayObject *a; /* check it's a numpy array */ if (!PyArray_Check(o)) { PyErr_Format(PyExc_ValueError, "object must be a numpy array"); return 0; } a = (PyArrayObject *)o; /* check array of doubles */ if (PyArray_DTYPE(a)->type_num != NPY_DOUBLE) { PyErr_Format(PyExc_ValueError, "ArrayObject must be of type Float (a C double)"); return 0; } /* check no. dimensions */ if (PyArray_NDIM(a) != 2) { PyErr_Format(PyExc_ValueError, "ArrayObject must be 2-dimensional (is %d)", PyArray_NDIM(a)); return 0; } /* check the 2nd dimension is 3D */ if (PyArray_DIM(a, 1) != DQ_d) { PyErr_Format(PyExc_ValueError, "ArrayObject must be an array of %dD vectors (2nd array size is %" NPY_INTP_FMT ")", DQ_d, PyArray_DIM(a, 1)); return 0; } return 1; }
/* * Allocates a result array for a reduction operation, with * dimensions matching 'arr' except set to 1 with 0 stride * wherever axis_flags is True. Dropping the reduction axes * from the result must be done later by the caller once the * computation is complete. * * This function always allocates a base class ndarray. * * If 'dtype' isn't NULL, this function steals its reference. */ static PyArrayObject * allocate_reduce_result(PyArrayObject *arr, npy_bool *axis_flags, PyArray_Descr *dtype, int subok) { npy_intp strides[NPY_MAXDIMS], stride; npy_intp shape[NPY_MAXDIMS], *arr_shape = PyArray_DIMS(arr); npy_stride_sort_item strideperm[NPY_MAXDIMS]; int idim, ndim = PyArray_NDIM(arr); if (dtype == NULL) { dtype = PyArray_DTYPE(arr); Py_INCREF(dtype); } PyArray_CreateSortedStridePerm(PyArray_NDIM(arr), PyArray_STRIDES(arr), strideperm); /* Build the new strides and shape */ stride = dtype->elsize; memcpy(shape, arr_shape, ndim * sizeof(shape[0])); for (idim = ndim-1; idim >= 0; --idim) { npy_intp i_perm = strideperm[idim].perm; if (axis_flags[i_perm]) { strides[i_perm] = 0; shape[i_perm] = 1; } else { strides[i_perm] = stride; stride *= shape[i_perm]; } } /* Finally, allocate the array */ return (PyArrayObject *)PyArray_NewFromDescr( subok ? Py_TYPE(arr) : &PyArray_Type, dtype, ndim, shape, strides, NULL, 0, subok ? (PyObject *)arr : NULL); }