473,425 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

class encapsulates monomials and polynomials

badech
16
Hello ,
can you explain me what does this function ( class ) do ?

Expand|Select|Wrap|Line Numbers
  1. class Doublet:
  2.     def __init__(self,referenceMonome,refSuiv):
  3.         self.refMon = referenceMonome
  4.         self.suiv = refSuiv
  5.     def __repr__(self):
  6.         res=''
  7.         cour=self
  8.         while cour!=None:
  9.             res+=str(cour.refMon)
  10.             cour=cour.suiv
  11.         if res[0]=='+':
  12.             return res[1:]
  13.         return res   
thank you
Jan 3 '07 #1
24 2592
bartonc
6,596 Expert 4TB
Hello ,
can you explain me what does this function ( class ) do ?

Expand|Select|Wrap|Line Numbers
  1. class Doublet:
  2.     def __init__(self,referenceMonome,refSuiv):
  3.         self.refMon = referenceMonome
  4.         self.suiv = refSuiv
  5.     def __repr__(self):
  6.         res=''
  7.         cour=self
  8.         while cour!=None:
  9.             res+=str(cour.refMon)
  10.             cour=cour.suiv
  11.         if res[0]=='+':
  12.             return res[1:]
  13.         return res   
thank you
I don't know French and don't know many who do, and much of this lacks any context, but:
If suiv is "next" then this it looks like this is a linked list of some type and when this object's repr() is called, it returns referenceMonome from each member in the chain. It then removes the "+" from the head of the result.
It would be very helpful if you could translate your posts for us (non-bilingual) experts. Thanks,
Barton
Jan 4 '07 #2
bvdet
2,851 Expert Mod 2GB
Hello ,
can you explain me what does this function ( class ) do ?

Expand|Select|Wrap|Line Numbers
  1. class Doublet:
  2.     def __init__(self,referenceMonome,refSuiv):
  3.         self.refMon = referenceMonome
  4.         self.suiv = refSuiv
  5.     def __repr__(self):
  6.         res=''
  7.         cour=self
  8.         while cour!=None:
  9.             res+=str(cour.refMon)
  10.             cour=cour.suiv
  11.         if res[0]=='+':
  12.             return res[1:]
  13.         return res   
thank you
Monome - Monomial? Like Barton, I don't know French.
It looks like the class is accumulating a series of monomials - similar to another of your threads. A monomial is an algebraic expression containing only one term as in:
Expand|Select|Wrap|Line Numbers
  1. 7*x**2
Just guessing.
Jan 4 '07 #3
bartonc
6,596 Expert 4TB
Monome - Monomial? Like Barton, I don't know French.
It looks like the class is accumulating a series of monomials - similar to another of your threads. A monomial is an algebraic expression containing only one term as in:
Expand|Select|Wrap|Line Numbers
  1. 7*x**2
Just guessing.
I thought maybe it's collecting friends. Could be.
Jan 4 '07 #4
badech
16
Hello
i'm sorry , I had not given many details which could have
helped you to better include/understand my question
here is my problem
the framed functions are given
# Polynomials with two variables:
# the choices:
# the monomials which make a polynomial make only this polynomial:
# they do not compose of another polynomial
# if two polynomials contain the same monomial, this monomial exists in two specimens
# the monomials of a polynomial NEVER have null coefficient


__________________________________________________ _______________

Expand|Select|Wrap|Line Numbers
  1. class Monome: # monome =monomial
  2.     def __init__(self, coefficient, exposantX, exposantY): #exposant = power
  3.         self.coeff = coefficient 
  4.         self.expoX = exposantX 
  5.         self.expoY = exposantY 
  6.     def __repr__(self): 
  7.         s=str(self.coeff)+"*x^"+str(self.expoX)+"*y^"+str(self.expoY) 
  8.         if self.coeff<0: return s 
  9.         return "+"+s 
  10.     def __lt__(self, autre):  #autre=other
  11.         return (self.expoX < autre.expoX) or ((self.expoX == 
  12. autre.expoX) and (self.expoY < autre.expoY)) 
  13.     def __gt__(self,autre): 
  14.         return (self.expoX > autre.expoX) or ((self.expoX == 
  15. autre.expoX) and (self.expoY >autre.expoY)) 
__________________________________________________ _______________


__________________________________________________ _______________

Expand|Select|Wrap|Line Numbers
  1. class Doublet: 
  2.     def __init__(self,referenceMonome,refSuiv):   # suiv=next
  3.         self.refMon = referenceMonome 
  4.         self.suiv = refSuiv 
  5.     def __repr__(self): 
  6.         res=''  # res=result
  7.         cour=self  # cour = current
  8.         while cour!=None: 
  9.             res+=str(cour.refMon) 
  10.             cour=cour.suiv 
  11.         if res[0]=='+': 
  12.             return res[1:] 
  13.         return res 
__________________________________________________ _______________


__________________________________________________ _______________
Expand|Select|Wrap|Line Numbers
  1. def degre(mon): #degré = degree
  2.     return(mon.expoX + mon.expoY) 
__________________________________________________ _______________


__________________________________________________ _______________
Expand|Select|Wrap|Line Numbers
  1. def degreX (mon): 
  2.     return mon.expoX 
__________________________________________________ _______________




the functions which follow, I must correct them and/or supplement them

Expand|Select|Wrap|Line Numbers
  1. def insereMonome(coefficient, exposantX, exposantY, doublett) : #insere=insert
  2.     # to seek the last monomial before this new monomial 
  3. # to make insertion by creating the monomial which will be placed 
  4. # placed well for the ascending order "
  5.         newRefMon = Monome(coefficient, exposantX, exposantY) 
  6.         print "nouveau monome : ",newRefMon  
  7.                                                               # nouveau monome = new monomial
  8.         return None 
  9.  
  10.  
  11. def insereCopieMonome( refMon, doublett): 
  12.     #DO NOT modify this function
  13.     return insereMonome(refMon.coeff, refMon.expoX, refMon.expoY, 
  14. doublett) 
  15.  
  16.  
  17. def ajouteDoubletsRec(listeDoublet1, listeDoublet2): #ajoute=add
  18.     # returns a new list of doublets obtained by 
  19.     # fusion of the two lists in new one
  20.     # ATTENTION: not to forget to treat the case where two opposite 
  21.     # monomials are added!!!!! 
  22.     # in this case, not to reveal a monomial with a null coefficient.
  23.     return None 
  24.  
  25.  
  26. def multMonomeMonome(refMon1, refMon2): #mult=multiply
  27.     return Monome(refMon1.coeff * refMon2.coeff, refMon1.expoX + 
  28. refMon2.expoX, refMon1.expoY + refMon2.expoY) 
  29. def multMonomeListeDoublets(refMon, listeDoublets): 
  30.     res = None 
  31.     return res 
  32.  
  33.  
  34. def multDeuxListesDoublets(ld1, ld2): 
  35. # multDeuxListesDoublets = mulitply 2 lists of doublets
  36. # doublet = list of monomials
  37.     res = None 
  38.     return res 
  39.  
__________________________________________________ _______________
Expand|Select|Wrap|Line Numbers
  1. class Polynome: #polynome =polynomial
  2.     def __init__(self,listePrincipale): #liste pricipale=main list
  3.         self.lp = listePrincipale 
  4.         self.lDegre = creerListeDegre(listePrincipale)  creerListe=create list
  5.         triDegre(self.lDegre) # tri =sorting
  6.  
  7.     def __repr__(self): 
  8.         doub = self.lp 
  9.         res = " liste pour l'ordre < \n" #l ist for the order
  10.         res = res + str(doub) 
  11.         res = res + "\n \n " 
  12.         res = res + " liste pour l'ordre du degré total \n" 
  13.                                                               #list for the order of the total degree
  14.         doub = self.lDegre 
  15.         res = res + str(doub) 
  16.         return res 
__________________________________________________ _______________

__________________________________________________ _______________
Expand|Select|Wrap|Line Numbers
  1. def creerListeDegre(ld): 
  2.     if ld == None: 
  3.         return None 
  4.     else: 
  5.         return Doublet(ld.refMon, creerListeDegre(ld.suiv)) 
__________________________________________________ _________________________*__

Expand|Select|Wrap|Line Numbers
  1. def triDegre(doublett): #tri =sorting
  2.     # we modify by exchange the values of the fields "refMon"of the listdoublett,
  3.     # so that  the list doublett is increasing for the order of the degree 
  4.  
  5. return ...
  6.  
  7.  
  8. def add(pol1, pol2): 
  9.         return None 
  10.  
  11.  
  12. def mul(pol1, pol2): 
  13.         return None 
  14.  
  15.  
  16.  
  17. # END of the definition of the functions and methods
  18. #  -------------------------------------------------------------- 
  19. #tests on the monomials
  20. #m1 = Monome(5,2,6) 
  21. #m2 = Monome(6,2,8) 
  22. #m3 = Monome(-4,3,1) 
  23. #m4 = Monome(-6,0,1) 
  24. #print "m1 : ",m1, "     m2 : ",m2 
  25. #print "m3 : ",m3, "    m4 : ",m4 
  26. #print m1 < m2 
  27. #print m3 < m4 
  28. #print 
  29. #tests on the doublets 
  30. #d1 = Doublet(m1,None) 
  31. #print "doublet d1 : ",d1 
  32. #doublet1 = Doublet(Monome(-4,7,1),Doublet(Monome(2,4,3),None)) 
  33. #doublet2 = Doublet(m1,Doublet(m2,None)) 
  34. #print 
  35. #print"doublet1 : ",doublet1, "   doublet2 : ",doublet2 
  36. #print 
  37. # tests of the function insereMonome
  38. #doub1 = insereMonome(5, 2, 6, None) 
  39. #doub1 = insereMonome(6,3,8, doub1) 
  40. #doub2 = insereMonome(5,10,3, None) 
  41. #doub2 = insereMonome(4,2,6, doub2) 
  42. #doub2 = insereMonome(-3,11,7, doub2) 
  43. #doub2 = insereMonome(-6, 3,8, doub2) 
  44. #doub2 = insereMonome(2,3,0, doub2) 
  45. #print "doub1 : ",doub1 
  46. #print"    doub2 : ",doub2 
  47. # tests of the function ajouteDoubletsRec 
  48. #listeAjout = ajouteDoubletsRec(doub1, doub2) 
  49. #print "sum of 2 lists : ", listeAjout 
  50.  
  51.  
  52. # tests of the function multMonomeListeDoublets
  53. # TESTS TO BE BUILT ( we must write some tests of this 
  54. # function ( multMonomeListeDoublets)
  55.  
  56. # tests of the function multDeuxListesDoublets 
  57. # TESTS TO BE BUILT ( we must write some tests of this 
  58. # function ( multDeuxListesDoublets)
  59.  
  60. # tests of the function creerListeDegre 
  61. #TESTS TO BE BUILT ( we must write some tests of this 
  62. # function ( creerListeDegre)
  63.  
  64. # tests of the function triDegre 
  65. #TESTS TO BE BUILT ( we must write some tests of this 
  66. # function ( triDegre )
  67.  
  68. # tests of polynomials: representation, addition, multiplication
  69. # TESTS TO BE BUILT ( we must write some tests )
  70.  
  71. #pol1 = Polynome(doublet1) 
  72. #pol2 = Polynome(doublet2) 
  73. #som = add(pol1, pol2) 
  74. #prod = mult(pol1, pol2) 
  75. #print som 
  76. #print prod 
  77.  
for example , the calss Doublet causes problems to me
Expand|Select|Wrap|Line Numbers
  1. >If, for example, I write
  2.  
  3. > >>> a=Monome(2,7,11) 
  4. > >>> a 
  5. > +2*x^7*y^11 
  6. > >>> b=Monome(-1,17,35) 
  7. > >>> b 
  8. > -1*x^17*y^35 
  9. > >>> c=Doublet(a,b) 
  10. > >>> t 
  11. > Traceback (most recent call last): 
  12. >   File "<pyshell#5>", line 1, in <module> 
  13. >     t 
  14. >   File "C:\...\polynomesXY2_DEVOIR_Enonce.py", line 36, in __repr__ 
  15. >     res+=str(cour.refMon) 
  16. > AttributeError: Monome instance has no attribute 'refMon' 
  17.  

this returns an error message

could you help me PLEASE?
thank you in advance and I hope to have been clear ( understood )
thank you
Jan 5 '07 #5
bartonc
6,596 Expert 4TB
I've added [ code] [ /code] tags. I'll take a look now. In the mean time, please learn to use these (without spaces) in "Posting Guidelines" in several places on this forum.
Jan 5 '07 #6
badech
16
Hello bartonc ,
can you change the title please ?
thanks
Jan 6 '07 #7
badech
16
thank you bartonc :)
Jan 6 '07 #8
bvdet
2,851 Expert Mod 2GB
I added spaces at the plus and minus signs so the polynomial is easier to read.
Expand|Select|Wrap|Line Numbers
  1. m1 = Monome(5,2,6) 
  2. m2 = Monome(6,2,8) 
  3. print "\nm1 : ", m1
  4. print "m2 : ", m2 
  5. d2 = Doublet(m1,Doublet(m2, Doublet(Monome(4,7,1), None)))
  6. print "Doublet(m1,Doublet(m2, Doublet(Monome(-4,7,1), None))): ", d2
Output:
Expand|Select|Wrap|Line Numbers
  1. m1 :   + 5*x^2*y^6
  2. m2 :   + 6*x^2*y^8
  3. Doublet(m1,Doublet(m2, Doublet(Monome(-4,7,1), None))):   + 5*x^2*y^6 + 6*x^2*y^8 + 4*x^7*y^1
The first argument to Doublet is a Monome object and the second is None or a Doublet object
Jan 6 '07 #9
bvdet
2,851 Expert Mod 2GB
badech,

I was playing with an instance of your Doublet class and noticed there was no iteration method. I have never used 'yield' in code before:
Expand|Select|Wrap|Line Numbers
  1. # Doublet iterator
  2. def iterDoub(d):
  3.     while d:
  4.         yield d.refMon
  5.         d = d.suiv
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> print "Doublet 'd1':", d1
  2. Doublet 'd1':  + 9*x^2*y^6 + 2*x^3*y^0 + 5*x^3*y^9 - 2*x^4*y^2 + 5*x^10*y^3 - 3*x^11*y^7
  3. >>> for d in iterDoub(d1):
  4. ...     print d
  5. ...     
  6.  + 9*x^2*y^6
  7.  + 2*x^3*y^0
  8.  + 5*x^3*y^9
  9.  - 2*x^4*y^2
  10.  + 5*x^10*y^3
  11.  - 3*x^11*y^7
  12. >>> 
It seems interestingly simple. Maybe you can use it.
Jan 7 '07 #10
bartonc
6,596 Expert 4TB
badech,

I was playing with an instance of your Doublet class and noticed there was no iteration method. I have never used 'yield' in code before:
Expand|Select|Wrap|Line Numbers
  1. # Doublet iterator
  2. def iterDoub(d):
  3.     while d:
  4.         yield d.refMon
  5.         d = d.suiv
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> print "Doublet 'd1':", d1
  2. Doublet 'd1':  + 9*x^2*y^6 + 2*x^3*y^0 + 5*x^3*y^9 - 2*x^4*y^2 + 5*x^10*y^3 - 3*x^11*y^7
  3. >>> for d in iterDoub(d1):
  4. ...     print d
  5. ...     
  6.  + 9*x^2*y^6
  7.  + 2*x^3*y^0
  8.  + 5*x^3*y^9
  9.  - 2*x^4*y^2
  10.  + 5*x^10*y^3
  11.  - 3*x^11*y^7
  12. >>> 
It seems interestingly simple. Maybe you can use it.
Great post, B.V.! It's awesome that you are finding inspiration from members' questions.
Jan 7 '07 #11
bvdet
2,851 Expert Mod 2GB
Great post, B.V.! It's awesome that you are finding inspiration from members' questions.
Thanks Barton. I am learning new things regularly by following threads and participating where I can.

badech - Have you made any progress on your assignment? Show us what you have accomplished. Try to solve it one step at a time. We should not write it for you, but I am sure we can help if you are having problems with your code.
Jan 7 '07 #12
bvdet
2,851 Expert Mod 2GB
I did not care for the way the multinomials were represented, so I reworked special methods '__repr__':
Expand|Select|Wrap|Line Numbers
  1. class Monome: # monome = monomial
  2.     def __init__(self, coefficient, exposantX, exposantY): #exposant = power
  3.         self.coeff = coefficient 
  4.         self.expoX = exposantX 
  5.         self.expoY = exposantY
  6.  
  7.     def __repr__(self):
  8.         if abs(self.coeff) == 1:
  9.             coeffStr = "("
  10.         else:
  11.             coeffStr = '%s(' % (abs(self.coeff))
  12.         if self.coeff < 0:
  13.             preStr = "-"
  14.         else:
  15.             preStr = "+"
  16.         return '%s%sx^%s*y^%s)' % (preStr, coeffStr, self.expoX, self.expoY)
  17.  
  18.     def __lt__(self, autre):  #autre=other
  19.         return (self.expoX < autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY < autre.expoY)) 
  20.     def __gt__(self,autre): 
  21.         return (self.expoX > autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY >autre.expoY))
  22.  
  23. class Doublet: 
  24.     def __init__(self,referenceMonome,refSuiv):   # suiv=next
  25.         self.refMon = referenceMonome 
  26.         self.suiv = refSuiv
  27.  
  28.     def __repr__(self):
  29.         res = str(self.refMon)
  30.         a = self.suiv
  31.         while a:
  32.             res += str(a.refMon)
  33.             a = a.suiv
  34.         if res[0] == '+': 
  35.             return res[1:] 
  36.         return res
This is what it looks like now:
Expand|Select|Wrap|Line Numbers
  1. Doublet 'd1': 9(x^2*y^6)+2(x^3*y^0)+5(x^3*y^9)-2(x^4*y^2)+5(x^10*y^3)-3(x^11*y^7)
  2. Iteration over Doublet 'd1':
  3. +9(x^2*y^6)
  4. +2(x^3*y^0)
  5. +5(x^3*y^9)
  6. -2(x^4*y^2)
  7. +5(x^10*y^3)
  8. -3(x^11*y^7)
  9.  
  10. 6(x^3*y^8)+5(x^3*y^9)-2(x^4*y^2)
  11. 4(x^2*y^6)+2(x^3*y^0)-6(x^3*y^8)+5(x^10*y^3)-3(x^11*y^7)
  12. -(x^-2*y^-3)+7(x^4*y^12)+4(x^5*y^6)+6(x^7*y^1)
Jan 7 '07 #13
bartonc
6,596 Expert 4TB
Thanks Barton. I am learning new things regularly by following threads and participating where I can.

badech - Have you made any progress on your assignment? Show us what you have accomplished. Try to solve it one step at a time. We should not write it for you, but I am sure we can help if you are having problems with your code.
I often wish that I had more time to play with some of these exercises. I'm right in the middle of good sized project with a deadline looming.

Maybe no progress, maybe just (less that avid poster) - I know that he checked in this morning.
Jan 7 '07 #14
badech
16
Hello
I am blocked on the first question, I do not manage to write the function insereMonome because I do not understand how the "calss" function.
can you help me to write this first function , and then i'll do the others
thank you
Jan 7 '07 #15
bartonc
6,596 Expert 4TB
Hello
I am blocked on the first question, I do not manage to write the function insereMonome because I do not understand how the "calss" function.
can you help me to write this first function , and then i'll do the others
thank you
Perhaps this is what you are looking for?
Expand|Select|Wrap|Line Numbers
  1. class Monome: # monome = monomial
  2.     def __init__(self, coefficient, exposantX, exposantY): #exposant = power
  3.         self.coeff = coefficient
  4.         self.expoX = exposantX
  5.         self.expoY = exposantY
  6.  
  7.     def __repr__(self):
  8.         if abs(self.coeff) == 1:
  9.             coeffStr = "("
  10.         else:
  11.             coeffStr = '%s(' % (abs(self.coeff))
  12.         if self.coeff < 0:
  13.             preStr = "-"
  14.         else:
  15.             preStr = "+"
  16.         return '%s%sx^%s*y^%s)' % (preStr, coeffStr, self.expoX, self.expoY)
  17.  
  18.     def __lt__(self, autre):  #autre=other
  19.         return (self.expoX < autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY < autre.expoY))
  20.     def __gt__(self,autre):
  21.         return (self.expoX > autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY >autre.expoY))
  22.  
  23. class Doublet:
  24.     def __init__(self,referenceMonome,refSuiv):   # suiv=next
  25.         self.refMon = referenceMonome
  26.         self.suiv = refSuiv
  27.  
  28.     def __repr__(self):
  29.         res = str(self.refMon)
  30.         a = self.suiv
  31.         while a:
  32.             res += str(a.refMon)
  33.             a = a.suiv
  34.         if res[0] == '+':
  35.             return res[1:]
  36.         return res
  37.  
  38.  
  39. m1 = Monome(5,2,6)
  40. m2 = Monome(6,2,8)
  41. print "\nm1 : ", m1
  42. print "m2 : ", m2
  43. d2 = Doublet(m1,Doublet(m2, Doublet(Monome(4,7,1), None)))
  44. print "Doublet(m1,Doublet(m2, Doublet(Monome(-4,7,1), None))): ", d2
  45.  
Jan 7 '07 #16
bvdet
2,851 Expert Mod 2GB
Hello
I am blocked on the first question, I do not manage to write the function insereMonome because I do not understand how the "calss" function.
can you help me to write this first function , and then i'll do the others
thank you
The sort function acts like a bubble sort. I learned this from a thread a couple of weeks ago. Function 'insereMonome' is a wrapper for the 'Doublet' class and returns a sorted Doublet object.
Expand|Select|Wrap|Line Numbers
  1. # sort function on Doublet object in ascending order
  2. def sortDoub(d):
  3.     while d != None:
  4.         if d.suiv != None:
  5.             d2 = d.suiv
  6.             while d2 != None:
  7.                 if d.refMon > d2.refMon:
  8.                     d.refMon, d2.refMon = d2.refMon, d.refMon
  9.                 d2 = d2.suiv
  10.         d = d.suiv
  11.  
  12. # Add new Monome object to existing Doublet object and sort in ascending order
  13. def insereMonome(coefficient, exposantX, exposantY, d=None): #insere=insert
  14.     newDoub = Doublet(Monome(coefficient, exposantX, exposantY), d)
  15.     sortDoub(newDoub)
  16.     return newDoub
Sample output:
Expand|Select|Wrap|Line Numbers
  1. doub1 = insereMonome(6,3,8, doub1):  5(x^2*y^6)+6(x^3*y^8)-2(x^4*y^2)
  2. doub2 = insereMonome(2,3,0, doub2):  4(x^2*y^6)+2(x^3*y^0)-6(x^3*y^8)+5(x^10*y^3)-3(x^11*y^7)
  3. Combine and simplify 2 Doublet objects:  9(x^2*y^6)+2(x^3*y^0)-2(x^4*y^2)+5(x^10*y^3)-3(x^11*y^7)
Here's a hint to simplify a multinomial Doublet object:
Iterate on the object and create a dictionary where you can sum coefficients with like exponents. From the dictionary, create and return a new and simplified Doublet object, skipping a monomial with a coeffieient of 0.
Jan 7 '07 #17
badech
16
euh...
no
these functions are given
i want to write a function which allows to insert a monomial in a polynomial
for example , if
P=2*X^5*Y^3+5*X^3*Y^7-2
and M= -7*X^4Y^6
then
insereMonome(M,P) returns
2*X^5*Y^3 -7*X^4Y^6 +5*X^3*Y^7-2
Jan 7 '07 #18
badech
16
perfect ...
thank you very much bvdet :)
Jan 7 '07 #19
bvdet
2,851 Expert Mod 2GB
I often wish that I had more time to play with some of these exercises. I'm right in the middle of good sized project with a deadline looming.

Maybe no progress, maybe just (less that avid poster) - I know that he checked in this morning.
I understand about deadlines. I will have some coming up in the near future. Now you know where I will be if I disappear for a few days.
Jan 7 '07 #20
badech
16
what do you think about this one ?
Expand|Select|Wrap|Line Numbers
  1. def ajouteDoubletsRec(listeDoublet1, listeDoublet2):
  2.     # renvoie une nouvelle liste de doublets obtenue par
  3.     # fusion des deux listes en une nouvelle
  4.     # ATTENTION : ne pas oublier de traîter le cas où l'on additionne deux monomes opposés !!!!!
  5.     # dans ce cas, NE PAS faire apparaître un monome avec un coefficient nul.
  6.     if listeDoublet1==None :
  7.         return listeDoublet2
  8.     elif listeDoublet2==None :
  9.         return listeDoublet1
  10.     else :
  11.         listeDoublet1.suiv=ajouteDoubletsRec(listeDoublet1.suiv, listeDoublet2)
  12.     sortDoub(listeDoublet1)
  13.     return listeDoublet1
Jan 7 '07 #21
badech
16
def ajouteDoubletsRec(listeDoublet1, listeDoublet2): #ajoute=add
...
# ATTENTION: not to forget to treat the case where two opposite
# monomials are added!!!!!
# in this case, not to reveal a monomial with a null coefficient
.
return None
...this part is missing
Jan 7 '07 #22
bvdet
2,851 Expert Mod 2GB
what do you think about this one ?
Expand|Select|Wrap|Line Numbers
  1. def ajouteDoubletsRec(listeDoublet1, listeDoublet2):
  2.     # renvoie une nouvelle liste de doublets obtenue par
  3.     # fusion des deux listes en une nouvelle
  4.     # ATTENTION : ne pas oublier de traîter le cas où l'on additionne deux monomes opposés !!!!!
  5.     # dans ce cas, NE PAS faire apparaître un monome avec un coefficient nul.
  6.     if listeDoublet1==None :
  7.         return listeDoublet2
  8.     elif listeDoublet2==None :
  9.         return listeDoublet1
  10.     else :
  11.         listeDoublet1.suiv=ajouteDoubletsRec(listeDoublet1.suiv, listeDoublet2)
  12.     sortDoub(listeDoublet1)
  13.     return listeDoublet1
badech,

The function creates a never ending loop. You need to test your code in an IDE such as Pythonwin or Idle.

Remember that a Doublet is a combination of a Monome and another Doublet. I suggest that you iterate on one of the Doublets and combine each iteration Monome with the other Doublet.
Expand|Select|Wrap|Line Numbers
  1. outDoub = d1
  2. for m in iterDoub(d2):
  3.     outDoub = Doublet(m, outDoub)
  4.     return simplify_and_sort(outDoub)
You will need to write the simplification function. See my earlier post for suggestions. Test your code, and it should look something like this (Pythonwin):
Expand|Select|Wrap|Line Numbers
  1. >>> d4
  2. 2(x^2*y^3)+16(x^2*y^6)+2(x^4*y^6)+14(x^8*y^9)+(x^9*y^3)
  3. >>> d2
  4. -7(x^9*y^3)+8(x^9*y^3)+(x^2*y^3)+4(x^2*y^6)+4(x^2*y^6)+(x^4*y^6)-4(x^5*y^7)+4(x^5*y^7)+7(x^8*y^9)
  5. >>> d11 = ajouteDoubletsRec(d4, d2)
  6. >>> d11
  7. 3(x^2*y^3)+24(x^2*y^6)+3(x^4*y^6)+21(x^8*y^9)+2(x^9*y^3)
  8. >>> 
Jan 8 '07 #23
bvdet
2,851 Expert Mod 2GB
I have another improvement (in my mind) for the displayed representation of a Monome object:
Expand|Select|Wrap|Line Numbers
  1. class Monome: # monome = monomial
  2.     def __init__(self, coefficient, exposantX, exposantY): #exposant = power
  3.         self.coeff = coefficient 
  4.         self.expoX = exposantX 
  5.         self.expoY = exposantY
  6.  
  7.     def __repr__(self):
  8.         if abs(self.coeff) == 1:
  9.             coeffStr = "("
  10.         else:
  11.             coeffStr = '%s(' % (abs(self.coeff))
  12.         if self.coeff < 0:
  13.             preStr = "-"
  14.         else:
  15.             preStr = "+"
  16.         if self.expoX == 0:
  17.             return '%s%sy^%s)' % (preStr, coeffStr, self.expoY)
  18.         elif self.expoY == 0:
  19.             return '%s%sx^%s)' % (preStr, coeffStr, self.expoX)
  20.         return '%s%sx^%s*y^%s)' % (preStr, coeffStr, self.expoX, self.expoY)
Since a number raised to the '0' power == 1, it need not be included in the representation:
Expand|Select|Wrap|Line Numbers
  1. Doublet 'd1': 5(y^6)+4(x^2*y^6)+2(x^3)+5(x^3*y^9)-2(x^4*y^2)+5(x^10*y^3)-3(x^11*y^7)
  2. Iteration over Doublet 'd1':
  3. +5(y^6)
  4. +4(x^2*y^6)
  5. +2(x^3)
  6. +5(x^3*y^9)
  7. -2(x^4*y^2)
  8. +5(x^10*y^3)
  9. -3(x^11*y^7)
Jan 8 '07 #24
bvdet
2,851 Expert Mod 2GB
Here is another function for sorting a Doublet object. The sort itself should be much faster because it uses the built-in list.sort() method, but the sort list is created by iteration. List items can be deleted or moved around to change the sort results. It returns a sorted Doublet object, so the object can be passed directly to a return statement or assigned to a variable.
Expand|Select|Wrap|Line Numbers
  1. # Doublet iterator
  2. def iterDoub(d):
  3.     while d:
  4.         yield d.refMon
  5.         d = d.suiv
  6.  
  7. # return a sorted Doublet object in descending order of degree 
  8. def cmpDoub(d):
  9.     mList = []
  10.     for m in iterDoub(d):
  11.         mList.append((m.expoX+m.expoY, m.expoX, m.expoY, m.coeff, m))
  12.     mList.sort()
  13.     outDoub = None
  14.     for item in mList:
  15.         outDoub = Doublet(Monome(item[3], item[1], item[2]), outDoub)
  16.     return outDoub        
  17.  
  18. # Add new Monome object to existing Doublet object and sort in descending order of degree
  19. def insereMonome(coefficient, exposantX, exposantY, d=None): #insere=insert
  20.     return cmpDoub(Doublet(Monome(coefficient, exposantX, exposantY), d))
Sample output:
Expand|Select|Wrap|Line Numbers
  1. doub2 = insereMonome(5,10,3, None):  5(x^10*y^3)
  2. doub2 = insereMonome(4,2,6, doub2):  5(x^10*y^3)+4(x^2*y^6)
  3. doub2 = insereMonome(-3,11,7, doub2):  -3(x^11*y^7)+5(x^10*y^3)+4(x^2*y^6)
  4. doub2 = insereMonome(-6,3,8, doub2):  -3(x^11*y^7)+5(x^10*y^3)-6(x^3*y^8)+4(x^2*y^6)
  5. doub2 = insereMonome(2,3,0, doub2):  -3(x^11*y^7)+5(x^10*y^3)-6(x^3*y^8)+4(x^2*y^6)+2(x^3)
  6. doub2 = insereMonome(2,10,3, doub2):  -3(x^11*y^7)+5(x^10*y^3)+2(x^10*y^3)-6(x^3*y^8)+4(x^2*y^6)+2(x^3)
  7. doub2 = insereMonome(4,3,0, doub2):  -3(x^11*y^7)+5(x^10*y^3)+2(x^10*y^3)-6(x^3*y^8)+4(x^2*y^6)+4(x^3)+2(x^3)
  8. doub2 = insereMonome(2,2,6, doub2):  -3(x^11*y^7)+5(x^10*y^3)+2(x^10*y^3)-6(x^3*y^8)+4(x^2*y^6)+2(x^2*y^6)+4(x^3)+2(x^3)
HTH
Jan 11 '07 #25

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris | last post by:
Does anyone know of a good standalone implementation of multivariable polynomials in python? Thanks, Chris
7
by: adrin | last post by:
hi, anyone knows how does an algorithm for calculating GCD of two polynomials look like ? please help me if you can, or give some relevant links :)
5
by: thirtyseven | last post by:
I was wondering if anyone knew of some public code for a c++ class that represents a value, but can preserve fractions and powers and such without just turning the value into a float. Hard to...
8
by: Joe | last post by:
I have several classes which I need serialized. Here is their class definitions: public class TopContainer { private LevelTwoType m_levelTwo; public LevelTwoType LevelTwo { get {
1
by: beck2 | last post by:
hello i am angela and i would like if its kind of anyone to help me by writing me the code of adding 2 oplynomials? polynomials should be entered as where 3,5 and 1 are coefficients of X to the...
4
by: Matthias | last post by:
how do i make a program that gets numbers from a user. and then stores it into an array. using polynomials if the user entered 7 5 4 3 2 the polynomial would be 7x^4+5x^3+4x^2+3x+2 the program...
1
by: Motanyane Tlotliso | last post by:
I wish someone to help me in creating a programm in C++ that can be able to sum two polynomials, subtract one from the other and multiply both polynomials together using a ADT's(struct term). I...
0
by: =?ISO-8859-1?Q?J=FCrgen_B=F6hm?= | last post by:
Hello, (I already posted this at comp.lang.c++.moderated, but received no answers, maybe it is better placed here?): to implement a little symbolic computation system, dealing with...
1
by: madman228 | last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.