Ask Your Question

counting number of matrices in finite field

asked 2020-12-02 16:21:39 +0200

smokzeta gravatar image

updated 2020-12-02 18:30:03 +0200

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/3Z

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-02 20:03:37 +0200

tmonteil gravatar image

updated 2020-12-03 10:17:21 +0200

First, 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]

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.


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.

edit flag offensive delete link more


Thank 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)

smokzeta gravatar imagesmokzeta ( 2020-12-02 20:21:13 +0200 )edit

How 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)]?

John Palmieri gravatar imageJohn Palmieri ( 2020-12-03 07:07:08 +0200 )edit

@john-palmieri storing all matrices in a list might cost some memory (unless you plan to reuse them), you should better iterate over them.

tmonteil gravatar imagetmonteil ( 2020-12-03 10:18:44 +0200 )edit

@smokzeta i edited my answer to answer your question, i did not see the -x1 and though you want to iterate over all matrices.

tmonteil gravatar imagetmonteil ( 2020-12-03 10:22:24 +0200 )edit

thank you so much for helping me you are the best :)

smokzeta gravatar imagesmokzeta ( 2020-12-03 11:01:45 +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


Asked: 2020-12-02 16:21:39 +0200

Seen: 80 times

Last updated: Dec 03 '20