title: Speed up python code for computing matrix cofactors
body: <p>As part of a complex task, I need to compute <a href="http://en.wikipedia.org/wiki/Cofactor_%28linear_algebra%29" rel="nofollow noreferrer">matrix cofactors</a>.  I did this in a straightforward way using this <a href="https://stackoverflow.com/questions/3858213/numpy-routine-for-computing-matrix-minors">nice code for computing matrix minors</a>.  Here is my code:</p>
  
  <pre><code>def matrix_cofactor(matrix):
      C = np.zeros(matrix.shape)
      nrows, ncols = C.shape
      for row in xrange(nrows):
          for col in xrange(ncols):
              minor = matrix[np.array(range(row)+range(row+1,nrows))[:,np.newaxis],
                             np.array(range(col)+range(col+1,ncols))]
              C[row, col] = (-1)**(row+col) * np.linalg.det(minor)
      return C
  </code></pre>
  
  <p>It turns out that this matrix cofactor code is the bottleneck, and I would like to optimize the code snippet above.  Any ideas as to how to do this?</p>