|
| 1 | +def points_to_polynomial(coordinates): |
| 2 | + """ |
| 3 | + coordinates is a two dimensional matrix: [[x, y], [x, y], ...] |
| 4 | + number of points you want to use |
| 5 | +
|
| 6 | + >>> print(points_to_polynomial([])) |
| 7 | + The program cannot work out a fitting polynomial. |
| 8 | + >>> print(points_to_polynomial([[]])) |
| 9 | + The program cannot work out a fitting polynomial. |
| 10 | + >>> print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) |
| 11 | + f(x)=x^2*0.0+x^1*-0.0+x^0*0.0 |
| 12 | + >>> print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) |
| 13 | + f(x)=x^2*0.0+x^1*-0.0+x^0*1.0 |
| 14 | + >>> print(points_to_polynomial([[1, 3], [2, 3], [3, 3]])) |
| 15 | + f(x)=x^2*0.0+x^1*-0.0+x^0*3.0 |
| 16 | + >>> print(points_to_polynomial([[1, 1], [2, 2], [3, 3]])) |
| 17 | + f(x)=x^2*0.0+x^1*1.0+x^0*0.0 |
| 18 | + >>> print(points_to_polynomial([[1, 1], [2, 4], [3, 9]])) |
| 19 | + f(x)=x^2*1.0+x^1*-0.0+x^0*0.0 |
| 20 | + >>> print(points_to_polynomial([[1, 3], [2, 6], [3, 11]])) |
| 21 | + f(x)=x^2*1.0+x^1*-0.0+x^0*2.0 |
| 22 | + >>> print(points_to_polynomial([[1, -3], [2, -6], [3, -11]])) |
| 23 | + f(x)=x^2*-1.0+x^1*-0.0+x^0*-2.0 |
| 24 | + >>> print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) |
| 25 | + f(x)=x^2*5.0+x^1*-18.0+x^0*18.0 |
| 26 | + """ |
| 27 | + try: |
| 28 | + check = 1 |
| 29 | + more_check = 0 |
| 30 | + d = coordinates[0][0] |
| 31 | + for j in range(len(coordinates)): |
| 32 | + if j == 0: |
| 33 | + continue |
| 34 | + if d == coordinates[j][0]: |
| 35 | + more_check += 1 |
| 36 | + solved = "x=" + str(coordinates[j][0]) |
| 37 | + if more_check == len(coordinates) - 1: |
| 38 | + check = 2 |
| 39 | + break |
| 40 | + elif more_check > 0 and more_check != len(coordinates) - 1: |
| 41 | + check = 3 |
| 42 | + else: |
| 43 | + check = 1 |
| 44 | + |
| 45 | + if len(coordinates) == 1 and coordinates[0][0] == 0: |
| 46 | + check = 2 |
| 47 | + solved = "x=0" |
| 48 | + except Exception: |
| 49 | + check = 3 |
| 50 | + |
| 51 | + x = len(coordinates) |
| 52 | + |
| 53 | + if check == 1: |
| 54 | + count_of_line = 0 |
| 55 | + matrix = [] |
| 56 | + # put the x and x to the power values in a matrix |
| 57 | + while count_of_line < x: |
| 58 | + count_in_line = 0 |
| 59 | + a = coordinates[count_of_line][0] |
| 60 | + count_line = [] |
| 61 | + while count_in_line < x: |
| 62 | + count_line.append(a ** (x - (count_in_line + 1))) |
| 63 | + count_in_line += 1 |
| 64 | + matrix.append(count_line) |
| 65 | + count_of_line += 1 |
| 66 | + |
| 67 | + count_of_line = 0 |
| 68 | + # put the y values into a vector |
| 69 | + vector = [] |
| 70 | + while count_of_line < x: |
| 71 | + count_in_line = 0 |
| 72 | + vector.append(coordinates[count_of_line][1]) |
| 73 | + count_of_line += 1 |
| 74 | + |
| 75 | + count = 0 |
| 76 | + |
| 77 | + while count < x: |
| 78 | + zahlen = 0 |
| 79 | + while zahlen < x: |
| 80 | + if count == zahlen: |
| 81 | + zahlen += 1 |
| 82 | + if zahlen == x: |
| 83 | + break |
| 84 | + bruch = (matrix[zahlen][count]) / (matrix[count][count]) |
| 85 | + for counting_columns, item in enumerate(matrix[count]): |
| 86 | + # manipulating all the values in the matrix |
| 87 | + matrix[zahlen][counting_columns] -= item * bruch |
| 88 | + # manipulating the values in the vector |
| 89 | + vector[zahlen] -= vector[count] * bruch |
| 90 | + zahlen += 1 |
| 91 | + count += 1 |
| 92 | + |
| 93 | + count = 0 |
| 94 | + # make solutions |
| 95 | + solution = [] |
| 96 | + while count < x: |
| 97 | + solution.append(vector[count] / matrix[count][count]) |
| 98 | + count += 1 |
| 99 | + |
| 100 | + count = 0 |
| 101 | + solved = "f(x)=" |
| 102 | + |
| 103 | + while count < x: |
| 104 | + remove_e = str(solution[count]).split("E") |
| 105 | + if len(remove_e) > 1: |
| 106 | + solution[count] = remove_e[0] + "*10^" + remove_e[1] |
| 107 | + solved += "x^" + str(x - (count + 1)) + "*" + str(solution[count]) |
| 108 | + if count + 1 != x: |
| 109 | + solved += "+" |
| 110 | + count += 1 |
| 111 | + |
| 112 | + return solved |
| 113 | + |
| 114 | + elif check == 2: |
| 115 | + return solved |
| 116 | + else: |
| 117 | + return "The program cannot work out a fitting polynomial." |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + print(points_to_polynomial([])) |
| 122 | + print(points_to_polynomial([[]])) |
| 123 | + print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) |
| 124 | + print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) |
| 125 | + print(points_to_polynomial([[1, 3], [2, 3], [3, 3]])) |
| 126 | + print(points_to_polynomial([[1, 1], [2, 2], [3, 3]])) |
| 127 | + print(points_to_polynomial([[1, 1], [2, 4], [3, 9]])) |
| 128 | + print(points_to_polynomial([[1, 3], [2, 6], [3, 11]])) |
| 129 | + print(points_to_polynomial([[1, -3], [2, -6], [3, -11]])) |
| 130 | + print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) |
0 commit comments