440,235 Members | 1,022 Online Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

# class encapsulates monomials and polynomials

 P: 16 Hello , can you explain me what does this function ( class ) do ? Expand|Select|Wrap|Line Numbers class Doublet:     def __init__(self,referenceMonome,refSuiv):         self.refMon = referenceMonome         self.suiv = refSuiv     def __repr__(self):         res=''         cour=self         while cour!=None:             res+=str(cour.refMon)             cour=cour.suiv         if res=='+':             return res[1:]         return res    thank you Jan 3 '07 #1
24 Replies

 Expert 5K+ P: 6,596 Hello , can you explain me what does this function ( class ) do ? Expand|Select|Wrap|Line Numbers class Doublet:     def __init__(self,referenceMonome,refSuiv):         self.refMon = referenceMonome         self.suiv = refSuiv     def __repr__(self):         res=''         cour=self         while cour!=None:             res+=str(cour.refMon)             cour=cour.suiv         if res=='+':             return res[1:]         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

 Expert Mod 2.5K+ P: 2,851 Hello , can you explain me what does this function ( class ) do ? Expand|Select|Wrap|Line Numbers class Doublet:     def __init__(self,referenceMonome,refSuiv):         self.refMon = referenceMonome         self.suiv = refSuiv     def __repr__(self):         res=''         cour=self         while cour!=None:             res+=str(cour.refMon)             cour=cour.suiv         if res=='+':             return res[1:]         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 7*x**2 Just guessing. Jan 4 '07 #3

 Expert 5K+ P: 6,596 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 7*x**2 Just guessing. I thought maybe it's collecting friends. Could be. Jan 4 '07 #4

 P: 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 class Monome: # monome =monomial     def __init__(self, coefficient, exposantX, exposantY): #exposant = power         self.coeff = coefficient          self.expoX = exposantX          self.expoY = exposantY      def __repr__(self):          s=str(self.coeff)+"*x^"+str(self.expoX)+"*y^"+str(self.expoY)          if self.coeff<0: return s          return "+"+s      def __lt__(self, autre):  #autre=other         return (self.expoX < autre.expoX) or ((self.expoX ==  autre.expoX) and (self.expoY < autre.expoY))      def __gt__(self,autre):          return (self.expoX > autre.expoX) or ((self.expoX ==  autre.expoX) and (self.expoY >autre.expoY))  __________________________________________________ _______________ __________________________________________________ _______________ Expand|Select|Wrap|Line Numbers class Doublet:      def __init__(self,referenceMonome,refSuiv):   # suiv=next         self.refMon = referenceMonome          self.suiv = refSuiv      def __repr__(self):          res=''  # res=result         cour=self  # cour = current         while cour!=None:              res+=str(cour.refMon)              cour=cour.suiv          if res=='+':              return res[1:]          return res  __________________________________________________ _______________ __________________________________________________ _______________ Expand|Select|Wrap|Line Numbers def degre(mon): #degré = degree     return(mon.expoX + mon.expoY)  __________________________________________________ _______________ __________________________________________________ _______________ Expand|Select|Wrap|Line Numbers def degreX (mon):      return mon.expoX  __________________________________________________ _______________ the functions which follow, I must correct them and/or supplement them Expand|Select|Wrap|Line Numbers def insereMonome(coefficient, exposantX, exposantY, doublett) : #insere=insert     # to seek the last monomial before this new monomial  # to make insertion by creating the monomial which will be placed  # placed well for the ascending order "         newRefMon = Monome(coefficient, exposantX, exposantY)          print "nouveau monome : ",newRefMon                                                                 # nouveau monome = new monomial         return None      def insereCopieMonome( refMon, doublett):      #DO NOT modify this function     return insereMonome(refMon.coeff, refMon.expoX, refMon.expoY,  doublett)      def ajouteDoubletsRec(listeDoublet1, listeDoublet2): #ajoute=add     # returns a new list of doublets obtained by      # fusion of the two lists in new one     # 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      def multMonomeMonome(refMon1, refMon2): #mult=multiply     return Monome(refMon1.coeff * refMon2.coeff, refMon1.expoX +  refMon2.expoX, refMon1.expoY + refMon2.expoY)  def multMonomeListeDoublets(refMon, listeDoublets):      res = None      return res      def multDeuxListesDoublets(ld1, ld2):  # multDeuxListesDoublets = mulitply 2 lists of doublets # doublet = list of monomials     res = None      return res    __________________________________________________ _______________ Expand|Select|Wrap|Line Numbers class Polynome: #polynome =polynomial     def __init__(self,listePrincipale): #liste pricipale=main list         self.lp = listePrincipale          self.lDegre = creerListeDegre(listePrincipale)  creerListe=create list         triDegre(self.lDegre) # tri =sorting       def __repr__(self):          doub = self.lp          res = " liste pour l'ordre < \n" #l ist for the order         res = res + str(doub)          res = res + "\n \n "          res = res + " liste pour l'ordre du degré total \n"                                                                #list for the order of the total degree         doub = self.lDegre          res = res + str(doub)          return res  __________________________________________________ _______________ __________________________________________________ _______________ Expand|Select|Wrap|Line Numbers def creerListeDegre(ld):      if ld == None:          return None      else:          return Doublet(ld.refMon, creerListeDegre(ld.suiv))  __________________________________________________ _________________________*__ Expand|Select|Wrap|Line Numbers def triDegre(doublett): #tri =sorting     # we modify by exchange the values of the fields "refMon"of the listdoublett,     # so that  the list doublett is increasing for the order of the degree    return ...     def add(pol1, pol2):          return None      def mul(pol1, pol2):          return None        # END of the definition of the functions and methods #  --------------------------------------------------------------  #tests on the monomials #m1 = Monome(5,2,6)  #m2 = Monome(6,2,8)  #m3 = Monome(-4,3,1)  #m4 = Monome(-6,0,1)  #print "m1 : ",m1, "     m2 : ",m2  #print "m3 : ",m3, "    m4 : ",m4  #print m1 < m2  #print m3 < m4  #print  #tests on the doublets  #d1 = Doublet(m1,None)  #print "doublet d1 : ",d1  #doublet1 = Doublet(Monome(-4,7,1),Doublet(Monome(2,4,3),None))  #doublet2 = Doublet(m1,Doublet(m2,None))  #print  #print"doublet1 : ",doublet1, "   doublet2 : ",doublet2  #print  # tests of the function insereMonome #doub1 = insereMonome(5, 2, 6, None)  #doub1 = insereMonome(6,3,8, doub1)  #doub2 = insereMonome(5,10,3, None)  #doub2 = insereMonome(4,2,6, doub2)  #doub2 = insereMonome(-3,11,7, doub2)  #doub2 = insereMonome(-6, 3,8, doub2)  #doub2 = insereMonome(2,3,0, doub2)  #print "doub1 : ",doub1  #print"    doub2 : ",doub2  # tests of the function ajouteDoubletsRec  #listeAjout = ajouteDoubletsRec(doub1, doub2)  #print "sum of 2 lists : ", listeAjout      # tests of the function multMonomeListeDoublets # TESTS TO BE BUILT ( we must write some tests of this  # function ( multMonomeListeDoublets)   # tests of the function multDeuxListesDoublets  # TESTS TO BE BUILT ( we must write some tests of this  # function ( multDeuxListesDoublets)   # tests of the function creerListeDegre  #TESTS TO BE BUILT ( we must write some tests of this  # function ( creerListeDegre)   # tests of the function triDegre  #TESTS TO BE BUILT ( we must write some tests of this  # function ( triDegre )   # tests of polynomials: representation, addition, multiplication # TESTS TO BE BUILT ( we must write some tests )   #pol1 = Polynome(doublet1)  #pol2 = Polynome(doublet2)  #som = add(pol1, pol2)  #prod = mult(pol1, pol2)  #print som  #print prod    for example , the calss Doublet causes problems to me Expand|Select|Wrap|Line Numbers >If, for example, I write   > >>> a=Monome(2,7,11)  > >>> a  > +2*x^7*y^11  > >>> b=Monome(-1,17,35)  > >>> b  > -1*x^17*y^35  > >>> c=Doublet(a,b)  > >>> t  > Traceback (most recent call last):  >   File "", line 1, in   >     t  >   File "C:\...\polynomesXY2_DEVOIR_Enonce.py", line 36, in __repr__  >     res+=str(cour.refMon)  > AttributeError: Monome instance has no attribute 'refMon'    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

 Expert 5K+ P: 6,596 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

 P: 16 Hello bartonc , can you change the title please ? thanks Jan 6 '07 #7

 P: 16 thank you bartonc :) Jan 6 '07 #8

 Expert Mod 2.5K+ P: 2,851 I added spaces at the plus and minus signs so the polynomial is easier to read. Expand|Select|Wrap|Line Numbers m1 = Monome(5,2,6)  m2 = Monome(6,2,8)  print "\nm1 : ", m1 print "m2 : ", m2  d2 = Doublet(m1,Doublet(m2, Doublet(Monome(4,7,1), None))) print "Doublet(m1,Doublet(m2, Doublet(Monome(-4,7,1), None))): ", d2 Output: Expand|Select|Wrap|Line Numbers m1 :   + 5*x^2*y^6 m2 :   + 6*x^2*y^8 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

 Expert Mod 2.5K+ P: 2,851 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 # Doublet iterator def iterDoub(d):     while d:         yield d.refMon         d = d.suiv Example: Expand|Select|Wrap|Line Numbers >>> print "Doublet 'd1':", d1 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 >>> for d in iterDoub(d1): ...     print d ...       + 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 >>>  It seems interestingly simple. Maybe you can use it. Jan 7 '07 #10

 Expert 5K+ P: 6,596 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 # Doublet iterator def iterDoub(d):     while d:         yield d.refMon         d = d.suiv Example: Expand|Select|Wrap|Line Numbers >>> print "Doublet 'd1':", d1 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 >>> for d in iterDoub(d1): ...     print d ...       + 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 >>>  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

 Expert Mod 2.5K+ P: 2,851 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

 Expert Mod 2.5K+ P: 2,851 I did not care for the way the multinomials were represented, so I reworked special methods '__repr__': Expand|Select|Wrap|Line Numbers class Monome: # monome = monomial     def __init__(self, coefficient, exposantX, exposantY): #exposant = power         self.coeff = coefficient          self.expoX = exposantX          self.expoY = exposantY       def __repr__(self):         if abs(self.coeff) == 1:             coeffStr = "("         else:             coeffStr = '%s(' % (abs(self.coeff))         if self.coeff < 0:             preStr = "-"         else:             preStr = "+"         return '%s%sx^%s*y^%s)' % (preStr, coeffStr, self.expoX, self.expoY)       def __lt__(self, autre):  #autre=other         return (self.expoX < autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY < autre.expoY))      def __gt__(self,autre):          return (self.expoX > autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY >autre.expoY))   class Doublet:      def __init__(self,referenceMonome,refSuiv):   # suiv=next         self.refMon = referenceMonome          self.suiv = refSuiv       def __repr__(self):         res = str(self.refMon)         a = self.suiv         while a:             res += str(a.refMon)             a = a.suiv         if res == '+':              return res[1:]          return res This is what it looks like now: Expand|Select|Wrap|Line Numbers 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) Iteration over 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)   6(x^3*y^8)+5(x^3*y^9)-2(x^4*y^2) 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) -(x^-2*y^-3)+7(x^4*y^12)+4(x^5*y^6)+6(x^7*y^1) Jan 7 '07 #13

 Expert 5K+ P: 6,596 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

 P: 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

 Expert 5K+ P: 6,596 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 class Monome: # monome = monomial     def __init__(self, coefficient, exposantX, exposantY): #exposant = power         self.coeff = coefficient         self.expoX = exposantX         self.expoY = exposantY       def __repr__(self):         if abs(self.coeff) == 1:             coeffStr = "("         else:             coeffStr = '%s(' % (abs(self.coeff))         if self.coeff < 0:             preStr = "-"         else:             preStr = "+"         return '%s%sx^%s*y^%s)' % (preStr, coeffStr, self.expoX, self.expoY)       def __lt__(self, autre):  #autre=other         return (self.expoX < autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY < autre.expoY))     def __gt__(self,autre):         return (self.expoX > autre.expoX) or ((self.expoX == autre.expoX) and (self.expoY >autre.expoY))   class Doublet:     def __init__(self,referenceMonome,refSuiv):   # suiv=next         self.refMon = referenceMonome         self.suiv = refSuiv       def __repr__(self):         res = str(self.refMon)         a = self.suiv         while a:             res += str(a.refMon)             a = a.suiv         if res == '+':             return res[1:]         return res     m1 = Monome(5,2,6) m2 = Monome(6,2,8) print "\nm1 : ", m1 print "m2 : ", m2 d2 = Doublet(m1,Doublet(m2, Doublet(Monome(4,7,1), None))) print "Doublet(m1,Doublet(m2, Doublet(Monome(-4,7,1), None))): ", d2   Jan 7 '07 #16

 Expert Mod 2.5K+ P: 2,851 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 # sort function on Doublet object in ascending order def sortDoub(d):     while d != None:         if d.suiv != None:             d2 = d.suiv             while d2 != None:                 if d.refMon > d2.refMon:                     d.refMon, d2.refMon = d2.refMon, d.refMon                 d2 = d2.suiv         d = d.suiv   # Add new Monome object to existing Doublet object and sort in ascending order def insereMonome(coefficient, exposantX, exposantY, d=None): #insere=insert     newDoub = Doublet(Monome(coefficient, exposantX, exposantY), d)     sortDoub(newDoub)     return newDoub Sample output: Expand|Select|Wrap|Line Numbers doub1 = insereMonome(6,3,8, doub1):  5(x^2*y^6)+6(x^3*y^8)-2(x^4*y^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) 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

 P: 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

 P: 16 perfect ... thank you very much bvdet :) Jan 7 '07 #19

 Expert Mod 2.5K+ P: 2,851 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

 P: 16 what do you think about this one ? Expand|Select|Wrap|Line Numbers def ajouteDoubletsRec(listeDoublet1, listeDoublet2):     # renvoie une nouvelle liste de doublets obtenue par     # fusion des deux listes en une nouvelle     # ATTENTION : ne pas oublier de traîter le cas où l'on additionne deux monomes opposés !!!!!     # dans ce cas, NE PAS faire apparaître un monome avec un coefficient nul.     if listeDoublet1==None :         return listeDoublet2     elif listeDoublet2==None :         return listeDoublet1     else :         listeDoublet1.suiv=ajouteDoubletsRec(listeDoublet1.suiv, listeDoublet2)     sortDoub(listeDoublet1)     return listeDoublet1 Jan 7 '07 #21

 P: 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

 Expert Mod 2.5K+ P: 2,851 what do you think about this one ? Expand|Select|Wrap|Line Numbers def ajouteDoubletsRec(listeDoublet1, listeDoublet2):     # renvoie une nouvelle liste de doublets obtenue par     # fusion des deux listes en une nouvelle     # ATTENTION : ne pas oublier de traîter le cas où l'on additionne deux monomes opposés !!!!!     # dans ce cas, NE PAS faire apparaître un monome avec un coefficient nul.     if listeDoublet1==None :         return listeDoublet2     elif listeDoublet2==None :         return listeDoublet1     else :         listeDoublet1.suiv=ajouteDoubletsRec(listeDoublet1.suiv, listeDoublet2)     sortDoub(listeDoublet1)     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 outDoub = d1 for m in iterDoub(d2):     outDoub = Doublet(m, outDoub)     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 >>> d4 2(x^2*y^3)+16(x^2*y^6)+2(x^4*y^6)+14(x^8*y^9)+(x^9*y^3) >>> d2 -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) >>> d11 = ajouteDoubletsRec(d4, d2) >>> d11 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) >>>  Jan 8 '07 #23

 Expert Mod 2.5K+ P: 2,851 I have another improvement (in my mind) for the displayed representation of a Monome object: Expand|Select|Wrap|Line Numbers class Monome: # monome = monomial     def __init__(self, coefficient, exposantX, exposantY): #exposant = power         self.coeff = coefficient          self.expoX = exposantX          self.expoY = exposantY       def __repr__(self):         if abs(self.coeff) == 1:             coeffStr = "("         else:             coeffStr = '%s(' % (abs(self.coeff))         if self.coeff < 0:             preStr = "-"         else:             preStr = "+"         if self.expoX == 0:             return '%s%sy^%s)' % (preStr, coeffStr, self.expoY)         elif self.expoY == 0:             return '%s%sx^%s)' % (preStr, coeffStr, self.expoX)         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 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) Iteration over 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) Jan 8 '07 #24

 Expert Mod 2.5K+ P: 2,851 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 # Doublet iterator def iterDoub(d):     while d:         yield d.refMon         d = d.suiv   # return a sorted Doublet object in descending order of degree  def cmpDoub(d):     mList = []     for m in iterDoub(d):         mList.append((m.expoX+m.expoY, m.expoX, m.expoY, m.coeff, m))     mList.sort()     outDoub = None     for item in mList:         outDoub = Doublet(Monome(item, item, item), outDoub)     return outDoub           # Add new Monome object to existing Doublet object and sort in descending order of degree def insereMonome(coefficient, exposantX, exposantY, d=None): #insere=insert     return cmpDoub(Doublet(Monome(coefficient, exposantX, exposantY), d)) Sample output: Expand|Select|Wrap|Line Numbers doub2 = insereMonome(5,10,3, None):  5(x^10*y^3) doub2 = insereMonome(4,2,6, doub2):  5(x^10*y^3)+4(x^2*y^6) doub2 = insereMonome(-3,11,7, doub2):  -3(x^11*y^7)+5(x^10*y^3)+4(x^2*y^6) 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) 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) 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) 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) 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 