-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathcholesky_matrix_decomposition.py
More file actions
48 lines (38 loc) · 1.46 KB
/
cholesky_matrix_decomposition.py
File metadata and controls
48 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Cholesky Matrix Decomposition
Decompose a Hermitian positive-definite matrix A into a lower-triangular
matrix V such that V * V^T = A. Mainly used for numerical solution of
linear equations Ax = b.
Reference: https://en.wikipedia.org/wiki/Cholesky_decomposition
Complexity:
Time: O(n^3)
Space: O(n^2)
"""
from __future__ import annotations
import math
def cholesky_decomposition(matrix: list[list[float]]) -> list[list[float]] | None:
"""Compute the Cholesky decomposition of a positive-definite matrix.
Args:
matrix: Hermitian positive-definite matrix (n x n).
Returns:
Lower-triangular matrix V such that V * V^T = matrix,
or None if the matrix cannot be decomposed.
Examples:
>>> cholesky_decomposition([[4, 12, -16], [12, 37, -43], [-16, -43, 98]])
[[2.0, 0.0, 0.0], [6.0, 1.0, 0.0], [-8.0, 5.0, 3.0]]
"""
size = len(matrix)
for row in matrix:
if len(row) != size:
return None
result = [[0.0] * size for _ in range(size)]
for j in range(size):
diagonal_sum = sum(result[j][k] ** 2 for k in range(j))
diagonal_sum = matrix[j][j] - diagonal_sum
if diagonal_sum <= 0:
return None
result[j][j] = math.sqrt(diagonal_sum)
for i in range(j + 1, size):
off_diagonal_sum = sum(result[i][k] * result[j][k] for k in range(j))
result[i][j] = (matrix[i][j] - off_diagonal_sum) / result[j][j]
return result