class Poly(object): | |
def __init__(self, coef_initial=0, exponent_initial = 0, coefs=None): | |
""" Create a polynomial with exactly one term, with coefficient | |
coeff and exponent exponent, where coeff is an integer and exponent is a non-negative integer | |
Defaults to the polynomial zero """ | |
if coefs == None: | |
# if coeff == 0, then exponent should be 0 | |
if coef_initial == 0: | |
exponent_initial = 0 | |
self.coefs = ([0]*exponent_initial) | |
self.coefs.append(coef_initial) | |
else: | |
self.coefs = coefs | |
""" | |
Permite criar o Poly de duas maneiras:\ | |
Poly(coef_initial=5, exponent_initial=3) - [0,0,0,5] | |
Poly(coefs=[0,1,2,3]) - [0,1,2,3] | |
""" | |
def format1(self, c, e): | |
if e != 0: | |
f = "{}x^{}".format(c,e) | |
else: | |
f = str(c) | |
return f | |
def __str__(self): | |
# [7, 0, -5, 0, 3] | |
s = "" | |
for exp, c in list(enumerate(self.coefs))[::-1]: | |
if c != 0: | |
if exp == 0: | |
s += str(c) + " + " | |
else: | |
s += str(c) + "x^" + str(exp) + " + " | |
return s[:-2] #" 3x^4 + -5x^2 + 7 " | |
""" Alternativa """ | |
return " + ".join([ self.format1(c,e) for e,c in enumerate(self.coefs) if c != 0 ][::-1]) | |
def isZero(self): | |
""" Devolve se o polinomio e igual ao polinomio zero """ | |
return all([i == 0 for i in self.coefs]) | |
def coeff(self, exp=0): | |
# p.coefs = [1,0,5,0,2,7] | |
# p.coeff[4] = 2 | |
if exp >= len(self.coefs): | |
return 0 | |
else: | |
return self.coefs[exp] | |
"Alternativa:" | |
""" | |
try: | |
return self.coefs[exp] | |
except: | |
return 0 | |
""" | |
def degree(self): | |
v = len(self.coefs)-1 | |
ultimoIndice = v | |
while self.coefs[ultimoIndice] == 0 and ultimoIndice > 0: | |
v -= 1 | |
ultimoIndice -= 1 | |
return v | |
def __add__(self, o): | |
lg = self.coefs | |
lp = o.coefs | |
if len(lg) < len(lp): | |
lg, lp = lp, lg | |
nv = [ i for i in lg ] | |
for i, v in enumerate(lp): | |
nv[i] += v | |
return Poly(coefs=nv) | |
""" | |
# Alternativa | |
ma = max(len(self.coefs), len(o.coefs)) | |
nl = [] | |
for i in range(ma): | |
v = 0 | |
if i < len(self.coefs): | |
v += self.coefs[i] | |
if i < len(o.coefs): | |
v += o.coefs[i] | |
nl.append( v ) | |
# Preeencher poly | |
# Alternativa com map | |
def f(a,b): | |
if a == None: | |
return b | |
if b == None: | |
return a | |
return a+b | |
nv = map(f, self.coefs, o.coefs) | |
# Preeencher poly | |
""" | |
def minus(self): | |
nv = [ -e for e in self.coefs ] | |
return Poly(coefs=nv) | |
def __sub__(self, o): | |
return self + o.minus() | |
def __mul__(self, o): | |
p = Poly() | |
for e1, c1 in enumerate(self.coefs): | |
for e2, c2 in enumerate(o.coefs): | |
p = p + Poly(coef_initial=c1*c2, exp_initial=e1+e2) | |
return p | |
def evaluate(self, x): | |
v = 0 | |
for e, c in enumerate(self.coefs): | |
v += c * x**e | |
return v | |
#print Poly(5,4) + Poly(3,2) # deve devolver - Poly.coefs = [0,0,3,0,5] | |
#print Poly(coefs=[0,0,1,0,5]).minus() # [0,0,1,0,5] -> [0,0,-1,0,-5] | |
print Poly(5,4) - Poly(3,2) # deve devolver - Poly.coefs = [0,0,-3,0,5] | |
""" | |
def testa(p): | |
print "str():", str(p) | |
print "isZero():", p.isZero() | |
print "coeff(4):", p.coeff(4) | |
print "degree():", p.degree() | |
print "----" | |
testa(Poly(coef_initial=0, exponent_initial=0)) | |
testa(Poly(coef_initial=5, exponent_initial=4)) | |
p = Poly(coef_initial=5, exponent_initial=4) | |
p.coefs = [1,0,5,0,2,7] # BATOTA!!!!!!!!!!!!!! | |
testa(p) | |
p_ = Poly(coef_initial=5, exponent_initial=4) | |
p_.coefs = [0,0,0,0,0,0] | |
testa(p_) | |
p_2 = Poly(coef_initial=5, exponent_initial=4) | |
p_2.coefs = [1,0,0,0,0,0] | |
testa(p_2) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment