Skip to content

Instantly share code, notes, and snippets.

@felipecruz
Created November 24, 2016 01:09
Show Gist options
  • Save felipecruz/cd86e4e03b55f9c9c9225014b8ccefdf to your computer and use it in GitHub Desktop.
Save felipecruz/cd86e4e03b55f9c9c9225014b8ccefdf to your computer and use it in GitHub Desktop.
func MatrixCholeskyDecomposition(matrix Matrix) Matrix {
columns := matrix.cols()
rows := matrix.rows()
result_matrix := DenseZeros(rows, columns)
var val float64
for i := 0; i < rows; i++ {
for j := 0; j <= i; j++ {
accumulator := 0.
for k := 0; k < j; k++ {
accumulator += result_matrix.at(i, k) * result_matrix.at(j, k)
}
if i == j {
val = math.Sqrt(matrix.at(i, j) - accumulator)
} else if j < i {
val = (matrix.at(i, j) - accumulator) / result_matrix.at(j, j)
}
result_matrix.set(i, j, val)
}
}
return result_matrix
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment