Determinant by Pivots: A Python Algorithm

Implement an algorithm to compute determinants in Python.

Formulas for determinant

There are three formulas for calculating determinants.

  • Determinant by pivots
  • Determinant by permutations
  • Determinant by cofactors and minors

Determinant by pivots

The pivot formula we’ll implement in this lesson is based on the property that the determinant of a triangular matrix is the product of its diagonal entries.

det(A)=a11,a22,,anndet(A)= \prod a_{11},a_{22},\cdots,a_{nn}

Elimination converts a matrix to an echelon matrix (triangular matrix). Every non-zero entry on the diagonal of the echelon matrix is the pivot. A diagonal entry equaling zero implies the following:

  • The matrix has dependent columns. That is, the matrix is singular.
  • The product of the diagonal entries is zero. That is, the determinant is zero.

The operations involved in elimination preserve the solution set system. However, not all operations preserve the determinant. As explained in the previous lesson, row sum has no effect on the determinant of the matrix. However, row swap and row scaling operations change the determinant. With each row swap operation, the sign of the determinant is toggled, whereas with every row scaling operation, the determinant is also scaled proportionally. If we track these changes during elimination, we can write the pivot formula as follows:

det(A)=P×a11,a22,,anndet(A) = P \times \prod a_{11},a_{22},\cdots,a_{nn}

where PP is the number containing all sign reversals and scaling factors. For computing PP, we could keep an integer variable that’s initialized with 11, multiplied by 1-1 for each row swap, and multiplied by a factor, ff, for each row scale.

The np.linalg.det() Python function doesn’t rely on elimination to convert a matrix to a triangular form. For square matrices, we could convert them to their triangular matrix by using only the row sum operation. Let’s look at an example of this:

Get hands-on with 1200+ tech skills courses.