Evaluating Polynomial coefficients

后端 未结 5 1869
甜味超标
甜味超标 2020-12-18 13:07

I\'m trying to write a function that takes as input a list of coefficients (a0, a1, a2, a3.....a n) of a polynomial p(x) and the value x. The function will return p(x), whic

相关标签:
5条回答
  • 2020-12-18 13:24

    simple:

    def poly(lst, x): 
      n, tmp = 0, 0
      for a in lst:
        tmp = tmp + (a * (x**n))
        n += 1
    
      return tmp
    
    print poly([1,2,3], 2)
    

    simple recursion:

    def poly(lst, x, i = 0):
      try:
        tmp = lst.pop(0)
      except IndexError:
        return 0
      return tmp * (x ** (i)) + poly(lst, x, i+1)
    
    print poly([1,2,3], 2)
    
    0 讨论(0)
  • 2020-12-18 13:25
    def evalPoly(lst, x):
        total = 0
        for power, coeff in enumerate(lst): # starts at 0 by default
            total += (x**power) * coeff
        return total
    

    Alternatively, you can use a list and then use sum:

    def evalPoly(lst, x):
            total = []
            for power, coeff in enumerate(lst):
                total.append((x**power) * coeff)
            return sum(total)
    

    Without enumerate:

    def evalPoly(lst, x):
        total, power = 0, 0
        for coeff in lst:
            total += (x**power) * coeff
            power += 1
        return total
    

    Alternative to non-enumerate method:

    def evalPoly(lst, x):
        total = 0
        for power in range(len(lst)):
            total += (x**power) * lst[power] # lst[power] is the coefficient
        return total
    

    Also @DSM stated, you can put this together in a single line:

    def evalPoly(lst, x):
        return sum((x**power) * coeff for power, coeff in enumerate(lst))
    

    Or, using lambda:

    evalPoly = lambda lst, x: sum((x**power) * coeff for power, coeff in enumerate(lst))
    

    Recursive solution:

    def evalPoly(lst, x, power = 0):
        if power == len(lst): return (x**power) * lst[power]
        return ((x**power) * lst[power]) + evalPoly(lst, x, power + 1)
    

    enumerate(iterable, start) is a generator expression (so it uses yield instead of return that yields a number and then an element of the iterable. The number is equivalent to the index of the element + start.

    From the Python docs, it is also the same as:

    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    0 讨论(0)
  • 2020-12-18 13:26

    The most efficient way is to evaluate the polynomial backwards using Horner's Rule. Very easy to do in Python:

    # Evaluate a polynomial in reverse order using Horner's Rule,
    # for example: a3*x^3+a2*x^2+a1*x+a0 = ((a3*x+a2)x+a1)x+a0
    def poly(lst, x):
        total = 0
        for a in reversed(lst):
            total = total*x+a
        return total
    
    0 讨论(0)
  • 2020-12-18 13:33

    Either with recursion, or without, the essence of the solution is to create a loop on "n", because the polynomial starts at x^0 and goes up to a_n.x^n and that's the variable you should also consider as an input. Besides that, use a trick called multiply and accumulate to be able to calculate partial results on each loop iteration.

    0 讨论(0)
  • 2020-12-18 13:38
    def evalPoly(lst, x, power):
        if power == 0:
            return lst[power]
        return ((x**power) * lst[power]) + evalPoly(lst, x, power - 1)
    
    lst = [7, 1, 2, 3]
    x = 5
    print(evalPoly(lst, x, 3))
    

    Equation to evaluate is - 3x^3 + 2x^2 + x + 7 when x = 5, result is - 437

    0 讨论(0)
提交回复
热议问题