q1ss.binalg.base

Abstract base class for binary tensors.

Shape

Shape

Type alias for tensor shapes.

alias of tuple[int, …]

bintensor

class bintensor(data, *, readonly=False, copy=False)[source]

Bases: object

Abstract base class for mutable binary tensors.

exception ReadonlyError[source]

Bases: ValueError

Specialised ValueError subclass for when attempting to mutate a readonly tensor.

exception ShapeError[source]

Bases: ValueError

Specialised ValueError subclass for shape errors.

__add__(other)[source]

Componentwise mod 2 addition (bitwise XOR). :raises ShapeError: if the tensors have different shapes.

Parameters:

other (Self | Integral)

Return type:

Self

__iadd__(other)[source]

Inplace componentwise mod 2 addition (bitwise XOR). :raises ShapeError: if the tensors have different shapes.

Parameters:

other (Self | Integral)

Return type:

Self

__imul__(other)[source]

Inplace componentwise mod 2 multiplication (bitwise AND). :raises ShapeError: if the tensors have different shapes.

Parameters:

other (Self | Integral)

Return type:

Self

__isub__(other)[source]

Alias for __iadd__.

Parameters:

other (Self | Integral)

Return type:

Self

__mul__(other)[source]

Componentwise mod 2 multiplication (bitwise AND). :raises ShapeError: if the tensors have different shapes.

Parameters:

other (Self | Integral)

Return type:

Self

__neg__()[source]

Componentwise mod 2 negation: returns the tensor unchanged.

Return type:

Self

static __new__(cls, data, *, readonly=False, copy=False)[source]

Creates a new tensor with given shape from the given binary data. The class BinTensor cannot be instantiated directly.

If readonly=True, the resulting tensor and its data are readonly. If copy=True, a fresh copy of the given data is used.

Warning

The internal logic of the BinTensor class and its subclasses presumes that the given data will not be mutated externally to the BinTensor object after construction. If a fresh copy is needed, pass copy=True at construction.

Parameters:
  • data (npt.ArrayLike)

  • readonly (bool; default = False)

  • copy (bool; default = False)

Return type:

Self

__pos__()[source]

Returns the tensor unchanged.

Return type:

Self

__sub__(other)[source]

Alias for __add__.

Parameters:

other (Self | Integral)

Return type:

Self

copy(*, readonly=False)[source]

Returns a copy of this tensor. If readonly=True, the resulting copy is readonly.

Parameters:

readonly (bool; default = False)

Return type:

Self

property data

The underlying binary data.

Return type:

BinTensor

property is_zero

Whether the tensor is the constant zero tensor.

Return type:

bool

classmethod make_readonly(*tensors)[source]

Makes all given tensors readonly.

Parameters:

tensors (bintensor; variadic positional)

Return type:

None

property readonly

Whether the tensor is readonly.

Return type:

bool

property shape

The shape of this bit tensor.

Return type:

Shape