# Generating all 2x2 matrices with entries from 0 to 3 whose det is 1 with mod 2 arithmetic

I am trying to make a list of 2x2 matrices whose entries are all possible permutations of 0,1,2 and det=1 mod 2 arithmetic.

First I tried to create a Table like this

T1 = Table[{{i, j}, {m, n}}, {i, 0, 2}, {j, 0, 2}, {n, 0, 2},{m,0,2}]


But how to extract it as a list of matrices in MatrixForm and including the condition of determinant one?

• I saw that you changed the requirement for the determinant. I added that option to my answer. – MarcoB Dec 10 '19 at 16:19

Using your Table expression, you will want to Flatten the results to obtain a list of matrices, rather than nested lists:

t1 = Table[{{i, j}, {m, n}}, {i, 0, 2}, {j, 0, 2}, {n, 0, 2}, {m, 0, 2}]~Flatten~3;


The same thing can be obtained more directly using Tuples:

t1 = Tuples[Range[0, 2], {2, 2}]


You will then Select those matrices that have a unity determinant and apply MatrixForm to them individually using Map:

MatrixForm /@ Select[t1, Det[#] == 1 &] The question was recently changed to specify that the determinant should be 1 when considered modulo 2. This can be included as well:

MatrixForm /@ Select[t1, Mod[Det[#], 2] == 1 &] It is worth it to add the usual caveat: MatrixForm is a formatting wrapper that pretty-prints your matrices, but it will hinder further evaluation; see Why does MatrixForm affect calculations?.

• Cool, didn't know about the Tuples[...,{2,2}] form to directly obtain the matrices, nice! – Thies Heidecke Dec 10 '19 at 15:25
• @Thies Yeah, it's pretty handy! – MarcoB Dec 10 '19 at 15:27
n = 2;
sol = Solve[{Mod[a d - b c, 2] == 1,
And @@ Thread[0 <= {a, b, c, d} <= n]}, {a, b, c, d}, Integers];

solutions = {{a, b}, {c, d}} /. sol;

Length @ solutions


16

TeXForm @ Grid[Partition[MatrixForm /@ solutions, 4]]


$$\begin{array}{cccc} \left( \begin{array}{cc} 0 & 1 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 1 \\ 1 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 1 \\ 1 & 2 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ \end{array} \right) \\ \left( \begin{array}{cc} 1 & 0 \\ 1 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 0 \\ 2 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 1 & 0 \\ \end{array} \right) \\ \left( \begin{array}{cc} 1 & 1 \\ 1 & 2 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 2 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 2 \\ 0 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 2 \\ 1 & 1 \\ \end{array} \right) \\ \left( \begin{array}{cc} 1 & 2 \\ 2 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 2 & 1 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 2 & 1 \\ 1 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 2 & 1 \\ 1 & 2 \\ \end{array} \right) \\ \end{array}$$

For n = 3 we get

Length @ solutions


96

Grid[Partition[MatrixForm /@ solutions, 12]] • Can you please include the mod 2 condition also? I mean to say the det one condition is in modular arithmetic 2. Thanks – ricci1729 Dec 10 '19 at 16:05
• @ricci1729, please see the updated version. – kglr Dec 10 '19 at 16:18
• Thanks a lot for your help. One last question .. How to find the number of the total matrics? I used Length but it did not give the result. Please let me know. – ricci1729 Dec 10 '19 at 16:21
• Length @ sol or Length @ solutions – kglr Dec 10 '19 at 16:26

As long as you don't deal with big matrices or big ranges of possible integer entries, i.e. you don't have to fear combinatorial explosion, you can just brute force generate all of them, and then select the proper ones via Select:

matrices = With[{n = 2, legalmatrixentries = Range[0, 2]},
Select[
Tuples[legalmatrixentries, {n,n}],
OddQ @* Det (* (Mod[Det[#], k] == 1 &) for general modular arithmetic mod k *)
]
]
MatrixForm /@ matrices


$$\left\{\left( \begin{array}{cc} 0 & 1 \\ 1 & 0 \\ \end{array} \right),\left( \begin{array}{cc} 0 & 1 \\ 1 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 0 & 1 \\ 1 & 2 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 0 \\ 1 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 0 \\ 2 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 1 \\ 1 & 0 \\ \end{array} \right),\\\left( \begin{array}{cc} 1 & 1 \\ 1 & 2 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 1 \\ 2 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 2 \\ 0 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 2 \\ 1 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 1 & 2 \\ 2 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 2 & 1 \\ 1 & 0 \\ \end{array} \right),\left( \begin{array}{cc} 2 & 1 \\ 1 & 1 \\ \end{array} \right),\left( \begin{array}{cc} 2 & 1 \\ 1 & 2 \\ \end{array} \right)\right\}$$

Related integer sequences:

• Nr. of solutions for the mod2 case: A210370
• Nr. of solutions for the non mod2 case: A171503

# Updated

• Updated answer to give solutions for modular arithmetic (mod 2)
• You could give multiple dimensions to Tuples to get arrays directly, e.g. Tuples[legalmatrixentries, {n, n}], so you can avoid the Partition. – MarcoB Dec 10 '19 at 15:25
• wrong range btw – Alucard Dec 10 '19 at 15:26
• @MarcoB thanks for pointing that out, didn't know about it, i like! thanks! – Thies Heidecke Dec 10 '19 at 15:28
• @Alucard The title and the code example in the OP are not matching, so i just picked the example from the title. – Thies Heidecke Dec 10 '19 at 15:28
• Thanks a lot. Actually I did a little mistake while making the question... I wish to make the list of matrices with modular arithmetic 2 along with det one condition. I tried Modulus==2 & but it didn't work. Please let me how to incorporate the additional condition. Thanks a lot. – ricci1729 Dec 10 '19 at 15:49