tensorstore.DimExpression.diagonal : DimExpression

Extracts the diagonal of the selected dimensions.

The selection dimensions are removed from the resultant index space, and a new dimension corresponding to the diagonal is added as the first dimension, with an input domain equal to the intersection of the input domains of the selection dimensions. The new dimension selection is equal to ts.d[0], corresponding to the newly added diagonal dimension.

The lower and upper bounds of the new diagonal dimension are implicit if, and only if, the lower or upper bounds, respectively, of every selected dimension are implicit.

Examples

>>> transform = ts.IndexTransform(input_shape=[2, 3],
...                               input_labels=["x", "y"])
>>> transform[ts.d['x', 'y'].diagonal]
Rank 1 -> 2 index space transform:
  Input domain:
    0: [0, 2)
  Output index maps:
    out[0] = 0 + 1 * in[0]
    out[1] = 0 + 1 * in[0]
>>> transform = ts.IndexTransform(3)
>>> transform[ts.d[0, 2].diagonal]
Rank 2 -> 3 index space transform:
  Input domain:
    0: (-inf*, +inf*)
    1: (-inf*, +inf*)
  Output index maps:
    out[0] = 0 + 1 * in[0]
    out[1] = 0 + 1 * in[1]
    out[2] = 0 + 1 * in[0]

Note

If zero dimensions are selected, diagonal simply results in a new singleton dimension as the first dimension, equivalent to [ts.newaxis]:

>>> transform = ts.IndexTransform(1)
>>> transform[ts.d[()].diagonal]
Rank 2 -> 1 index space transform:
  Input domain:
    0: (-inf*, +inf*)
    1: (-inf*, +inf*)
  Output index maps:
    out[0] = 0 + 1 * in[1]

If only one dimension is selected, diagonal is equivalent to .label[''].transpose[0]:

>>> transform = ts.IndexTransform(input_labels=['x', 'y'])
>>> transform[ts.d[1].diagonal]
Rank 2 -> 2 index space transform:
  Input domain:
    0: (-inf*, +inf*)
    1: (-inf*, +inf*) "x"
  Output index maps:
    out[0] = 0 + 1 * in[1]
    out[1] = 0 + 1 * in[0]