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

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

enter image description here


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

pretty-printed matrices


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

printed matrices with modulo 2 restriction


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


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)