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]
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.