ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 06 Feb 2018 18:14:04 -0600It takes so long to generate dictionary of GL elements.http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/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?
Mon, 05 Feb 2018 19:03:25 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/Comment by Symbol 1 for <p>The following command</p>
<pre><code>GL4dicthash={hash(g):None for g in GL(4,2)}
</code></pre>
<p>takes 9 seconds to execute. On the other hand</p>
<pre><code>GL4dict={g:None for g in GL(4,2)}
</code></pre>
<p>takes minutes and does not seem to terminate.</p>
<p>If I understand python dictionary correctly, they should take about the same time. So what happened?</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=41006#post-id-41006@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.Tue, 06 Feb 2018 18:14:04 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=41006#post-id-41006Comment by dan_fulea for <p>The following command</p>
<pre><code>GL4dicthash={hash(g):None for g in GL(4,2)}
</code></pre>
<p>takes 9 seconds to execute. On the other hand</p>
<pre><code>GL4dict={g:None for g in GL(4,2)}
</code></pre>
<p>takes minutes and does not seem to terminate.</p>
<p>If I understand python dictionary correctly, they should take about the same time. So what happened?</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40993#post-id-40993Mutability and hashability are two different things. In the above case, for instance:
sage: G = GL(4,2)
sage: g = G.one()
sage: g.__hash__()
0
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] ] )
20160
sage: G.order()
20160
In order to define a dictionary where `g`is 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?Tue, 06 Feb 2018 02:50:18 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40993#post-id-40993Comment by slelievre for <p>The following command</p>
<pre><code>GL4dicthash={hash(g):None for g in GL(4,2)}
</code></pre>
<p>takes 9 seconds to execute. On the other hand</p>
<pre><code>GL4dict={g:None for g in GL(4,2)}
</code></pre>
<p>takes minutes and does not seem to terminate.</p>
<p>If I understand python dictionary correctly, they should take about the same time. So what happened?</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40990#post-id-40990What version of Sage were you using?Mon, 05 Feb 2018 23:12:52 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40990#post-id-40990Comment by Symbol 1 for <p>The following command</p>
<pre><code>GL4dicthash={hash(g):None for g in GL(4,2)}
</code></pre>
<p>takes 9 seconds to execute. On the other hand</p>
<pre><code>GL4dict={g:None for g in GL(4,2)}
</code></pre>
<p>takes minutes and does not seem to terminate.</p>
<p>If I understand python dictionary correctly, they should take about the same time. So what happened?</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40989#post-id-40989Found this https://trac.sagemath.org/ticket/10950. Maybe I should update my Sage.Mon, 05 Feb 2018 19:36:48 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40989#post-id-40989Comment by Symbol 1 for <p>The following command</p>
<pre><code>GL4dicthash={hash(g):None for g in GL(4,2)}
</code></pre>
<p>takes 9 seconds to execute. On the other hand</p>
<pre><code>GL4dict={g:None for g in GL(4,2)}
</code></pre>
<p>takes minutes and does not seem to terminate.</p>
<p>If I understand python dictionary correctly, they should take about the same time. So what happened?</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40988#post-id-40988It seems `hash(g)` lies between 0 and 15 for all `g`. It does not make any sense to me.Mon, 05 Feb 2018 19:21:25 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=40988#post-id-40988Answer by dan_fulea for <p>The following command</p>
<pre><code>GL4dicthash={hash(g):None for g in GL(4,2)}
</code></pre>
<p>takes 9 seconds to execute. On the other hand</p>
<pre><code>GL4dict={g:None for g in GL(4,2)}
</code></pre>
<p>takes minutes and does not seem to terminate.</p>
<p>If I understand python dictionary correctly, they should take about the same time. So what happened?</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?answer=41003#post-id-41003The 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.
Tue, 06 Feb 2018 16:11:39 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?answer=41003#post-id-41003Comment by Symbol 1 for <p>The generation took on my slow linux machine approximatively 10s CPU time...</p>
<pre><code>sage: %timeit -n1 GL4dict = { g : None for g in GL(4,2) }
1 loop, best of 3: 9.29 s per loop
</code></pre>
<p>This is</p>
<pre><code>sage: version()
'SageMath version 8.1, Release Date: 2017-12-07'
</code></pre>
<p>Please update to the new version, if there is such a big run time difference.</p>
http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=41005#post-id-41005Thank 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`.Tue, 06 Feb 2018 18:05:00 -0600http://ask.sagemath.org/question/40987/it-takes-so-long-to-generate-dictionary-of-gl-elements/?comment=41005#post-id-41005