Solve polynomial equation in python

Here is an example from simpy docs:

>>> from sympy import *
>>> x = symbols('x')
>>> from sympy import roots, solve_poly_system

>>> solve(x**3 + 2*x + 3, x)
           ____          ____
     1   \/ 11 *I  1   \/ 11 *I
[-1, - - --------, - + --------]
     2      2      2      2

>>> p = Symbol('p')
>>> q = Symbol('q')

>>> sorted(solve(x**2 + p*x + q, x))
          __________           __________
         /  2                 /  2
   p   \/  p  - 4*q     p   \/  p  - 4*q
[- - + -------------, - - - -------------]
   2         2          2         2

>>> solve_poly_system([y - x, x - 5], x, y)
[(5, 5)]

>>> solve_poly_system([y**2 - x**3 + 1, y*x], x, y)
                                   ___                 ___
                             1   \/ 3 *I         1   \/ 3 *I
[(0, I), (0, -I), (1, 0), (- - + -------, 0), (- - - -------, 0)]
                             2      2            2      2

(a link to the docs with this example)

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The following article contains programs to compute a polynomial equation given that the coefficients of the polynomial are stored in a List.

    Examples:

    # Evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3
    Input: poly[] = {2, -6, 2, -1}, x = 3
    Output: 5
    
    # Evaluate value of 2x3 + 3x + 1 for x = 2
    Input: poly[] = {2, 0, 3, 1}, x = 2
    Output: 23
    
    # Evaluate value of 2x + 5 for x = 5
    Input: poly[] = {2, 5}, x = 5
    Output: 15

    The equation will be of type:

    Solve polynomial equation in python

    We will be provided with the value of variable and we have to compute the value of the polynomial at that point. To do so we have two approaches.

    Approach

    • Naive Method: Using for loop to compute the value.
    • Optimised Method: Using Horner’s Method for computing the value.

    Naive method:

    In this approach, the following methodology will be followed. This is the most naive approach to do such questions.

    • First coefficient cn will be multiplied with xn
    • Then coefficient cn-1 will be multiplied with xn-1
    • The results produced in the above two steps will be added
    • This will go on till all the coefficient are covered.

    Example:

    Python3

    poly = [2, -6, 2, -1]

    x = 3

    n = len(poly)

    result = 0

    for i in range(n):

        Sum = poly[i]

        for j in range(n - i - 1):

            Sum = Sum * x

        result = result + Sum

    print(result)

    Output:

    5

    Time Complexity: O(n2)

    Optimized method:

    Horner’s method can be used to evaluate polynomial in O(n) time. To understand the method, let us consider the example of 2x3 – 6x2 + 2x – 1. The polynomial can be evaluated as ((2x – 6)x + 2)x – 1. The idea is to initialize result as the coefficient of xn which is 2 in this case, repeatedly multiply the result with x and add the next coefficient to result. Finally, return the result. 

    Python3

    def horner(poly, n, x):

        result = poly[0]

        for i in range(1, n):

            result = result*x + poly[i]

        return result

    poly = [2, -6, 2, -1]

    x = 3

    n = len(poly)

    print("Value of polynomial is:", horner(poly, n, x))

    Output:

    Value of polynomial is: 5

    Time Complexity: O(n)


    How do you solve a polynomial in Python?

    Python Program to Compute a Polynomial Equation.
    Import the math module..
    Take in the coefficients of the polynomial equation and store it in a list..
    Take in the value of x..
    Use a for loop and while loop to compute the value of the polynomial expression for the first three terms and store it in a sum variable..

    How do you input a polynomial equation in Python?

    This code will be used to take input of the polynomial as y for instance. y = x**2 + x*4 and it takes input of x as float value like if i give 2 for x then this expression will prints 12.

    How do you find the roots of a polynomial in Python?

    To compute the roots of a polynomials, use the polynomial. polyroots() method in Python Numpy. The method returns an array of the roots of the polynomial. If all the roots are real, then out is also real, otherwise it is complex.

    How do you print a polynomial in Python?

    The class is given a list that represents the coefficients of the polynomial and their exponents are given by the position the coefficients are in the list. For example [2,-3,0,5] would give 2x^3-3x^2+5 . When trying to print p1 = Polynomial([2,3,4]) I get p1 = 3x+4x^2 .