I'd like to write a program in Python where user define a deegre of polynomial and coefficients (a,b,c). When program create a polynomial expression with this data I'd like to use it like function because I need this to other operations. How can i get it? For example when I have polynomial= x^n+a^n-1+b^n-2+c^-3 I'd like to use it in polynomial(x) to calculate value.
Now the creating polynomial method looks:
def polynomial(n,a,b,c):return a*x**n+b*x**3-c*x
class Polynomial:def __init__(self, coeficents, degrees=None):if degrees = None:self.degree = list(reversed(range(len(coeficents))))else:self.degree = degreesself.coeficents = coeficentsdef __call__(self, x):print(self.coeficents)print(self.degree)return sum([self.coeficents[i]*x**self.degree[i] for i in range(len(self.coeficents))])p = Polynomial([1,2,4],[10,2,0])
print(p(2))
This will compute the polynomial x^10 + 2x^2 + 4
at x = 2
. It should be very clear how to use with your example.