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, ...], quantale: Quantale | 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, ...]

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

Result with matched dimension pairs removed via join.

Source code in src/quivers/enriched/ends_coends.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def coend(
    tensor: torch.Tensor,
    contra_dims: tuple[int, ...],
    co_dims: tuple[int, ...],
    quantale: Quantale | 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.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        Result with matched dimension pairs removed via join.
    """
    from quivers.core.quantales import PRODUCT_FUZZY

    if quantale is None:
        quantale = 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, quantale.join)

end

end(tensor: Tensor, contra_dims: tuple[int, ...], co_dims: tuple[int, ...], quantale: Quantale | 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, ...]

quantale

The enrichment algebra. Defaults to PRODUCT_FUZZY.

TYPE: Quantale or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

Result with matched dimension pairs removed via meet.

Source code in src/quivers/enriched/ends_coends.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def end(
    tensor: torch.Tensor,
    contra_dims: tuple[int, ...],
    co_dims: tuple[int, ...],
    quantale: Quantale | 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.
    quantale : Quantale or None
        The enrichment algebra. Defaults to PRODUCT_FUZZY.

    Returns
    -------
    torch.Tensor
        Result with matched dimension pairs removed via meet.
    """
    from quivers.core.quantales import PRODUCT_FUZZY

    if quantale is None:
        quantale = 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, quantale.meet)