Ticket #4539: extplural-more3.patch

File extplural-more3.patch, 11.9 KB (added by AlexanderDreyer, 6 years ago)
  • sage/rings/polynomial/plural.pxd

    diff -r 02bd6ab343a2 sage/rings/polynomial/plural.pxd
    a b  
    11include "../../libs/singular/singular-cdefs.pxi"
    22
    33from sage.rings.ring cimport Ring
    4 from sage.structure.element cimport RingElement
     4from sage.structure.element cimport RingElement, Element
     5from sage.structure.parent cimport Parent
    56from sage.libs.singular.function cimport RingWrap
    67from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular
    78
    89
    910cdef class NCPolynomialRing_plural(Ring):
    1011    cdef object __ngens
     12    cdef object _c
     13    cdef object _d
    1114    cdef object __term_order
    1215    cdef public object _has_singular
    1316    cdef public object _magma_gens, _magma_cache
     17#    cdef _richcmp_c_impl(left, Parent right, int op)
     18    cdef int _cmp_c_impl(left, Parent right) except -2
    1419   
    1520    cdef ring *_ring
    1621#    cdef NCPolynomial_plural _one_element
  • sage/rings/polynomial/plural.pyx

    diff -r 02bd6ab343a2 sage/rings/polynomial/plural.pyx
    a b  
    3333cdef class NCPolynomialRing_plural(Ring):
    3434    def __init__(self, base_ring, n, names, c, d, order='degrevlex', check = True):
    3535        order = TermOrder(order,n)
     36        self._relations = None
    3637        n = int(n)
    3738        if n < 0:
    3839            raise ValueError, "Multivariate Polynomial Rings must " + \
     
    4243                PolynomialRing
    4344
    4445        P = PolynomialRing(base_ring, n, names, order)
    45         c = c.change_ring(P)
    46         d = d.change_ring(P)
     46        self._c = c.change_ring(P)
     47        self._d = d.change_ring(P)
    4748
    48         #print "c:",c
    49         #print "c.parent()",c.parent()
     49       
     50        #print "c:",self._c
     51        #print "c.parent()",self._c.parent()
    5052        #print "type(c):",type(c)
    51         #print "d:",d
    52         #print "d.parent()",d.parent()
     53        #print "d:",self._d
     54        #print "d.parent()",self._d.parent()
    5355        #print "type(d):",type(d)
    5456
    5557        from sage.libs.singular.function import singular_function
    5658        ncalgebra = singular_function('nc_algebra')
    5759
    58         cdef RingWrap rw = ncalgebra(c, d, ring = P)
     60        cdef RingWrap rw = ncalgebra(self._c, self._d, ring = P)
    5961        self._ring = rw._ring
    6062        self._ring.ShortOut = 0
    6163
     
    7274        self._one_element = new_NCP(self, p_ISet(1, self._ring))
    7375        self._zero_element = new_NCP(self, NULL)
    7476       
    75         self._relations = self.relations()
    7677
    7778        if check:
    7879            import sage.libs.singular
     
    183184            sage: P(0)
    184185            0
    185186        """
    186        
     187
    187188        if element == 0:
    188189            return self._zero_element
    189190        if element == 1:
     
    198199        base_ring = self.base_ring()
    199200       
    200201        if(_ring != currRing): rChangeCurrRing(_ring)
    201 
     202       
     203        if PY_TYPE_CHECK(element, NCPolynomial_plural):
     204            if element.parent() is <object>self:
     205                return element
     206            elif element.parent() == self:
     207                # is this safe?
     208                _p = p_Copy((<NCPolynomial_plural>element)._poly, _ring)
    202209
    203210        if PY_TYPE_CHECK(element, CommutativeRingElement):
    204211            # base ring elements
     
    289296       """
    290297       return hash(self.__repr__())
    291298
    292 ##     def _richcmp_(self, right, int op):
    293 ##         return (<Parent>self)._richcmp_helper(right, op)
    294      
     299
     300    def __cmp__(self, right):
     301        r"""
     302        Multivariate polynomial rings are said to be equal if:
    295303       
    296 ## ##     cdef int _cmp_c_impl(left, Parent right) except -2:
    297 ## ##          return 0#cmp(type(left),type(right))
     304        - their base rings match,
     305        - their generator names match,
     306        - their term orderings match, and
     307        - their relations match.
    298308
    299 ##     def __cmp__(self, x):
    300 ##         """
    301 ##         EXAMPLES::
    302309
    303 ##         """
    304 ##         if PY_TYPE_CHECK(x, NCPolynomialRing_plural):
    305 ##             print "huhu"
    306 ##             return 0
    307 ##         print "hihi"
    308 ##         return 0# return cmp(type(self), type(x))
    309    
    310 ##     def __richcmp__(self, right, int op):
    311 ##         print "CMP"
    312 ##         print "base", self.base_ring()
    313 ##         print "gens"
    314 ##         for elt in  self.gens():
    315 ##             print elt
    316 ##         print self.term_order()
    317 ##         for elt in  self.relations():
    318 ##             print elt
    319 ##         print  PY_TYPE_CHECK(right, NCPolynomialRing_plural), "huhu"
     310        EXAMPLES::
     311           sage: A.<x,y,z> = FreeAlgebra(QQ, 3)
     312           sage: P = A.g_algebra(relations={y*x:-x*y}, order = 'lex')
    320313
    321 ##         print "base", right.base_ring()
    322 ##         print "gens"
    323 ##         for elt in  right.gens():
    324 ##             print elt
    325 ##         print right.term_order()
    326 ##         for elt in  right.relations():
    327 ##             print elt
    328 ##         return False
    329    
    330 ##         print cmp(self.base_ring(), right.base_ring())
    331 ##         print cmp( map(str, self.gens()),  map(str, right.gens()))
    332 ##         print cmp( map(str, self.relations()),  map(str, right.relations()))
     314           sage: P == P
     315           True
     316           sage: Q = A.g_algebra(relations={y*x:-x*y}, order = 'lex')
     317           sage: Q == P
     318           True
     319                     
     320           sage: from sage.matrix.constructor  import Matrix
     321           sage: c = Matrix(3)
     322           sage: c[0,1] = -1
     323           sage: c[0,2] = 1
     324           sage: c[1,2] = 1
     325           sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural
     326           sage: R.<x,y,z> = NCPolynomialRing_plural(QQ, 3, c = c, d = Matrix(3), order='lex')
     327           sage: R == P
     328           True
     329           
     330           sage: c[0,1] = -2
     331           sage: R.<x,y,z> = NCPolynomialRing_plural(QQ, 3, c = c, d = Matrix(3), order='lex')
     332           sage: P == R
     333           False
     334        """
    333335
    334 ##         return False
    335 ##         if PY_TYPE_CHECK(right, NCPolynomialRing_plural):
    336 ##             return cmp( (self.base_ring(), map(str, self.gens()),
    337 ##                          self.term_order(), map(str, self.relations())),
    338 ##                         (right.base_ring(), map(str, right.gens()),
    339 ##                          right.term_order(), map(str, right.relations()))
    340 ##                         )
    341 ##         else:
    342 ##             return cmp(type(self),type(right))
     336        if PY_TYPE_CHECK(right, NCPolynomialRing_plural):
    343337
    344 ##     #       return False
    345  
    346  #   def __richcmp__(left, right, int op):
    347  #       return (<ParentWithGens>left)._richcmp(right, op)   
    348  #       return False
    349 ##         r"""
    350 ##         Multivariate polynomial rings are said to be equal if:
    351        
    352 ##         - their base rings match,
    353 ##         - their generator names match,
    354 ##         - their term orderings match, and
    355 ##         - their relations match.
     338            return cmp( (self.base_ring(), map(str, self.gens()),
     339                         self.term_order(), self._c, self._d),
     340                        (right.base_ring(), map(str, right.gens()),
     341                         right.term_order(),
     342                         (<NCPolynomialRing_plural>right)._c,
     343                         (<NCPolynomialRing_plural>right)._d)
     344                        )
     345        else:
     346            return cmp(type(self),type(right))
    356347
    357 
    358 ##         EXAMPLES::
    359 ##            sage: A.<x,y,z> = FreeAlgebra(QQ, 3)
    360 ##            sage: P = A.g_algebra(relations={y*x:-x*y}, order = 'lex')
    361 
    362 ##            sage: P == P
    363 ##            True
    364 ##            sage: Q = copy(P)
    365 ##            sage: Q == P
    366 ##            True
    367                      
    368 ##            sage: from sage.matrix.constructor  import Matrix
    369 ##            sage: c = Matrix(3)
    370 ##            sage: c[0,1] = -1
    371 ##            sage: c[0,2] = 1
    372 ##            sage: c[1,2] = 1
    373 ##            sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural
    374 ##            sage: R.<x,y,z> = NCPolynomialRing_plural(QQ, 3, c = c, d = Matrix(3), order='lex')
    375 ##            sage: R == P
    376 ##            True
    377            
    378 ##            sage: c[0,1] = -2
    379 ##            sage: R.<x,y,z> = NCPolynomialRing_plural(QQ, 3, c = c, d = Matrix(3), order='lex')
    380 ##            sage: P == R
    381 ##            False
    382 ##         """
    383 ###         return (<Parent>left)._richcmp(right, op)
    384           ##return (<ParentWithGens>left)._richcmp(right, op) 
    385 ##     cdef int _cmp_c_impl(left, Parent right) except -2:
    386 ##         r"""
    387 ##         Multivariate polynomial rings are said to be equal if:
    388        
    389 ##         - their base rings match,
    390 ##         - their generator names match,
    391 ##         - their term orderings match, and
    392 ##         - their relations match.
    393 
    394 
    395 ##         EXAMPLES::
    396 ##            sage: A.<x,y,z> = FreeAlgebra(QQ, 3)
    397 ##            sage: P = A.g_algebra(relations={y*x:-x*y}, order = 'lex')
    398 
    399 ##            sage: P == P
    400 ##            True
    401 ##            sage: Q = copy(P)
    402 ##            sage: Q == P
    403 ##            True
    404                      
    405 ##            sage: from sage.matrix.constructor  import Matrix
    406 ##            sage: c = Matrix(3)
    407 ##            sage: c[0,1] = -1
    408 ##            sage: c[0,2] = 1
    409 ##            sage: c[1,2] = 1
    410 ##            sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural
    411 ##            sage: R.<x,y,z> = NCPolynomialRing_plural(QQ, 3, c = c, d = Matrix(3), order='lex')
    412 ##            sage: R == P
    413 ##            True
    414            
    415 ##            sage: c[0,1] = -2
    416 ##            sage: R.<x,y,z> = NCPolynomialRing_plural(QQ, 3, c = c, d = Matrix(3), order='lex')
    417 ##            sage: P == R
    418 ##            False
    419 ##         """       
    420 ##         print "huhu", PY_TYPE_CHECK(right, NCPolynomialRing_plural)
    421 ##         if PY_TYPE_CHECK(right, NCPolynomialRing_plural):
    422 ##             return True
    423 ##             return cmp( (left.base_ring(), map(str, left.gens()),
    424 ##                          left.term_order(), map(str, left.relations())),
    425 ##                         (right.base_ring(), map(str, right.gens()),
    426 ##                          right.term_order(), map(str, right.relations()))
    427 ##                         )
    428 ##         else:
    429 ##             return cmp(type(left),type(right))
    430        
    431 
    432    
    433348    def __pow__(self, n, _):
    434349        """
    435350        Return the free module of rank `n` over this ring.
     
    466381        """
    467382#TODO: print the relations
    468383        varstr = ", ".join([ rRingVar(i,self._ring)  for i in range(self.__ngens) ])
    469         return "Noncommutative Multivariate Polynomial Ring in %s over %s, nc-relations: %s"%(varstr,self.base_ring(),self._relations)
     384        return "Noncommutative Multivariate Polynomial Ring in %s over %s, nc-relations: %s"%(varstr,self.base_ring(), self.relations())
    470385
    471386
    472387    def _ringlist(self):
     
    486401            sage: P # indirect doctest
    487402            Noncommutative Multivariate Polynomial Ring in x, y over Rational Field, nc-relations: ...
    488403        """
    489 #TODO: get the relations
    490         L = self._ringlist()
    491 
    492         assert( len(L) == 6 )
    493 
    494         C = L[4]
    495         D = L[5]
     404        if self._relations is not None:
     405            return self._relations
    496406
    497407        from sage.algebras.free_algebra import FreeAlgebra
    498408        A = FreeAlgebra( self.base_ring(), self.ngens(), self.gens() )
     
    501411        n = self.ngens()
    502412        for r in range(0, n-1, 1):
    503413            for c in range(r+1, n, 1):
    504                 if  ((C[r, c] * self.gen(r) * self.gen(c) + D[r, c]) != self.gen(r) * self.gen(c)) or add_commutative:
    505                     res[ A.gen(c) * A.gen(r) ] = C[r, c] * self.gen(r) * self.gen(c) + D[r, c]
     414                if  (self.gen(c) * self.gen(r) != self.gen(r) * self.gen(c)) or add_commutative:
     415                    res[ A.gen(c) * A.gen(r) ] = self.gen(c) * self.gen(r) # C[r, c] * P.gen(r) * P.gen(c) + D[r, c]
    506416       
    507         return res
    508 
    509 
     417           
     418        self._relations = res
     419        return self._relations
    510420
    511421    def ngens(self):
    512422        """