
""" polystuff.py (Python 3.x) For a curriculum segment when we're not yet wanting to build user-defined classes. showpoly -- return an evaluable string with x the variable evalpoly -- return a numeric result given an evaluable string and x's value multipoly -- return the product of two polynomials as a tuple of tuples """ from operator import itemgetter def showpoly(A): """ Return a close-to-classic polynomial as an evaluable Python string literal, given a sequence of tuples for input (see multipoly below). Some prettifying. >>> p1 = ((4,2),(-4,1),(-7,0)) >>> showpoly(p1) '4 * x**2 - 4 * x**1 - 7' """ terms = [] for coeff, exp in sorted(A, key=itemgetter(1), reverse=True): terms.append( "{sign} {coeff}{var}".format( sign = "+" if coeff > 0 else "-", coeff = abs(coeff), var = ' * x**{}'.format(exp) if exp>0 else "" )) ans = " ".join(terms) if ans[0] == "+": # prettify ans = ans[1:].strip() return ans def evalpoly(strpoly, x): """ Evaluates a polynomial string literal for a given x value >>> p1 = ((4,2),(-4,1),(-7,0)) >>> thestring = showpoly(p1) >>> thestring '4 * x**2 - 4 * x**1 - 7' >>> evalpoly(thestring, 10) 353 """ return eval(strpoly, {"x":x}) def multipoly(A, B): """ Multiply two polynomials, represented as sequences of tuples e.g.: 3*x**3 + 2*x**2 - x - 1 would be ((3,3),(2,2),(-1,1),(-1,0)) with internal pairs in any order (term order is arbitrary) >>> p1 = ((1,1),(-1,0)) >>> p2 = ((1,1),(1,0)) >>> multipoly(p1,p2) [(-1, 0), (1, 2)] >>> p1 = ((4,2),(-4,1),(-7,0)) >>> p2 = ((1,1),(3,0)) >>> multipoly(p1,p2) [(-21, 0), (-19, 1), (8, 2), (4, 3)] """ results = [] termsAB = [] # Cartesian product of terms in poly A and poly B # multiply coefficients, add exponents for termA in A: for termB in B: termsAB.append(( termA[0] * termB[0], # coefficients termA[1] + termB[1] # exponents )) # find maximum exponent degree = max([term[1] for term in termsAB]) # collect like terms (same exponent) for exp in range(degree+1): # from 0 to degree inclusive newterm = ( sum([term[0] for term in termsAB if term[1] == exp]), exp) if newterm[0] != 0: results.append(newterm) return results def _test( ): import doctest doctest.testmod() if __name__ == '__main__': _test()

Hello! Here is my representation of polynomials in Python as dictionaries. zero = {} one = {0:1} two = {0:2} x = {1:1} x2 = {2:1} # x^2 poly = {2:3, 1:4, 0:5} # 3*x^2 + 4*x + 5 # Some functions... def add_poly(poly1, poly2): # w1(x)+w2(x) tmp = dict(poly1) for k in poly2: tmp[k] = tmp.get(k,0) + poly2[k] if tmp[k] == 0: del tmp[k] return tmp def times_poly(number, poly): # n*w(x) if number == 0: return {} tmp = {} for k in poly: tmp[k] = number*poly[k] return tmp def mul_poly(poly1, poly2): # w1(x)*w2(x) tmp = {} for i in poly1: for j in poly2: tmp[i+j] = tmp.get(i+j,0) + poly1[i]*poly2[j] return tmp def diff_poly(poly): # [c*x^n]' = c*n*x^(n-1) tmp = {} for i in poly: tmp[i-1] = i*poly[i] if -1 in tmp: del tmp[-1] return tmp def eval_poly(poly, x): # w(x), Horner i = max(i for i in poly) p = poly[i] while i > 0: i = i-1 p = p * x + poly.get(i,0) return p def combine_poly(poly1, poly2): # w1(w2(x)), Horner i = max(k for k in poly1) tmp = {0:poly1[i]} while i > 0: i = i-1 tmp = add_poly(mul_poly(tmp, poly2), {0:poly1.get(i,0)}) return tmp def pow_poly(poly, n): # return combine_poly({n:1}, poly) tmp = {0:1} while n > 0: tmp = mul_poly(tmp, poly) n = n-1 return tmp Regards, Andrzej Kapanowski

I link back to our posts from here: http://mathforum.org/kb/message.jspa?messageID=7381491&tstart=0 <http://mathforum.org/kb/message.jspa?messageID=7381491&tstart=0>The next turn of the spiral might involve taking such functions and making them methods of a Polynomial object. Example framework: http://www.4dsolutions.net/ocn/python/simppoly.py So you'd have operator overloading and write things like:
p1 = Poly( {2:3, 1:4, 0:5} ) p2 = Poly( {1:1, 0:5} ) p1**4 p1 * p2 p1 + p2 etc.
Once you have polynomials as "math objects" you might compare them with other objects that also add, multiply etc. Questions about closure arise i.e. what operations might take you outside the Polynomial type for answers, etc. <http://www.4dsolutions.net/ocn/python/simppoly.py>Kirby On Thu, Feb 10, 2011 at 3:17 AM, Andrzej Kapanowski <ufkapano@gmail.com>wrote:
Hello! Here is my representation of polynomials in Python as dictionaries.
participants (2)
-
Andrzej Kapanowski
-
kirby urner