pangolin.interface.vindex#
- pangolin.interface.vindex(var, *indices)#
Index an RV using fully vectorized indexing.
varis treated as a scalar function of its indices, which are broadcast according to the called interface’s currentconfig.broadcastingmode. Slices are converted to one-dimensional integer arrays before broadcasting. The result therefore uses Pangolin’s explicit scalar broadcasting rules rather than NumPy’s mixed basic/advanced indexing rules.The name honors
vindexfrom NEP 21, although Pangolin’s precise semantics differ.Every dimension must be explicitly indexed, or covered by a single ellipsis. Missing trailing indices are not filled with full slices. For a matrix
x,x.vindex[0]raisesValueError; usex.vindex[0, :]orx.vindex[0, ...]instead. The same requirement applies to explicitvindexcalls. Expanded slices still follow the broadcasting rule.- Parameters:
var (RVLike) – RV or array-like value to index.
*indices (Index) – One scalar, array-like, RV, or slice per dimension of
var. A single ellipsis may stand for the required number of full slices.
- Returns:
indexed – Indexed RV whose shape is the common broadcast shape of the converted indices.
- Raises:
TypeError – If no indices are supplied. Use an ellipsis to index a scalar RV.
ValueError – If the number of indices does not match the rank of
var, an ellipsis is invalid, or the index shapes cannot be broadcast under the configured mode.
- Return type:
Examples
Index a 2D array with two matching 2D arrays:
>>> from pangolin import ir >>> import pangolin.interface as pi >>> A = pi.constant([[0., 0, 0], [1, 1, 1], [2, 2, 2]]) >>> B = pi.constant([[0, 1, 2], [2, 1, 0]]) >>> C = pi.constant([[2, 1, 1], [0, 0, 0]]) >>> D = pi.vindex(A, B, C) >>> ir.print_upstream(D) shape | statement ------ | --------- (3, 3) | a = [[0. 0. 0.] [1. 1. 1.] [2. 2. 2.]] (2, 3) | b = [[0 1 2] [2 1 0]] (2, 3) | c = [[2 1 1] [0 0 0]] (2, 3) | d = vmap(vmap(index, [None, 0, 0], 3), [None, 0, 0], 2)(a, b, c)
With a square matrix, matching slices extract the diagonal:
>>> A = pi.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> B = pi.vindex(A, slice(None), slice(None)) >>> ir.print_upstream(B) shape | statement ------ | --------- (3, 3) | a = [[1 2 3] [4 5 6] [7 8 9]] (3,) | b = [0 1 2] (3,) | c = [0 1 2] (3,) | d = vmap(index, [None, 0, 0], 3)(a, b, c)
Reversing one slice extracts the anti-diagonal:
>>> C = pi.vindex(A, slice(None), slice(None, None, -1)) >>> ir.print_upstream(C) shape | statement ------ | --------- (3, 3) | a = [[1 2 3] [4 5 6] [7 8 9]] (3,) | b = [0 1 2] (3,) | c = [2 1 0] (3,) | d = vmap(index, [None, 0, 0], 3)(a, b, c)