### Navigation

- index
- modules |
- next |
- previous |
- Matrices and Spaces of Matrices »

# Operation Tables

This module implements general operation tables, which are very matrix-like.

class `sage.matrix.operation_table.``OperationTable`(S, operation, names='letters', elements=None)

  Bases: `sage.structure.sage_object.SageObject`

  An object that represents a binary operation as a table.

  Primarily this object is used to provide a `multiplication_table()` for objects in the category of magmas (monoids, groups, ...) and `addition_table()` for objects in the category of commutative additive magmas (additive monoids, groups, ...).

  INPUT:

  - `S` - a finite algebraic structure (or finite iterable)

  - `operation` - a function of two variables that accepts pairs

      of elements from `S`. A natural source of such functions is the Python `operator` module, and in particular `operator.add()` and `operator.mul()`. This may also be a function defined with `lambda` or `def.`

  - `names` - (default: `'letters'`) The type of names used, values are:

    - `'letters'` - lowercase ASCII letters are used for a base 26 representation of the elements’ positions in the list given by `column_keys()`, padded to a common width with leading ‘a’s.
    - `'digits'` - base 10 representation of the elements’ positions in the list given by `column_keys()`, padded to a common width with leading zeros.
    - `'elements'` - the string representations of the elements themselves.
    - a list - a list of strings, where the length of the list equals the number of elements.

  - `elements` - (default: `None`) A list of elements of `S`, in forms that can be coerced into the structure, eg. their string representations. This may be used to impose an alternate ordering on the elements of \(S`\), perhaps when this is used in the context of a particular structure. The default is to use whatever ordering the `S.list()` method returns. \(elements`\) can also be a subset which is closed under the operation, useful perhaps when the set is infinite.

  OUTPUT: An object with methods that abstracts multiplication tables, addition tables, Cayley tables, etc. It should be general enough to be useful for any finite algebraic structure whose elements can be combined with a binary operation. This is not necessarily meant be constructed directly, but instead should be useful for constructing operation tables of various algebraic structures that have binary operations.

  EXAMPLES:

  In it’s most basic use, the table needs a structure and an operation:

      sage: from sage.matrix.operation_table import OperationTable
      sage: G=SymmetricGroup(3)
      sage: OperationTable(G, operation = operator.mul)
      *  a b c d e f
       +------------
      a| a b c d e f
      b| b a d c f e
      c| c e a f b d
      d| d f b e a c
      e| e c f a d b
      f| f d e b c a

  With two operations present, we can specify which operation we want:

      sage: from sage.matrix.operation_table import OperationTable
      sage: R=Integers(6)
      sage: OperationTable(R, operation=operator.add)
      +  a b c d e f
       +------------
      a| a b c d e f
      b| b c d e f a
      c| c d e f a b
      d| d e f a b c
      e| e f a b c d
      f| f a b c d e

  The default symbol set for elements is lowercase ASCII letters, which take on a base 26 flavor for structures with more than 26 elements.

      sage: from sage.matrix.operation_table import OperationTable
      sage: G=DihedralGroup(14)
      sage: OperationTable(G, operator.mul, names='letters')
       *  aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
        +------------------------------------------------------------------------------------
      aa| aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
      ab| ab aa ad ac af ae ah ag aj ai al ak an am ap ao ar aq at as av au ax aw az ay bb ba
      ac| ac ba aa ae ad ag af ai ah ak aj am al ao an aq ap as ar au at aw av ay ax bb ab az
      ad| ad bb ab af ac ah ae aj ag al ai an ak ap am ar ao at aq av as ax au az aw ba aa ay
      ae| ae az ba ag aa ai ad ak af am ah ao aj aq al as an au ap aw ar ay at bb av ab ac ax
      af| af ay bb ah ab aj ac al ae an ag ap ai ar ak at am av ao ax aq az as ba au aa ad aw
      ag| ag ax az ai ba ak aa am ad ao af aq ah as aj au al aw an ay ap bb ar ab at ac ae av
      ah| ah aw ay aj bb al ab an ac ap ae ar ag at ai av ak ax am az ao ba aq aa as ad af au
      ai| ai av ax ak az am ba ao aa aq ad as af au ah aw aj ay al bb an ab ap ac ar ae ag at
      aj| aj au aw al ay an bb ap ab ar ac at ae av ag ax ai az ak ba am aa ao ad aq af ah as
      ak| ak at av am ax ao az aq ba as aa au ad aw af ay ah bb aj ab al ac an ae ap ag ai ar
      al| al as au an aw ap ay ar bb at ab av ac ax ae az ag ba ai aa ak ad am af ao ah aj aq
      am| am ar at ao av aq ax as az au ba aw aa ay ad bb af ab ah ac aj ae al ag an ai ak ap
      an| an aq as ap au ar aw at ay av bb ax ab az ac ba ae aa ag ad ai af ak ah am aj al ao
      ao| ao ap ar aq at as av au ax aw az ay ba bb aa ab ad ac af ae ah ag aj ai al ak am an
      ap| ap ao aq ar as at au av aw ax ay az bb ba ab aa ac ad ae af ag ah ai aj ak al an am
      aq| aq an ap as ar au at aw av ay ax bb az ab ba ac aa ae ad ag af ai ah ak aj am ao al
      ar| ar am ao at aq av as ax au az aw ba ay aa bb ad ab af ac ah ae aj ag al ai an ap ak
      as| as al an au ap aw ar ay at bb av ab ax ac az ae ba ag aa ai ad ak af am ah ao aq aj
      at| at ak am av ao ax aq az as ba au aa aw ad ay af bb ah ab aj ac al ae an ag ap ar ai
      au| au aj al aw an ay ap bb ar ab at ac av ae ax ag az ai ba ak aa am ad ao af aq as ah
      av| av ai ak ax am az ao ba aq aa as ad au af aw ah ay aj bb al ab an ac ap ae ar at ag
      aw| aw ah aj ay al bb an ab ap ac ar ae at ag av ai ax ak az am ba ao aa aq ad as au af
      ax| ax ag ai az ak ba am aa ao ad aq af as ah au aj aw al ay an bb ap ab ar ac at av ae
      ay| ay af ah bb aj ab al ac an ae ap ag ar ai at ak av am ax ao az aq ba as aa au aw ad
      az| az ae ag ba ai aa ak ad am af ao ah aq aj as al au an aw ap ay ar bb at ab av ax ac
      ba| ba ac ae aa ag ad ai af ak ah am aj ao al aq an as ap au ar aw at ay av bb ax az ab
      bb| bb ad af ab ah ac aj ae al ag an ai ap ak ar am at ao av aq ax as az au ba aw ay aa

  Another symbol set is base 10 digits, padded with leading zeros to make a common width.

      sage: from sage.matrix.operation_table import OperationTable
      sage: G=AlternatingGroup(4)
      sage: OperationTable(G, operator.mul, names='digits')
       *  00 01 02 03 04 05 06 07 08 09 10 11
        +------------------------------------
      00| 00 01 02 03 04 05 06 07 08 09 10 11
      01| 01 02 00 05 03 04 07 08 06 11 09 10
      02| 02 00 01 04 05 03 08 06 07 10 11 09
      03| 03 06 09 00 07 10 01 04 11 02 05 08
      04| 04 08 10 02 06 11 00 05 09 01 03 07
      05| 05 07 11 01 08 09 02 03 10 00 04 06
      06| 06 09 03 10 00 07 04 11 01 08 02 05
      07| 07 11 05 09 01 08 03 10 02 06 00 04
      08| 08 10 04 11 02 06 05 09 00 07 01 03
      09| 09 03 06 07 10 00 11 01 04 05 08 02
      10| 10 04 08 06 11 02 09 00 05 03 07 01
      11| 11 05 07 08 09 01 10 02 03 04 06 00

  If the group’s elements are not too cumbersome, or the group is small, then the string representation of the elements can be used.

      sage: from sage.matrix.operation_table import OperationTable
      sage: G=AlternatingGroup(3)
      sage: OperationTable(G, operator.mul, names='elements')
            *       () (1,2,3) (1,3,2)
             +------------------------
           ()|      () (1,2,3) (1,3,2)
      (1,2,3)| (1,2,3) (1,3,2)      ()
      (1,3,2)| (1,3,2)      () (1,2,3)

  You can give the elements any names you like, but they need to be ordered in the same order as returned by the `column_keys()` method.

      sage: from sage.matrix.operation_table import OperationTable
      sage: G=QuaternionGroup()
      sage: T=OperationTable(G, operator.mul)
      sage: T.column_keys()
      ((), (1,2,3,4)(5,6,7,8), ..., (1,8,3,6)(2,7,4,5))
      sage: names=['1', 'I', '-1', '-I', 'J', '-K', '-J', 'K']
      sage: T.change_names(names=names)
      sage: sorted(T.translation().items())
      [('-1', (1,3)(2,4)(5,7)(6,8)),..., ('K', (1,8,3,6)(2,7,4,5))]
      sage: T
       *   1  I -1 -I  J -K -J  K
        +------------------------
       1|  1  I -1 -I  J -K -J  K
       I|  I -1 -I  1  K  J -K -J
      -1| -1 -I  1  I -J  K  J -K
      -I| -I  1  I -1 -K -J  K  J
       J|  J -K -J  K -1 -I  1  I
      -K| -K -J  K  J  I -1 -I  1
      -J| -J  K  J -K  1  I -1 -I
       K|  K  J -K -J -I  1  I -1

  With the right functions and a list comprehension, custom names can be easier. A multiplication table for hex digits (without carries):

      sage: from sage.matrix.operation_table import OperationTable
      sage: R=Integers(16)
      sage: names=[hex(Integer(a)) for a in R]
      sage: OperationTable(R, operation=operator.mul, names=names)
      *  0 1 2 3 4 5 6 7 8 9 a b c d e f
       +--------------------------------
      0| 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      1| 0 1 2 3 4 5 6 7 8 9 a b c d e f
      2| 0 2 4 6 8 a c e 0 2 4 6 8 a c e
      3| 0 3 6 9 c f 2 5 8 b e 1 4 7 a d
      4| 0 4 8 c 0 4 8 c 0 4 8 c 0 4 8 c
      5| 0 5 a f 4 9 e 3 8 d 2 7 c 1 6 b
      6| 0 6 c 2 8 e 4 a 0 6 c 2 8 e 4 a
      7| 0 7 e 5 c 3 a 1 8 f 6 d 4 b 2 9
      8| 0 8 0 8 0 8 0 8 0 8 0 8 0 8 0 8
      9| 0 9 2 b 4 d 6 f 8 1 a 3 c 5 e 7
      a| 0 a 4 e 8 2 c 6 0 a 4 e 8 2 c 6
      b| 0 b 6 1 c 7 2 d 8 3 e 9 4 f a 5
      c| 0 c 8 4 0 c 8 4 0 c 8 4 0 c 8 4
      d| 0 d a 7 4 1 e b 8 5 2 f c 9 6 3
      e| 0 e c a 8 6 4 2 0 e c a 8 6 4 2
      f| 0 f e d c b a 9 8 7 6 5 4 3 2 1

  This should be flexible enough to create a variety of such tables.

      sage: from sage.matrix.operation_table import OperationTable
      sage: from operator import xor
      sage: T=OperationTable(ZZ, xor, elements=range(8))
      sage: T
      .  a b c d e f g h
       +----------------
      a| a b c d e f g h
      b| b a d c f e h g
      c| c d a b g h e f
      d| d c b a h g f e
      e| e f g h a b c d
      f| f e h g b a d c
      g| g h e f c d a b
      h| h g f e d c b a
      sage: names=['000', '001','010','011','100','101','110','111']
      sage: T.change_names(names)
      sage: T.set_print_symbols('^', '\\land')
      sage: T
        ^  000 001 010 011 100 101 110 111
         +--------------------------------
      000| 000 001 010 011 100 101 110 111
      001| 001 000 011 010 101 100 111 110
      010| 010 011 000 001 110 111 100 101
      011| 011 010 001 000 111 110 101 100
      100| 100 101 110 111 000 001 010 011
      101| 101 100 111 110 001 000 011 010
      110| 110 111 100 101 010 011 000 001
      111| 111 110 101 100 011 010 001 000

      sage: T = OperationTable([False, True], operator.or_, names = 'elements')
      sage: T
          .  False  True
           +------------
      False| False  True
       True|  True  True

  TESTS:

  Empty structures behave acceptably, though the ASCII table looks a bit odd. The LaTeX version works much better.

      sage: from sage.matrix.operation_table import OperationTable
      sage: L=FiniteSemigroups().example(())
      sage: L
      An example of a finite semigroup: the left regular band generated by ()
      sage: T=OperationTable(L, operation=operator.mul)
      sage: T
      *
       +
      sage: T._latex_()
      '{\\setlength{\\arraycolsep}{2ex}\n\\begin{array}{r|*{0}{r}}\n\\multicolumn{1}{c|}{\\ast}\\\\\\hline\n\\end{array}}'

  If the algebraic structure cannot be listed (like when it is infinite) then there is no way to create a table.

      sage: from sage.matrix.operation_table import OperationTable
      sage: OperationTable(ZZ, operator.mul)
      Traceback (most recent call last):
      ...
      ValueError: Integer Ring is infinite

  The value of `elements` must be a subset of the algebraic structure, in forms that can be coerced into the structure. Here we demonstrate the proper use first:

      sage: from sage.matrix.operation_table import OperationTable
      sage: H=CyclicPermutationGroup(4)
      sage: H.list()
      [(), (1,2,3,4), (1,3)(2,4), (1,4,3,2)]
      sage: elts = ['()', '(1,3)(2,4)']
      sage: OperationTable(H, operator.mul, elements=elts)
      *  a b
       +----
      a| a b
      b| b a

  This can be rewritten so as to pass the actual elements of the group `H`, using a simple `for` loop:

      sage: L = H.list()    #list of elements of the group H
      sage: elts = [L[i] for i in {0, 2}]
      sage: elts
      [(), (1,3)(2,4)]
      sage: OperationTable(H, operator.mul, elements=elts)
      *  a b
       +----
      a| a b
      b| b a

  Here are a couple of improper uses

      sage: elts.append(5)
      sage: OperationTable(H, operator.mul, elements=elts)
      Traceback (most recent call last):
      ...
      TypeError: unable to coerce 5 into Cyclic group of order 4 as a permutation group
      sage: elts[2]='(1,3,2,4)'
      sage: OperationTable(H, operator.mul, elements=elts)
      Traceback (most recent call last):
      ...
      TypeError: unable to coerce (1,3,2,4) into Cyclic group of order 4 as a permutation group
      sage: elts[2]='(1,2,3,4)'
      sage: OperationTable(H, operator.mul, elements=elts)
      Traceback (most recent call last):
      ...
      ValueError: (1,3)(2,4)*(1,2,3,4)=(1,4,3,2), and so the set is not closed

  Unusable functions should be recognized as such:

      sage: H=CyclicPermutationGroup(4)
      sage: OperationTable(H, operator.add)
      Traceback (most recent call last):
      ...
      TypeError: elements () and () of Cyclic group of order 4 as a permutation group are incompatible with operation: <built-in function add>
      sage: from operator import xor
      sage: OperationTable(H, xor)
      Traceback (most recent call last):
      ...
      TypeError: elements () and () of Cyclic group of order 4 as a permutation group are incompatible with operation: <built-in function xor>

  TODO:

  Provide color and grayscale graphical representations of tables. See commented-out stubs in source code.

  AUTHOR:

  - Rob Beezer (2010-03-15)
  `change_names`(names)

    For an existing operation table, change the names used for the elements.

    INPUT:

    - `names` - the type of names used, values are:
      - `'letters'` - lowercase ASCII letters are used for a base 26 representation of the elements’ positions in the list given by `list()`, padded to a common width with leading ‘a’s.
      - `'digits'` - base 10 representation of the elements’ positions in the list given by `list()`, padded to a common width with leading zeros.
      - `'elements'` - the string representations of the elements themselves.
      - a list - a list of strings, where the length of the list equals the number of elements.

    OUTPUT: `None`. This method changes the table “in-place”, so any printed version will change and the output of the `dict()` will also change. So any items of interest about a particular table need to be copied/saved prior to calling this method.

    EXAMPLES:

    More examples can be found in the documentation for `OperationTable` since creating a new operation table uses the same routine.

        sage: from sage.matrix.operation_table import OperationTable
        sage: D=DihedralGroup(2)
        sage: T=OperationTable(D, operator.mul)
        sage: T
        *  a b c d
         +--------
        a| a b c d
        b| b a d c
        c| c d a b
        d| d c b a
        sage: T.translation()['c']
        (1,2)
        sage: T.change_names('digits')
        sage: T
        *  0 1 2 3
         +--------
        0| 0 1 2 3
        1| 1 0 3 2
        2| 2 3 0 1
        3| 3 2 1 0
        sage: T.translation()['2']
        (1,2)
        sage: T.change_names('elements')
        sage: T
                 *          ()      (3,4)      (1,2) (1,2)(3,4)
                  +--------------------------------------------
                ()|         ()      (3,4)      (1,2) (1,2)(3,4)
             (3,4)|      (3,4)         () (1,2)(3,4)      (1,2)
             (1,2)|      (1,2) (1,2)(3,4)         ()      (3,4)
        (1,2)(3,4)| (1,2)(3,4)      (1,2)      (3,4)         ()
        sage: T.translation()['(1,2)']
        (1,2)
        sage: T.change_names(['w', 'x', 'y', 'z'])
        sage: T
        *  w x y z
         +--------
        w| w x y z
        x| x w z y
        y| y z w x
        z| z y x w
        sage: T.translation()['y']
        (1,2)

  `column_keys`()

    Returns a tuple of the elements used to build the table.

    Note

    `column_keys` and `row_keys` are identical. Both list the elements in the order used to label the table.

    OUTPUT:

    The elements of the algebraic structure used to build the table, as a list. But most importantly, elements are present in the list in the order which they appear in the table’s column headings.

    EXAMPLES:

        sage: from sage.matrix.operation_table import OperationTable
        sage: G=AlternatingGroup(3)
        sage: T=OperationTable(G, operator.mul)
        sage: T.column_keys()
        ((), (1,2,3), (1,3,2))

  `matrix_of_variables`()

    This method provides some backward compatibility for Cayley tables of groups, whose output was restricted to this single format.

    EXAMPLES:

    The output here is from the doctests for the old `cayley_table()` method for permutation groups.

        sage: from sage.matrix.operation_table import OperationTable
        sage: G=PermutationGroup(['(1,2,3)', '(2,3)'])
        sage: T=OperationTable(G, operator.mul)
        sage: T.matrix_of_variables()
        [x0 x1 x2 x3 x4 x5]
        [x1 x0 x3 x2 x5 x4]
        [x2 x4 x0 x5 x1 x3]
        [x3 x5 x1 x4 x0 x2]
        [x4 x2 x5 x0 x3 x1]
        [x5 x3 x4 x1 x2 x0]
        sage: T.column_keys()[3]*T.column_keys()[3] == T.column_keys()[4]
        True

  `row_keys`()

    Returns a tuple of the elements used to build the table.

    Note

    `column_keys` and `row_keys` are identical. Both list the elements in the order used to label the table.

    OUTPUT:

    The elements of the algebraic structure used to build the table, as a list. But most importantly, elements are present in the list in the order which they appear in the table’s column headings.

    EXAMPLES:

        sage: from sage.matrix.operation_table import OperationTable
        sage: G=AlternatingGroup(3)
        sage: T=OperationTable(G, operator.mul)
        sage: T.column_keys()
        ((), (1,2,3), (1,3,2))

  `set_print_symbols`(ascii, latex)

    Set the symbols used for text and LaTeX printing of operation tables.

    INPUT:

    - `ascii` - a single character for text table
    - `latex` - a string to represent an operation in LaTeX math mode. Note the need for double-backslashes to escape properly.

    EXAMPLE:

        sage: from sage.matrix.operation_table import OperationTable
        sage: G=AlternatingGroup(3)
        sage: T=OperationTable(G, operator.mul)
        sage: T.set_print_symbols('@', '\\times')
        sage: T
        @  a b c
         +------
        a| a b c
        b| b c a
        c| c a b
        sage: T._latex_()
        '{\\setlength{\\arraycolsep}{2ex}\n\\begin{array}{r|*{3}{r}}\n\\multicolumn{1}{c|}{\\times}&a&b&c\\\\\\hline\n{}a&a&b&c\\\\\n{}b&b&c&a\\\\\n{}c&c&a&b\\\\\n\\end{array}}'

    TESTS:

        sage: from sage.matrix.operation_table import OperationTable
        sage: G=AlternatingGroup(3)
        sage: T=OperationTable(G, operator.mul)
        sage: T.set_print_symbols('@', 5)
        Traceback (most recent call last):
        ...
        ValueError: LaTeX symbol must be a string, not 5
        sage: T.set_print_symbols('@x@', '\\times')
        Traceback (most recent call last):
        ...
        ValueError: ASCII symbol should be a single character, not @x@
        sage: T.set_print_symbols(5, '\\times')
        Traceback (most recent call last):
        ...
        ValueError: ASCII symbol should be a single character, not 5

  `table`()

    Returns the table as a list of lists, using integers to reference the elements.

    OUTPUT: The rows of the table, as a list of rows, each row being a list of integer entries. The integers correspond to the order of the elements in the headings of the table and the order of the output of the `list()` method.

    EXAMPLE:

        sage: from sage.matrix.operation_table import OperationTable
        sage: C=CyclicPermutationGroup(3)
        sage: T=OperationTable(C, operator.mul)
        sage: T.table()
        [[0, 1, 2], [1, 2, 0], [2, 0, 1]]

  `translation`()

    Returns a dictionary associating names with elements.

    OUTPUT: A dictionary whose keys are strings used as names for entries of the table and values that are the actual elements of the algebraic structure.

    EXAMPLES:

        sage: from sage.matrix.operation_table import OperationTable
        sage: G=AlternatingGroup(3)
        sage: T=OperationTable(G, operator.mul, names=['p','q','r'])
        sage: sorted(T.translation().items())
        [('p', ()), ('q', (1,2,3)), ('r', (1,3,2))]

### Navigation

- index
- modules |
- next |
- previous |
- Matrices and Spaces of Matrices »