Ask Your Question

It takes so long to generate dictionary of GL elements.

asked 2018-02-06 02:03:25 +0100

Symbol 1 gravatar image

The following command

GL4dicthash={hash(g):None for g in GL(4,2)}

takes 9 seconds to execute. On the other hand

GL4dict={g:None for g in GL(4,2)}

takes minutes and does not seem to terminate.

If I understand python dictionary correctly, they should take about the same time. So what happened?

edit retag flag offensive close merge delete


It seems hash(g) lies between 0 and 15 for all g. It does not make any sense to me.

Symbol 1 gravatar imageSymbol 1 ( 2018-02-06 02:21:25 +0100 )edit

Found this Maybe I should update my Sage.

Symbol 1 gravatar imageSymbol 1 ( 2018-02-06 02:36:48 +0100 )edit

What version of Sage were you using?

slelievre gravatar imageslelievre ( 2018-02-06 06:12:52 +0100 )edit

Mutability and hashability are two different things. In the above case, for instance:

sage: G = GL(4,2)
sage: g =
sage: g.__hash__()

There is a __hash__ method, so g is hashable. There are $16$ different hash-values:

sage: list( set( [ g.__hash__() for g in G ] ) )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

But $G$ has order

sage: prod( [ 2^4-2^k for k in [0..3] ] )
sage: G.order()

In order to define a dictionary where gis a key, this element should be immutable. Then for each new key one has to check if the key is already used. This takes time. And before we try to understand the time problem, why do we need to work with such a complicated dictionary?

dan_fulea gravatar imagedan_fulea ( 2018-02-06 09:50:18 +0100 )edit

@slelievre before 8.0 now 8.1 and everything is good now. @dan_fulea: I am implementing a group action that is quite tedious. GL(4,2) acts on [0..15] by left-multiplying its binary expression as a column vector in GF(2)^4. And then act on Subsets([0..15]). I would like to cache the action as there are only 20160·16 possibilities. Moreover, if there is a (fast) way to turn g into 16-bit int then I need an array of size 65536·16, which is still affordable. What would make it triumph is that looking up array entry takes ~100ns.

Symbol 1 gravatar imageSymbol 1 ( 2018-02-07 01:14:04 +0100 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-02-06 23:11:39 +0100

dan_fulea gravatar image

The generation took on my slow linux machine approximatively 10s CPU time...

sage: %timeit -n1 GL4dict = { g : None for g in GL(4,2) }
1 loop, best of 3: 9.29 s per loop

This is

sage: version()
'SageMath version 8.1, Release Date: 2017-12-07'

Please update to the new version, if there is such a big run time difference.

edit flag offensive delete link more


Thank you. I did update from 8.0 to 8.1. I was not aware of the version-issue because I thought I was quite new. Now everything works fine. hash(g) gives crazy numbers such as -32759827368972486.

Symbol 1 gravatar imageSymbol 1 ( 2018-02-07 01:05:00 +0100 )edit

@Symbol 1 -- you can accept the answer if it solves the problem in your question.

If an answer solves your question, please accept it by clicking the "accept" button (the one with a check mark, below the upvote button, score, and downvote button, at the top left of the answer).

This will mark the question as solved in the list of questions on the main page of Ask Sage, as well as in lists of questions related to a particular query or keyword.

slelievre gravatar imageslelievre ( 2018-09-22 09:31:58 +0100 )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


Asked: 2018-02-06 02:03:25 +0100

Seen: 265 times

Last updated: Feb 06 '18