# pol1v.py class pol1v (list): def __add__ (A,B): if not isinstance(B,pol1v): return A+pol1v(A.tipo,B) if not A.lun: return B if not B.lun: return A lunA=A.lun; lunB=B.lun; coeff=[] if lunA<=lunB: for i in xrange(lunA): coeff.append(A[i]+B[i]) for i in xrange(lunA,lunB): coeff.append(B[i]) return pol1v(A.tipo,*coeff) else: return B+A def __call__ (A,alfa): b=0 for ak in reversed(A): b=b*alfa+ak return b def __init__ (A,tipo,*coeff): A.extend(map(tipo,coeff)); A.riduci() A.tipo=tipo; A.lun=len(A) def __mul__ (A,B): if not isinstance(B,pol1v): return A*pol1v(A.tipo,B) if not A.lun or not B.lun: return pol1v(A.tipo,0) if A.lun==1: a0=A[0]; return pol1v(A.tipo,*map(lambda x: a0*x,B)) if B.lun==1: b0=B[0]; return pol1v(A.tipo,*map(lambda x: b0*x,A)) coeff=[] for k in xrange(A.lun+B.lun-1): ck=0 for i in xrange(A.lun): if 0<=k-i<=B.lun-1: ck+=A[i]*B[k-i] coeff.append(ck) return pol1v(A.tipo,*coeff) def __neg__ (A): return pol1v(A.tipo,*map(lambda x: -x,A)) def __radd__ (A,B): if not isinstance(B,pol1v): return A+B def __rmul__ (A,B): if not isinstance(B,pol1v): return A*B def __rsub__ (A,B): if not isinstance(B,pol1v): return -A+B def __str__ (A): if A.tipo==complex: return str(list(A)) if not A.lun: return '0' u=[]; potx='' for i,a in enumerate(A): if not a: potx+='x'; continue if a==1 and i>0: b=' + ' elif a==-1 and i>0: b=' - ' elif a>0: b=' + %s' %(str(a)) else: b=' - %s' %(str(-a)) u.append('%s%s' %(b,potx)); potx+='x' u=''.join(u).lstrip() if u.startswith('+'): u=u[1:].lstrip() return u def __sub__ (A,B): return A+(-B) def der (A): if A.lun<=1: return pol1v(A.tipo,0) u=[] for i,a in enumerate(A[1:]): u.append((i+1)*a) return pol1v(A.tipo,*u) def riduci (A): while A and not A[-1]: A.pop() def sviluppo (A,alfa): coeff=[]; g=A; fatt=1.0 for k in xrange(A.lun+1): coeff.append(g(alfa)/fatt); g=g.der(); fatt*=(k+1) return pol1v(A.tipo,*coeff)