method
determinant
v1_9_3_125 -
Show latest stable
- Class:
Matrix
determinant()public
Returns the determinant of the matrix.
Beware that using Float values can yield erroneous results because of their lack of precision. Consider using exact types like Rational or BigDecimal instead.
Matrix[[7,6], [3,9]].determinant => 45
# File lib/matrix.rb, line 1006
def determinant
Matrix.Raise ErrDimensionMismatch unless square?
m = @rows
case row_size
# Up to 4x4, give result using Laplacian expansion by minors.
# This will typically be faster, as well as giving good results
# in case of Floats
when 0
+1
when 1
+ m[0][0]
when 2
+ m[0][0] * m[1][1] - m[0][1] * m[1][0]
when 3
m0, m1, m2 = m
+ m0[0] * m1[1] * m2[2] - m0[0] * m1[2] * m2[1] - m0[1] * m1[0] * m2[2] + m0[1] * m1[2] * m2[0] + m0[2] * m1[0] * m2[1] - m0[2] * m1[1] * m2[0]
when 4
m0, m1, m2, m3 = m
+ m0[0] * m1[1] * m2[2] * m3[3] - m0[0] * m1[1] * m2[3] * m3[2] - m0[0] * m1[2] * m2[1] * m3[3] + m0[0] * m1[2] * m2[3] * m3[1] + m0[0] * m1[3] * m2[1] * m3[2] - m0[0] * m1[3] * m2[2] * m3[1] - m0[1] * m1[0] * m2[2] * m3[3] + m0[1] * m1[0] * m2[3] * m3[2] + m0[1] * m1[2] * m2[0] * m3[3] - m0[1] * m1[2] * m2[3] * m3[0] - m0[1] * m1[3] * m2[0] * m3[2] + m0[1] * m1[3] * m2[2] * m3[0] + m0[2] * m1[0] * m2[1] * m3[3] - m0[2] * m1[0] * m2[3] * m3[1] - m0[2] * m1[1] * m2[0] * m3[3] + m0[2] * m1[1] * m2[3] * m3[0] + m0[2] * m1[3] * m2[0] * m3[1] - m0[2] * m1[3] * m2[1] * m3[0] - m0[3] * m1[0] * m2[1] * m3[2] + m0[3] * m1[0] * m2[2] * m3[1] + m0[3] * m1[1] * m2[0] * m3[2] - m0[3] * m1[1] * m2[2] * m3[0] - m0[3] * m1[2] * m2[0] * m3[1] + m0[3] * m1[2] * m2[1] * m3[0]
else
# For bigger matrices, use an efficient and general algorithm.
# Currently, we use the Gauss-Bareiss algorithm
determinant_bareiss
end
end Related methods
- Instance methods
- *
- **
- +
- -
- /
- ==
- []
- clone
- coerce
- collect
- column
- column_vectors
- component
- conj
- conjugate
- det
- det_e
- determinant
- determinant_e
- diagonal?
- each
- each_with_index
- eigen
- eigensystem
- element
- elements_to_f
- elements_to_i
- elements_to_r
- empty?
- eql?
- find_index
- hash
- hermitian?
- imag
- imaginary
- index
- inspect
- inv
- inverse
- lower_triangular?
- lup
- lup_decomposition
- map
- minor
- normal?
- orthogonal?
- permutation?
- rank
- rank_e
- real
- real?
- rect
- rectangular
- regular?
- round
- row
- row_size
- row_vectors
- singular?
- square?
- symmetric?
- t
- to_a
- to_s
- tr
- trace
- transpose
- unitary?
- upper_triangular?
- zero?
- Class methods
- I
- []
- build
- column_vector
- columns
- diagonal
- empty
- identity
- new
- row_vector
- rows
- scalar
- unit
- zero
- Private methods
-
new -
[]= -
determinant_bareiss -
inverse_from -
new_matrix -
set_component -
set_element