Linear automata over rings
The discrete Laplacian as a linear automaton on a ring, over the field of modulus elements.
- Name:
linear_ring_automaton - Field:
computer_science/automata - Formalism: lattice dynamics
- References: Ceccherini-Silberstein and Coornaert 2023
Factory
@register_model(
field='computer_science/automata',
tags=('group', 'cellular automaton', 'linear', 'Laplacian'),
references=('Ceccherini-Silberstein and Coornaert 2023',),
defaults={'until': 40, 'dt': 1},
observables=_mean_observables,
)
def linear_ring_automaton(
size: int = 101,
*,
modulus: int = 3,
) -> System:
"""Return the discrete Laplacian as a linear automaton on a ring, over the field of `modulus` elements.
The ring is the cyclic group of order `size`, a periodic lattice, and every cell becomes
`x(g - 1) + x(g + 1) - 2 x(g)` modulo the field size, the element `a^-1 + a - 2` of the group ring. From a single
1 in the middle the values spread as binomial coefficients reduced modulo the field size, a self-similar triangle.
Args:
size:
The order of the cyclic group, the number of cells of the ring.
modulus:
The prime size of the field.
Returns:
system:
The model, with the cells `cells.value`.
Examples:
>>> from skcomplex.models.computer_science.automata import linear_ring_automaton
>>> linear_ring_automaton(11).elements[0].check().prepare().size_
11
"""
values = np.zeros(size)
values[size // 2] = 1.0
return System(
elements=[
LatticeElement(
'cells',
element=LaplacianCellElement,
space=LatticeSpace(size),
values={'value': values, 'modulus': modulus},
),
],
interactions=[
LocalRuleInteraction(
'laplacian',
target='cells',
state='value',
rule=_laplacian,
neighborhood=BoxNeighborhood(),
),
],
)
Components
class LaplacianCellElement(Element):
"""Cell of an automaton over the field of a prime number of elements, such as the discrete Laplacian on a ring.
Args:
label:
The label of the cell or the lattice group.
visual:
How the element is drawn, or `None` for `default_visual_`.
value:
The initial value, an integer from 0 to `modulus - 1`.
modulus:
The prime size of the field.
Examples:
>>> from skcomplex.models.computer_science.automata import LaplacianCellElement
>>> LaplacianCellElement('cell').modulus
3
"""
def __init__(
self,
label: str,
visual: VisualSpec | None = None,
*,
value: Annotated[float, StateSpec(domain=ZZ)] = 0,
modulus: Annotated[float, ParameterSpec(domain=RealSet.open(0, oo))] = 3,
) -> None:
super().__init__(label, visual=visual)
self.value = value
self.modulus = modulus
class LocalRuleInteraction(Interaction):
"""Local rule of a cellular automaton, which sets the next value of every cell from its ordered neighbourhood.
The rule is a table of the next value of every configuration of the neighbourhood, `TableSpec`, a rule of the sum
of the neighbourhood, `TotalisticSpec`, a rule of the cell and the sum of its neighbours, `OuterTotalisticSpec`, an
object whose `table()` returns a `TableSpec`, or a function. A function receives one array per neighbour of every
read state, in neighbourhood order, with one value per cell, the uniform array `random` when the rule is stochastic,
and the parameters of the bound group it names, and returns the next values of the states, one array per state.
Args:
label:
The label of the interaction, unique among the parts of its system.
target:
The path of the group on the cells of a lattice, the nodes of a network, or a Cayley graph.
state:
The name of the state the rule sets, or a tuple of the names of the states it sets together.
rule:
The table, the totalistic rule, or the function.
neighborhood:
The ordered neighbourhood of every cell, such as `VonNeumannNeighborhood(center=True)`.
reads:
The names of the states whose neighbourhoods a function reads, or `None` for the states it sets.
schedule:
`'synchronous'`, `'random-sequential'`, `'random-site'`, `'checkerboard'`, or `'colored'`.
stochastic:
Whether a function receives the uniform random array `random`.
Raises:
ValidationError:
The state or the read states are not names, or the neighbourhood is not a neighbourhood.
Examples:
>>> from skcomplex.core.interactions import LocalRuleInteraction
>>> from skcomplex.core.spaces import VonNeumannNeighborhood
>>> from skcomplex.specs import TotalisticSpec
>>> parity = TotalisticSpec({1: 1, 3: 1})
>>> around = VonNeumannNeighborhood(n_dim=1, center=True)
>>> rule = LocalRuleInteraction('parity', target='cells', state='cell', rule=parity, neighborhood=around)
>>> rule.check().schedule
'synchronous'
"""
def __init__(
self,
label: str,
*,
target: Annotated[str, RoleSpec()],
state: str | tuple[str, ...],
rule: object,
neighborhood: BaseNeighborhood,
reads: Sequence[str] | None = None,
schedule: str = 'synchronous',
stochastic: bool = False,
) -> None:
super().__init__(label)
self.target = target
self.state = state
self.rule = rule
self.neighborhood = neighborhood
self.reads = reads
self.schedule = schedule
self.stochastic = stochastic
def _get_names(self) -> tuple[str, ...]:
"""Return the names of the states the rule sets."""
return (self.state,) if isinstance(self.state, str) else tuple(self.state)
def _check(self) -> None:
"""Raise an error for states that are not names, or a neighbourhood that is not one.
Raises:
ValidationError:
The state or the read states are not names, or the neighbourhood is not a neighbourhood.
"""
super()._check()
names = [*self._get_names(), *(self.reads or ())]
if not names or not all(isinstance(name, str) and name.isidentifier() for name in names):
error_msg = f'The local rule {self.label!r} sets {self.state!r} and reads {self.reads!r}. Give state names.'
raise ValidationError(error_msg)
if not isinstance(self.neighborhood, BaseNeighborhood):
error_msg = f'The neighbourhood of the local rule {self.label!r} is {self.neighborhood!r}. Give one.'
raise ValidationError(error_msg)
def _get_laws(self, namespace: SimpleNamespace) -> list[BaseLaw]:
"""Return the local rule law of the states of the target.
Args:
namespace:
The namespace of the symbols the laws can use, with `namespace.target`.
Returns:
laws:
The local rule law.
"""
targets = tuple(getattr(self._get_member(namespace, 'target'), name) for name in self._get_names())
reads = (
None
if self.reads is None
else tuple(getattr(self._get_member(namespace, 'target'), name) for name in self.reads)
)
return [
LocalRuleLaw(
targets[0] if isinstance(self.state, str) else targets,
self.neighborhood,
self.rule,
reads=reads,
schedule=self.schedule,
stochastic=self.stochastic,
label=self.label,
),
]