Workhorse
Workhorse

Reputation: 1560

Numpy Matrix Determinant Not Working as Expected?

I have a question in which I am asked to show that the determinant of matrix B equals 0. Matrix B is defined as:

import numpy as np
from numpy import linalg as m

B = np.array([[-1-3.j,-8-10.j,0-3.j], 
                [-7-3.j,-4-9.j,-3-2.j], 
                [11-3.j,-16-12.j,6-5.j] 
              ])
print(B)
[[ -1. -3.j  -8.-10.j   0. -3.j]
 [ -7. -3.j  -4. -9.j  -3. -2.j]
 [ 11. -3.j -16.-12.j   6. -5.j]]

The determinant is straightforward using numpy

m.linalg.det(B)
(-8.126832540256171e-14-1.5987211554602298e-14j)

Which is clearly not equal to zero.

I double checked my answer using https://www.symbolab.com/ and the determinant is definitely zero.

I feel like I am doing something ridiculously silly, but can't quite figure out what. Any help?

Upvotes: 0

Views: 1450

Answers (2)

dmuir
dmuir

Reputation: 4431

To expand a little on Nils answer:

There are various ways to compute determinants. The way taught in algebra classes -- laplace expansion -- is a reasonable way to go for small (eg 3 x 3) matrices, but rapidly becomes impossible -- because of the number of computations required -- for larger matrices.

In your case, where all the real and imaginary parts are small integers, such a computation would evaluate the determinant exactly, as 0.

In python linalg.det uses a different approach, where you factorise the matrix into factors -- triangular matrices and permutations -- whose determinants can easily be computed, and then the determinant of the product is the product of the determinants of the factors. This is a order N cubed computation, and so can be used for even quite large matrices.

However such factorisations are (a little) inaccurate; the original matrix will not be exactly equal to the product. Thus the determinant will also be, most likely, a little inaccurate.

Upvotes: 0

Nils Werner
Nils Werner

Reputation: 36765

What you're seeing are really tiny numbers that are almost equal to zero. They're not exactly equal to zero only due to numerical inaccuracies.

That's why we're usually not testing them for equality but for closeness

np.allclose(np.linalg.det(B), 0). # True

Upvotes: 2

Related Questions