Skip to content

Instantly share code, notes, and snippets.

@dionsaputra
Created November 22, 2020 15:52
Show Gist options
  • Save dionsaputra/1db5fc8677b07c840fd304eef9cd5896 to your computer and use it in GitHub Desktop.
Save dionsaputra/1db5fc8677b07c840fd304eef9cd5896 to your computer and use it in GitHub Desktop.
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