Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" Common Automata and Transducers (Finite State Machines Generators)
Automata and Transducers in Sage can be built through the :class:`automata <AutomatonGenerators>` and :class:`transducers <TransducerGenerators>` objects, respectively. It contains generators for common finite state machines. For example,
::
sage: I = transducers.Identity([0, 1, 2])
generates an identity transducer on the alphabet `\{0, 1, 2\}`.
To construct automata and transducers manually, you can use the classes :class:`Automaton` and :class:`Transducer`, respectively. See :doc:`finite_state_machine` for more details and a lot of :ref:`examples <finite_state_machine_examples>`.
**Automata**
.. csv-table:: :class: contentstable :widths: 30, 70 :delim: |
:meth:`~AutomatonGenerators.AnyLetter` | Return an automaton recognizing any letter. :meth:`~AutomatonGenerators.AnyWord` | Return an automaton recognizing any word. :meth:`~AutomatonGenerators.EmptyWord` | Return an automaton recognizing the empty word. :meth:`~AutomatonGenerators.Word` | Return an automaton recognizing the given word. :meth:`~AutomatonGenerators.ContainsWord` | Return an automaton recognizing words containing the given word.
**Transducers**
.. csv-table:: :class: contentstable :widths: 30, 70 :delim: |
:meth:`~TransducerGenerators.Identity` | Returns a transducer realizing the identity map. :meth:`~TransducerGenerators.abs` | Returns a transducer realizing absolute value. :meth:`~TransducerGenerators.map` | Returns a transducer realizing a function. :meth:`~TransducerGenerators.operator` | Returns a transducer realizing a binary operation. :meth:`~TransducerGenerators.all` | Returns a transducer realizing logical ``and``. :meth:`~TransducerGenerators.any` | Returns a transducer realizing logical ``or``. :meth:`~TransducerGenerators.add` | Returns a transducer realizing addition. :meth:`~TransducerGenerators.sub` | Returns a transducer realizing subtraction. :meth:`~TransducerGenerators.CountSubblockOccurrences` | Returns a transducer counting the occurrences of a subblock. :meth:`~TransducerGenerators.Wait` | Returns a transducer writing ``False`` until first (or k-th) true input is read. :meth:`~TransducerGenerators.weight` | Returns a transducer realizing the Hamming weight. :meth:`~TransducerGenerators.GrayCode` | Returns a transducer realizing binary Gray code. :meth:`~TransducerGenerators.Recursion` | Returns a transducer defined by recursions.
AUTHORS:
- Clemens Heuberger (2014-04-07): initial version - Sara Kropf (2014-04-10): some changes in TransducerGenerator - Daniel Krenn (2014-04-15): improved common docstring during review - Clemens Heuberger, Daniel Krenn, Sara Kropf (2014-04-16--2014-05-02): A couple of improvements. Details see :trac:`16141`, :trac:`16142`, :trac:`16143`, :trac:`16186`. - Sara Kropf (2014-04-29): weight transducer - Clemens Heuberger, Daniel Krenn (2014-07-18): transducers Wait, all, any - Clemens Heuberger (2014-08-10): transducer Recursion - Clemens Heuberger (2015-07-31): automaton word - Daniel Krenn (2015-09-14): cleanup :trac:`18227`
ACKNOWLEDGEMENT:
- Clemens Heuberger, Daniel Krenn and Sara Kropf are supported by the Austrian Science Fund (FWF): P 24644-N26.
Functions and methods ---------------------
""" #***************************************************************************** # Copyright (C) 2014--2015 Clemens Heuberger <clemens.heuberger@aau.at> # 2014--2015 Daniel Krenn <dev@danielkrenn.at> # 2014 Sara Kropf <sara.kropf@aau.at> # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
r""" A collection of constructors for several common automata.
A list of all automata in this database is available via tab completion. Type "``automata.``" and then hit tab to see which automata are available.
The automata currently in this class include:
- :meth:`~AnyLetter` - :meth:`~AnyWord` - :meth:`~EmptyWord` - :meth:`~Word` - :meth:`~ContainsWord` """
r""" Return an automaton recognizing any letter of the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list, the input alphabet
OUTPUT:
An :class:`~Automaton`.
EXAMPLES::
sage: A = automata.AnyLetter([0, 1]) sage: A([]) False sage: A([0]) True sage: A([1]) True sage: A([0, 0]) False
.. SEEALSO::
:meth:`AnyWord` """ initial_states=[z], final_states=[o])
r""" Return an automaton recognizing any word of the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list, the input alphabet
OUTPUT:
An :class:`~Automaton`.
EXAMPLES::
sage: A = automata.AnyWord([0, 1]) sage: A([0]) True sage: A([1]) True sage: A([0, 1]) True sage: A([0, 2]) False
This is equivalent to taking the :meth:`~FiniteStateMachine.kleene_star` of :meth:`AnyLetter` and minimizing the result. This method immediately gives a minimized version::
sage: B = automata.AnyLetter([0, 1]).kleene_star().minimization().relabeled() sage: B == A True
.. SEEALSO::
:meth:`AnyLetter`, :meth:`Word`. """ initial_states=[z], final_states=[z])
r""" Return an automaton recognizing the empty word.
INPUT:
- ``input_alphabet`` -- (default: ``None``) an iterable or ``None``.
OUTPUT:
An :class:`~Automaton`.
EXAMPLES::
sage: A = automata.EmptyWord() sage: A([]) True sage: A([0]) False
.. SEEALSO::
:meth:`AnyLetter`, :meth:`AnyWord`. """ final_states=[z], input_alphabet=input_alphabet)
r""" Return an automaton recognizing the given word.
INPUT:
- ``word`` -- an iterable.
- ``input_alphabet`` -- a list or ``None``. If ``None``, then the letters occurring in the word are used.
OUTPUT:
An :class:`~Automaton`.
EXAMPLES::
sage: A = automata.Word([0]) sage: A.transitions() [Transition from 0 to 1: 0|-] sage: [A(w) for w in ([], [0], [1])] [False, True, False] sage: A = automata.Word([0, 1, 0]) sage: A.transitions() [Transition from 0 to 1: 0|-, Transition from 1 to 2: 1|-, Transition from 2 to 3: 0|-] sage: [A(w) for w in ([], [0], [0, 1], [0, 1, 1], [0, 1, 0])] [False, False, False, False, True]
If the input alphabet is not given, it is derived from the given word. ::
sage: A.input_alphabet [0, 1] sage: A = automata.Word([0, 1, 0], input_alphabet=[0, 1, 2]) sage: A.input_alphabet [0, 1, 2]
.. SEEALSO::
:meth:`AnyWord`, :meth:`ContainsWord`.
TESTS::
sage: from sage.rings.integer import is_Integer sage: all(is_Integer(s.label()) for s in A.states()) True """ for i, letter in enumerate(letters)], initial_states=[ZZ(0)], final_states=[ZZ(length)], input_alphabet=input_alphabet)
r""" Return an automaton recognizing the words containing the given word as a factor.
INPUT:
- ``word`` -- a list (or other iterable) of letters, the word we are looking for.
- ``input_alphabet`` -- a list or other iterable, the input alphabet.
OUTPUT:
An :class:`~Automaton`.
EXAMPLES::
sage: A = automata.ContainsWord([0, 1, 0, 1, 1], ....: input_alphabet=[0, 1]) sage: A([1, 0, 1, 0, 1, 0, 1, 1, 0, 0]) True sage: A([1, 0, 1, 0, 1, 0, 1, 0]) False
This is equivalent to taking the concatenation of :meth:`AnyWord`, :meth:`Word` and :meth:`AnyWord` and minimizing the result. This method immediately gives a minimized version::
sage: B = (automata.AnyWord([0, 1]) * ....: automata.Word([0, 1, 0, 1, 1], [0, 1]) * ....: automata.AnyWord([0, 1])).minimization() sage: B.is_equivalent(A) True
.. SEEALSO::
:meth:`~TransducerGenerators.CountSubblockOccurrences`, :meth:`AnyWord`, :meth:`Word`. """
and what[:len(pattern)] == pattern
transition_function, input_alphabet=input_alphabet, initial_states=[()], final_states=[word])
r""" A collection of constructors for several common transducers.
A list of all transducers in this database is available via tab completion. Type "``transducers.``" and then hit tab to see which transducers are available.
The transducers currently in this class include:
- :meth:`~Identity` - :meth:`~abs` - :meth:`~TransducerGenerators.operator` - :meth:`~all` - :meth:`~any` - :meth:`~add` - :meth:`~sub` - :meth:`~CountSubblockOccurrences` - :meth:`~Wait` - :meth:`~GrayCode` - :meth:`~Recursion` """
""" Returns the identity transducer realizing the identity map.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
OUTPUT:
A transducer mapping each word over ``input_alphabet`` to itself.
EXAMPLES::
sage: T = transducers.Identity([0, 1]) sage: sorted(T.transitions()) [Transition from 0 to 0: 0|0, Transition from 0 to 0: 1|1] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T.input_alphabet [0, 1] sage: T.output_alphabet [0, 1] sage: T([0, 1, 0, 1, 1]) [0, 1, 0, 1, 1]
""" [(0, 0, d, d) for d in input_alphabet], input_alphabet=input_alphabet, output_alphabet=input_alphabet, initial_states=[0], final_states=[0])
""" Returns a transducer counting the number of (possibly overlapping) occurrences of a block in the input.
INPUT:
- ``block`` -- a list (or other iterable) of letters.
- ``input_alphabet`` -- a list or other iterable.
OUTPUT:
A transducer counting (in unary) the number of occurrences of the given block in the input. Overlapping occurrences are counted several times.
Denoting the block by `b_0\ldots b_{k-1}`, the input word by `i_0\ldots i_L` and the output word by `o_0\ldots o_L`, we have `o_j = 1` if and only if `i_{j-k+1}\ldots i_{j} = b_0\ldots b_{k-1}`. Otherwise, `o_j = 0`.
EXAMPLES:
#. Counting the number of ``10`` blocks over the alphabet ``[0, 1]``::
sage: T = transducers.CountSubblockOccurrences( ....: [1, 0], ....: [0, 1]) sage: sorted(T.transitions()) [Transition from () to (): 0|0, Transition from () to (1,): 1|0, Transition from (1,) to (): 0|1, Transition from (1,) to (1,): 1|0] sage: T.input_alphabet [0, 1] sage: T.output_alphabet [0, 1] sage: T.initial_states() [()] sage: T.final_states() [(), (1,)]
Check some sequence::
sage: T([0, 1, 0, 1, 1, 0]) [0, 0, 1, 0, 0, 1]
#. Counting the number of ``11`` blocks over the alphabet ``[0, 1]``::
sage: T = transducers.CountSubblockOccurrences( ....: [1, 1], ....: [0, 1]) sage: sorted(T.transitions()) [Transition from () to (): 0|0, Transition from () to (1,): 1|0, Transition from (1,) to (): 0|0, Transition from (1,) to (1,): 1|1]
Check some sequence::
sage: T([0, 1, 0, 1, 1, 0]) [0, 0, 0, 0, 1, 0]
#. Counting the number of ``1010`` blocks over the alphabet ``[0, 1, 2]``::
sage: T = transducers.CountSubblockOccurrences( ....: [1, 0, 1, 0], ....: [0, 1, 2]) sage: sorted(T.transitions()) [Transition from () to (): 0|0, Transition from () to (1,): 1|0, Transition from () to (): 2|0, Transition from (1,) to (1, 0): 0|0, Transition from (1,) to (1,): 1|0, Transition from (1,) to (): 2|0, Transition from (1, 0) to (): 0|0, Transition from (1, 0) to (1, 0, 1): 1|0, Transition from (1, 0) to (): 2|0, Transition from (1, 0, 1) to (1, 0): 0|1, Transition from (1, 0, 1) to (1,): 1|0, Transition from (1, 0, 1) to (): 2|0] sage: input = [0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 2] sage: output = [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0] sage: T(input) == output True
.. SEEALSO::
:meth:`~AutomatonGenerators.ContainsWord` """
and what[:len(pattern)] == pattern
and len(block_as_tuple) > len(current): else:
transition_function, input_alphabet=input_alphabet, output_alphabet=[0, 1], initial_states=[()])
r""" Writes ``False`` until reading the ``threshold``-th occurrence of a true input letter; then writes ``True``.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
- ``threshold`` -- a positive integer specifying how many occurrences of ``True`` inputs are waited for.
OUTPUT:
A transducer writing ``False`` until the ``threshold``-th true (Python's standard conversion to boolean is used to convert the actual input to boolean) input is read. Subsequently, the transducer writes ``True``.
EXAMPLES::
sage: T = transducers.Wait([0, 1]) sage: T([0, 0, 1, 0, 1, 0]) [False, False, True, True, True, True] sage: T2 = transducers.Wait([0, 1], threshold=2) sage: T2([0, 0, 1, 0, 1, 0]) [False, False, False, False, True, True] """
input_alphabet=input_alphabet, initial_states=[0])
r""" Return a transducer which realizes a function on the alphabet.
INPUT:
- ``f`` -- function to realize.
- ``input_alphabet`` -- a list or other iterable.
OUTPUT:
A transducer mapping an input letter `x` to `f(x)`.
EXAMPLES:
The following binary transducer realizes component-wise absolute value (this transducer is also available as :meth:`.abs`)::
sage: T = transducers.map(abs, [-1, 0, 1]) sage: T.transitions() [Transition from 0 to 0: -1|1, Transition from 0 to 0: 0|0, Transition from 0 to 0: 1|1] sage: T.input_alphabet [-1, 0, 1] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([-1, 1, 0, 1]) [1, 1, 0, 1]
.. SEEALSO::
:meth:`Automaton.with_output() <sage.combinat.finite_state_machine.Automaton.with_output>`. """ input_alphabet=input_alphabet, initial_states=[0], final_states=[0])
r""" Returns a transducer which realizes an operation on tuples over the given input alphabet.
INPUT:
- ``operator`` -- operator to realize. It is a function which takes ``number_of_operands`` input arguments (each out of ``input_alphabet``).
- ``input_alphabet`` -- a list or other iterable.
- ``number_of_operands`` -- (default: `2`) it specifies the number of input arguments the operator takes.
OUTPUT:
A transducer mapping an input letter `(i_1, \dots, i_n)` to `\mathrm{operator}(i_1, \dots, i_n)`. Here, `n` equals ``number_of_operands``.
The input alphabet of the generated transducer is the Cartesian product of ``number_of_operands`` copies of ``input_alphabet``.
EXAMPLES:
The following binary transducer realizes component-wise addition (this transducer is also available as :meth:`.add`)::
sage: import operator sage: T = transducers.operator(operator.add, [0, 1]) sage: T.transitions() [Transition from 0 to 0: (0, 0)|0, Transition from 0 to 0: (0, 1)|1, Transition from 0 to 0: (1, 0)|1, Transition from 0 to 0: (1, 1)|2] sage: T.input_alphabet [(0, 0), (0, 1), (1, 0), (1, 1)] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([(0, 0), (0, 1), (1, 0), (1, 1)]) [0, 1, 1, 2]
Note that for a unary operator the input letters of the new transducer are tuples of length `1`::
sage: T = transducers.operator(abs, ....: [-1, 0, 1], ....: number_of_operands=1) sage: T([-1, 1, 0]) Traceback (most recent call last): ... ValueError: Invalid input sequence. sage: T([(-1,), (1,), (0,)]) [1, 1, 0]
Compare this with the transducer generated by :meth:`.map`::
sage: T = transducers.map(abs, ....: [-1, 0, 1]) sage: T([-1, 1, 0]) [1, 1, 0]
In fact, this transducer is also available as :meth:`.abs`::
sage: T = transducers.abs([-1, 0, 1]) sage: T([-1, 1, 0]) [1, 1, 0] """
input_alphabet=pairs, initial_states=[0], final_states=[0])
""" Returns a transducer which realizes logical ``and`` over the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
- ``number_of_operands`` -- (default: `2`) specifies the number of input arguments for the ``and`` operation.
OUTPUT:
A transducer mapping an input word `(i_{01}, \ldots, i_{0d})\ldots (i_{k1}, \ldots, i_{kd})` to the word `(i_{01} \land \cdots \land i_{0d})\ldots (i_{k1} \land \cdots \land i_{kd})`.
The input alphabet of the generated transducer is the Cartesian product of ``number_of_operands`` copies of ``input_alphabet``.
EXAMPLES:
The following transducer realizes letter-wise logical ``and``::
sage: T = transducers.all([False, True]) sage: T.transitions() [Transition from 0 to 0: (False, False)|False, Transition from 0 to 0: (False, True)|False, Transition from 0 to 0: (True, False)|False, Transition from 0 to 0: (True, True)|True] sage: T.input_alphabet [(False, False), (False, True), (True, False), (True, True)] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([(False, False), (False, True), (True, False), (True, True)]) [False, False, False, True]
More than two operands and other input alphabets (with conversion to boolean) are also possible::
sage: T3 = transducers.all([0, 1], number_of_operands=3) sage: T3([(0, 0, 0), (1, 0, 0), (1, 1, 1)]) [False, False, True] """ input_alphabet, number_of_operands)
""" Returns a transducer which realizes logical ``or`` over the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
- ``number_of_operands`` -- (default: `2`) specifies the number of input arguments for the ``or`` operation.
OUTPUT:
A transducer mapping an input word `(i_{01}, \ldots, i_{0d})\ldots (i_{k1}, \ldots, i_{kd})` to the word `(i_{01} \lor \cdots \lor i_{0d})\ldots (i_{k1} \lor \cdots \lor i_{kd})`.
The input alphabet of the generated transducer is the Cartesian product of ``number_of_operands`` copies of ``input_alphabet``.
EXAMPLES:
The following transducer realizes letter-wise logical ``or``::
sage: T = transducers.any([False, True]) sage: T.transitions() [Transition from 0 to 0: (False, False)|False, Transition from 0 to 0: (False, True)|True, Transition from 0 to 0: (True, False)|True, Transition from 0 to 0: (True, True)|True] sage: T.input_alphabet [(False, False), (False, True), (True, False), (True, True)] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([(False, False), (False, True), (True, False), (True, True)]) [False, True, True, True]
More than two operands and other input alphabets (with conversion to boolean) are also possible::
sage: T3 = transducers.any([0, 1], number_of_operands=3) sage: T3([(0, 0, 0), (1, 0, 0), (1, 1, 1)]) [False, True, True] """ input_alphabet, number_of_operands)
""" Returns a transducer which realizes addition on pairs over the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
- ``number_of_operands`` -- (default: `2`) it specifies the number of input arguments the operator takes.
OUTPUT:
A transducer mapping an input word `(i_{01}, \ldots, i_{0d})\ldots (i_{k1}, \ldots, i_{kd})` to the word `(i_{01} + \cdots + i_{0d})\ldots (i_{k1} + \cdots + i_{kd})`.
The input alphabet of the generated transducer is the Cartesian product of ``number_of_operands`` copies of ``input_alphabet``.
EXAMPLES:
The following transducer realizes letter-wise addition::
sage: T = transducers.add([0, 1]) sage: T.transitions() [Transition from 0 to 0: (0, 0)|0, Transition from 0 to 0: (0, 1)|1, Transition from 0 to 0: (1, 0)|1, Transition from 0 to 0: (1, 1)|2] sage: T.input_alphabet [(0, 0), (0, 1), (1, 0), (1, 1)] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([(0, 0), (0, 1), (1, 0), (1, 1)]) [0, 1, 1, 2]
More than two operands can also be handled::
sage: T3 = transducers.add([0, 1], number_of_operands=3) sage: T3.input_alphabet [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] sage: T3([(0, 0, 0), (0, 1, 0), (0, 1, 1), (1, 1, 1)]) [0, 1, 2, 3] """ input_alphabet, number_of_operands=number_of_operands)
""" Returns a transducer which realizes subtraction on pairs over the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
OUTPUT:
A transducer mapping an input word `(i_0, i'_0)\ldots (i_k, i'_k)` to the word `(i_0 - i'_0)\ldots (i_k - i'_k)`.
The input alphabet of the generated transducer is the Cartesian product of two copies of ``input_alphabet``.
EXAMPLES:
The following transducer realizes letter-wise subtraction::
sage: T = transducers.sub([0, 1]) sage: T.transitions() [Transition from 0 to 0: (0, 0)|0, Transition from 0 to 0: (0, 1)|-1, Transition from 0 to 0: (1, 0)|1, Transition from 0 to 0: (1, 1)|0] sage: T.input_alphabet [(0, 0), (0, 1), (1, 0), (1, 1)] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([(0, 0), (0, 1), (1, 0), (1, 1)]) [0, -1, 1, 0] """
r""" Returns a transducer which realizes the Hamming weight of the input over the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
- ``zero`` -- the zero symbol in the alphabet used
OUTPUT:
A transducer mapping `i_0\ldots i_k` to `(i_0\neq 0)\ldots(i_k\neq 0)`.
The Hamming weight is defined as the number of non-zero digits in the input sequence over the alphabet ``input_alphabet`` (see :wikipedia:`Hamming_weight`). The output sequence of the transducer is a unary encoding of the Hamming weight. Thus the sum of the output sequence is the Hamming weight of the input.
EXAMPLES::
sage: W = transducers.weight([-1, 0, 2]) sage: W.transitions() [Transition from 0 to 0: -1|1, Transition from 0 to 0: 0|0, Transition from 0 to 0: 2|1] sage: unary_weight = W([-1, 0, 0, 2, -1]) sage: unary_weight [1, 0, 0, 1, 1] sage: weight = add(unary_weight) sage: weight 3
Also the joint Hamming weight can be computed::
sage: v1 = vector([-1, 0]) sage: v0 = vector([0, 0]) sage: W = transducers.weight([v1, v0]) sage: unary_weight = W([v1, v0, v1, v0]) sage: add(unary_weight) 2
For the input alphabet ``[-1, 0, 1]`` the weight transducer is the same as the absolute value transducer :meth:`~TransducerGenerators.abs`::
sage: W = transducers.weight([-1, 0, 1]) sage: A = transducers.abs([-1, 0, 1]) sage: W == A True
For other input alphabets, we can specify the zero symbol::
sage: W = transducers.weight(['a', 'b'], zero='a') sage: add(W(['a', 'b', 'b'])) 2 """ initial_states=[0], final_states=[0])
""" Returns a transducer which realizes the letter-wise absolute value of an input word over the given input alphabet.
INPUT:
- ``input_alphabet`` -- a list or other iterable.
OUTPUT:
A transducer mapping `i_0\ldots i_k` to `|i_0|\ldots |i_k|`.
EXAMPLES:
The following transducer realizes letter-wise absolute value::
sage: T = transducers.abs([-1, 0, 1]) sage: T.transitions() [Transition from 0 to 0: -1|1, Transition from 0 to 0: 0|0, Transition from 0 to 0: 1|1] sage: T.initial_states() [0] sage: T.final_states() [0] sage: T([-1, -1, 0, 1]) [1, 1, 0, 1]
"""
""" Returns a transducer converting the standard binary expansion to Gray code.
INPUT:
Nothing.
OUTPUT:
A transducer.
Cf. the :wikipedia:`Gray_code` for a description of the Gray code.
EXAMPLES::
sage: G = transducers.GrayCode() sage: G Transducer with 3 states sage: for v in srange(0, 10): ....: print("{} {}".format(v, G(v.digits(base=2)))) 0 [] 1 [1] 2 [1, 1] 3 [0, 1] 4 [0, 1, 1] 5 [1, 1, 1] 6 [1, 0, 1] 7 [0, 0, 1] 8 [0, 0, 1, 1] 9 [1, 0, 1, 1]
In the example :ref:`Gray Code <finite_state_machine_gray_code_example>` in the documentation of the :doc:`finite_state_machine` module, the Gray code transducer is derived from the algorithm converting the binary expansion to the Gray code. The result is the same as the one given here. """ [0, 2, o, None], [1, 1, z, z], [1, 2, o, o], [2, 1, z, o], [2, 2, o, z]], initial_states=[0], final_states=[1], with_final_word_out=[0])
['K', 'r', 'k', 's', 't'])
word_function=None, output_rings=[ZZ, QQ]): """ Parse one equation as admissible in :meth:`~.Recursion`.
INPUT:
- ``equation`` -- An equation of the form
- ``f(base^K * n + r) == f(base^k * n + s) + t`` for some integers ``0 <= k < K``, ``r`` and some ``t``---valid for all ``n`` such that the arguments on both sides are non-negative---
or the form
- ``f(r) == t`` for some integer ``r`` and some ``t``.
- ``base`` -- see :meth:`~Recursion`.
- ``function`` -- see :meth:`~Recursion`.
- ``var`` -- see :meth:`~Recursion`.
- ``output_rings`` -- see :meth:`~Recursion`.
OUTPUT:
A ``RecursionRule`` if the equation is of the first form described above and a dictionary ``{r: [t]}`` otherwise.
EXAMPLES::
sage: var('n') n sage: function('f') f sage: transducers._parse_recursion_equation_( ....: f(8*n + 7) == f(2*n + 3) + 5, ....: 2, f, n) RecursionRule(K=3, r=7, k=1, s=3, t=[5]) sage: transducers._parse_recursion_equation_( ....: f(42) == 5, ....: 2, f, n) {42: [5]}
TESTS:
The following tests check that the equations are well-formed::
sage: transducers._parse_recursion_equation_(f(4*n + 1), 2, f, n) Traceback (most recent call last): ... ValueError: f(4*n + 1) is not an equation with ==.
::
sage: transducers._parse_recursion_equation_(f(n) + 1 == f(2*n), ....: 2, f, n) Traceback (most recent call last): ... ValueError: f(n) + 1 is not an evaluation of f.
::
sage: transducers._parse_recursion_equation_(f(2*n, 5) == 3, ....: 2, f, n) Traceback (most recent call last): ... ValueError: f(2*n, 5) does not have one argument.
::
sage: transducers._parse_recursion_equation_(f(1/n) == f(n) + 3, ....: 2, f, n) Traceback (most recent call last): ....: ValueError: 1/n is not a polynomial in n.
::
sage: transducers._parse_recursion_equation_(f(n^2 + 5) == 3, ....: 2, f, n) Traceback (most recent call last): ... ValueError: n^2 + 5 is not a polynomial of degree 1.
::
sage: transducers._parse_recursion_equation_(f(3*n + 5) == f(n) + 7, ....: 2, f, n) Traceback (most recent call last): ... ValueError: 3 is not a power of 2.
::
sage: transducers._parse_recursion_equation_(f(n + 5) == f(n) + 7, ....: 2, f, n) Traceback (most recent call last): ... ValueError: 1 is less than 2.
::
sage: transducers._parse_recursion_equation_( ....: f(2*n + 1) == f(n + 1) + f(n) + 2, ....: 2, f, n) Traceback (most recent call last): ... ValueError: f(n + 1) + f(n) + 2 does not contain exactly one summand which is an evaluation of f.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == sin(n) + 2, ....: 2, f, n) Traceback (most recent call last): ... ValueError: sin(n) + 2 does not contain exactly one summand which is an evaluation of f.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f(n) + n + 2, ....: 2, f, n) Traceback (most recent call last): ... ValueError: n + 2 contains n.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == sin(n), ....: 2, f, n) Traceback (most recent call last): ... ValueError: sin(n) is not an evaluation of f.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f(n, 2), ....: 2, f, n) Traceback (most recent call last): ... ValueError: f(n, 2) does not have exactly one argument.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f(1/n), ....: 2, f, n) Traceback (most recent call last): ... ValueError: 1/n is not a polynomial in n.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f(n^2 + 5), ....: 2, f, n) Traceback (most recent call last): ... ValueError: n^2 + 5 is not a polynomial of degree 1.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f(3*n + 5), ....: 2, f, n) Traceback (most recent call last): ... ValueError: 3 is not a power of 2.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f((1/2)*n + 5), ....: QQ(2), f, n) Traceback (most recent call last): ... ValueError: 1/2 is less than 1.
::
sage: transducers._parse_recursion_equation_(f(2*n + 1) == f(2*n + 5), ....: 2, f, n) Traceback (most recent call last): ... ValueError: 2 is greater or equal than 2. """
except (ValueError,TypeError): pass
else:
% equation) "%s is not an equation with two operands." % equation
% (left_side, function)) (left_side,))
"in %s." % (left_side.operands()[0], var))
% (polynomial_left,))
except RuntimeError: K = 1 % (base_power_K, base)) % (base_power_K, base))
if o.operator() == function] if o.operator() != function] "%s does not contain exactly one summand which " "is an evaluation of %s." % (right_side, function)) % (t, var)) else:
% (next_function, function)) % (next_function,))
% (next_function.operands()[0], var)) % (polynomial_right,)) % (base_power_k, base)) % (base_power_k,)) % (base_power_k, base_power_K))
function(base**k * var + s) + t "Parsing of %s failed for unknown reasons." % (equation,)
input_alphabet=None, word_function=None, is_zero=None, output_rings=[ZZ, QQ]): r""" Return a transducer realizing the given recursion when reading the digit expansion with base ``base``.
INPUT:
- ``recursions`` -- list or iterable of equations. Each equation has either the form
- ``f(base^K * n + r) == f(base^k * n + s) + t`` for some integers ``0 <= k < K``, ``r`` and some ``t``---valid for all ``n`` such that the arguments on both sides are non-negative---
or the form
- ``f(r) == t`` for some integer ``r`` and some ``t``.
Alternatively, an equation may be replaced by a ``transducers.RecursionRule`` with the attributes ``K``, ``r``, ``k``, ``s``, ``t`` as above or a tuple ``(r, t)``. Note that ``t`` *must* be a list in this case.
- ``base`` -- base of the digit expansion.
- ``function`` -- symbolic function ``f`` occuring in the recursions.
- ``var`` -- symbolic variable.
- ``input_alphabet`` -- (default: ``None``) a list of digits to be used as the input alphabet. If ``None`` and the base is an integer, ``input_alphabet`` is chosen to be ``srange(base.abs())``.
- ``word_function`` -- (default: ``None``) a symbolic function. If not ``None``, ``word_function(arg1, ..., argn)`` in a symbolic recurrence relation is interpreted as a transition with output ``[arg1, ..., argn]``. This could not be entered in a symbolic recurrence relation because lists do not coerce into the :class:`~sage.symbolic.ring.SymbolicRing`.
- ``is_zero`` -- (default: ``None``) a callable. The recursion relations are only well-posed if there is no cycle with non-zero output and input consisting of zeros. This parameter is used to determine whether the output of such a cycle is non-zero. By default, the output must evaluate to ``False`` as a boolean.
- ``output_rings`` -- (default: ``[ZZ, QQ]``) a list of rings. The output labels are converted into the first ring of the list in which they are contained. If they are not contained in any ring, they remain in whatever ring they are after parsing the recursions, typically the symbolic ring.
OUTPUT:
A transducer ``T``.
The transducer is constructed such that ``T(expansion) == f(n)`` if ``expansion`` is the digit expansion of ``n`` to the base ``base`` with the given input alphabet as set of digits. Here, the ``+`` on the right hand side of the recurrence relation is interpreted as the concatenation of words.
The formal equations and initial conditions in the recursion have to be selected such that ``f`` is uniquely defined.
EXAMPLES:
- The following example computes the Hamming weight of the ternary expansion of integers. ::
sage: function('f') f sage: var('n') n sage: T = transducers.Recursion([ ....: f(3*n + 1) == f(n) + 1, ....: f(3*n + 2) == f(n) + 1, ....: f(3*n) == f(n), ....: f(0) == 0], ....: 3, f, n) sage: T.transitions() [Transition from (0, 0) to (0, 0): 0|-, Transition from (0, 0) to (0, 0): 1|1, Transition from (0, 0) to (0, 0): 2|1]
To illustrate what this transducer does, we consider the example of `n=601`::
sage: ternary_expansion = 601.digits(base=3) sage: ternary_expansion [1, 2, 0, 1, 1, 2] sage: weight_sequence = T(ternary_expansion) sage: weight_sequence [1, 1, 1, 1, 1] sage: sum(weight_sequence) 5
Note that the digit zero does not show up in the output because the equation ``f(3*n) == f(n)`` means that no output is added to ``f(n)``.
- The following example computes the Hamming weight of the non-adjacent form, cf. the :wikipedia:`Non-adjacent_form`. ::
sage: function('f') f sage: var('n') n sage: T = transducers.Recursion([ ....: f(4*n + 1) == f(n) + 1, ....: f(4*n - 1) == f(n) + 1, ....: f(2*n) == f(n), ....: f(0) == 0], ....: 2, f, n) sage: T.transitions() [Transition from (0, 0) to (0, 0): 0|-, Transition from (0, 0) to (1, 1): 1|-, Transition from (1, 1) to (0, 0): 0|1, Transition from (1, 1) to (1, 0): 1|1, Transition from (1, 0) to (1, 1): 0|-, Transition from (1, 0) to (1, 0): 1|-] sage: [(s.label(), s.final_word_out) ....: for s in T.iter_final_states()] [((0, 0), []), ((1, 1), [1]), ((1, 0), [1])]
As we are interested in the weight only, we also output `1` for numbers congruent to `3` mod `4`. The actual expansion is computed in the next example.
Consider the example of `29=(100\bar 101)_2` (as usual, the digit `-1` is denoted by `\bar 1` and digits are written from the most significant digit at the left to the least significant digit at the right; for the transducer, we have to give the digits in the reverse order)::
sage: NAF = [1, 0, -1, 0, 0, 1] sage: ZZ(NAF, base=2) 29 sage: binary_expansion = 29.digits(base=2) sage: binary_expansion [1, 0, 1, 1, 1] sage: T(binary_expansion) [1, 1, 1] sage: sum(T(binary_expansion)) 3
Indeed, the given non-adjacent form has three non-zero digits.
- The following example computes the non-adjacent form from the binary expansion, cf. the :wikipedia:`Non-adjacent_form`. In contrast to the previous example, we actually compute the expansion, not only the weight.
We have to write the output `0` when converting an even number. This cannot be encoded directly by an equation in the symbolic ring, because ``f(2*n) == f(n) + 0`` would be equivalent to ``f(2*n) == f(n)`` and an empty output would be written. Therefore, we wrap the output in the symbolic function ``w`` and use the parameter ``word_function`` to announce this.
Similarly, we use ``w(-1, 0)`` to write an output word of length `2` in one iteration. Finally, we write ``f(0) == w()`` to write an empty word upon completion.
Moreover, there is a cycle with output ``[0]`` which---from the point of view of this method---is a contradicting recursion. We override this by the parameter ``is_zero``. ::
sage: var('n') n sage: function('f w') (f, w) sage: T = transducers.Recursion([ ....: f(2*n) == f(n) + w(0), ....: f(4*n + 1) == f(n) + w(1, 0), ....: f(4*n - 1) == f(n) + w(-1, 0), ....: f(0) == w()], ....: 2, f, n, ....: word_function=w, ....: is_zero=lambda x: sum(x).is_zero()) sage: T.transitions() [Transition from (0, 0) to (0, 0): 0|0, Transition from (0, 0) to (1, 1): 1|-, Transition from (1, 1) to (0, 0): 0|1,0, Transition from (1, 1) to (1, 0): 1|-1,0, Transition from (1, 0) to (1, 1): 0|-, Transition from (1, 0) to (1, 0): 1|0] sage: for s in T.iter_states(): ....: print("{} {}".format(s, s.final_word_out)) (0, 0) [] (1, 1) [1, 0] (1, 0) [1, 0]
We again consider the example of `n=29`::
sage: T(29.digits(base=2)) [1, 0, -1, 0, 0, 1, 0]
The same transducer can also be entered bypassing the symbolic equations::
sage: R = transducers.RecursionRule sage: TR = transducers.Recursion([ ....: R(K=1, r=0, k=0, s=0, t=[0]), ....: R(K=2, r=1, k=0, s=0, t=[1, 0]), ....: R(K=2, r=-1, k=0, s=0, t=[-1, 0]), ....: (0, [])], ....: 2, ....: is_zero=lambda x: sum(x).is_zero()) sage: TR == T True
- Here is an artificial example where some of the `s` are negative::
sage: function('f') f sage: var('n') n sage: T = transducers.Recursion([ ....: f(2*n + 1) == f(n-1) + 1, ....: f(2*n) == f(n), ....: f(1) == 1, ....: f(0) == 0], 2, f, n) sage: T.transitions() [Transition from (0, 0) to (0, 0): 0|-, Transition from (0, 0) to (1, 1): 1|-, Transition from (1, 1) to (-1, 1): 0|1, Transition from (1, 1) to (0, 0): 1|1, Transition from (-1, 1) to (-1, 2): 0|-, Transition from (-1, 1) to (1, 2): 1|-, Transition from (-1, 2) to (-1, 1): 0|1, Transition from (-1, 2) to (0, 0): 1|1, Transition from (1, 2) to (-1, 2): 0|1, Transition from (1, 2) to (1, 2): 1|1] sage: [(s.label(), s.final_word_out) ....: for s in T.iter_final_states()] [((0, 0), []), ((1, 1), [1]), ((-1, 1), [0]), ((-1, 2), [0]), ((1, 2), [1])]
- Abelian complexity of the paperfolding sequence (cf. [HKP2015]_, Example 2.8)::
sage: T = transducers.Recursion([ ....: f(4*n) == f(2*n), ....: f(4*n+2) == f(2*n+1)+1, ....: f(16*n+1) == f(8*n+1), ....: f(16*n+5) == f(4*n+1)+2, ....: f(16*n+11) == f(4*n+3)+2, ....: f(16*n+15) == f(2*n+2)+1, ....: f(1) == 2, f(0) == 0] ....: + [f(16*n+jj) == f(2*n+1)+2 for jj in [3,7,9,13]], ....: 2, f, n) sage: T.transitions() [Transition from (0, 0) to (0, 1): 0|-, Transition from (0, 0) to (1, 1): 1|-, Transition from (0, 1) to (0, 1): 0|-, Transition from (0, 1) to (1, 1): 1|1, Transition from (1, 1) to (1, 2): 0|-, Transition from (1, 1) to (3, 2): 1|-, Transition from (1, 2) to (1, 3): 0|-, Transition from (1, 2) to (5, 3): 1|-, Transition from (3, 2) to (3, 3): 0|-, Transition from (3, 2) to (7, 3): 1|-, Transition from (1, 3) to (1, 3): 0|-, Transition from (1, 3) to (1, 1): 1|2, Transition from (5, 3) to (1, 2): 0|2, Transition from (5, 3) to (1, 1): 1|2, Transition from (3, 3) to (1, 1): 0|2, Transition from (3, 3) to (3, 2): 1|2, Transition from (7, 3) to (1, 1): 0|2, Transition from (7, 3) to (2, 1): 1|1, Transition from (2, 1) to (1, 1): 0|1, Transition from (2, 1) to (2, 1): 1|-] sage: for s in T.iter_states(): ....: print("{} {}".format(s, s.final_word_out)) (0, 0) [] (0, 1) [] (1, 1) [2] (1, 2) [2] (3, 2) [2, 2] (1, 3) [2] (5, 3) [2, 2] (3, 3) [2, 2] (7, 3) [2, 2] (2, 1) [1, 2] sage: list(sum(T(n.bits())) for n in srange(1, 21)) [2, 3, 4, 3, 4, 5, 4, 3, 4, 5, 6, 5, 4, 5, 4, 3, 4, 5, 6, 5]
- We now demonstrate the use of the ``output_rings`` parameter. If no ``output_rings`` are specified, the output labels are converted into ``ZZ``::
sage: function('f') f sage: var('n') n sage: T = transducers.Recursion([ ....: f(2*n + 1) == f(n) + 1, ....: f(2*n) == f(n), ....: f(0) == 2], ....: 2, f, n) sage: for t in T.transitions(): ....: print([x.parent() for x in t.word_out]) [] [Integer Ring] sage: [x.parent() for x in T.states()[0].final_word_out] [Integer Ring]
In contrast, if ``output_rings`` is set to the empty list, the results are not converted::
sage: T = transducers.Recursion([ ....: f(2*n + 1) == f(n) + 1, ....: f(2*n) == f(n), ....: f(0) == 2], ....: 2, f, n, output_rings=[]) sage: for t in T.transitions(): ....: print([x.parent() for x in t.word_out]) [] [Symbolic Ring] sage: [x.parent() for x in T.states()[0].final_word_out] [Symbolic Ring]
Finally, we use a somewhat questionable conversion::
sage: T = transducers.Recursion([ ....: f(2*n + 1) == f(n) + 1, ....: f(2*n) == f(n), ....: f(0) == 0], ....: 2, f, n, output_rings=[GF(5)]) sage: for t in T.transitions(): ....: print([x.parent() for x in t.word_out]) [] [Finite Field of size 5]
.. TODO::
Extend the method to
- non-integral bases,
- higher dimensions.
ALGORITHM:
See [HKP2015]_, Section 6. However, there are also recursion transitions for states of level `<\kappa` if the recursion rules allow such a transition. Furthermore, the intermediate step of a non-deterministic transducer is left out by implicitly using recursion transitions. The well-posedness is checked in a truncated version of the recursion digraph.
TESTS:
The following tests fail due to missing or superfluous recursions or initial conditions. ::
sage: var('n') n sage: function('f') f sage: transducers.Recursion([f(2*n) == f(n)], ....: 2, f, n) Traceback (most recent call last): ... ValueError: Missing recursions for input congruent to [1] modulo 2.
::
sage: transducers.Recursion([f(2*n + 1) == f(n), ....: f(4*n) == f(2*n) + 1, ....: f(2*n) == f(n) +1], ....: 2, f, n) Traceback (most recent call last): ... ValueError: Conflicting rules congruent to 0 modulo 4.
::
sage: transducers.Recursion([f(2*n + 1) == f(n) + 1, ....: f(2*n) == f(n), ....: f(0) == 0, ....: f(42) == 42], 2, f, n) Traceback (most recent call last): ... ValueError: Superfluous initial values for [42].
::
sage: transducers.Recursion([f(2*n + 1) == f(n) + 1, ....: f(2*n) == f(n - 2) + 4, ....: f(0) == 0], 2, f, n) Traceback (most recent call last): ... ValueError: Missing initial values for [2].
Here is an example of a transducer with a conflicting rule (it cannot hold for `n = 0`)::
sage: T = transducers.Recursion([ ....: f(2*n + 1) == f(n - 1), ....: f(2*n) == f(n) + 1, ....: f(1) == 1, ....: f(0) == 0], 2, f, n) Traceback (most recent call last): ... ValueError: Conflicting recursion for [0]. """
else: equation, base, function, var, word_function, output_rings) else: assert False
for k in range(max_K + 1)]
# Aim: residues[K][R] = RuleRight(k, s, t) # if and only if # f(base^K n + R) = f(base^k n + s) + t
r=remainder, k=given_rule.k, s=given_rule.s - base**given_rule.k*q, t=given_rule.t) "Conflicting rules congruent to %d modulo %d." % (R, base**(rule.K + m))) s=rule.s + ell * base**rule.k, t=rule.t)
for R, rule in enumerate(residues[max_K]) if rule is None] "to %s modulo %s." % (missing_residues, base**max_K))
""" Compute recursion transition leaving state ``(carry, level)``.
INPUT:
- ``carry`` -- integer.
- ``level`` -- integer.
- ``force_nonnegative_target`` -- boolean. If ``True``, only recursion transitions leading to a non-negative carry are returned.
OUTPUT:
A tuple ``((new_carry, new_level), output)`` if a recursion transition matching the specifications exists; otherwise, ``None``. """ else:
""" Compute the target and output of a maximal path of recursion transitions starting at state ``(carry, level)``.
INPUT:
- ``carry`` -- integer.
- ``level`` -- integer.
- ``force_nonnegative_target`` -- boolean. If ``True``, only recursion transitions leading to a non-negative carry are allowed.
OUTPUT:
A tuple ``((new_carry, new_level), output)``. """ c, j, force_nonnegative_target)
state_carry, state_level, False) # no more recursion transition is possible, # so this is now a storing transition # We now may proceed along recursion transitions # as long as the carries stay non-negative. carry, level, True)
initial_states=[(0, 0)], input_alphabet=input_alphabet)
""" Compute the list of outgoing edges of ``n`` in the recursion digraph.
INPUT:
- ``n`` -- integer.
OUTPUT:
A list ``[(A(n), label)]`` if `A(n)<\infty`; otherwise an empty list. """ else:
""" Compute f(n) as defined by the recursion """
{carry: dict(edge_recursion_digraph(carry)) for carry in carries if carry >= 0}, multiedges=False)
initial_values_set)
"Missing initial values for %s." % sorted(list(missing_initial_values)))
raise ValueError( "Missing initial condition for one of %s." % cycle[1:]) raise ValueError( "Too many initial conditions, only give one of %s." % cycle[1:]) for edge in recursion_digraph.\ outgoing_edge_iterator(cycle[1:])], []) "Conflicting recursion for %s." % cycle[1:])
required_initial_values)
"Superfluous initial values for %s." % sorted(list(superfluous_initial_values)))
else:
# Easy access to the automaton and transducer generators from the command line: |