def get_poly_1_deg(points, values): sum_points = sum(points) sum_points_2 = sum([point**2 for point in points]) sum_values = sum(values) sum_v_p = sum([values[i] * points[i] for i in range(0, len(points))]) a = Matrix(2) a.matrix = [[len(points), sum_points], [sum_points, sum_points_2]] b = Vector(2) b.vector = [sum_values, sum_v_p] l, u, p = lup(a) coefficients = lup_solve(l, u, p, b).vector return parse_expr(f'{coefficients[0]} + {coefficients[1]} * x')
def get_poly_2_deg(points, values): sum_points = sum(points) sum_points_2 = sum([point**2 for point in points]) sum_points_3 = sum([point**3 for point in points]) sum_points_4 = sum([point**4 for point in points]) sum_values = sum(values) sum_v_p = sum([values[i] * points[i] for i in range(0, len(points))]) sum_v_p2 = sum([values[i] * points[i]**2 for i in range(0, len(points))]) a = Matrix(3) a.matrix = [[len(points), sum_points, sum_points_2], [sum_points, sum_points_2, sum_points_3], [sum_points_2, sum_points_3, sum_points_4]] b = Vector(3) b.vector = [sum_values, sum_v_p, sum_v_p2] l, u, p = lup(a) coefficients = lup_solve(l, u, p, b).vector return parse_expr( f'{coefficients[0]} + {coefficients[1]} * x + {coefficients[2]} * x ** 2' )