class encapsulates monomials and polynomials

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • badech
    New Member
    • Jan 2007
    • 16

    class encapsulates monomials and polynomials

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

    Code:
    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[0]=='+':
                return res[1:]
            return res
    thank you
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by badech
    Hello ,
    can you explain me what does this function ( class ) do ?

    Code:
    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[0]=='+':
                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

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by badech
      Hello ,
      can you explain me what does this function ( class ) do ?

      Code:
      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[0]=='+':
                  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:
      Code:
      7*x**2
      Just guessing.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bvdet
        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:
        Code:
        7*x**2
        Just guessing.
        I thought maybe it's collecting friends. Could be.

        Comment

        • badech
          New Member
          • Jan 2007
          • 16

          #5
          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


          _______________ _______________ _______________ _______________ _____

          Code:
          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))
          _______________ _______________ _______________ _______________ _____


          _______________ _______________ _______________ _______________ _____

          Code:
          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[0]=='+': 
                      return res[1:] 
                  return res
          _______________ _______________ _______________ _______________ _____


          _______________ _______________ _______________ _______________ _____
          Code:
          def degre(mon): #degré = degree
              return(mon.expoX + mon.expoY)
          _______________ _______________ _______________ _______________ _____


          _______________ _______________ _______________ _______________ _____
          Code:
          def degreX (mon): 
              return mon.expoX
          _______________ _______________ _______________ _______________ _____




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

          Code:
          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
          _______________ _______________ _______________ _______________ _____
          Code:
          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
          _______________ _______________ _______________ _______________ _____

          _______________ _______________ _______________ _______________ _____
          Code:
          def creerListeDegre(ld): 
              if ld == None: 
                  return None 
              else: 
                  return Doublet(ld.refMon, creerListeDegre(ld.suiv))
          _______________ _______________ _______________ _______________ _______________ *__

          Code:
          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
          Code:
          >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 "<pyshell#5>", line 1, in <module> 
          >     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
          Last edited by bartonc; Jan 5 '07, 02:38 AM. Reason: added [code][/code] tags

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            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.

            Comment

            • badech
              New Member
              • Jan 2007
              • 16

              #7
              Hello bartonc ,
              can you change the title please ?
              thanks

              Comment

              • badech
                New Member
                • Jan 2007
                • 16

                #8
                thank you bartonc :)

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  I added spaces at the plus and minus signs so the polynomial is easier to read.
                  Code:
                  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:
                  Code:
                  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

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    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:
                    Code:
                    # Doublet iterator
                    def iterDoub(d):
                        while d:
                            yield d.refMon
                            d = d.suiv
                    Example:
                    Code:
                    >>> 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.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by bvdet
                      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:
                      Code:
                      # Doublet iterator
                      def iterDoub(d):
                          while d:
                              yield d.refMon
                              d = d.suiv
                      Example:
                      Code:
                      >>> 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.

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by bartonc
                        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.

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          I did not care for the way the multinomials were represented, so I reworked special methods '__repr__':
                          Code:
                          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[0] == '+': 
                                      return res[1:] 
                                  return res
                          This is what it looks like now:
                          Code:
                          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)

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by bvdet
                            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.

                            Comment

                            • badech
                              New Member
                              • Jan 2007
                              • 16

                              #15
                              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

                              Comment

                              Working...