Template Broadcasting

xtensors.template_broadcaster(dims: Sequence[str | int], channels: Sequence[Literal['x', 'y'] | None]) Broadcaster

Return a template broadcaster with the specified dimensions and channels

class xtensors.AxisSelector

Abstract base class for objects that implement select_axis

__init__() None
abstract select_axis(X: XTensor, channel: str | None = None) int | None

Select an axis from the tensor X

Parameters:
  • X – tensor

  • channel – (optional) a string specifying the channel. If self.channel is set, axis selection will only be performed when the channel parameter matches self.channel, or else None is returned.

class xtensors.DimNameSelector(dimname: str, required: bool = False, channel: str | None = None)

Bases: AxisSelector

Select an axis based on its name

__init__(dimname: str, required: bool = False, channel: str | None = None) None
Parameters:
  • dimname – target axis name

  • required – if True, a KeyError will be raised if no axis named dimname is found when selecting an axis from a tensor.

  • channel – Selecting channel.

select_axis(X: XTensor, channel: str | None = None) int | None

Select an axis from the tensor X

Parameters:
  • X – tensor

  • channel – (optional) a string specifying the channel. If self.channel is set, axis selection will only be performed when the channel parameter matches self.channel, or else None is returned.

class xtensors.IndexSelector(axis: int, channel: str | None = None)

Bases: AxisSelector

Select an axis based on the index

__init__(axis: int, channel: str | None = None) None
Parameters:
  • axis – The axis index, negative indices are supported

  • channel – Selecting channel.

select_axis(X: XTensor, channel: str | None = None) int | None

Select an axis from the tensor X

Parameters:
  • X – tensor

  • channel – (optional) a string specifying the channel. If self.channel is set, axis selection will only be performed when the channel parameter matches self.channel, or else None is returned.

class xtensors.Template(*selectors: AxisSelector)

A tensor broadcasting template that can be reused to broadcast multiple tensors.

A template is basically a list of AxisSelector instances, each specifying how an axis (integer) should be selected.

__init__(*selectors: AxisSelector) None
Parameters:

selectors – A list of AxisSelector

check_well_defined()

Check if the AxisSelector s are configured in a self-consistent manner.

In particular, this method checks if there are DimNameSelector s with duplicate names or AxisSelector s with duplicate indices.

Raises:

ValueError if the check fails

cast_and_update(X: XTensor, channel: str | None = None) XTensor

Cast the input tensor according to the template and update self’s internal state (i.e. dimension names and coordinates).

clear()

Reset the internal states.

property dims: Dims

The dimension names gathered from casting tensors.

property coords: Coords

The coordinates names gathered from casting tensors.