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

Function PyArray_BroadcastToShape

numpy/core/src/multiarray/iterators.c:205–271  ·  view source on GitHub ↗

NUMPY_API * Get Iterator broadcast to a particular shape */

Source from the content-addressed store, hash-verified

203 * Get Iterator broadcast to a particular shape
204 */
205NPY_NO_EXPORT PyObject *
206PyArray_BroadcastToShape(PyObject *obj, npy_intp *dims, int nd)
207{
208 PyArrayIterObject *it;
209 int i, diff, j, compat, k;
210 PyArrayObject *ao = (PyArrayObject *)obj;
211
212 if (PyArray_NDIM(ao) > nd) {
213 goto err;
214 }
215 compat = 1;
216 diff = j = nd - PyArray_NDIM(ao);
217 for (i = 0; i < PyArray_NDIM(ao); i++, j++) {
218 if (PyArray_DIMS(ao)[i] == 1) {
219 continue;
220 }
221 if (PyArray_DIMS(ao)[i] != dims[j]) {
222 compat = 0;
223 break;
224 }
225 }
226 if (!compat) {
227 goto err;
228 }
229 it = (PyArrayIterObject *)PyArray_malloc(sizeof(PyArrayIterObject));
230 if (it == NULL) {
231 return NULL;
232 }
233 PyObject_Init((PyObject *)it, &PyArrayIter_Type);
234
235 PyArray_UpdateFlags(ao, NPY_ARRAY_C_CONTIGUOUS);
236 if (PyArray_ISCONTIGUOUS(ao)) {
237 it->contiguous = 1;
238 }
239 else {
240 it->contiguous = 0;
241 }
242 Py_INCREF(ao);
243 it->ao = ao;
244 it->size = PyArray_MultiplyList(dims, nd);
245 it->nd_m1 = nd - 1;
246 if (nd != 0) {
247 it->factors[nd-1] = 1;
248 }
249 for (i = 0; i < nd; i++) {
250 it->dims_m1[i] = dims[i] - 1;
251 k = i - diff;
252 if ((k < 0) || PyArray_DIMS(ao)[k] != dims[i]) {
253 it->contiguous = 0;
254 it->strides[i] = 0;
255 }
256 else {
257 it->strides[i] = PyArray_STRIDES(ao)[k];
258 }
259 it->backstrides[i] = it->strides[i] * it->dims_m1[i];
260 if (i > 0) {
261 it->factors[nd-i-1] = it->factors[nd-i] * dims[nd-i];
262 }

Callers 1

ufunc_atFunction · 0.85

Calls 5

PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_UpdateFlagsFunction · 0.85
PyArray_MultiplyListFunction · 0.85
PyArray_STRIDESFunction · 0.85

Tested by

no test coverage detected