Module garoupa.algebra.math

Operations with permutations

Functions

def int2pmat(number, side)
Expand source code
def int2pmat(number, side):
    """Convert number into permutation.

    Pads to side.

    Usage:

    >>> int2pmat(4, 4)
    [0, 2, 1, 3]

    Parameters
    ----------
    number
    side

    Returns
    -------

    """
    available = list(range(side))
    mat = []
    for i in range(side, 0, -1):
        number, r = divmod(number, i)
        mat.append(available.pop(r))
    mat.extend(available)
    return mat

Convert number into permutation.

Pads to side.

Usage:

>>> int2pmat(4, 4)
[0, 2, 1, 3]

Parameters

number
 
side
 

Returns

def pmat2int(matrix)
Expand source code
def pmat2int(matrix):
    """Convert permutation to number.

    Usage:

    >>> pmat2int([0, 2, 1, 3])
    4

    Parameters
    ----------
    matrix

    Returns
    -------

    """
    radix = len(matrix)
    available = list(range(radix))
    i = 1
    res = 0
    for row in matrix:
        idx = available.index(row)
        del available[idx]
        res += idx * i
        i *= radix
        radix -= 1
    return res

Convert permutation to number.

Usage:

>>> pmat2int([0, 2, 1, 3])
4

Parameters

matrix
 

Returns

def pmat_inv(m)
Expand source code
def pmat_inv(m):
    size = len(m)
    r = list(range(size))
    for i in range(size):
        r[m[i]] = i
    return r
def pmat_mult(a, b)
Expand source code
def pmat_mult(a, b):
    """Multiply two permutations.

    Parameters
    ----------
    a
        list of positive integers plus zero
    b
        list of positive integers plus zero

    Returns
    -------

    """
    if len(a) != len(b):  # pragma: no cover
        raise Exception("a and b should have same length.")
    return [a[x] for x in b]

Multiply two permutations.

Parameters

a
list of positive integers plus zero
b
list of positive integers plus zero

Returns

def pmat_transpose(m)
Expand source code
def pmat_transpose(m):
    """Transpose a permutation.

    Original author (CC BY-SA 4.0 LICENSE):
    https://codereview.stackexchange.com/questions/241511/how-to-efficiently-fast-calculate-the-transpose-of-a-permutation-matrix-in-p/241524?noredirect=1#comment473994_241524

    Parameters
    ----------
    m
        list of positive integers plus zero

    Returns
    -------
        list of positive integers plus zero
    """
    n = len(m)
    tr_ls = [0] * n

    for l in m:
        tr_ls[n - 1 - m[l]] = n - 1 - l

    return tr_ls

Transpose a permutation.

Original author (CC BY-SA 4.0 LICENSE): https://codereview.stackexchange.com/questions/241511/how-to-efficiently-fast-calculate-the-transpose-of-a-permutation-matrix-in-p/241524?noredirect=1#comment473994_241524

Parameters

m
list of positive integers plus zero

Returns

list of positive integers plus zero