Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 1467152

Browse files
Release Managervbraun
authored andcommitted
Trac #16186: extend transducers.add to arbitrary input-length
At the moment {{{ sage: T = transducers.add([0, 1]) }}} creates a transducer which adds the values of the input-pair. This is now extended to arbitrary tuple-lengths. (This came up during the review of #16142.) Depends on #16663 in order to avoid trivial merge conflict later on. URL: http://trac.sagemath.org/16186 Reported by: dkrenn Ticket author(s): Clemens Heuberger, Daniel Krenn Reviewer(s): Daniel Krenn, Clemens Heuberger
2 parents b5dd91b + 93c23f6 commit 1467152

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/sage/combinat/finite_state_machine_generators.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ def any(self, input_alphabet, number_of_operands=2):
470470
input_alphabet, number_of_operands)
471471

472472

473-
def add(self, input_alphabet):
473+
474+
def add(self, input_alphabet, number_of_operands=2):
474475
"""
475476
Returns a transducer which realizes addition on pairs over the
476477
given input alphabet.
@@ -479,13 +480,17 @@ def add(self, input_alphabet):
479480
480481
- ``input_alphabet`` -- a list or other iterable.
481482
483+
- ``number_of_operands`` -- (default: `2`) it specifies the number
484+
of input arguments the operator takes.
485+
482486
OUTPUT:
483487
484-
A transducer mapping an input word `(i_0, i'_0)\ldots (i_k, i'_k)`
485-
to the word `(i_0 + i'_0)\ldots (i_k + i'_k)`.
488+
A transducer mapping an input word
489+
`(i_{01}, \ldots, i_{0d})\ldots (i_{k1}, \ldots, i_{kd})` to the word
490+
`(i_{01} + \cdots + i_{0d})\ldots (i_{k1} + \cdots + i_{kd})`.
486491
487492
The input alphabet of the generated transducer is the cartesian
488-
product of two copies of ``input_alphabet``.
493+
product of ``number_of_operands`` copies of ``input_alphabet``.
489494
490495
EXAMPLE:
491496
@@ -506,9 +511,19 @@ def add(self, input_alphabet):
506511
[0]
507512
sage: T([(0, 0), (0, 1), (1, 0), (1, 1)])
508513
[0, 1, 1, 2]
514+
515+
More than two operands can also be handled::
516+
517+
sage: T3 = transducers.add([0, 1], number_of_operands=3)
518+
sage: T3.input_alphabet
519+
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
520+
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
521+
sage: T3([(0, 0, 0), (0, 1, 0), (0, 1, 1), (1, 1, 1)])
522+
[0, 1, 2, 3]
509523
"""
510-
import operator
511-
return self.operator(operator.add, input_alphabet)
524+
return self.operator(lambda *args: sum(args),
525+
input_alphabet,
526+
number_of_operands=number_of_operands)
512527

513528

514529
def sub(self, input_alphabet):

0 commit comments

Comments
 (0)