From fca297a6e1684fec92906e93b6598ae9c89b1d15 Mon Sep 17 00:00:00 2001 From: Gattocrucco Date: Mon, 25 Aug 2025 18:06:25 +0200 Subject: [PATCH] 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). --- 100_Numpy_exercises_with_hints_with_solutions.md | 3 ++- 100_Numpy_exercises_with_solutions.md | 3 ++- source/exercises100.ktx | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 409c00f..633ef1f 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -993,7 +993,8 @@ print(sliding_window_view(Z, window_shape=4)) 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: diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index c7285d2..352c272 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -993,7 +993,8 @@ print(sliding_window_view(Z, window_shape=4)) 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: diff --git a/source/exercises100.ktx b/source/exercises100.ktx index d1457aa..4ba6b53 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -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: