[Tutor] attaching the first element in a list to a string
Benoit Dupire
bdupire@seatech.fau.edu
Wed, 02 May 2001 21:33:02 -0400
I modified your program in this way:
class Poly:
def __init__ ( self, v='x' , c = [0]):
"""__init__():
Initializes a polynomial
Default variable is x
Default polynomial is zero"""
self.var = v
self.coef = c
self.deg = len(c)-1
self.length = len(c)
def __str__ (self):
"""__str__():
Converts a polynomial into a string"""
x = `self.coef[0]`
for i in range (1, self.deg+1):
x = x + "+" +`self.coef[i]` + self.var + "^" + `i`
return x
def integrate(self):
"""Input: an instance of a polynommial
Output: a polynomial that is the integral of the input polynomial
Side Effects: None"""
coef=[0]
self.deg= self.deg +1
for i in range(0,self.deg):
coef.append(self.coef[i]/float(i+1))
self.coef= coef
degree = input('Enter the degree of your polynomial')
coef=[]
for i in range (0,degree + 1):
coefficient = input('Enter the coefficient for x^ %i ? ' %i)
coef.append(coefficient)
s= Poly (c = coef)
The result is now the good one:
>>> reload(poly)
Enter the degree of your polynomial3
Enter the coefficient for x^ 0 ? 2
Enter the coefficient for x^ 1 ? 5
Enter the coefficient for x^ 2 ? 6
Enter the coefficient for x^ 3 ? 1
<module 'poly' from 'C:\Python20\poly.py'>
>>> poly.s.integrate()
>>> print poly.s
0+2.0x^1+2.5x^2+2.0x^3+0.25x^4
What are the difference with your program?
integrate is now a method of the class Poly
so i can use it like this...
s= Poly( c=[1 5 6]) # here, we use __init__
print s # here we use __str__
s.integrate() # guess what ?
So the first argument of integrate() is now 'self' (the polynome itself)
I first increment the degree (because that's the definition of the integration
of a polynom)
The first coeff should normally be 'k', we need an initial condition to set it,
so for now i put 0
next coeff = coeff (degree 0) / (0+1)
next coeff = coeff (degree 1)/ (1+1)
etc...
When i have the new list of coeff, i set it for the current polynom (
self.coef= coef)
You can also add your derivation function as a method, and use it like
s.derivate()
Benoit
Julieta Rangel wrote:
> I have this problem. I'm trying to write a program that calculates the
> integral of a polynomial. This is what I have so far:
>
> import string
> class Poly:
> def __init__ ( self, v='x' , c = [0]):
> """__init__():
> Initializes a polynomial
> Default variable is x
> Default polynomial is zero"""
> self.var = v
> self.coef = c
> self.deg = len(c)-1
> self.length = len(c)
> def __str__ (self):
> """__str__():
> Converts a polynomial into a string"""
> x = `self.coef[0]`
> for i in range (1, self.deg+1):
> x = x + "+" +`self.coef[i]` + self.var + "^" + `i`
> return x
>
> class Poly2:
> def __init__ (self, v='x' , c = [0]):
> """__init__():
> Initializes a polynomial
> Default variable is x
> Default polynomial is zero"""
> self.var = v
> self.pc = c
> self.deg =len(c)-1
> self.length = len(c)
> def __str__(self):
> """__str__():
> Converts a second polynomial into a string"""
> x = `self.pc[0]`
> for i in range (0, self.deg+1):
> x = x + "+" +`self.pc[i]`+ self.var + "^" +`i+2`
> return x
>
> def Integrate(p):
> """Input: an instance of a polynommial
> Output: a polynomial that is the integral of the input polynomial
> Side Effects: None"""
> pv=p.var
> pc=[]
> for i in range(0,p.deg):
> I=p.coef[i+1]/float(i+2)
> pc.append(I)
> return Poly2 (v=pv,c=pc)
>
> degree = input('Enter the degree of your polynomial')
> coef=[]
> for i in range (0,degree + 1):
> coefficient = input('Enter the coefficient for x^ %i ? ' %i)
> coef.append(coefficient)#attach the coefficient entered to the end of
> the
> #list named coef.
>
> s= Poly (c = coef)
> t= Integrate(s)
> # This is some code to test new functions
>
> print "The integral of %s is: \n %s" % (s,t)
> print " "
>
> My problem is that when I run it I get this:
>
> >>>reload(poly1)
> Enter the degree of your polynomial3
> Enter the coefficient for x^ 0 ? 2
> Enter the coefficient for x^ 1 ? 5
> Enter the coefficient for x^ 2 ? 6
> Enter the coefficient for x^ 3 ? 1
> The integral of 2+5x^1+6x^2+1x^3 is:
> 2.5+2.5x^2+2.0x^3+0.25x^4
>
> As you can see, the answer would be right if it weren't for the first term
> in the polynomial. The first term should be a 2x, so the answer should look
> like this: 2x^1+2.5x^2+2x^3+0.25x^4. Can anyone help me fix this problem?
> I think that I need to find a way to have the first term on the list labeled
> pc, which is 2 to be taken into consideration on the string representing the
> polynomial. Am I right? If so, How can I accomplish this? Also, I need to
> find a way so that when the coefficient of x^0 ==0 , the output should
> display a constant c along with the result, so that the answer looks like
> c+2.5x^2+2x^3+0.23x^4.
> Can anyone guide me in the right direction? All inputs or comments about my
> program are welcomed and appreciated.
>
> Julieta
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Benoit Dupire
Graduate Student
----------------
I'd like to buy a new Boomerang. How can i get rid of the old one?