ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 03 Dec 2020 19:39:41 +0100counting number of matrices in finite fieldhttps://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/I am new to sage and I want to count the number of matrices per rank in a given finite field.
Example: M=matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])
in Z/3ZWed, 02 Dec 2020 16:21:39 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/Answer by tmonteil for <p>I am new to sage and I want to count the number of matrices per rank in a given finite field.</p>
<p>Example: M=matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])
in Z/3Z</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?answer=54527#post-id-54527First, you can define the finite with 3 elements as follows:
sage: F = GF(3)
sage: F
Finite Field of size 3
The very cool thing in Sage is that you can define *parents* (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field `F`:
sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
Then we can look at the list of possible matrices (warning, the following produces a large output):
sage: list(M)
To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):
sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
Then you can check that there are 11232 matrices of rank 3, and do on.
sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.
**EDIT**
If you want to iterate over some particular matrices like matrices of the form `matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])`, you can use the product `F^8` of `F` by itself 8 times using `itertools`:
sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
Note that in Pyhon (hence Sage) the indices start at 0, not 1.Wed, 02 Dec 2020 20:03:37 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?answer=54527#post-id-54527Comment by John Palmieri for <p>First, you can define the finite with 3 elements as follows:</p>
<pre><code>sage: F = GF(3)
sage: F
Finite Field of size 3
</code></pre>
<p>The very cool thing in Sage is that you can define <em>parents</em> (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field <code>F</code>:</p>
<pre><code>sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
</code></pre>
<p>Then we can look at the list of possible matrices (warning, the following produces a large output):</p>
<pre><code>sage: list(M)
</code></pre>
<p>To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
</code></pre>
<p>Then you can check that there are 11232 matrices of rank 3, and do on. </p>
<pre><code>sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
</code></pre>
<p>All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.</p>
<p><strong>EDIT</strong></p>
<p>If you want to iterate over some particular matrices like matrices of the form <code>matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])</code>, you can use the product <code>F^8</code> of <code>F</code> by itself 8 times using <code>itertools</code>:</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
</code></pre>
<p>Note that in Pyhon (hence Sage) the indices start at 0, not 1.</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54535#post-id-54535How about something like this: `L = [matrix(2, 2, [x, y, z, -x]) for x in GF(3) for y in GF(3) for z in GF(3)]`?Thu, 03 Dec 2020 07:07:08 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54535#post-id-54535Comment by tmonteil for <p>First, you can define the finite with 3 elements as follows:</p>
<pre><code>sage: F = GF(3)
sage: F
Finite Field of size 3
</code></pre>
<p>The very cool thing in Sage is that you can define <em>parents</em> (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field <code>F</code>:</p>
<pre><code>sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
</code></pre>
<p>Then we can look at the list of possible matrices (warning, the following produces a large output):</p>
<pre><code>sage: list(M)
</code></pre>
<p>To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
</code></pre>
<p>Then you can check that there are 11232 matrices of rank 3, and do on. </p>
<pre><code>sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
</code></pre>
<p>All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.</p>
<p><strong>EDIT</strong></p>
<p>If you want to iterate over some particular matrices like matrices of the form <code>matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])</code>, you can use the product <code>F^8</code> of <code>F</code> by itself 8 times using <code>itertools</code>:</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
</code></pre>
<p>Note that in Pyhon (hence Sage) the indices start at 0, not 1.</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54537#post-id-54537@john-palmieri storing all matrices in a list might cost some memory (unless you plan to reuse them), you should better iterate over them.Thu, 03 Dec 2020 10:18:44 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54537#post-id-54537Comment by tmonteil for <p>First, you can define the finite with 3 elements as follows:</p>
<pre><code>sage: F = GF(3)
sage: F
Finite Field of size 3
</code></pre>
<p>The very cool thing in Sage is that you can define <em>parents</em> (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field <code>F</code>:</p>
<pre><code>sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
</code></pre>
<p>Then we can look at the list of possible matrices (warning, the following produces a large output):</p>
<pre><code>sage: list(M)
</code></pre>
<p>To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
</code></pre>
<p>Then you can check that there are 11232 matrices of rank 3, and do on. </p>
<pre><code>sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
</code></pre>
<p>All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.</p>
<p><strong>EDIT</strong></p>
<p>If you want to iterate over some particular matrices like matrices of the form <code>matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])</code>, you can use the product <code>F^8</code> of <code>F</code> by itself 8 times using <code>itertools</code>:</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
</code></pre>
<p>Note that in Pyhon (hence Sage) the indices start at 0, not 1.</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54538#post-id-54538@smokzeta i edited my answer to answer your question, i did not see the `-x1` and though you want to iterate over all matrices.Thu, 03 Dec 2020 10:22:24 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54538#post-id-54538Comment by smokzeta for <p>First, you can define the finite with 3 elements as follows:</p>
<pre><code>sage: F = GF(3)
sage: F
Finite Field of size 3
</code></pre>
<p>The very cool thing in Sage is that you can define <em>parents</em> (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field <code>F</code>:</p>
<pre><code>sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
</code></pre>
<p>Then we can look at the list of possible matrices (warning, the following produces a large output):</p>
<pre><code>sage: list(M)
</code></pre>
<p>To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
</code></pre>
<p>Then you can check that there are 11232 matrices of rank 3, and do on. </p>
<pre><code>sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
</code></pre>
<p>All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.</p>
<p><strong>EDIT</strong></p>
<p>If you want to iterate over some particular matrices like matrices of the form <code>matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])</code>, you can use the product <code>F^8</code> of <code>F</code> by itself 8 times using <code>itertools</code>:</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
</code></pre>
<p>Note that in Pyhon (hence Sage) the indices start at 0, not 1.</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54539#post-id-54539thank you so much for helping me you are the best :)Thu, 03 Dec 2020 11:01:45 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54539#post-id-54539Comment by smokzeta for <p>First, you can define the finite with 3 elements as follows:</p>
<pre><code>sage: F = GF(3)
sage: F
Finite Field of size 3
</code></pre>
<p>The very cool thing in Sage is that you can define <em>parents</em> (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field <code>F</code>:</p>
<pre><code>sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
</code></pre>
<p>Then we can look at the list of possible matrices (warning, the following produces a large output):</p>
<pre><code>sage: list(M)
</code></pre>
<p>To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
</code></pre>
<p>Then you can check that there are 11232 matrices of rank 3, and do on. </p>
<pre><code>sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
</code></pre>
<p>All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.</p>
<p><strong>EDIT</strong></p>
<p>If you want to iterate over some particular matrices like matrices of the form <code>matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])</code>, you can use the product <code>F^8</code> of <code>F</code> by itself 8 times using <code>itertools</code>:</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
</code></pre>
<p>Note that in Pyhon (hence Sage) the indices start at 0, not 1.</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54528#post-id-54528Thank you for answering my question. I am trying to count them on modules like
M=matrix([[x1,x2],[x3,-x1]]) or M=matrix([x1,x2,x3],[x4,x5,x6],[x7,x8,-x1-x5]])
Is there a way where I can put the variables and make sage run them through GF(p)Wed, 02 Dec 2020 20:21:13 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54528#post-id-54528Comment by John Palmieri for <p>First, you can define the finite with 3 elements as follows:</p>
<pre><code>sage: F = GF(3)
sage: F
Finite Field of size 3
</code></pre>
<p>The very cool thing in Sage is that you can define <em>parents</em> (check the doc) and iterate over its elements. So, we can construct the matrix space of 3 by 3 matrices over the field <code>F</code>:</p>
<pre><code>sage: M = MatrixSpace(F,3,3)
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Finite Field of size 3
</code></pre>
<p>Then we can look at the list of possible matrices (warning, the following produces a large output):</p>
<pre><code>sage: list(M)
</code></pre>
<p>To count the number of matrices per rank, you can use a counter and iterate over all matrices (by incrementing the entry of the counter that corresponds to the rank of the matrix):</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: for m in M:
....: c[m.rank()] += 1
</code></pre>
<p>Then you can check that there are 11232 matrices of rank 3, and do on. </p>
<pre><code>sage: c
Counter({3: 11232, 2: 8112, 1: 338, 0: 1})
sage: c[2]
8112
</code></pre>
<p>All that said, note that there are formulas to provide such numbers without having to iterate over all matrices (since at least [Landsberg 1893]), but i guess this was not the question.</p>
<p><strong>EDIT</strong></p>
<p>If you want to iterate over some particular matrices like matrices of the form <code>matrix([[x1, x2, x3],[ x4,-x1, x5],[x6,x7,x8]])</code>, you can use the product <code>F^8</code> of <code>F</code> by itself 8 times using <code>itertools</code>:</p>
<pre><code>sage: from collections import Counter
sage: c = Counter()
sage: from itertools import product
sage: P = product(F,repeat=8)
sage: for x in P:
....: c[matrix([[x[0], x[1], x[2]],[x[3],-x[0], x[4]],[x[5],x[6],x[7]]]).rank()] += 1
sage: c
Counter({3: 3780, 2: 2658, 1: 122, 0: 1})
</code></pre>
<p>Note that in Pyhon (hence Sage) the indices start at 0, not 1.</p>
https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54544#post-id-54544@tmonteil: good point, maybe it's a little better to use `L = (matrix(2, 2, [x, y, z, -x]) for x in GF(3) for y in GF(3) for z in GF(3))` instead. (Parentheses instead of brackets to get a generator object.)Thu, 03 Dec 2020 19:39:41 +0100https://ask.sagemath.org/question/54520/counting-number-of-matrices-in-finite-field/?comment=54544#post-id-54544