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

Function _fix_unknown_dimension

numpy/core/src/multiarray/shape.c:477–522  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

475}
476
477static int
478_fix_unknown_dimension(PyArray_Dims *newshape, PyArrayObject *arr)
479{
480 npy_intp *dimensions;
481 npy_intp s_original = PyArray_SIZE(arr);
482 npy_intp i_unknown, s_known;
483 int i, n;
484
485 dimensions = newshape->ptr;
486 n = newshape->len;
487 s_known = 1;
488 i_unknown = -1;
489
490 for (i = 0; i < n; i++) {
491 if (dimensions[i] < 0) {
492 if (i_unknown == -1) {
493 i_unknown = i;
494 }
495 else {
496 PyErr_SetString(PyExc_ValueError,
497 "can only specify one unknown dimension");
498 return -1;
499 }
500 }
501 else if (npy_mul_sizes_with_overflow(&s_known, s_known,
502 dimensions[i])) {
503 raise_reshape_size_mismatch(newshape, arr);
504 return -1;
505 }
506 }
507
508 if (i_unknown >= 0) {
509 if (s_known == 0 || s_original % s_known != 0) {
510 raise_reshape_size_mismatch(newshape, arr);
511 return -1;
512 }
513 dimensions[i_unknown] = s_original / s_known;
514 }
515 else {
516 if (s_original != s_known) {
517 raise_reshape_size_mismatch(newshape, arr);
518 return -1;
519 }
520 }
521 return 0;
522}
523
524/*NUMPY_API
525 *

Callers 1

PyArray_NewshapeFunction · 0.85

Calls 1

Tested by

no test coverage detected