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))
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.
– jsbueno
(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)
– jsbueno
@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?
– Bacco
Guys, thanks, I edited again, I hope I’ve been clearer.
– Wilson Vital
P
shall be a list of the coefficients?– Woss
It will be a list that I will find the best values using a "least Squares"
– Wilson Vital
I voted to reopen the question. I believe you can do something like this: https://repl.it/@acwoss/Fonddarlingscreenscraper
– Woss
It worked @Andersoncarloswoss, thank you.
– Wilson Vital