Ask Your Question
1

Computing dimensions

asked 2021-02-16 00:54:19 +0200

phcosta gravatar image

updated 2021-02-16 02:17:53 +0200

I have the following routine divided into 3 steps:

1)

p = dict()
P = Permutations(3)
for i in (1 .. 3):
    for u in (1 .. 3):
        p[i, u] = [1 if P[m][i-1] == u else 0
                   for m in (0 .. 5)]

2)

for i in (1 .. 3):
     for u in (1 .. 3):
         for j in (1 .. 3):
             for v in (1 .. 3):
                 if i != j and u != v:
                     p[i, j, u, v] = [1 if P[m][i-1] == u and P[m][j-1] == v else 0
                                      for m in (0 .. 5)]

3)

A1 = matrix([p[i, u]
             for i in (1 .. 3)
             for u in (1 .. 3)])
A2 = matrix([p[i, j, u, v]
             for i in (1 .. 3) for j in (1 .. 3) if i != j
             for u in (1 .. 3) for v in (1 .. 3) if u != v])
C1 = A1.stack(A2)
E1 = A1.right_kernel()
E2 = C1.right_kernel()
d1 = E1.dimension()
d2 = E2.dimension()

I'm interested in dimensions d1 and d2.

If I go to permutations of 4, I will need to adjust all for loops to (1 .. 4) and to add one more step similar to step 2 to construct one more matrix like

A3 =  matrix([p[i, j, k, u, v, w]
              for i in (1 .. 4) for j in (1 .. 4)
              for u in (1 .. 4) for v in (1 .. 4)
              for k in (1 .. 4) for w in (1 .. 4)
              if i != j and i != k and j != k
              and u != v and u != w and v != w])

and also

C2 = C1.stack(A3)
E3 = C2.right_kernel()
d3 = E3.dimension()

So... I was wondering if there is some "easy" way to put this code in some kind of "generic" program. I mean, I could repeat the whole idea for permutations of 4 or 5 and so on, but it is not very practical. Any suggestions would be great.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2021-02-16 02:26:30 +0200

slelievre gravatar image

updated 2021-02-16 10:34:53 +0200

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()
    C = matrix(ZZ, 0, N)
    d = []
    for k in range(1, n):
        Arr = Arrangements([1 .. n], 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 = 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]
edit flag offensive delete link more

Comments

Many thanks. Do you have any suggestions of references to learn a bit more about programming?

phcosta gravatar imagephcosta ( 2021-02-16 15:36:08 +0200 )edit

I would recommend the book Computational mathematics with SageMath which is released under a Creative commons license and can be downloaded for free in pdf form in three languages or purchased in paper form in two languages.

slelievre gravatar imageslelievre ( 2021-02-16 16:13:09 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-02-16 00:54:19 +0200

Seen: 62 times

Last updated: Feb 16