@@ -12,27 +12,31 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str:
1212 ...
1313 ValueError: The program cannot work out a fitting polynomial.
1414 >>> points_to_polynomial([[1, 0], [2, 0], [3, 0]])
15- 'f(x)=x^2*0.0+x^1*- 0.0+x^0*0.0'
15+ 'f(x)=x^2*0.0+x^1*0.0+x^0*0.0'
1616 >>> points_to_polynomial([[1, 1], [2, 1], [3, 1]])
17- 'f(x)=x^2*0.0+x^1*- 0.0+x^0*1.0'
17+ 'f(x)=x^2*0.0+x^1*0.0+x^0*1.0'
1818 >>> points_to_polynomial([[1, 3], [2, 3], [3, 3]])
19- 'f(x)=x^2*0.0+x^1*- 0.0+x^0*3.0'
19+ 'f(x)=x^2*0.0+x^1*0.0+x^0*3.0'
2020 >>> points_to_polynomial([[1, 1], [2, 2], [3, 3]])
21- 'f(x)=x^2*0.0 +x^1*1.0+x^0*0.0'
21+ 'f(x)=x^2*4.9343245538895844e-17 +x^1*1.0+x^0*0.0'
2222 >>> points_to_polynomial([[1, 1], [2, 4], [3, 9]])
23- 'f(x)=x^2*1.0+x^1*- 0.0+x^0*0.0'
23+ 'f(x)=x^2*1.0+x^1*0.0+x^0*0.0'
2424 >>> points_to_polynomial([[1, 3], [2, 6], [3, 11]])
25- 'f(x)=x^2*1.0 +x^1*-0.0 +x^0*2.0 '
25+ 'f(x)=x^2*0.9999999999999996 +x^1*9.992007221626407e-16 +x^0*1.9999999999999993 '
2626 >>> points_to_polynomial([[1, -3], [2, -6], [3, -11]])
27- 'f(x)=x^2*-1.0 +x^1*-0.0 +x^0*-2.0 '
27+ 'f(x)=x^2*-0.9999999999999996 +x^1*-9.992007221626407e-16 +x^0*-1.9999999999999993 '
2828 >>> points_to_polynomial([[1, 5], [2, 2], [3, 9]])
29- 'f(x)=x^2*5.0+x^1*-18.0 +x^0*18.0 '
29+ 'f(x)=x^2*5.0+x^1*-18.000000000000004 +x^0*18.000000000000004 '
3030 >>> points_to_polynomial([[1, 1], [1, 2], [1, 3]])
3131 'x=1'
3232 >>> points_to_polynomial([[1, 1], [2, 2], [2, 2]])
3333 Traceback (most recent call last):
3434 ...
3535 ValueError: The program cannot work out a fitting polynomial.
36+ >>> points_to_polynomial([[0, 1], [1, 2], [2, 5]])
37+ 'f(x)=x^2*1.0+x^1*0.0+x^0*1.0'
38+ >>> points_to_polynomial([[0, 0], [1, 1], [2, 4]])
39+ 'f(x)=x^2*1.0+x^1*0.0+x^0*0.0'
3640 """
3741 if len (coordinates ) == 0 or not all (len (pair ) == 2 for pair in coordinates ):
3842 raise ValueError ("The program cannot work out a fitting polynomial." )
@@ -62,6 +66,12 @@ def points_to_polynomial(coordinates: list[list[int]]) -> str:
6266 vector : list [float ] = [coordinates [count_of_line ][1 ] for count_of_line in range (x )]
6367
6468 for count in range (x ):
69+
70+ # Partial pivoting: swap in the row with the largest absolute pivot value
71+ max_row = max (range (count , x ), key = lambda r : abs (matrix [r ][count ]))
72+ matrix [count ], matrix [max_row ] = matrix [max_row ], matrix [count ]
73+ vector [count ], vector [max_row ] = vector [max_row ], vector [count ]
74+
6575 for number in range (x ):
6676 if count == number :
6777 continue
0 commit comments