define a polynomial of any degree in Python

Asked

Viewed 1,982 times

0

I work with numerical calculus, my goal is to make use of a "least Squares". I’m defining a function in Python as follows:

def F(P,x):
    return P[0] + P[1]*x + P[2]*x**2 + P[3]*x**3

Then I will define these coefficients P[0], P[1],...

This example is easy, because the polynomial is of degree 3, but I intend to make a polynomial of degree greater than 10. So I wanted to facilitate the definition of this polynomial. Let’s say that before I define degree = 3, degree = 4 or any value and make practical the definition of any polynomial of degree in Python, that is, I choose the degree, say 3 and define me returning in the following way:

def F(P,x):
    return P[0] + P[1]*x + P[2]*x^2 + P[3]*x^3

If I choose 4, return me the following way:

def F(P,x):
    return P[0] + P[1]*x + P[2]*x^2 + P[3]*x^3 + P[4]**x^4
  • Wilson - try to be clearer about what you really want to do in Python. What do you want? A function to which you pass "4" and return a string with the polynomial? Or that one that you pass the coefficients, and it returns you an object that can be called as a function, (and then calculates the polynomial) and still with a cute representation? Or do you want an object that can hold the polynomial in its symbolic form, so that it can be summed, multiplied, etc... with other polynomials before any numerical calculus? All this is possible in Python, but we have no way to guess.

  • (By the way: as the question has been closed, it is easier for you to redo a new question than to edit it and wait for it to be reopened)

  • 1

    @jsbueno If it has too many closed questions the system can trigger a time limiter restricting new posts. Ideally he should click [Dit], elaborate the question right here making clear the doubt and wait for possible opening (as instructed in the yellow frame above). Imagine if he re-asks the question and still missing important details and is closed, will make a third later?

  • Guys, thanks, I edited again, I hope I’ve been clearer.

  • P shall be a list of the coefficients?

  • It will be a list that I will find the best values using a "least Squares"

  • 2

    I voted to reopen the question. I believe you can do something like this: https://repl.it/@acwoss/Fonddarlingscreenscraper

  • It worked @Andersoncarloswoss, thank you.

Show 3 more comments

1 answer

2


Now it’s simple - you just really want a Python function that does the numerical calculation of the polynomial, defined by the coefficients in P.

So you just make one for running through the coefficients, and using the enmerate to have the exponent of X associated with each coefficient. enumerate works like this: for each element of a sequence, it returns another sequence of two elements in which the first is the index and the second is the element itself.

Then we calculate each portion of the polynomial and add them all up - doing "in full":

def F(P, x):
    result = 0
    for exponent, coeficient in enumerate(P):
        result += coeficient * x ** exponent
    return result

It works if P is a list or any other sequence, since the for in Python always traverses a sequence.

There is a more advanced syntax also that allows the use of for as an "inline" expression, not as a command on a separate line. This model creates a "Generator Expression" that can be passed directly to the built-in function sum:

def F(P, x):
    return sum(c * x ** e for (e, c) in enumerate(P))

And last but not least, you can have a polynomial class - and over time add functionalities to it - if the class gets a list of coefficients in its __init__, may have a method __call__ which allows its polynomial to calculate its value for a given "x" - and a __repr__ which has a legal representation of the polynomial:

class Poli:
    def __init__(self, coeficients):
         self.coeficients = coeficients
    @property
    def degree(self):
         return len(coeficients) - 1
    def __call__(self, x):
         return sum(c * x ** e for (e, c) in enumerate(self.coeficients))
    def __repr__(self):
         return f"P({})".format(" + ".join(f"{c} * x ** {e}" for e, c in enumerate(self.coeficients))
  • @jsnueno I took the second mode, I found it more peaceful to work, since I am an apprentice in language. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.