NUMPY_API * Get Iterator broadcast to a particular shape */
| 203 | * Get Iterator broadcast to a particular shape |
| 204 | */ |
| 205 | NPY_NO_EXPORT PyObject * |
| 206 | PyArray_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 | } |
no test coverage detected