Skip to content

Natural Transformations

A natural transformation from one functor to another gives, for each object, a component that converts the first functor's output into the second's (Mac Lane, 1998). Its naturality equation says that converting and then mapping an arrow agrees with mapping and then converting, for every arrow of the source category. The module gives the NaturalTransformation interface and natural transformations defined by their components.

natural_transformations

Natural transformations between endofunctors.

A natural transformation η: F ⇒ G is a family of morphisms {η_A: F(A) → G(A)} indexed by objects, satisfying the naturality condition:

η_B ∘ F(f) = G(f) ∘ η_A   for all f: A → B

This module provides:

NaturalTransformation (abstract)
└── ComponentwiseNT — defined by a callable producing components

NaturalTransformation

NaturalTransformation(source: Functor, target: Functor)

Bases: ABC

Abstract natural transformation η: F ⇒ G.

Subclasses must implement component to produce η_A for each object A.

PARAMETER DESCRIPTION
source

The source functor F.

TYPE: Functor

target

The target functor G.

TYPE: Functor

Source code in src/quivers/categorical/natural_transformations.py
def __init__(self, source: Functor, target: Functor) -> None:
    self._source = source
    self._target = target

source property

source: Functor

The source functor F.

target property

target: Functor

The target functor G.

component abstractmethod

component(obj: SetObject) -> Morphism

Return the component η_A: F(A) → G(A).

PARAMETER DESCRIPTION
obj

The object A.

TYPE: SetObject

RETURNS DESCRIPTION
Morphism

The morphism η_A: F(A) → G(A).

Source code in src/quivers/categorical/natural_transformations.py
@abstractmethod
def component(self, obj: SetObject) -> Morphism:
    """Return the component η_A: F(A) → G(A).

    Parameters
    ----------
    obj : SetObject
        The object A.

    Returns
    -------
    Morphism
        The morphism η_A: F(A) → G(A).
    """
    ...

verify_naturality

verify_naturality(f: Morphism, atol: float = 1e-05) -> bool

Verify the naturality condition for a given morphism.

Checks that η_B ∘ F(f) ≈ G(f) ∘ η_A for f: A → B.

PARAMETER DESCRIPTION
f

A morphism f: A → B to check naturality against.

TYPE: Morphism

atol

Absolute tolerance for tensor comparison.

TYPE: float DEFAULT: 1e-05

RETURNS DESCRIPTION
bool

True if the naturality square commutes within tolerance.

Source code in src/quivers/categorical/natural_transformations.py
def verify_naturality(self, f: Morphism, atol: float = 1e-5) -> bool:
    """Verify the naturality condition for a given morphism.

    Checks that η_B ∘ F(f) ≈ G(f) ∘ η_A for f: A → B.

    Parameters
    ----------
    f : Morphism
        A morphism f: A → B to check naturality against.
    atol : float
        Absolute tolerance for tensor comparison.

    Returns
    -------
    bool
        True if the naturality square commutes within tolerance.
    """
    a = f.domain
    b = f.codomain

    eta_a = self.component(a)
    eta_b = self.component(b)

    # left path: η_B ∘ F(f)
    f_mapped = self._source.map_morphism(f)
    left = (f_mapped >> eta_b).tensor

    # right path: G(f) ∘ η_A
    g_mapped = self._target.map_morphism(f)
    right = (eta_a >> g_mapped).tensor

    return torch.allclose(left, right, atol=atol)

ComponentwiseNT

ComponentwiseNT(source: Functor, target: Functor, component_fn: Callable[[SetObject], Morphism])

Bases: NaturalTransformation

A natural transformation defined by a callable.

Wraps a function obj ↦ morphism into a NaturalTransformation. Useful for constructing natural transformations from explicit component definitions.

PARAMETER DESCRIPTION
source

The source functor F.

TYPE: Functor

target

The target functor G.

TYPE: Functor

component_fn

A function that, given an object A, returns η_A: F(A) → G(A).

TYPE: Callable[[SetObject], Morphism]

Source code in src/quivers/categorical/natural_transformations.py
def __init__(
    self,
    source: Functor,
    target: Functor,
    component_fn: Callable[[SetObject], Morphism],
) -> None:
    super().__init__(source, target)
    self._component_fn = component_fn

component

component(obj: SetObject) -> Morphism

Return η_A by calling the stored function.

Source code in src/quivers/categorical/natural_transformations.py
def component(self, obj: SetObject) -> Morphism:
    """Return η_A by calling the stored function."""
    return self._component_fn(obj)

Cited sources

Saunders Mac Lane. Categories for the Working Mathematician. Volume 5 of Graduate Texts in Mathematics. Springer, 2 edition, 1998. URL: https://link.springer.com/book/10.1007/978-1-4757-4721-8, doi:10.1007/978-1-4757-4721-8.