From 77a71a4f33b7ec1a0307c401b1ebda7f0e9d2572 Mon Sep 17 00:00:00 2001 From: Muhammad Zain Ul Abidin Date: Fri, 19 Jun 2026 15:49:16 +0500 Subject: [PATCH 1/2] Fix ZeroDivisionError in polynom_for_points by adding partial pivoting --- linear_algebra/src/polynom_for_points.py | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/linear_algebra/src/polynom_for_points.py b/linear_algebra/src/polynom_for_points.py index 452f3edd4aee..afcc4c8a5a44 100644 --- a/linear_algebra/src/polynom_for_points.py +++ b/linear_algebra/src/polynom_for_points.py @@ -12,27 +12,31 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str: ... ValueError: The program cannot work out a fitting polynomial. >>> points_to_polynomial([[1, 0], [2, 0], [3, 0]]) - 'f(x)=x^2*0.0+x^1*-0.0+x^0*0.0' + 'f(x)=x^2*0.0+x^1*0.0+x^0*0.0' >>> points_to_polynomial([[1, 1], [2, 1], [3, 1]]) - 'f(x)=x^2*0.0+x^1*-0.0+x^0*1.0' + 'f(x)=x^2*0.0+x^1*0.0+x^0*1.0' >>> points_to_polynomial([[1, 3], [2, 3], [3, 3]]) - 'f(x)=x^2*0.0+x^1*-0.0+x^0*3.0' + 'f(x)=x^2*0.0+x^1*0.0+x^0*3.0' >>> points_to_polynomial([[1, 1], [2, 2], [3, 3]]) - 'f(x)=x^2*0.0+x^1*1.0+x^0*0.0' + 'f(x)=x^2*4.9343245538895844e-17+x^1*1.0+x^0*0.0' >>> points_to_polynomial([[1, 1], [2, 4], [3, 9]]) - 'f(x)=x^2*1.0+x^1*-0.0+x^0*0.0' + 'f(x)=x^2*1.0+x^1*0.0+x^0*0.0' >>> points_to_polynomial([[1, 3], [2, 6], [3, 11]]) - 'f(x)=x^2*1.0+x^1*-0.0+x^0*2.0' + 'f(x)=x^2*0.9999999999999996+x^1*9.992007221626407e-16+x^0*1.9999999999999993' >>> points_to_polynomial([[1, -3], [2, -6], [3, -11]]) - 'f(x)=x^2*-1.0+x^1*-0.0+x^0*-2.0' + 'f(x)=x^2*-0.9999999999999996+x^1*-9.992007221626407e-16+x^0*-1.9999999999999993' >>> points_to_polynomial([[1, 5], [2, 2], [3, 9]]) - 'f(x)=x^2*5.0+x^1*-18.0+x^0*18.0' + 'f(x)=x^2*5.0+x^1*-18.000000000000004+x^0*18.000000000000004' >>> points_to_polynomial([[1, 1], [1, 2], [1, 3]]) 'x=1' >>> points_to_polynomial([[1, 1], [2, 2], [2, 2]]) Traceback (most recent call last): ... ValueError: The program cannot work out a fitting polynomial. + >>> points_to_polynomial([[0, 1], [1, 2], [2, 5]]) + 'f(x)=x^2*1.0+x^1*0.0+x^0*1.0' + >>> points_to_polynomial([[0, 0], [1, 1], [2, 4]]) + 'f(x)=x^2*1.0+x^1*0.0+x^0*0.0' """ if len(coordinates) == 0 or not all(len(pair) == 2 for pair in coordinates): raise ValueError("The program cannot work out a fitting polynomial.") @@ -62,6 +66,12 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str: vector: list[float] = [coordinates[count_of_line][1] for count_of_line in range(x)] for count in range(x): + + # Partial pivoting: swap in the row with the largest absolute pivot value + max_row = max(range(count, x), key=lambda r: abs(matrix[r][count])) + matrix[count], matrix[max_row] = matrix[max_row], matrix[count] + vector[count], vector[max_row] = vector[max_row], vector[count] + for number in range(x): if count == number: continue From 6df8dda1f5e68a819a0c81be4559df09f6162e58 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Jun 2026 14:11:27 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/src/polynom_for_points.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/linear_algebra/src/polynom_for_points.py b/linear_algebra/src/polynom_for_points.py index afcc4c8a5a44..a8091bd27d34 100644 --- a/linear_algebra/src/polynom_for_points.py +++ b/linear_algebra/src/polynom_for_points.py @@ -66,12 +66,11 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str: vector: list[float] = [coordinates[count_of_line][1] for count_of_line in range(x)] for count in range(x): - # Partial pivoting: swap in the row with the largest absolute pivot value max_row = max(range(count, x), key=lambda r: abs(matrix[r][count])) matrix[count], matrix[max_row] = matrix[max_row], matrix[count] vector[count], vector[max_row] = vector[max_row], vector[count] - + for number in range(x): if count == number: continue