Ask Your Question

Revision history [back]

Here is a refactoring of the code as a function of n, called dimensions for lack of a better name.

The documentation could better describe what sequence of dimensions is being computed.

def dimensions(n):
    r"""
    Return the sequence of dimensions for this `n`.

    EXAMPLES::

        sage: dimensions(3)
        [1, 0]
        sage: dimensions(4)
        [14, 1, 0]
        sage: dimensions(5)
        [103, 42, 1, 0]
    """
    P = Permutations(n)
    N = P.cardinality()
    A = dict()
    for k in range(1, n):
        Arr = Arrangements([1 .. n], k)
        A[k] = matrix(ZZ, [[all(p[i - 1] == u for i, u in zip(ii, uu))
                            for p in P] for ii in Arr for uu in Arr])
    C = matrix(ZZ, 0, N)
    d = []
    for k in range(1, n):
        C = C.stack(A[k])
        d.append(N - C.rank())
    return d

No surprise, it takes longer and longer as n grows:

sage: %time dimensions(3)
CPU times: user 4.18 ms, sys: 202 µs, total: 4.39 ms
Wall time: 4.29 ms
[1, 0]

sage: %time dimensions(4)
CPU times: user 169 ms, sys: 5.79 ms, total: 175 ms
Wall time: 189 ms
[14, 1, 0]

sage: %time dimensions(5)
CPU times: user 14.1 s, sys: 103 ms, total: 14.2 s
Wall time: 14.3 s
[103, 42, 1, 0]

Here is a refactoring of the code as a function of n, called dimensions for lack of a better name.

The documentation could better describe what sequence of dimensions is being computed.

def dimensions(n):
    r"""
    Return the sequence of dimensions for this `n`.

    EXAMPLES::

        sage: dimensions(3)
        [1, 0]
        sage: dimensions(4)
        [14, 1, 0]
        sage: dimensions(5)
        [103, 42, 1, 0]
    """
    P = Permutations(n)
    N = P.cardinality()
    A = dict()
C = matrix(ZZ, 0, N)
    d = []
    for k in range(1, n):
        Arr = Arrangements([1 .. n], k)
        A[k] A = matrix(ZZ, [[all(p[i - 1] == u for i, u in zip(ii, uu))
                          for p in P] for ii in Arr for uu in Arr])
     C = matrix(ZZ, 0, N)
    d = []
    for k in range(1, n):
        C = C.stack(A[k])
C.stack(A)
        d.append(N - C.rank())
    return d

No surprise, it takes longer and longer as n grows:

sage: %time dimensions(3)
CPU times: user 4.18 ms, sys: 202 µs, total: 4.39 ms
Wall time: 4.29 ms
[1, 0]

sage: %time dimensions(4)
CPU times: user 169 ms, sys: 5.79 ms, total: 175 ms
Wall time: 189 ms
[14, 1, 0]

sage: %time dimensions(5)
CPU times: user 14.1 s, sys: 103 ms, total: 14.2 s
Wall time: 14.3 s
[103, 42, 1, 0]