title: Finding all invertible square matrices
body: <p>I want to write a function where given the two integers "n" and "p", it generates all the invertible matrices of order n where the elements come from {0,1,...,p-1}.
  I have the following code :</p>
  
  <pre><code>import itertools 
  import numpy as np 
  
  def invertible_matrices(n, p):
      invertibleMatrices = set()
      # generates all the possible matrices
      x = [y for y in range(p)]
      a = [j for j in itertools.product(x, repeat=n)]
      b = {k for k in itertools.product(a, repeat=n)}
      for each in b:
          if np.linalg.det(each) != 0:
              invertibleMatrices.add(each)
      return invertibleMatrices
  </code></pre>
  
  <p>for <code>n=2</code> and <code>p=2</code> it works fine but for <code>n=2</code> and <code>p=3</code> I get <code>50</code> while the answer is <code>48</code>.
  any help would be appreciated.</p>
  
  <p>p.s: if you're familiar with group theory I'm trying to find all the elements of <a href="https://en.wikipedia.org/wiki/General_linear_group#Over_finite_fields" rel="nofollow noreferrer">GL(n, p)</a> (the general linear group over a finite field with p elements)</p>