improve solution to exercise 82

The current solution uses a fixed arbitrary threshold of 1e-10 on the
singular values, I replaced it with the common heuristic n*eps*max(s).
This commit is contained in:
Gattocrucco
2025-08-25 18:06:25 +02:00
parent 9be9c8cd23
commit fca297a6e1
3 changed files with 6 additions and 3 deletions

View File

@@ -1225,7 +1225,8 @@ hint: np.linalg.svd, np.linalg.matrix_rank
Z = np.random.uniform(0,1,(10,10))
U, S, V = np.linalg.svd(Z) # Singular Value Decomposition
rank = np.sum(S > 1e-10)
threshold = len(S) * S.max() * np.finfo(S.dtype).eps
rank = np.sum(S > threshold)
print(rank)
# alternative solution: