[Tutor] Re: [Python-Help] functions to integrate, multiply and divide polynomials
Julieta Rangel
julieta_rangel@hotmail.com
Mon, 23 Apr 2001 20:27:03 -0500
<html><DIV>
<P><BR><BR></P>
<DIV></DIV>
<P><BR><BR></P></DIV>
<DIV></DIV>
<DIV></DIV>
<P><BR><BR></P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>From: "Alex Martelli" <ALEAXIT@YAHOO.COM>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>To: "Julieta" <JULIETA_RANGEL@HOTMAIL.COM>, <HELP@PYTHON.ORG>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Subject: Re: [Python-Help] functions to integrate, multiply and divide polynomials
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Date: Sun, 22 Apr 2001 00:15:39 +0200
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>"Julieta" <JULIETA_RANGEL@HOTMAIL.COM>writes:
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>"""
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>I am currently working on a project, which is intended to build functions to
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>1)Integrate a polynomial, 2) to multiply and 3) to divide two polynomials,
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>and to test that the class and function are working properly. I am new at
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>using Python, so any kind of help would be appreciated. Questions or
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>comments feel free to e-mail me at julieta_rangel@hotmail.com
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>"""
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>The natural Python approach is to first build a Polynomial *class* to
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>encapsulate
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>the task of holding the coefficients. There are many choices you can make
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>about
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>how to represent a polynomial, and encapsulating the representation (thus
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>the
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>choice!) in a class, with methods to return the degree of the polynomial and
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>the
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>coefficient for any power (ensure a 0.0 is returned for arbitrarily high
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>power, for
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>ease of other computations!), lets you tune representation freely.
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Don't make the error of mixing higher-level functionality in the class that
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>just
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>holds the representation; have a *low*-level class that "just holds the
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>poly's
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>representation", and use it from higher-level functionality (inheritance
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>works
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>well for that, for example). To test correct functioning, you'll want to be
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>able
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>to read ("parse") a string representing a polynomial, creating a Polynomial
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>instance object, and emit a readable representation from one such instance.
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Being able to build and modify the Polynomial instance will also be
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>important
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>for the other functions (a way to arbitrarily set any power's coefficient,
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>for
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>example -- remember the Polynomial instance will need to update its degree
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>when you do that).
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Once you have the representation class, integration and multiplication are
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>easy. Division is hard, but there are good resources on the net, that
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>explain how it's done by hand --
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>http://www.sosmath.com/algebra/factor/fac01/fac01.html.
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Once you CAN do it by hand, coding it into Python is not very hard.
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>The key for each operation is to think in term of loops over all powers
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>up to the highest degree of interest. You know the formulas that give
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>the coefficient of each power in the result in terms of those in the
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>operands; for example, in multiplication, each power i in one operand
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>and j in the other will add the product of their coefficients to the
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>coefficient of power i+j in the result. Start to build the result with
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>all coefficients zero, write two nested loops over the powers in the
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>operands, and it takes less to write in Python than in English -- as
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>long as your low-level class supplies the right methods, of course.
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Alex
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>> Alex, Daniel, and Gregor</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>Thanks for your suggestions. The manual math part of multiplying, integrating, differentiating, and dividing polynomials does not seem that bad. I'm a high school math teacher, so I have an idea of the manual labor needed in this project; however, it is the programming that I need to work on. If you don't mind, would you take a look at the following and tell me if I'm headed in the right direction?</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>> import string<BR>class Poly:<BR> def __init__ ( self, v = 'x', c = [0]):<BR> """__init__():<BR> Initializes a polynomial<BR> Default variable is x<BR> Default polynomial is zero"""<BR> self.var = v<BR> self.coef = c<BR> self.deg = len(c)-1<BR> def __str__ (self):<BR> """__str__():<BR> Converts a polynomial into a string"""<BR> x = `self.coef[0]`<BR> for i in range (1, self.deg+1):<BR> x = x + " + " + `self.coef[i]` + self.var + "^" + `i`<BR> return x</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>def Derivative(p):<BR> """Input: an instance of a polynomial<BR> Output: a polynomial that is the derivative of the input polynomial<BR> Side Effects: None"""<BR> pv=p.var<BR> pc=[]<BR> if p.deg==0:<BR> return Poly (v=pv,c=[0])<BR> else:<BR> for i in range(0,p.deg):<BR> d=(i+1)*p.coef[i+1]<BR> pc.append(d)<BR> return Poly (v=pv,c=pc)<BR>______________________________________________________</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P># This next line imports the polynomial class and the Derivative function<BR>from dkPoly import Poly<BR>from dkPoly import Derivative</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P># This is just a start of the function Integrate<BR># IT DOES NOT WORK !!!!!!<BR>def Integrate(p):<BR> """Input: an instance of a polynomial<BR> Output: a polynomial that is the integral of the input polynomial<BR> Side Effects: None"""<BR> pv=p.var<BR> return p</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P># This is just a start of the function Multiply<BR># IT DOES NOT WORK !!!!!!<BR>def Multiply(p,q):<BR> """Input: two instances of a polynomial<BR> Output: a polynomial that is the product of the input polynomials<BR> Side Effects: None"""<BR> pv=p.var<BR> return p</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P># This is just some code to test that the class and function are working properly</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>r = Poly (c=[5])<BR>p = Poly (c = [-1,1,2])<BR>q = Poly (v = 'y', c = [-1,0,2,0,4,3])<BR>print "The degree of polynomial %s is %d." % (p,p.deg)<BR>print " "<BR>print "The degree of polynomial %s is %d." % (q,q.deg)<BR>print " "<BR>print "The degree of polynomial %s is %d." % (r,r.deg)<BR>print " "<BR>m = Derivative(p)<BR>n = Derivative(q)<BR>o = Derivative(r)<BR>print "The derivative of %s is: \n %s" % (p,m)<BR>print " "<BR>print "The derivative of %s is: \n %s" % (q,n)<BR>print " "<BR>print "The derivative of %s is: \n %s" % (r,o)<BR>print " "</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P># This is some code to test new functions<BR>t = Integrate(p)<BR>u = Multiply(p,q)<BR>print "The intgral of %s is: \n %s" % (p,t)<BR>print " "<BR>print "The product of %s and \n %s is: \n %s" % (p,q,u)<BR>print " "<BR></P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>If you see a serious mistake, or have any suggestions for this code, would you please let me know? Any input is greatly appreciated.</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<P>Julieta</P>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>_________________________________________________________
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Do You Yahoo!?
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>Get your free @yahoo.com address at http://mail.yahoo.com
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV>
<DIV></DIV><br clear=all><hr>Get your FREE download of MSN Explorer at <a href="http://explorer.msn.com">http://explorer.msn.com</a><br></p></html>