Skip to content

Ends and Coends

End and coend computations in enriched categories.

ends_coends

Ends and coends: general contraction principles for V-enriched categories.

The coend ∫^X F(X,X) computes the join (existential) over the diagonal of matched contravariant/covariant dimensions. The end ∫_X F(X,X) computes the meet (universal) over the diagonal.

These generalize composition (coend over shared object) and the hom in the functor category (end over all objects).

coend

coend(tensor: Tensor, contra_dims: tuple[int, ...], co_dims: tuple[int, ...], algebra: Algebra | None = None) -> Tensor

Compute coend ∫^X F(X,X) via diagonal extraction and join.

For each matched pair (contra_dims[i], co_dims[i]), restricts the tensor to the diagonal (where the contravariant and covariant indices agree) and then joins (existential quantification) over those matched dimensions.

PARAMETER DESCRIPTION
tensor

The input tensor representing a bifunctor F(X, Y) with both contravariant and covariant occurrences of X.

TYPE: Tensor

contra_dims

Dimension indices for the contravariant occurrences of X.

TYPE: tuple[int, ...]

co_dims

Dimension indices for the covariant occurrences of X. Must be same length as contra_dims, with matching sizes.

TYPE: tuple[int, ...]

algebra

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Algebra or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

Result with matched dimension pairs removed via join.

Source code in src/quivers/enriched/ends_coends.py
def coend(
    tensor: torch.Tensor,
    contra_dims: tuple[int, ...],
    co_dims: tuple[int, ...],
    algebra: Algebra | None = None,
) -> torch.Tensor:
    """Compute coend ∫^X F(X,X) via diagonal extraction and join.

    For each matched pair (contra_dims[i], co_dims[i]), restricts the
    tensor to the diagonal (where the contravariant and covariant
    indices agree) and then joins (existential quantification) over
    those matched dimensions.

    Parameters
    ----------
    tensor : torch.Tensor
        The input tensor representing a bifunctor F(X, Y) with both
        contravariant and covariant occurrences of X.
    contra_dims : tuple[int, ...]
        Dimension indices for the contravariant occurrences of X.
    co_dims : tuple[int, ...]
        Dimension indices for the covariant occurrences of X.
        Must be same length as contra_dims, with matching sizes.
    algebra : Algebra or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        Result with matched dimension pairs removed via join.
    """
    if algebra is None:
        algebra = PRODUCT_FUZZY

    if len(contra_dims) != len(co_dims):
        raise ValueError(
            f"contra_dims and co_dims must have same length, "
            f"got {len(contra_dims)} and {len(co_dims)}"
        )

    if len(contra_dims) == 0:
        return tensor

    return _trace_and_reduce(tensor, contra_dims, co_dims, algebra.join)

end

end(tensor: Tensor, contra_dims: tuple[int, ...], co_dims: tuple[int, ...], algebra: Algebra | None = None) -> Tensor

Compute end ∫_X F(X,X) via diagonal extraction and meet.

Dual to coend: restricts to diagonal and applies meet (universal quantification) over matched dimensions.

PARAMETER DESCRIPTION
tensor

The input tensor.

TYPE: Tensor

contra_dims

Dimension indices for contravariant occurrences.

TYPE: tuple[int, ...]

co_dims

Dimension indices for covariant occurrences.

TYPE: tuple[int, ...]

algebra

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Algebra or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

Result with matched dimension pairs removed via meet.

Source code in src/quivers/enriched/ends_coends.py
def end(
    tensor: torch.Tensor,
    contra_dims: tuple[int, ...],
    co_dims: tuple[int, ...],
    algebra: Algebra | None = None,
) -> torch.Tensor:
    """Compute end ∫_X F(X,X) via diagonal extraction and meet.

    Dual to coend: restricts to diagonal and applies meet (universal
    quantification) over matched dimensions.

    Parameters
    ----------
    tensor : torch.Tensor
        The input tensor.
    contra_dims : tuple[int, ...]
        Dimension indices for contravariant occurrences.
    co_dims : tuple[int, ...]
        Dimension indices for covariant occurrences.
    algebra : Algebra or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        Result with matched dimension pairs removed via meet.
    """
    if algebra is None:
        algebra = PRODUCT_FUZZY

    if len(contra_dims) != len(co_dims):
        raise ValueError(
            f"contra_dims and co_dims must have same length, "
            f"got {len(contra_dims)} and {len(co_dims)}"
        )

    if len(contra_dims) == 0:
        return tensor

    return _trace_and_reduce(tensor, contra_dims, co_dims, algebra.meet)