Skip to content

Rational algebra

RationalAlgebra is the field of rational numbers as a member of the algebra hierarchy, computed exactly on rational tensors.

rationals

The field of rational numbers, computed exactly.

RationalAlgebra is the semiring (ℚ, +, ·, 0, 1) of the algebra hierarchy whose elements are exact: every operation takes and returns RationalTensor values, whose sums, products, differences, and quotients carry no rounding. A finite model composed in it (a chain of stochastic matrices, the sum over the worlds of a finite enumeration) yields the exact rational its float counterpart only approximates, which is what makes an expected value statable exactly in a test.

An operand that is not already rational enters by the reading every exact computation uses (quivers.core.exact): integer and truth entries exactly, and a floating entry as the shortest decimal that rounds to it, so the float 0.1 is the rational 1/10.

RATIONAL module-attribute

RATIONAL = RationalAlgebra()

The RationalAlgebra singleton.

RationalAlgebra

Bases: RealAlgebra

The field of rational numbers (ℚ, +, ·), computed exactly.

It is the subfield of quivers.core.algebras.RealAlgebra whose elements are exact: the operations are the real ones, applied to rational tensors, so a sum or product of rationals is its exact value and a quotient by a nonzero rational is too. Every operand is read by as_rational first.

name property

name: str

"Rational".

RETURNS DESCRIPTION
str

The name.

tensor_op

tensor_op(a: Tensor, b: Tensor) -> Tensor

The exact product a b.

PARAMETER DESCRIPTION
a

Left operand.

TYPE: Tensor

b

Right operand.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

The product, a rational tensor.

Source code in src/quivers/core/rationals.py
def tensor_op(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    """The exact product ``a b``.

    Parameters
    ----------
    a : torch.Tensor
        Left operand.
    b : torch.Tensor
        Right operand.

    Returns
    -------
    torch.Tensor
        The product, a rational tensor.
    """
    return as_rational(a) * as_rational(b)

join

join(t: Tensor, dim: int | tuple[int, ...]) -> Tensor

The exact sum over axes.

PARAMETER DESCRIPTION
t

Operands.

TYPE: Tensor

dim

Axes to reduce.

TYPE: int or tuple of int

RETURNS DESCRIPTION
Tensor

The sum, which is 0 over no terms.

Source code in src/quivers/core/rationals.py
def join(self, t: torch.Tensor, dim: int | tuple[int, ...]) -> torch.Tensor:
    """The exact sum over axes.

    Parameters
    ----------
    t : torch.Tensor
        Operands.
    dim : int or tuple of int
        Axes to reduce.

    Returns
    -------
    torch.Tensor
        The sum, which is 0 over no terms.
    """
    return super().join(as_rational(t), dim)

join_pair

join_pair(a: Tensor, b: Tensor) -> Tensor

The exact sum a + b.

PARAMETER DESCRIPTION
a

Left operand.

TYPE: Tensor

b

Right operand.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

The sum, a rational tensor.

Source code in src/quivers/core/rationals.py
def join_pair(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    """The exact sum ``a + b``.

    Parameters
    ----------
    a : torch.Tensor
        Left operand.
    b : torch.Tensor
        Right operand.

    Returns
    -------
    torch.Tensor
        The sum, a rational tensor.
    """
    return as_rational(a) + as_rational(b)

meet

meet(t: Tensor, dim: int | tuple[int, ...]) -> Tensor

The exact minimum, refused over no terms.

PARAMETER DESCRIPTION
t

Operands.

TYPE: Tensor

dim

Axes to reduce.

TYPE: int or tuple of int

RETURNS DESCRIPTION
Tensor

The minimum.

RAISES DESCRIPTION
ValueError

If a reduced axis has size zero.

Source code in src/quivers/core/rationals.py
def meet(self, t: torch.Tensor, dim: int | tuple[int, ...]) -> torch.Tensor:
    """The exact minimum, refused over no terms.

    Parameters
    ----------
    t : torch.Tensor
        Operands.
    dim : int or tuple of int
        Axes to reduce.

    Returns
    -------
    torch.Tensor
        The minimum.

    Raises
    ------
    ValueError
        If a reduced axis has size zero.
    """
    return super().meet(as_rational(t), dim)

negate

negate(t: Tensor) -> Tensor

The additive inverse -t.

PARAMETER DESCRIPTION
t

Operands.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

-t, a rational tensor.

Source code in src/quivers/core/rationals.py
def negate(self, t: torch.Tensor) -> torch.Tensor:
    """The additive inverse ``-t``.

    Parameters
    ----------
    t : torch.Tensor
        Operands.

    Returns
    -------
    torch.Tensor
        ``-t``, a rational tensor.
    """
    return -as_rational(t)

subtract

subtract(a: Tensor, b: Tensor) -> Tensor

The exact difference a - b.

PARAMETER DESCRIPTION
a

The minuend.

TYPE: Tensor

b

The subtrahend.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

a - b, a rational tensor.

Source code in src/quivers/core/rationals.py
def subtract(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    """The exact difference ``a - b``.

    Parameters
    ----------
    a : torch.Tensor
        The minuend.
    b : torch.Tensor
        The subtrahend.

    Returns
    -------
    torch.Tensor
        ``a - b``, a rational tensor.
    """
    return as_rational(a) - as_rational(b)

divide

divide(a: Tensor, b: Tensor) -> Tensor

The exact quotient a / b.

PARAMETER DESCRIPTION
a

The dividend.

TYPE: Tensor

b

The divisor, with no zero element.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

a / b, a rational tensor.

RAISES DESCRIPTION
ZeroDivisionError

If an element of b is 0.

Source code in src/quivers/core/rationals.py
def divide(self, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor:
    """The exact quotient ``a / b``.

    Parameters
    ----------
    a : torch.Tensor
        The dividend.
    b : torch.Tensor
        The divisor, with no zero element.

    Returns
    -------
    torch.Tensor
        ``a / b``, a rational tensor.

    Raises
    ------
    ZeroDivisionError
        If an element of ``b`` is 0.
    """
    divisor = as_rational(b)
    self.require_nonzero(divisor)
    return as_rational(a) / divisor

contains

contains(t: Tensor) -> Tensor

Membership in ℚ.

PARAMETER DESCRIPTION
t

Entries.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

Whether each entry is a rational number: every entry of a rational, integer, or truth tensor, and every finite entry of a floating one.

Source code in src/quivers/core/rationals.py
def contains(self, t: torch.Tensor) -> torch.Tensor:
    """Membership in ``ℚ``.

    Parameters
    ----------
    t : torch.Tensor
        Entries.

    Returns
    -------
    torch.Tensor
        Whether each entry is a rational number: every entry of a
        rational, integer, or truth tensor, and every finite entry of a
        floating one.
    """
    if isinstance(t, RationalTensor):
        return torch.ones(tuple(t.shape), dtype=torch.bool)
    if t.is_floating_point():
        return torch.isfinite(t)
    return torch.ones(tuple(t.shape), dtype=torch.bool)

as_rational

as_rational(t: Tensor) -> RationalTensor

A tensor's entries as exact rationals.

PARAMETER DESCRIPTION
t

A rational, integer, truth, or floating tensor.

TYPE: Tensor

RETURNS DESCRIPTION
RationalTensor

The same numbers, a floating entry read as its shortest decimal.

RAISES DESCRIPTION
InexactOperation

If a floating entry is infinite or not a number, which no rational is.

Source code in src/quivers/core/rationals.py
def as_rational(t: torch.Tensor) -> RationalTensor:
    """A tensor's entries as exact rationals.

    Parameters
    ----------
    t : torch.Tensor
        A rational, integer, truth, or floating tensor.

    Returns
    -------
    RationalTensor
        The same numbers, a floating entry read as its shortest decimal.

    Raises
    ------
    quivers.core.exact.InexactOperation
        If a floating entry is infinite or not a number, which no rational
        is.
    """
    if isinstance(t, RationalTensor):
        return t
    if t.is_floating_point():
        import numpy as np

        host = t.detach().cpu().to(torch.float64)
        entries = np.empty(host.numel(), dtype=object)
        entries[:] = [exact_scalar(float(item)) for item in host.reshape(-1).tolist()]
        return RationalTensor(entries.reshape(tuple(host.shape)))
    return RationalTensor(entries_of(t))