## Kristen Lew, Homework 17 ## ExpMath, Spring 2012 # Post if you wish. P = Primes() var('x1,x2,x3') var('x') var('FAIL') ### Problem 1 ## Modify the function multivariate_leading_term in the file test.sage. so that # it returns the leading term of the polynomial instead. for instance # multivariate_leading_term(x3^10+x2*x2+x1*x3) should return x3^10. def multivariate_leading_term(f): Lst = [] L = list(f.iterator()) for i in L: Lst.append((i/i.substitute(x1=1,x2=1,x3=1)).substitute(x1=P.unrank(0),x2 =P.unrank(1),x3=P.unrank(2))) k=Lst.index(max(Lst)) return L[k] ### Problem 2 ## Implement in python for sage the Polynomial Ansatz as by implementing the procedures: # Di(L): the sequence of differences of consecutive entries of L # For example,Di([1,4,9]); should be [3,5]. # IsPol(L): inputs a list of length L and # returns a conjectured degree if it (conj.) a polynomial and FAIL otherwise # GuessPol(L,x): guesses a polynomial expression P(x) such that for i in L P(i)=L[i]. # For example, GuessPol([2,3,4,5,6,7,8],x); should yield x+1. def Di(L): L2=[] for i in range(len(L)-1): L2.append(L[i+1]-L[i]) return L2 def IsPol(L): L1=L for k in range(len(L)-4): L1=set(Di(L1)) if L1 == set([0]): return k return FAIL def GuessPol(L,x): d=IsPol(L) if d=FAIL: return d a=[] P=0 equa=[] for k in range(d+1): a.append(var('a_%d' % k) P=sum(a[k]*x^k for k in range(d+1)) for i in range(len(L)) equa.append(P.substitute(x=i)==L[i]) solut=solve(equa,a,solution_dict=True) ans=P.substutite(solut[0]) return ans