Method: NuLin.det

Defined in:
lib/nulin/det.rb

.det(matrix) ⇒ Object

Compute the determinant of the ‘matrix` and return it.

You get an exception NuLin::DimensionError if the matrix is not square The determinant of given matrix is computed using QR factorization.

Parameters:

  • matrix (NMatrix)

    target matrix



9
10
11
12
13
14
15
16
# File 'lib/nulin/det.rb', line 9

def det(matrix)
  unless matrix.square?
    raise DimensionError, "Determinant is computable only on a square matrix"
  end
  n, = matrix.shape
  r = qr(matrix).R
  (0 ... n).inject(1.0){|u, i| u*r[i,i] }
end