intro Link to heading

  • When have to work with function representing polynomial, e.g. plot polynomial function, the function can be made by summing all terms manually (Imam, 2018).
  • To calculate $f(x)$ for single value of $x$ there is at least two ways with different time complexity, $O(n^2)$ by adding each term for $x^j$ with multiplication and summation loops, and $O(n)$ by using Horner’s method (Taparia, 2022).
  • Python math library can be used to calculate $x^j$ and reduce previous approach with two loops into single loop (Bhojasia, 2022).
  • User-defined class can be constructed to meet only what is needed, coefficients, string representation in LaTeX, and calculate the value for every $x$ using callabes (Klein, 2023).
  • By representing a polynomial in its coefficent NumPy functions can be used to calculate the value for every $x$, finding roots of the the polynomial, perform differential and integral to it (Kitchin, 2013).
  • There are also some series, e.g. power, Chebyshev, Hermite, Laguerre, Legendre, provided by NumPy (Developers, 2022).
  • Here only polynomial functions with constant and variable coefficients are discussed.

constant coefficents Link to heading

  • Suppose there is a polynomial in the form of $$\tag{1} f(x) = 1 + 2x + 3x^2 + 4x^3 + 5x^4. $$
  • There are some approaches to calculate the value of $f(x)$.

approach 1 Link to heading

  • Code
    def f(x):
      y0 = 1
      y1 = 2 * x
      y2 = 3 * x**2
      y3 = 4 * x**3
      y4 = 5 * x**4
      y = y0 + y1 + y2 + y3 + y4
      return y
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwc33ef [20231230].
  • Output
    x	f(x)
    0	1
    1	15
    2	129
    3	547
    4	1593
    
  • This approach is straight forward and very clear, but it works only for one specific polynomial.

approach 2 Link to heading

  • Code
    import math
    
    def f(x):
      y0 = 1 * math.pow(x, 0)
      y1 = 2 * math.pow(x, 1)
      y2 = 3 * math.pow(x, 2)
      y3 = 4 * math.pow(x, 3)
      y4 = 5 * math.pow(x, 4)
      y = y0 + y1 + y2 + y3 + y4
      return y
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwcjjud [20231230].
  • Output
    x	f(x)
    0	1
    1	15
    2	129
    3	547
    4	1593
    
  • This approach is straight forward and also systematic since the power is also clear, but it works only for one specific polynomial.

approach 3 Link to heading

  • Code
    import math
    
    def f(x):
      c = [1, 2, 3, 4, 5];
      y0 = c[0] * math.pow(x, 0)
      y1 = c[1] * math.pow(x, 1)
      y2 = c[2] * math.pow(x, 2)
      y3 = c[3] * math.pow(x, 3)
      y4 = c[4] * math.pow(x, 4)
      y = y0 + y1 + y2 + y3 + y4
      return y
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwcwhvq [20231230].
  • Output
    x	f(x)
    0	1.0
    1	15.0
    2	129.0
    3	547.0
    4	1593.0
    
  • It is easier to modify for certain polynomial compared the previous code, since only one line c = [c0, c1, c2, c3, c4] that has to be changed.

approach 4 Link to heading

  • Code
    import math
    
    def f(x):
      c = [1, 2, 3, 4, 5];
      y = 0
      for i in range(len(c)):
        yi = c[i] * math.pow(x, i)
        y = y + yi
      return y
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwd8ve2 [20231230].
  • Output
    x	f(x)
    0	1.0
    1	15.0
    2	129.0
    3	547.0
    4	1593.0
    
  • It is the same the previous code, with more compact in calculating each term using yi = c[i] * math.pow(x, i).

approach 5 Link to heading

  • Code
    def f(x):
      c = [1, 2, 3, 4, 5];
      y = [(c[i] * x**i) for i in range(len(c))]
      return sum(y)
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwe4z2n [20231230].
  • Output
    x	f(x)
    0	1
    1	15
    2	129
    3	547
    4	1593
    
  • Using Python feature known as list comprehension more lines can be reduced.

approach 6 Link to heading

  • Code
    def f(x):
      c = [1, 2, 3, 4, 5];
      y = [cf * x**n for n, cf in enumerate(c)]
      return sum(y)
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwe8ed8 [20231230].
  • Output
    x	f(x)
    0	1
    1	15
    2	129
    3	547
    4	1593
    
  • With this code, number of lines is still the same but the use of Python enumerate function make the code more clear.

approach 7 Link to heading

  • Code
    def f(x):
      return sum([c * x**n for n, c in enumerate(coefs)])
    
    coefs = [1, 2, 3, 4, 5];
    
    print("x\tf(x)")
    for x in range(0, 5):
      print(x, f(x), sep='\t')
    
    url https://onecompiler.com/python/3zxwf2k3c [20231230].
  • Output
    x	f(x)
    0	1
    1	15
    2	129
    3	547
    4	1593
    
  • Now the function f(x) can be in one line and coefs list is put outside the function.
  • Notice that coefs can be modified before the function f(x) is called.

variable coefficients Link to heading

  • By modifying previous approach 7, following code can be obtained.
    def f(x, coefs):
      return sum([c * x**n for n, c in enumerate(coefs)])
    
    
    c1 = [1, 2, 3, 4, 5]
    print("x\tf(x)")
    for x in range(0, 4):
      print(x, f(x, c1), sep='\t')
    
    print()
    
    c2 = [1, 1, 1, 1]
    print("x\tf(x)")
    for x in range(0, 3):
      print(x, f(x, c2), sep='\t')
    
  • Output
    x	f(x)
    0	1
    1	15
    2	129
    3	547
    
    x	f(x)
    0	1
    1	4
    2	15
    
  • Notice that there are two polynomials, each with coefficients c1 and c2.
  • Call the function f(x, c) will calculate polynomial $f(x)$ with coefficients $c$.

challenges Link to heading

  • Modify last code to calculate $f(x) = -1 + x - x^2 + x^3$.
  • Calculate other polynomial $f(x) = 32x^7 - x^4 + 0.5$.

refs Link to heading