From eff2da82049189c4d90d73520733db77d58b3cca Mon Sep 17 00:00:00 2001 From: nic_dern Date: Sun, 11 Aug 2019 10:55:45 +0200 Subject: [PATCH 01/10] Added new algorithm which takes points as an input and outputs a polynom connecting them --- .../src/Python-Polynom-for-points.py | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 linear_algebra/src/Python-Polynom-for-points.py diff --git a/linear_algebra/src/Python-Polynom-for-points.py b/linear_algebra/src/Python-Polynom-for-points.py new file mode 100644 index 000000000000..8fd79d1912f8 --- /dev/null +++ b/linear_algebra/src/Python-Polynom-for-points.py @@ -0,0 +1,139 @@ +from decimal import * +getcontext() +getcontext().prec=28 + +count=0 +check=1 +zero_y=[] + +#number of points you want to use +try: + x=int(input("number of points: ").strip()) +except ValueError: + check=3 + +if check==1: + try: + #getting coordinates of the points and putting them into a basic array + coordinates=[] + while count < x: + xkoo=Decimal(input("x-koordinate: ").strip()) + ykoo=Decimal(input("y-koordinate: ").strip()) + if xkoo==0: + zero_y.append(ykoo) + else: + coordinates.append([xkoo, ykoo]) + count+=1 + print("\n") + + for i in range(len(zero_y)): + coordinates.append([0, zero_y[i]]) + + more_check=0 + + + d=coordinates[0][0] + for j in range(len(coordinates)): + if j==0: + continue + if d==coordinates[j][0]: + more_check+=1 + solved="x="+str(coordinates[j][0]) + if more_check == len(coordinates)-1: + check=2 + break + elif more_check > 0: + check=3 + else: + check=1 + + if len(coordinates)==1 and coordinates[0][0]==0: + check=2 + solved="x=0" + except IndexError: + check=3 + +if check==1: + print("Let's go!") + print("\n") + + count_of_line=0 + matrix=[] + #put the x and x to the power values in a matrix + while count_of_line < x: + count_in_line=0 + a=coordinates[count_of_line][0] + count_line=[] + while count_in_line < x: + count_line.append(a**(x-(count_in_line+1))) + count_in_line+=1 + matrix.append(count_line) + count_of_line+=1 + print("Generated matrix") + for i in range(len(matrix)): + print(matrix[i]) + print("\n") + + count_of_line=0 + #put the y values into a vector + vector=[] + while count_of_line < x: + count_in_line=0 + vector.append(coordinates[count_of_line][1]) + count_of_line+=1 + print("Generated vector") + print(vector) + print("\n") + + + count=0 + + while count < x: + zahlen=0 + while zahlen < x: + if count==zahlen: + zahlen+=1 + if zahlen==x: + break + bruch=(matrix[zahlen][count])/(matrix[count][count]) + counting_columns=0 + for i in range(len(matrix[count])): + #manipulating all the values in the matrix + matrix[zahlen][counting_columns]-=(matrix[count][i])*bruch + counting_columns+=1 + #manipulating the values in the vector + vector[zahlen]-=vector[count]*bruch + zahlen+=1 + count+=1 + print("Solved matrix") + for i in range(len(matrix)): + print(str(matrix[i])+" ["+str(vector[i])+"]") + print("\n") + + count=0 + #make solutions + solution=[] + while count < x: + solution.append(vector[count]/matrix[count][count]) + count+=1 + print("Solved everything") + print("\n") + + count=0 + solved="f(x)=" + + while count < x: + remove_e=str(solution[count]).split("E") + if len(remove_e) > 1: + solution[count]=remove_e[0]+"*10^"+remove_e[1] + solved+="x^"+ str(x-(count+1))+ "*" +str(solution[count]) + if count+1 != x: + solved+="+" + count+=1 + + print(solved) + +elif check==2: + print(solved) +else: + print("The program can not work out that function yet.") From 0383ba33ab85c9bcb28e4ad47442dcaf8f0a09b0 Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 11:30:13 +0200 Subject: [PATCH 02/10] Rename Python-Polynom-for-points.py to python-polynom-for-points.py --- ...{Python-Polynom-for-points.py => python-polynom-for-points.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename linear_algebra/src/{Python-Polynom-for-points.py => python-polynom-for-points.py} (100%) diff --git a/linear_algebra/src/Python-Polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py similarity index 100% rename from linear_algebra/src/Python-Polynom-for-points.py rename to linear_algebra/src/python-polynom-for-points.py From c918ca5bb10b478ec23205215aebd6657cc0547b Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 18:43:57 +0200 Subject: [PATCH 03/10] Update python-polynom-for-points.py --- .../src/python-polynom-for-points.py | 237 ++++++++---------- 1 file changed, 98 insertions(+), 139 deletions(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index 8fd79d1912f8..7c8c94943d13 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -1,139 +1,98 @@ -from decimal import * -getcontext() -getcontext().prec=28 - -count=0 -check=1 -zero_y=[] - -#number of points you want to use -try: - x=int(input("number of points: ").strip()) -except ValueError: - check=3 - -if check==1: - try: - #getting coordinates of the points and putting them into a basic array - coordinates=[] - while count < x: - xkoo=Decimal(input("x-koordinate: ").strip()) - ykoo=Decimal(input("y-koordinate: ").strip()) - if xkoo==0: - zero_y.append(ykoo) - else: - coordinates.append([xkoo, ykoo]) - count+=1 - print("\n") - - for i in range(len(zero_y)): - coordinates.append([0, zero_y[i]]) - - more_check=0 - - - d=coordinates[0][0] - for j in range(len(coordinates)): - if j==0: - continue - if d==coordinates[j][0]: - more_check+=1 - solved="x="+str(coordinates[j][0]) - if more_check == len(coordinates)-1: - check=2 - break - elif more_check > 0: - check=3 - else: - check=1 - - if len(coordinates)==1 and coordinates[0][0]==0: - check=2 - solved="x=0" - except IndexError: - check=3 - -if check==1: - print("Let's go!") - print("\n") - - count_of_line=0 - matrix=[] - #put the x and x to the power values in a matrix - while count_of_line < x: - count_in_line=0 - a=coordinates[count_of_line][0] - count_line=[] - while count_in_line < x: - count_line.append(a**(x-(count_in_line+1))) - count_in_line+=1 - matrix.append(count_line) - count_of_line+=1 - print("Generated matrix") - for i in range(len(matrix)): - print(matrix[i]) - print("\n") - - count_of_line=0 - #put the y values into a vector - vector=[] - while count_of_line < x: - count_in_line=0 - vector.append(coordinates[count_of_line][1]) - count_of_line+=1 - print("Generated vector") - print(vector) - print("\n") - - - count=0 - - while count < x: - zahlen=0 - while zahlen < x: - if count==zahlen: - zahlen+=1 - if zahlen==x: - break - bruch=(matrix[zahlen][count])/(matrix[count][count]) - counting_columns=0 - for i in range(len(matrix[count])): - #manipulating all the values in the matrix - matrix[zahlen][counting_columns]-=(matrix[count][i])*bruch - counting_columns+=1 - #manipulating the values in the vector - vector[zahlen]-=vector[count]*bruch - zahlen+=1 - count+=1 - print("Solved matrix") - for i in range(len(matrix)): - print(str(matrix[i])+" ["+str(vector[i])+"]") - print("\n") - - count=0 - #make solutions - solution=[] - while count < x: - solution.append(vector[count]/matrix[count][count]) - count+=1 - print("Solved everything") - print("\n") - - count=0 - solved="f(x)=" - - while count < x: - remove_e=str(solution[count]).split("E") - if len(remove_e) > 1: - solution[count]=remove_e[0]+"*10^"+remove_e[1] - solved+="x^"+ str(x-(count+1))+ "*" +str(solution[count]) - if count+1 != x: - solved+="+" - count+=1 - - print(solved) - -elif check==2: - print(solved) -else: - print("The program can not work out that function yet.") +def points_to_polynomial(coordinates[][]): + #the list is two dimensional: [[x, y],[x, y],...] + + from decimal import * + getcontext() + getcontext().prec=28 + + if __name__ == __main__ : + #number of points you want to use + try: + d=coordinates[0][0] + for j in range(len(coordinates)): + if j==0: + continue + if d==coordinates[j][0]: + more_check+=1 + solved="x="+str(coordinates[j][0]) + if more_check == len(coordinates)-1: + check=2 + break + elif more_check > 0 and more_check != len(coordinates)-1: + check=3 + else: + check=1 + + if len(coordinates)==1 and coordinates[0][0]==0: + check=2 + solved="x=0" + except: + check=3 + + if check==1: + count_of_line=0 + matrix=[] + #put the x and x to the power values in a matrix + while count_of_line < x: + count_in_line=0 + a=coordinates[count_of_line][0] + count_line=[] + while count_in_line < x: + count_line.append(a**(x-(count_in_line+1))) + count_in_line+=1 + matrix.append(count_line) + count_of_line+=1 + + count_of_line=0 + #put the y values into a vector + vector=[] + while count_of_line < x: + count_in_line=0 + vector.append(coordinates[count_of_line][1]) + count_of_line+=1 + + count=0 + + while count < x: + zahlen=0 + while zahlen < x: + if count==zahlen: + zahlen+=1 + if zahlen==x: + break + bruch=(matrix[zahlen][count])/(matrix[count][count]) + counting_columns=0 + for i in range(len(matrix[count])): + #manipulating all the values in the matrix + matrix[zahlen][counting_columns]-=(matrix[count][i])*bruch + counting_columns+=1 + #manipulating the values in the vector + vector[zahlen]-=vector[count]*bruch + zahlen+=1 + count+=1 + + count=0 + #make solutions + solution=[] + while count < x: + solution.append(vector[count]/matrix[count][count]) + count+=1 + + count=0 + solved="f(x)=" + + while count < x: + remove_e=str(solution[count]).split("E") + if len(remove_e) > 1: + solution[count]=remove_e[0]+"*10^"+remove_e[1] + solved+="x^"+ str(x-(count+1))+ "*" +str(solution[count]) + if count+1 != x: + solved+="+" + count+=1 + + return solved + + elif check==2: + return solved + else: + return "The program cannot work out a fitting polynomial." From 43351cb0732db607229157fff2f20d74782e0a4e Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 18:45:03 +0200 Subject: [PATCH 04/10] Update python-polynom-for-points.py --- linear_algebra/src/python-polynom-for-points.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index 7c8c94943d13..ce33dee92b44 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -5,7 +5,7 @@ def points_to_polynomial(coordinates[][]): getcontext() getcontext().prec=28 - if __name__ == __main__ : + if __name__ == "__main__" : #number of points you want to use try: d=coordinates[0][0] From fe8235584fca59c2f8371f916e0c5dccd34b7ec3 Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 18:50:06 +0200 Subject: [PATCH 05/10] Update python-polynom-for-points.py --- linear_algebra/src/python-polynom-for-points.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index ce33dee92b44..37bc4d874c59 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -1,5 +1,5 @@ -def points_to_polynomial(coordinates[][]): - #the list is two dimensional: [[x, y],[x, y],...] +def points_to_polynomial(coordinates): + #the list (coordinates) is two dimensional: [[x, y],[x, y],...] from decimal import * getcontext() From fc58a56f34c5574b091aac0514436551a2b73d7c Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 18:59:24 +0200 Subject: [PATCH 06/10] Update python-polynom-for-points.py --- linear_algebra/src/python-polynom-for-points.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index 37bc4d874c59..be2c66429daa 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -1,13 +1,15 @@ + +from decimal import * +getcontext().prec=28 + + def points_to_polynomial(coordinates): #the list (coordinates) is two dimensional: [[x, y],[x, y],...] - - from decimal import * - getcontext() - getcontext().prec=28 if __name__ == "__main__" : #number of points you want to use try: + more_check=0 d=coordinates[0][0] for j in range(len(coordinates)): if j==0: @@ -28,7 +30,10 @@ def points_to_polynomial(coordinates): solved="x=0" except: check=3 - + + + x=len(coordinates) + if check==1: count_of_line=0 matrix=[] From f4cdeffcdb6b7ec0cba23abbcaed9fe6c5871c89 Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 21:25:00 +0200 Subject: [PATCH 07/10] Update python-polynom-for-points.py --- .../src/python-polynom-for-points.py | 195 +++++++++--------- 1 file changed, 100 insertions(+), 95 deletions(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index be2c66429daa..664387e08c81 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -1,4 +1,4 @@ - + from decimal import * getcontext().prec=28 @@ -6,98 +6,103 @@ def points_to_polynomial(coordinates): #the list (coordinates) is two dimensional: [[x, y],[x, y],...] - if __name__ == "__main__" : - #number of points you want to use - try: - more_check=0 - d=coordinates[0][0] - for j in range(len(coordinates)): - if j==0: - continue - if d==coordinates[j][0]: - more_check+=1 - solved="x="+str(coordinates[j][0]) - if more_check == len(coordinates)-1: - check=2 - break - elif more_check > 0 and more_check != len(coordinates)-1: - check=3 - else: - check=1 - - if len(coordinates)==1 and coordinates[0][0]==0: - check=2 - solved="x=0" - except: - check=3 - - - x=len(coordinates) - - if check==1: - count_of_line=0 - matrix=[] - #put the x and x to the power values in a matrix - while count_of_line < x: - count_in_line=0 - a=coordinates[count_of_line][0] - count_line=[] - while count_in_line < x: - count_line.append(a**(x-(count_in_line+1))) - count_in_line+=1 - matrix.append(count_line) - count_of_line+=1 - - count_of_line=0 - #put the y values into a vector - vector=[] - while count_of_line < x: - count_in_line=0 - vector.append(coordinates[count_of_line][1]) - count_of_line+=1 - - count=0 - - while count < x: - zahlen=0 - while zahlen < x: - if count==zahlen: - zahlen+=1 - if zahlen==x: - break - bruch=(matrix[zahlen][count])/(matrix[count][count]) - counting_columns=0 - for i in range(len(matrix[count])): - #manipulating all the values in the matrix - matrix[zahlen][counting_columns]-=(matrix[count][i])*bruch - counting_columns+=1 - #manipulating the values in the vector - vector[zahlen]-=vector[count]*bruch + #number of points you want to use + try: + more_check=0 + d=coordinates[0][0] + for j in range(len(coordinates)): + if j==0: + continue + if d==coordinates[j][0]: + more_check+=1 + solved="x="+str(coordinates[j][0]) + if more_check == len(coordinates)-1: + check=2 + break + elif more_check > 0 and more_check != len(coordinates)-1: + check=3 + else: + check=1 + + if len(coordinates)==1 and coordinates[0][0]==0: + check=2 + solved="x=0" + except: + check=3 + + + x=len(coordinates) + + if check==1: + count_of_line=0 + matrix=[] + #put the x and x to the power values in a matrix + while count_of_line < x: + count_in_line=0 + a=coordinates[count_of_line][0] + count_line=[] + while count_in_line < x: + count_line.append(a**(x-(count_in_line+1))) + count_in_line+=1 + matrix.append(count_line) + count_of_line+=1 + + count_of_line=0 + #put the y values into a vector + vector=[] + while count_of_line < x: + count_in_line=0 + vector.append(coordinates[count_of_line][1]) + count_of_line+=1 + + count=0 + + while count < x: + zahlen=0 + while zahlen < x: + if count==zahlen: zahlen+=1 - count+=1 - - count=0 - #make solutions - solution=[] - while count < x: - solution.append(vector[count]/matrix[count][count]) - count+=1 - - count=0 - solved="f(x)=" - - while count < x: - remove_e=str(solution[count]).split("E") - if len(remove_e) > 1: - solution[count]=remove_e[0]+"*10^"+remove_e[1] - solved+="x^"+ str(x-(count+1))+ "*" +str(solution[count]) - if count+1 != x: - solved+="+" - count+=1 - - return solved - - elif check==2: - return solved - else: - return "The program cannot work out a fitting polynomial." + if zahlen==x: + break + bruch=(matrix[zahlen][count])/(matrix[count][count]) + counting_columns=0 + for i in range(len(matrix[count])): + #manipulating all the values in the matrix + matrix[zahlen][counting_columns]-=(matrix[count][i])*bruch + counting_columns+=1 + #manipulating the values in the vector + vector[zahlen]-=vector[count]*bruch + zahlen+=1 + count+=1 + + count=0 + #make solutions + solution=[] + while count < x: + solution.append(vector[count]/matrix[count][count]) + count+=1 + + count=0 + solved="f(x)=" + + while count < x: + remove_e=str(solution[count]).split("E") + if len(remove_e) > 1: + solution[count]=remove_e[0]+"*10^"+remove_e[1] + solved+="x^"+ str(x-(count+1))+ "*" +str(solution[count]) + if count+1 != x: + solved+="+" + count+=1 + + return solved + + elif check==2: + return solved + else: + return "The program cannot work out a fitting polynomial." + + +if __name__ == "__main__": + print(points_to_polynomial([])) + print(points_to_polynomial([[]])) + print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) # or some better matrix From 1f0fafc45f5b590a3e258c50c958541708bf189f Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Sun, 11 Aug 2019 21:26:05 +0200 Subject: [PATCH 08/10] Update python-polynom-for-points.py --- linear_algebra/src/python-polynom-for-points.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index 664387e08c81..373089186561 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -105,4 +105,4 @@ def points_to_polynomial(coordinates): if __name__ == "__main__": print(points_to_polynomial([])) print(points_to_polynomial([[]])) - print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) # or some better matrix + print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) From f47bdfcef4e7367b8dc2d48e8f83e3f9b607a362 Mon Sep 17 00:00:00 2001 From: Niclas Dern <52120196+nic-dern@users.noreply.github.com> Date: Mon, 12 Aug 2019 08:30:12 +0200 Subject: [PATCH 09/10] Update python-polynom-for-points.py --- linear_algebra/src/python-polynom-for-points.py | 1 + 1 file changed, 1 insertion(+) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index 373089186561..ebbcc213ca70 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -8,6 +8,7 @@ def points_to_polynomial(coordinates): #number of points you want to use try: + check=1 more_check=0 d=coordinates[0][0] for j in range(len(coordinates)): From c346323d1867e2651f2ceb37467a28eae6d052db Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 12 Aug 2019 09:13:40 +0200 Subject: [PATCH 10/10] Add doctests and run thru psf/black --- .../src/python-polynom-for-points.py | 167 ++++++++++-------- 1 file changed, 94 insertions(+), 73 deletions(-) diff --git a/linear_algebra/src/python-polynom-for-points.py b/linear_algebra/src/python-polynom-for-points.py index ebbcc213ca70..c884416b6dad 100644 --- a/linear_algebra/src/python-polynom-for-points.py +++ b/linear_algebra/src/python-polynom-for-points.py @@ -1,109 +1,130 @@ - -from decimal import * -getcontext().prec=28 - - def points_to_polynomial(coordinates): - #the list (coordinates) is two dimensional: [[x, y],[x, y],...] - - #number of points you want to use + """ + coordinates is a two dimensional matrix: [[x, y], [x, y], ...] + number of points you want to use + + >>> print(points_to_polynomial([])) + The program cannot work out a fitting polynomial. + >>> print(points_to_polynomial([[]])) + The program cannot work out a fitting polynomial. + >>> print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) + f(x)=x^2*0.0+x^1*-0.0+x^0*0.0 + >>> print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) + f(x)=x^2*0.0+x^1*-0.0+x^0*1.0 + >>> print(points_to_polynomial([[1, 3], [2, 3], [3, 3]])) + f(x)=x^2*0.0+x^1*-0.0+x^0*3.0 + >>> print(points_to_polynomial([[1, 1], [2, 2], [3, 3]])) + f(x)=x^2*0.0+x^1*1.0+x^0*0.0 + >>> print(points_to_polynomial([[1, 1], [2, 4], [3, 9]])) + f(x)=x^2*1.0+x^1*-0.0+x^0*0.0 + >>> print(points_to_polynomial([[1, 3], [2, 6], [3, 11]])) + f(x)=x^2*1.0+x^1*-0.0+x^0*2.0 + >>> print(points_to_polynomial([[1, -3], [2, -6], [3, -11]])) + f(x)=x^2*-1.0+x^1*-0.0+x^0*-2.0 + >>> print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) + f(x)=x^2*5.0+x^1*-18.0+x^0*18.0 + """ try: - check=1 - more_check=0 - d=coordinates[0][0] + check = 1 + more_check = 0 + d = coordinates[0][0] for j in range(len(coordinates)): - if j==0: + if j == 0: continue - if d==coordinates[j][0]: - more_check+=1 - solved="x="+str(coordinates[j][0]) - if more_check == len(coordinates)-1: - check=2 + if d == coordinates[j][0]: + more_check += 1 + solved = "x=" + str(coordinates[j][0]) + if more_check == len(coordinates) - 1: + check = 2 break - elif more_check > 0 and more_check != len(coordinates)-1: - check=3 + elif more_check > 0 and more_check != len(coordinates) - 1: + check = 3 else: - check=1 + check = 1 - if len(coordinates)==1 and coordinates[0][0]==0: - check=2 - solved="x=0" - except: - check=3 + if len(coordinates) == 1 and coordinates[0][0] == 0: + check = 2 + solved = "x=0" + except Exception: + check = 3 + x = len(coordinates) - x=len(coordinates) - - if check==1: - count_of_line=0 - matrix=[] - #put the x and x to the power values in a matrix + if check == 1: + count_of_line = 0 + matrix = [] + # put the x and x to the power values in a matrix while count_of_line < x: - count_in_line=0 - a=coordinates[count_of_line][0] - count_line=[] + count_in_line = 0 + a = coordinates[count_of_line][0] + count_line = [] while count_in_line < x: - count_line.append(a**(x-(count_in_line+1))) - count_in_line+=1 + count_line.append(a ** (x - (count_in_line + 1))) + count_in_line += 1 matrix.append(count_line) - count_of_line+=1 + count_of_line += 1 - count_of_line=0 - #put the y values into a vector - vector=[] + count_of_line = 0 + # put the y values into a vector + vector = [] while count_of_line < x: - count_in_line=0 + count_in_line = 0 vector.append(coordinates[count_of_line][1]) - count_of_line+=1 + count_of_line += 1 - count=0 + count = 0 while count < x: - zahlen=0 + zahlen = 0 while zahlen < x: - if count==zahlen: - zahlen+=1 - if zahlen==x: + if count == zahlen: + zahlen += 1 + if zahlen == x: break - bruch=(matrix[zahlen][count])/(matrix[count][count]) - counting_columns=0 - for i in range(len(matrix[count])): - #manipulating all the values in the matrix - matrix[zahlen][counting_columns]-=(matrix[count][i])*bruch - counting_columns+=1 - #manipulating the values in the vector - vector[zahlen]-=vector[count]*bruch - zahlen+=1 - count+=1 - - count=0 - #make solutions - solution=[] + bruch = (matrix[zahlen][count]) / (matrix[count][count]) + for counting_columns, item in enumerate(matrix[count]): + # manipulating all the values in the matrix + matrix[zahlen][counting_columns] -= item * bruch + # manipulating the values in the vector + vector[zahlen] -= vector[count] * bruch + zahlen += 1 + count += 1 + + count = 0 + # make solutions + solution = [] while count < x: - solution.append(vector[count]/matrix[count][count]) - count+=1 + solution.append(vector[count] / matrix[count][count]) + count += 1 - count=0 - solved="f(x)=" + count = 0 + solved = "f(x)=" while count < x: - remove_e=str(solution[count]).split("E") + remove_e = str(solution[count]).split("E") if len(remove_e) > 1: - solution[count]=remove_e[0]+"*10^"+remove_e[1] - solved+="x^"+ str(x-(count+1))+ "*" +str(solution[count]) - if count+1 != x: - solved+="+" - count+=1 + solution[count] = remove_e[0] + "*10^" + remove_e[1] + solved += "x^" + str(x - (count + 1)) + "*" + str(solution[count]) + if count + 1 != x: + solved += "+" + count += 1 return solved - elif check==2: + elif check == 2: return solved else: return "The program cannot work out a fitting polynomial." - + if __name__ == "__main__": print(points_to_polynomial([])) print(points_to_polynomial([[]])) + print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) + print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) + print(points_to_polynomial([[1, 3], [2, 3], [3, 3]])) + print(points_to_polynomial([[1, 1], [2, 2], [3, 3]])) + print(points_to_polynomial([[1, 1], [2, 4], [3, 9]])) + print(points_to_polynomial([[1, 3], [2, 6], [3, 11]])) + print(points_to_polynomial([[1, -3], [2, -6], [3, -11]])) print(points_to_polynomial([[1, 5], [2, 2], [3, 9]]))