Ticket #4539: plural_functions.2.patch

File plural_functions.2.patch, 17.2 KB (added by PolyBoRi, 6 years ago)
  • sage/libs/singular/function.pxd

    diff -r 54342f65c59c sage/libs/singular/function.pxd
    a b  
    2424
    2525cdef class Resolution:
    2626    cdef syStrategy *_resolution
    27     cdef MPolynomialRing_libsingular base_ring
     27    cdef object base_ring
    2828
    2929cdef class Converter(SageObject):
    3030    cdef leftv *args
     
    4444    cdef leftv * append_intvec(self, v) except NULL
    4545    cdef leftv * append_list(self, l) except NULL
    4646    cdef leftv * append_matrix(self, a) except NULL
    47     cdef leftv * append_ring(self, MPolynomialRing_libsingular r) except NULL
     47    cdef leftv * append_ring(self, r) except NULL
    4848    cdef leftv * append_module(self, m) except NULL
    4949    cdef to_sage_integer_matrix(self, intvec *mat)
    5050    cdef object to_sage_module_element_sequence_destructive(self, ideal *i)
     
    7070
    7171    cdef BaseCallHandler get_call_handler(self)
    7272    cdef bint function_exists(self)
    73     cdef MPolynomialRing_libsingular common_ring(self, tuple args, ring=?)
     73    cdef common_ring(self, tuple args, ring=?)
    7474
    7575cdef class SingularLibraryFunction(SingularFunction):
    7676    pass
  • sage/libs/singular/function.pyx

    diff -r 54342f65c59c sage/libs/singular/function.pyx
    a b  
    1313- Martin Albrecht (2009-07): clean up, enhancements, etc.
    1414- Michael Brickenstein (2009-10): extension to more Singular types
    1515- Martin Albrecht (2010-01): clean up, support for attributes
     16- Burcin Erocal (2010-7): plural support
    1617
    1718EXAMPLES:
    1819
     
    8182from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular
    8283
    8384from sage.rings.polynomial.plural cimport NCPolynomialRing_plural, \
    84         NCPolynomial_plural
     85        NCPolynomial_plural, new_NCP
    8586from sage.rings.polynomial.multi_polynomial_ideal import NCPolynomialIdeal
    8687
    8788from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal
     
    122123   
    123124    for (i, p) in enumerate(v):
    124125        component = <int>i+1
    125         poly_component = p_Copy(
    126             (<MPolynomial_libsingular>p)._poly, r)
     126        poly_component = copy_sage_polynomial_into_singular_poly(p)
    127127        p_iter = poly_component
    128128        while p_iter!=NULL:
    129129            p_SetComp(p_iter, component, r)
     
    132132        res=p_Add_q(res, poly_component, r)
    133133    return res
    134134
     135
    135136cdef class RingWrap:
    136137    """
    137138    A simple wrapper around Singular's rings.
     
    170171           sage: M = syz(I)
    171172           sage: resolution = mres(M, 0)
    172173        """
    173         assert PY_TYPE_CHECK(base_ring, MPolynomialRing_libsingular)
    174         self.base_ring = <MPolynomialRing_libsingular> base_ring
     174        #FIXME: still not working noncommutative
     175        assert is_sage_wrapper_for_singular_ring(base_ring)
     176        self.base_ring = base_ring
    175177    def __repr__(self):
    176178        """
    177179        EXAMPLE::
     
    229231    args.CleanUp()
    230232    omFreeBin(args, sleftv_bin)
    231233
     234# =====================================
     235# = Singular/Plural Abstraction Layer =
     236# =====================================
     237
     238def is_sage_wrapper_for_singular_ring(ring):
     239    if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular):
     240        return True
     241    if PY_TYPE_CHECK(ring, NCPolynomialRing_plural):
     242        return True
     243    return False
     244
     245cdef new_sage_polynomial(ring,  poly *p):
     246    if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular):
     247        return new_MP(ring, p)
     248    else:
     249        if PY_TYPE_CHECK(ring, NCPolynomialRing_plural):
     250            return new_NCP(ring, p)
     251    raise ValueError("not a singular or plural ring")
     252
     253def is_polynomial(p):
     254    return PY_TYPE_CHECK(p, MPolynomial_libsingular) or PY_TYPE_CHECK(p,  NCPolynomial_plural)
    232255
    233256def all_polynomials(s):
    234257    """
     
    245268        False
    246269    """
    247270    for p in s:
    248         if not isinstance(p, MPolynomial_libsingular):
     271        if not is_polynomial(p):
    249272            return False
    250273    return True
    251274
     275cdef poly* access_singular_poly(p) except <poly*> -1:
     276    if PY_TYPE_CHECK(p, MPolynomial_libsingular):
     277        return (<MPolynomial_libsingular> p)._poly
     278    else:
     279        if PY_TYPE_CHECK(p, NCPolynomial_plural):
     280            return (<NCPolynomial_plural> p)._poly
     281    raise ValueError("not a singular polynomial wrapper")
     282
     283cdef ring* access_singular_ring(r) except <ring*> -1:
     284    if PY_TYPE_CHECK(r, MPolynomialRing_libsingular):
     285        return (<MPolynomialRing_libsingular> r )._ring
     286    if PY_TYPE_CHECK(r, NCPolynomialRing_plural):
     287        return (<NCPolynomialRing_plural> r )._ring
     288    raise ValueError("not a singular polynomial ring wrapper")
     289 
     290cdef poly* copy_sage_polynomial_into_singular_poly(p):
     291    return p_Copy(access_singular_poly(p), access_singular_ring(p.parent()))
     292
    252293def all_vectors(s):
    253294    """
    254295    Checks if a sequence ``s`` consists of free module
     
    269310        False
    270311    """
    271312    for p in s:
    272         if not (isinstance(p, FreeModuleElement_generic_dense)\
    273             and isinstance(p.parent().base_ring(), MPolynomialRing_libsingular)):
     313        if not (PY_TYPE_CHECK(p, FreeModuleElement_generic_dense)\
     314            and is_sage_wrapper_for_singular_ring(p.parent().base_ring())):
    274315            return False
    275316    return True
    276317
    277318
     319cdef ring* sage_ring_to_singular_ring(ring) except <ring* > -1:
     320    if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular):
     321        return (<MPolynomialRing_libsingular>ring)._ring
     322    elif PY_TYPE_CHECK(ring, NCPolynomialRing_plural):
     323        return (<NCPolynomialRing_plural>ring)._ring
     324    raise ValueError, "no singular ring found"
     325
     326
     327
     328
     329
    278330
    279331cdef class Converter(SageObject):
    280332    """
     
    305357        cdef leftv *v
    306358        self.args = NULL
    307359        self._sage_ring = ring
    308         if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular):
    309             self._singular_ring = (<MPolynomialRing_libsingular>ring)._ring
    310         elif PY_TYPE_CHECK(ring, NCPolynomialRing_plural):
    311             self._singular_ring = (<NCPolynomialRing_plural>ring)._ring
     360        if ring is not None:
     361            self._singular_ring = sage_ring_to_singular_ring(ring)
    312362       
    313363        from  sage.matrix.matrix_mpolynomial_dense import Matrix_mpolynomial_dense
    314364        from sage.matrix.matrix_integer_dense import Matrix_integer_dense
    315365        for a in args:
    316             if PY_TYPE_CHECK(a, MPolynomial_libsingular) or \
    317                     PY_TYPE_CHECK(a, NCPolynomial_plural):
     366            if is_polynomial(a):
    318367                v = self.append_polynomial(a)
    319368
    320             elif PY_TYPE_CHECK(a, MPolynomialRing_libsingular) or \
    321                     PY_TYPE_CHECK(a, NCPolynomialRing_plural):
     369            elif is_sage_wrapper_for_singular_ring(a):
    322370                v = self.append_ring(a)
    323371
    324372            elif PY_TYPE_CHECK(a, MPolynomialIdeal) or \
     
    341389                v = self.append_resolution(a)
    342390
    343391            elif PY_TYPE_CHECK(a, FreeModuleElement_generic_dense)\
    344                 and isinstance(
    345                     a.parent().base_ring(),
    346                     MPolynomialRing_libsingular):
     392                and is_sage_wrapper_for_singular_ring(
     393                    a.parent().base_ring()):
    347394                v = self.append_vector(a)
    348395               
    349396            # as output ideals get converted to sequences
     
    480527        ncols = mat.ncols
    481528        nrows = mat.nrows
    482529        result = Matrix(self._sage_ring, nrows, ncols)
    483         #FIXME: NCPolynomial
    484         cdef MPolynomial_libsingular p
    485530        for i in xrange(nrows):
    486531            for j in xrange(ncols):
    487                 p = MPolynomial_libsingular(self._sage_ring)
    488                 p._poly = mat.m[i*ncols+j]
     532                p = new_sage_polynomial(self._sage_ring, mat.m[i*ncols+j])
    489533                mat.m[i*ncols+j]=NULL
    490534                result[i,j] = p
    491535        return result
     
    527571                else:
    528572                    previous = p_iter
    529573                    p_iter = pNext(p_iter)
    530             result.append(new_MP(self._sage_ring, first))
     574           
     575            result.append(new_sage_polynomial(self._sage_ring, first))
    531576        return free_module(result)
    532577         
    533578    cdef object to_sage_module_element_sequence_destructive( self, ideal *i):
     
    544589        #cdef MPolynomialRing_libsingular sage_ring = self._ring
    545590        cdef int j
    546591        cdef int rank=i.rank
    547         free_module = self._sage_ring**rank       
     592        free_module = self._sage_ring ** rank       
    548593        l = []
    549594
    550595        for j from 0 <= j < IDELEMS(i):
     
    578623        Append the polynomial ``p`` to the list.
    579624        """
    580625        cdef poly* _p
    581         if PY_TYPE_CHECK(p, MPolynomial_libsingular):
    582             _p = p_Copy((<MPolynomial_libsingular>p)._poly, <ring*>((<MPolynomial_libsingular>p)._parent)._ring)
    583         elif PY_TYPE_CHECK(p, NCPolynomial_plural):
    584             _p = p_Copy((<NCPolynomial_plural>p)._poly, <ring*>((<NCPolynomial_plural>p)._parent)._ring)
     626        _p = copy_sage_polynomial_into_singular_poly(p)
     627               
    585628        return self._append(_p, POLY_CMD)
    586629
    587630    cdef leftv *append_ideal(self,  i) except NULL:
     
    617660        cdef number *_n =  sa2si(n, self._singular_ring)
    618661        return self._append(<void *>_n, NUMBER_CMD)
    619662
    620     cdef leftv *append_ring(self, MPolynomialRing_libsingular r) except NULL:
     663    cdef leftv *append_ring(self, r) except NULL:
    621664        """
    622665        Append the ring ``r`` to the list.
    623666        """
    624         #FIXME
    625         cdef ring *_r =  <ring*> r._ring
     667        cdef ring *_r =  access_singular_ring(r)
    626668        _r.ref+=1
    627669        return self._append(<void *>_r, RING_CMD)
    628670
     
    10781120
    10791121        return prefix + get_docstring(self._name)
    10801122
    1081     cdef MPolynomialRing_libsingular common_ring(self, tuple args, ring=None):
     1123    cdef common_ring(self, tuple args, ring=None):
    10821124        """
    10831125        Return the common ring for the argument list ``args``.
    10841126
     
    10991141            if PY_TYPE_CHECK(a, MPolynomialIdeal) or \
    11001142                    PY_TYPE_CHECK(a, NCPolynomialIdeal):
    11011143                ring2 = a.ring()
    1102             elif PY_TYPE_CHECK(a, MPolynomial_libsingular) or \
    1103                     PY_TYPE_CHECK(a, NCPolynomial_plural):
     1144            elif is_polynomial(a):
    11041145                ring2 = a.parent()
    1105             elif PY_TYPE_CHECK(a, MPolynomialRing_libsingular) or \
    1106                     PY_TYPE_CHECK(a, NCPolynomialRing_plural):
     1146            elif is_sage_wrapper_for_singular_ring(a):
    11071147                ring2 = a
    1108             elif PY_TYPE_CHECK(a, int) or PY_TYPE_CHECK(a, long) or PY_TYPE_CHECK(a, basestring):
     1148            elif PY_TYPE_CHECK(a, int) or\
     1149                PY_TYPE_CHECK(a, long) or\
     1150                PY_TYPE_CHECK(a, basestring):
    11091151                continue
    11101152            elif PY_TYPE_CHECK(a, Matrix_integer_dense):
    11111153                continue
     
    11181160            elif PY_TYPE_CHECK(a, Resolution):
    11191161                ring2 = (<Resolution> a).base_ring
    11201162            elif PY_TYPE_CHECK(a, FreeModuleElement_generic_dense)\
    1121                 and PY_TYPE_CHECK(
    1122                     a.parent().base_ring(),
    1123                      MPolynomialRing_libsingular):
     1163                and is_sage_wrapper_for_singular_ring(
     1164                    a.parent().base_ring()):
    11241165                ring2 = a.parent().base_ring()
    11251166            elif ring is not None:
    11261167                a.parent() is ring
     
    11321173                raise ValueError("Rings do not match up.")
    11331174        if ring is None:
    11341175            raise ValueError("Could not detect ring.")
    1135         return <MPolynomialRing_libsingular>ring
     1176        return ring
    11361177
    11371178    def __reduce__(self):
    11381179        """
     
    11681209    global errorreported
    11691210   
    11701211    cdef ring *si_ring
    1171     if isinstance(R, MPolynomialRing_libsingular):
     1212    if PY_TYPE_CHECK(R, MPolynomialRing_libsingular):
    11721213        si_ring = (<MPolynomialRing_libsingular>R)._ring
    11731214    else:
    11741215        si_ring = (<NCPolynomialRing_plural>R)._ring
     
    13991440        <Resolution>
    14001441        sage: singular_list(resolution)
    14011442        [[(-2*y, 2, y + 1, 0), (0, -2, x - 1, 0), (x*y - y, -y + 1, 1, -y), (x^2 + 1, -x - 1, -1, -x)], [(-x - 1, y - 1, 2*x, -2*y)], [(0)]]
    1402 
    1403        
     1443        sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural
     1444        sage: from sage.matrix.constructor  import Matrix
     1445        sage: c=Matrix(2)
     1446        sage: c[0,1]=-1
     1447        sage: P = NCPolynomialRing_plural(QQ, 2, 'x,y', c=c, d=Matrix(2))
     1448        sage: (x,y)=[P.gen(i) for i in xrange(2)]
     1449        sage: I= Sequence([x*y,x+y], check=False, immutable=True)#P.ideal(x*y,x+y)
     1450        sage: twostd = singular_function("twostd")
     1451        sage: twostd(I)
     1452        [x + y, y^2]
     1453        sage: M=syz(I)
     1454        doctest...
     1455        sage: M
     1456        [(x + y, x*y)]
     1457        sage: syz(M, ring=P)
     1458        [(0)]
     1459        sage: mres(I, 0)
     1460        <Resolution>
     1461        sage: M=P**3
     1462        sage: v=M((100*x,5*y,10*y*x*y))
     1463        sage: leadcoef(v)
     1464        -10
     1465        sage: v = M([x+y,x*y+y**3,x])
     1466        sage: lead(v)
     1467        (0, y^3)
     1468        sage: jet(v, 2)
     1469        (x + y, x*y, x)
    14041470    """
    14051471
    14061472    cdef SingularFunction fnc
  • sage/modules/free_module.py

    diff -r 54342f65c59c sage/modules/free_module.py
    a b  
    182182from sage.structure.parent_gens import ParentWithGens
    183183from sage.misc.cachefunc import cached_method
    184184
     185from warnings import warn
     186
    185187###############################################################################
    186188#
    187189# Constructor functions
     
    345347        if not isinstance(sparse,bool):
    346348            raise TypeError, "Argument sparse (= %s) must be True or False" % sparse
    347349
     350
     351        # We should have two sided, left sided and right sided ideals,
     352        # but that's another story ....
     353        #
    348354        if not base_ring.is_commutative():
    349             raise TypeError, "The base_ring must be a commutative ring."
     355           
     356            warn("""You are constructing a free module
     357             over a noncommutative ring. Sage does
     358             not have a concept of left/right
     359             and both sided modules be careful. It's also
     360             not guarantied that all multiplications
     361             are done from the right side.""")
     362           
     363        #            raise TypeError, "The base_ring must be a commutative ring."
     364
    350365
    351366        if not sparse and isinstance(base_ring,sage.rings.real_double.RealDoubleField_class):
    352367            return RealDoubleVectorSpace_class(rank)
     
    558573            Category of modules with basis over Integer Ring
    559574
    560575        """
    561         if not isinstance(base_ring, commutative_ring.CommutativeRing):
    562             raise TypeError, "base_ring (=%s) must be a commutative ring"%base_ring
     576        if not base_ring.is_commutative():
     577            warn("""You are constructing a free module over a noncommutative ring. Sage does not have a concept of left/right and both sided modules be careful. It's also not guarantied that all multiplications are done from the right side.""")
     578            #raise TypeError, "base_ring (=%s) must be a commutative ring"%base_ring
     579           
    563580        rank = sage.rings.integer.Integer(rank)
    564581        if rank < 0:
    565582            raise ValueError, "rank (=%s) must be nonnegative"%rank
  • sage/rings/polynomial/plural.pyx

    diff -r 54342f65c59c sage/rings/polynomial/plural.pyx
    a b  
    1313from sage.libs.singular.polynomial cimport singular_polynomial_deg, singular_polynomial_str_with_changed_varnames, singular_polynomial_latex, singular_polynomial_str, singular_polynomial_div_coeff
    1414
    1515from sage.libs.singular.singular cimport si2sa, sa2si, overflow_check
    16 
     16from sage.rings.integer_ring import ZZ
    1717from term_order import TermOrder
    1818
    1919cdef class NCPolynomialRing_plural(Ring):
     
    4545
    4646        self.__ngens = n
    4747        ParentWithGens.__init__(self, base_ring, names)
     48        self._populate_coercion_lists_()
     49       
    4850        #MPolynomialRing_generic.__init__(self, base_ring, n, names, order)
    4951        #self._has_singular = True
    5052        assert(n == len(self._names))
    5153       
    5254        self._one_element = new_NCP(self,p_ISet(1, self._ring))
    5355        self._zero_element = new_NCP(self,NULL)
     56   
     57   
     58    def _element_constructor_(self, x):
     59       """
     60       Make sure x is a valid member of self, and return the constructed element.
     61       """
     62       if x==0:
     63           return self._zero_element
     64       raise NotImplementedError("not able to constructor "+repr(x))
     65       
     66    def _coerce_map_from_(self, S):
     67       """
     68       The only things that coerce into this ring are:
     69           - the integer ring
     70           - other localizations away from fewer primes
     71       """
     72       if S is ZZ:
     73           return True
     74       
     75    def __pow__(self, n, _):
     76        """
     77        Return the free module of rank `n` over this ring.
    5478
     79        EXAMPLES::
     80
     81        """
     82        import sage.modules.all
     83        return sage.modules.all.FreeModule(self, n)
     84   
     85   
     86   
    5587    def term_order(self):
    5688        return self.__term_order
    5789
     90
     91    def is_commutative(self):
     92        return False
     93   
     94    def is_field(self):
     95        return False
     96   
    5897    def _repr_(self):
    5998        """
    6099        EXAMPLE: