Skip to content

Day Convolution

Day convolution combines two presheaves over a monoidal category by summing, for each requested output object, the contributions of every pair of objects whose tensor product maps to it (Kelly, 1982) (Day convolution). Ordinary convolution, which sums over every way of splitting an index, is the case of a discrete monoid. Here presheaves and morphisms are finite tensors, and the chosen IdempotentSemiring fixes how the contributions combine, through its join.

day_convolution

Day convolution for V-enriched presheaves.

Given a monoidal structure (C, ⊗, I) on a V-enriched category C, the Day convolution defines a monoidal structure on the presheaf category [C^op, V]. For two presheaves F, G: C^op → V, their Day convolution is:

(F ⊛ G)(C) = ∫^{A,B} C(C, A ⊗ B) ⊗ F(A) ⊗ G(B)

In our finite setting, this becomes a computable coend (join over all pairs (A, B) such that A ⊗ B can map to C). The coend is a join, so V must be a complete lattice (an IdempotentSemiring). The products are contractions of quivers.core.wiring.EinsumWiring, so Day convolution and wiring share one evaluation and its cost claim.

The unit for Day convolution is the representable presheaf at the monoidal unit: y(I)(C) = C(C, I).

This module provides:

day_convolution(): compute F ⊛ G
day_unit(): the unit presheaf for ⊛
day_convolution_profunctors(): Day convolution on profunctors

day_convolution

day_convolution(f_values: Tensor, g_values: Tensor, objects: Sequence[SetObject], monoidal: MonoidalStructure, algebra: IdempotentSemiring, product_table: dict[tuple[int, int], tuple[int, Tensor]] | None = None) -> Tensor

Compute the Day convolution (F ⊛ G)(C) for each object C.

For finite discrete categories (no non-identity morphisms), the Day convolution simplifies to:

(F ⊛ G)(C) = ⋁_{A,B : A⊗B=C} F(A) ⊗ G(B)

With a product table that names, for each pair, the object C_k with A_i ⊗ B_j ≅ C_k and the weight h = C(C_k, A_i ⊗ B_j):

(F ⊛ G)(C_k) = ⋁_{(i, j) ↦ k} h(i, j) ⊗ F(A_i) ⊗ G(B_j)

The products over the pairs are the contraction ab, a, b -> ab of quivers.core.wiring.EinsumWiring (a, b -> ab without a table), and the coend sums them over the fiber of each object, so the work is 2 n^2 products and at most n f sums for n objects and a largest fiber of f pairs, never n^3.

PARAMETER DESCRIPTION
f_values

1D tensor of shape (n,) representing F evaluated at each object: F(objects[i]) = f_values[i].

TYPE: Tensor

g_values

1D tensor of shape (n,) representing G evaluated at each object: G(objects[i]) = g_values[i].

TYPE: Tensor

objects

The objects of the finite category (indexed 0..n-1).

TYPE: Sequence[SetObject]

monoidal

The monoidal structure providing ⊗ and I.

TYPE: MonoidalStructure

algebra

The enrichment algebra, a complete lattice.

TYPE: IdempotentSemiring

product_table

Optional precomputed product table mapping (i, j) to (k, hom_tensor) where objects[i] ⊗ objects[j] ≅ objects[k] and hom_tensor is the scalar C(objects[k], objects[i] ⊗ objects[j]). A pair it omits contributes nothing. If None, uses identity hom (discrete category).

TYPE: dict or None DEFAULT: None

RETURNS DESCRIPTION
Tensor

1D tensor of shape (n,) with (F ⊛ G)(objects[i]) at index i.

RAISES DESCRIPTION
TypeError

If the algebra is not an IdempotentSemiring.

ValueError

If the value tensors have the wrong shape, or (without a product table) the objects are not closed under ⊗.

Source code in src/quivers/enriched/day_convolution.py
def day_convolution(
    f_values: torch.Tensor,
    g_values: torch.Tensor,
    objects: Sequence[SetObject],
    monoidal: MonoidalStructure,
    algebra: IdempotentSemiring,
    product_table: dict[tuple[int, int], tuple[int, torch.Tensor]] | None = None,
) -> torch.Tensor:
    """Compute the Day convolution (F ⊛ G)(C) for each object C.

    For finite discrete categories (no non-identity morphisms),
    the Day convolution simplifies to:

        (F ⊛ G)(C) = ⋁_{A,B : A⊗B=C} F(A) ⊗ G(B)

    With a product table that names, for each pair, the object ``C_k``
    with ``A_i ⊗ B_j ≅ C_k`` and the weight ``h = C(C_k, A_i ⊗ B_j)``:

        (F ⊛ G)(C_k) = ⋁_{(i, j) ↦ k} h(i, j) ⊗ F(A_i) ⊗ G(B_j)

    The products over the pairs are the contraction ``ab, a, b -> ab``
    of `quivers.core.wiring.EinsumWiring` (``a, b -> ab`` without a
    table), and the coend sums them over the fiber of each object, so the
    work is ``2 n^2`` products and at most ``n f`` sums for ``n`` objects
    and a largest fiber of ``f`` pairs, never ``n^3``.

    Parameters
    ----------
    f_values : torch.Tensor
        1D tensor of shape (n,) representing F evaluated at each
        object: F(objects[i]) = f_values[i].
    g_values : torch.Tensor
        1D tensor of shape (n,) representing G evaluated at each
        object: G(objects[i]) = g_values[i].
    objects : Sequence[SetObject]
        The objects of the finite category (indexed 0..n-1).
    monoidal : MonoidalStructure
        The monoidal structure providing ⊗ and I.
    algebra : IdempotentSemiring
        The enrichment algebra, a complete lattice.
    product_table : dict or None
        Optional precomputed product table mapping (i, j) to
        (k, hom_tensor) where objects[i] ⊗ objects[j] ≅ objects[k]
        and hom_tensor is the scalar C(objects[k], objects[i] ⊗ objects[j]).
        A pair it omits contributes nothing. If None, uses identity hom
        (discrete category).

    Returns
    -------
    torch.Tensor
        1D tensor of shape (n,) with (F ⊛ G)(objects[i]) at index i.

    Raises
    ------
    TypeError
        If the algebra is not an `IdempotentSemiring`.
    ValueError
        If the value tensors have the wrong shape, or (without a
        product table) the objects are not closed under ``⊗``.
    """
    q = require_lattice(algebra, "Day convolution")
    n = len(objects)

    if f_values.shape != (n,) or g_values.shape != (n,):
        raise ValueError(
            f"f_values and g_values must have shape ({n},), got "
            f"{f_values.shape} and {g_values.shape}"
        )

    fibers: list[list[int]] = [[] for _ in range(n)]
    if product_table is None:
        for i in range(n):
            for j in range(n):
                prod = monoidal.product(objects[i], objects[j])
                matches = [k for k in range(n) if objects[k] == prod]
                if not matches:
                    raise ValueError(
                        f"Day convolution over a discrete category needs the "
                        f"objects closed under the monoidal product; "
                        f"{objects[i]!r} ⊗ {objects[j]!r} = {prod!r} is not "
                        f"among them"
                    )
                fibers[matches[0]].append(i * n + j)
        pairs = EinsumWiring(q, "a,b->ab").apply(f_values, g_values)
        return fiber_join(q, pairs.reshape(-1), fibers, q.zero_like((1,), pairs))

    unit = q.unit_like((), f_values)
    weights: list[torch.Tensor] = []
    for i in range(n):
        for j in range(n):
            entry = product_table.get((i, j))
            if entry is None:
                weights.append(unit)
                continue
            k, hom = entry
            fibers[k].append(i * n + j)
            weights.append(hom.to(dtype=f_values.dtype, device=f_values.device))
    table = torch.stack(weights).reshape(n, n)
    pairs = EinsumWiring(q, "ab,a,b->ab").apply(table, f_values, g_values)
    return fiber_join(q, pairs.reshape(-1), fibers, q.zero_like((1,), pairs))

day_unit

day_unit(objects: Sequence[SetObject], unit_index: int, algebra: IdempotentSemiring) -> Tensor

Create the unit presheaf for Day convolution.

The unit is the representable presheaf y(I) at the monoidal unit I. In the discrete case: y(I)(C) = I if C = I, else ⊥.

PARAMETER DESCRIPTION
objects

The objects of the finite category.

TYPE: Sequence[SetObject]

unit_index

The index of the monoidal unit in the objects list.

TYPE: int

algebra

The enrichment algebra, a complete lattice.

TYPE: IdempotentSemiring

RETURNS DESCRIPTION
Tensor

1D tensor of shape (n,) representing the unit presheaf.

RAISES DESCRIPTION
TypeError

If the algebra is not an IdempotentSemiring.

Source code in src/quivers/enriched/day_convolution.py
def day_unit(
    objects: Sequence[SetObject],
    unit_index: int,
    algebra: IdempotentSemiring,
) -> torch.Tensor:
    """Create the unit presheaf for Day convolution.

    The unit is the representable presheaf y(I) at the monoidal
    unit I. In the discrete case: y(I)(C) = I if C = I, else ⊥.

    Parameters
    ----------
    objects : Sequence[SetObject]
        The objects of the finite category.
    unit_index : int
        The index of the monoidal unit in the objects list.
    algebra : IdempotentSemiring
        The enrichment algebra, a complete lattice.

    Returns
    -------
    torch.Tensor
        1D tensor of shape (n,) representing the unit presheaf.

    Raises
    ------
    TypeError
        If the algebra is not an `IdempotentSemiring`.
    """
    q = require_lattice(algebra, "Day convolution")
    n = len(objects)
    result = torch.full((n,), q.zero)
    result[unit_index] = q.unit

    return result

day_convolution_profunctors

day_convolution_profunctors(p: Profunctor, q_prof: Profunctor, monoidal: MonoidalStructure, algebra: IdempotentSemiring) -> Profunctor

Day convolution of two profunctors.

Given profunctors P: A ↛ B and Q: C ↛ D, the Day convolution (with respect to the monoidal product) produces:

(P ⊛ Q): A⊗C ↛ B⊗D

with tensor:

(P ⊛ Q)(ac, bd) = P(a, b) ⊗ Q(c, d)

This is the external tensor product of profunctors, which extends the monoidal structure from C to Prof(C).

PARAMETER DESCRIPTION
p

Left profunctor P: A ↛ B.

TYPE: Profunctor

q_prof

Right profunctor Q: C ↛ D.

TYPE: Profunctor

monoidal

The monoidal structure providing ⊗.

TYPE: MonoidalStructure

algebra

The enrichment algebra, a complete lattice.

TYPE: IdempotentSemiring

RETURNS DESCRIPTION
Profunctor

The Day convolution P ⊛ Q: A⊗C ↛ B⊗D.

RAISES DESCRIPTION
TypeError

If the algebra is not an IdempotentSemiring.

Source code in src/quivers/enriched/day_convolution.py
def day_convolution_profunctors(
    p: Profunctor,
    q_prof: Profunctor,
    monoidal: MonoidalStructure,
    algebra: IdempotentSemiring,
) -> Profunctor:
    """Day convolution of two profunctors.

    Given profunctors P: A ↛ B and Q: C ↛ D, the Day convolution
    (with respect to the monoidal product) produces:

        (P ⊛ Q): A⊗C ↛ B⊗D

    with tensor:

        (P ⊛ Q)(ac, bd) = P(a, b) ⊗ Q(c, d)

    This is the external tensor product of profunctors, which
    extends the monoidal structure from C to Prof(C).

    Parameters
    ----------
    p : Profunctor
        Left profunctor P: A ↛ B.
    q_prof : Profunctor
        Right profunctor Q: C ↛ D.
    monoidal : MonoidalStructure
        The monoidal structure providing ⊗.
    algebra : IdempotentSemiring
        The enrichment algebra, a complete lattice.

    Returns
    -------
    Profunctor
        The Day convolution P ⊛ Q: A⊗C ↛ B⊗D.

    Raises
    ------
    TypeError
        If the algebra is not an `IdempotentSemiring`.
    """
    qnt = require_lattice(algebra, "Day convolution")

    contra = monoidal.product(p.contra, q_prof.contra)
    co = monoidal.product(p.co, q_prof.co)

    # the external product (P ⊛ Q)(ac, bd) = P(a, b) ⊗ Q(c, d), a
    # contraction that sums nothing and reorders the axes as a c b d
    n_a = p.contra.ndim
    n_b = p.co.ndim
    n_c = q_prof.contra.ndim
    n_d = q_prof.co.ndim
    letters = [chr(0x100 + index) for index in range(n_a + n_b + n_c + n_d)]
    a = "".join(letters[:n_a])
    b = "".join(letters[n_a : n_a + n_b])
    c = "".join(letters[n_a + n_b : n_a + n_b + n_c])
    d = "".join(letters[n_a + n_b + n_c :])
    result = EinsumWiring(qnt, f"{a}{b},{c}{d}->{a}{c}{b}{d}").apply(
        p.tensor, q_prof.tensor
    )

    return Profunctor(contra=contra, co=co, tensor=result, algebra=qnt)

Cited sources

G. M. Kelly. Basic Concepts of Enriched Category Theory. Cambridge University Press, 1982. Reprinted in Reprints in Theory and Applications of Categories 10 (2005), 1–136. URL: https://www.tac.mta.ca/tac/reprints/articles/10/tr10abs.html.