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\).

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 tmust 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
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.

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):

The following example computes the non-adjacent form from the
binary expansion, cf. the Wikipedia article 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.

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.

The Hamming weight is defined as the number of non-zero digits in the
input sequence over the alphabet input_alphabet (see
Wikipedia article 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.