Skip to content

Adding machine (odometer)

The adding machine r → r + 1 on the q-adic integers.

  • Class: AddingMachineSystem
  • Import: from skcomplex.systems.computer_science.automata import AddingMachineSystem
  • Field: computer_science/automata
  • Topics: odometer, q-adic integers, self-similar group, tree automorphism
  • References:
    • Ceccherini-Silberstein and Coornaert 2023

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.computer_science.automata import AddingMachineSystem

trajectory = AddingMachineSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
digits 6
base 2
initial None
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space lattice
Laws update
Randomness unknown
Domains integer
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class AddingMachineSystem(BaseSystem):
    """The adding machine `r → r + 1` on the q-adic integers.

    It is the odometer acting on the q-ary tree.

    The cells hold the digits of `r`, the lowest first, the first `digits` letters of its infinite word, a vertex of the
    q-ary tree. Every step adds 1: the lowest digit turns by the cycle `0 → 1 → ... → q - 1 → 0`, and a digit turns when
    every lower digit is `q - 1`, the carry (Ceccherini-Silberstein and Coornaert 2023, Exercise 6.59). The machine is
    an automorphism of the tree of infinite order whose orbits are dense, and on the first `digits` levels it visits all
    `q^digits` vertices in turn. The lowest digit reads `q - 1` beyond the segment, the carry of `+1`. The system has
    the digits `cells.digit`.

    Args:
        digits:
            The number of digits kept, the depth of the tree.

        base:
            The base `q`, at least 2.

        initial:
            The initial digits, the lowest first, or `None` for 0.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Raises:
        ParameterError:
            The base is below 2.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import AddingMachineSystem
        >>> AddingMachineSystem(3).prepare().simulate(until=5).get_values('cells.digit')[-1].tolist()
        [1.0, 0.0, 1.0]
    """

    field = 'computer_science/automata'
    topics = ('odometer', 'q-adic integers', 'self-similar group', 'tree automorphism')
    references = ('Ceccherini-Silberstein and Coornaert 2023',)
    until = 64
    dt = 1

    def __init__(
        self,
        digits: int = 6,
        *,
        base: int = 2,
        initial: npt.ArrayLike | None = None,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.digits = digits
        self.base = base
        self.initial = initial

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the digits and the addition of 1."""
        super()._prepare(context)
        if self.base < MIN_BASE:
            error_msg = f'The base of the adding machine is at least 2, but it is {self.base}.'
            raise ParameterError(error_msg)
        values = np.zeros(self.digits) if self.initial is None else np.asarray(self.initial, dtype=float)
        space = LatticeSpace(self.digits, boundary='fixed', fill=self.base - 1)
        self.system_ = System(
            self.label,
            elements=[DigitCellElement('cells', digit=values, space=space)],
            interactions=[CarryInteraction('carry', target='cells', base=self.base, digits=self.digits)],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • CarryInteraction, from Interaction
class DigitCellElement(Element):
    """Cell holding a digit of a q-adic integer, from 0 to `base - 1`.

    The digit is the label of a vertex of the q-ary tree. With `space`, the element holds one cell on every site.

    Args:
        label:
            The label of the cells.

        digit:
            The initial digit, or one digit per cell.

        space:
            The space with one cell on every site, such as a lattice, or `None` for one cell.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import DigitCellElement
        >>> DigitCellElement('cell', digit=1).digit
        1
    """

    def __init__(
        self,
        label: str,
        *,
        digit: Annotated[float | Array, ZZ] = 0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.digit = digit

class CarryInteraction(Interaction):
    """Addition of 1 to the digits of a q-adic integer, the lowest first.

    A digit turns by the cycle `0 → 1 → ... → q - 1 → 0` when every lower digit is `q - 1`, the carry, and keeps its
    value otherwise. The lowest digit reads `q - 1` beyond the digits, so it turns at every step.

    Args:
        label:
            The label of the interaction.

        target:
            The cells of the digits, whose state `digit` the addition changes.

        base:
            The base `q`.

        digits:
            The number of digits.

    Examples:
        >>> from skcomplex.systems.computer_science.automata import CarryInteraction
        >>> CarryInteraction('carry', target='cells', base=3).base
        3
    """

    def __init__(self, label: str, *, target: Role = 'cells', base: int = 2, digits: int = 6) -> None:
        super().__init__(label)
        self.target = target
        self.base = base
        self.digits = digits

    def _get_function(self) -> Callable[..., Array]:
        """Return the digit plus 1 modulo the base when every lower digit is `base - 1`, and the digit otherwise."""
        base = self.base

        def carry(*digits: Array, **kwargs: object) -> Array:
            window = np.stack(digits)
            carried = np.all(window[:-1] == base - 1, axis=0)
            return np.where(carried, (window[-1] + 1) % base, window[-1])

        return carry

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the carry, which reads every lower digit, as the law.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        lower = LatticeNeighborhood(tuple((offset,) for offset in range(1 - self.digits, 1)))
        digit = context.target.digit
        self.laws_: list[BaseLaw] = [
            UpdateLaw(digit, self._get_function(), neighborhood=lower, label=self.label),
        ]