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

Function update_shape

numpy/core/src/multiarray/array_coercion.c:554–605  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

552
553
554static int
555update_shape(int curr_ndim, int *max_ndim,
556 npy_intp out_shape[], int new_ndim,
557 const npy_intp new_shape[], npy_bool sequence,
558 enum _dtype_discovery_flags *flags)
559{
560 int success = 0; /* unsuccessful if array is ragged */
561 const npy_bool max_dims_reached = *flags & MAX_DIMS_WAS_REACHED;
562
563 if (curr_ndim + new_ndim > *max_ndim) {
564 success = -1;
565 /* Only update/check as many dims as possible, max_ndim is unchanged */
566 new_ndim = *max_ndim - curr_ndim;
567 }
568 else if (!sequence && (*max_ndim != curr_ndim + new_ndim)) {
569 /*
570 * Sequences do not update max_ndim, otherwise shrink and check.
571 * This is depth first, so if it is already set, `out_shape` is filled.
572 */
573 *max_ndim = curr_ndim + new_ndim;
574 /* If a shape was already set, this is also ragged */
575 if (max_dims_reached) {
576 success = -1;
577 }
578 }
579 for (int i = 0; i < new_ndim; i++) {
580 npy_intp curr_dim = out_shape[curr_ndim + i];
581 npy_intp new_dim = new_shape[i];
582
583 if (!max_dims_reached) {
584 out_shape[curr_ndim + i] = new_dim;
585 }
586 else if (new_dim != curr_dim) {
587 /* The array is ragged, and this dimension is unusable already */
588 success = -1;
589 if (!sequence) {
590 /* Remove dimensions that we cannot use: */
591 *max_ndim -= new_ndim - i;
592 }
593 else {
594 assert(i == 0);
595 /* max_ndim is usually not updated for sequences, so set now: */
596 *max_ndim = curr_ndim;
597 }
598 break;
599 }
600 }
601 if (!sequence) {
602 *flags |= MAX_DIMS_WAS_REACHED;
603 }
604 return success;
605}
606
607
608#define COERCION_CACHE_CACHE_SIZE 5

Callers 2

handle_scalarFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected