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

Function _validate_num_dims

numpy/core/src/umath/ufunc_object.c:1676–1736  ·  view source on GitHub ↗

* Validate that operands have enough dimensions, accounting for * possible flexible dimensions that may be absent. */

Source from the content-addressed store, hash-verified

1674 * possible flexible dimensions that may be absent.
1675 */
1676static int
1677_validate_num_dims(PyUFuncObject *ufunc, PyArrayObject **op,
1678 npy_uint32 *core_dim_flags,
1679 int *op_core_num_dims) {
1680 int i, j;
1681 int nin = ufunc->nin;
1682 int nop = ufunc->nargs;
1683
1684 for (i = 0; i < nop; i++) {
1685 if (op[i] != NULL) {
1686 int op_ndim = PyArray_NDIM(op[i]);
1687
1688 if (op_ndim < op_core_num_dims[i]) {
1689 int core_offset = ufunc->core_offsets[i];
1690 /* We've too few, but some dimensions might be flexible */
1691 for (j = core_offset;
1692 j < core_offset + ufunc->core_num_dims[i]; j++) {
1693 int core_dim_index = ufunc->core_dim_ixs[j];
1694 if ((core_dim_flags[core_dim_index] &
1695 UFUNC_CORE_DIM_CAN_IGNORE)) {
1696 int i1, j1, k;
1697 /*
1698 * Found a dimension that can be ignored. Flag that
1699 * it is missing, and unflag that it can be ignored,
1700 * since we are doing so already.
1701 */
1702 core_dim_flags[core_dim_index] |= UFUNC_CORE_DIM_MISSING;
1703 core_dim_flags[core_dim_index] ^= UFUNC_CORE_DIM_CAN_IGNORE;
1704 /*
1705 * Reduce the number of core dimensions for all
1706 * operands that use this one (including ours),
1707 * and check whether we're now OK.
1708 */
1709 for (i1 = 0, k=0; i1 < nop; i1++) {
1710 for (j1 = 0; j1 < ufunc->core_num_dims[i1]; j1++) {
1711 if (ufunc->core_dim_ixs[k++] == core_dim_index) {
1712 op_core_num_dims[i1]--;
1713 }
1714 }
1715 }
1716 if (op_ndim == op_core_num_dims[i]) {
1717 break;
1718 }
1719 }
1720 }
1721 if (op_ndim < op_core_num_dims[i]) {
1722 PyErr_Format(PyExc_ValueError,
1723 "%s: %s operand %d does not have enough "
1724 "dimensions (has %d, gufunc core with "
1725 "signature %s requires %d)",
1726 ufunc_get_name_cstr(ufunc),
1727 i < nin ? "Input" : "Output",
1728 i < nin ? i : i - nin, PyArray_NDIM(op[i]),
1729 ufunc->core_signature, op_core_num_dims[i]);
1730 return -1;
1731 }
1732 }
1733 }

Callers 1

Calls 2

PyArray_NDIMFunction · 0.85
ufunc_get_name_cstrFunction · 0.85

Tested by

no test coverage detected