Created
November 22, 2020 15:52
-
-
Save dionsaputra/1db5fc8677b07c840fd304eef9cd5896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun determinant(matrix: Matrix): Double { | |
require(matrix.rows == matrix.cols) | |
if (matrix.rows == 1) return matrix[0,0] | |
if (matrix.rows == 2) return ( | |
matrix[0,0] * matrix[1,1] - | |
matrix[0,1] * matrix[1,0] | |
) | |
var det = 0.0 | |
for (c in 0 until matrix.cols) { | |
var incValue = 1.0 | |
var decValue = 1.0 | |
for (r in 0 until matrix.rows) { | |
incValue *= matrix[r, (c+r) % matrix.cols] | |
decValue *= matrix[matrix.rows-r-1, (c+r) % matrix.cols] | |
} | |
det += (incValue - decValue) | |
} | |
return det | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment