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