Ticket #4539: plural_functions.patch

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

    diff -r 54342f65c59c sage/libs/singular/function.pxd
    a b  
    1919from sage.libs.singular.decl cimport ring as singular_ring
    2020from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular, MPolynomial_libsingular
    2121
     22cdef poly* access_singular_poly(p) except <poly*> -1
     23cdef singular_ring* access_singular_ring(r) except <singular_ring*> -1
     24
    2225cdef class RingWrap:
    2326    cdef singular_ring *_ring
    2427
    2528cdef class Resolution:
    2629    cdef syStrategy *_resolution
    27     cdef MPolynomialRing_libsingular base_ring
     30    cdef object base_ring
    2831
    2932cdef class Converter(SageObject):
    3033    cdef leftv *args
     
    4447    cdef leftv * append_intvec(self, v) except NULL
    4548    cdef leftv * append_list(self, l) except NULL
    4649    cdef leftv * append_matrix(self, a) except NULL
    47     cdef leftv * append_ring(self, MPolynomialRing_libsingular r) except NULL
     50    cdef leftv * append_ring(self, r) except NULL
    4851    cdef leftv * append_module(self, m) except NULL
    4952    cdef to_sage_integer_matrix(self, intvec *mat)
    5053    cdef object to_sage_module_element_sequence_destructive(self, ideal *i)
     
    7073
    7174    cdef BaseCallHandler get_call_handler(self)
    7275    cdef bint function_exists(self)
    73     cdef MPolynomialRing_libsingular common_ring(self, tuple args, ring=?)
     76    cdef common_ring(self, tuple args, ring=?)
    7477
    7578cdef class SingularLibraryFunction(SingularFunction):
    7679    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
     17- Michael Brickenstein (2010-7): plural support
    1618
    1719EXAMPLES:
    1820
     
    8183from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular
    8284
    8385from sage.rings.polynomial.plural cimport NCPolynomialRing_plural, \
    84         NCPolynomial_plural
     86        NCPolynomial_plural, new_NCP
    8587from sage.rings.polynomial.multi_polynomial_ideal import NCPolynomialIdeal
    8688
    8789from sage.rings.polynomial.multi_polynomial_ideal import MPolynomialIdeal
     
    122124   
    123125    for (i, p) in enumerate(v):
    124126        component = <int>i+1
    125         poly_component = p_Copy(
    126             (<MPolynomial_libsingular>p)._poly, r)
     127        poly_component = copy_sage_polynomial_into_singular_poly(p)
    127128        p_iter = poly_component
    128129        while p_iter!=NULL:
    129130            p_SetComp(p_iter, component, r)
     
    132133        res=p_Add_q(res, poly_component, r)
    133134    return res
    134135
     136
    135137cdef class RingWrap:
    136138    """
    137139    A simple wrapper around Singular's rings.
     
    170172           sage: M = syz(I)
    171173           sage: resolution = mres(M, 0)
    172174        """
    173         assert PY_TYPE_CHECK(base_ring, MPolynomialRing_libsingular)
    174         self.base_ring = <MPolynomialRing_libsingular> base_ring
     175        #FIXME: still not working noncommutative
     176        assert is_sage_wrapper_for_singular_ring(base_ring)
     177        self.base_ring = base_ring
    175178    def __repr__(self):
    176179        """
    177180        EXAMPLE::
     
    229232    args.CleanUp()
    230233    omFreeBin(args, sleftv_bin)
    231234
     235# =====================================
     236# = Singular/Plural Abstraction Layer =
     237# =====================================
    232238
    233 def all_polynomials(s):
     239def is_sage_wrapper_for_singular_ring(ring):
     240    if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular):
     241        return True
     242    if PY_TYPE_CHECK(ring, NCPolynomialRing_plural):
     243        return True
     244    return False
     245
     246cdef new_sage_polynomial(ring,  poly *p):
     247    if PY_TYPE_CHECK(ring, MPolynomialRing_libsingular):
     248        return new_MP(ring, p)
     249    else:
     250        if PY_TYPE_CHECK(ring, NCPolynomialRing_plural):
     251            return new_NCP(ring, p)
     252    raise ValueError("not a singular or plural ring")
     253
     254def is_singular_poly_wrapper(p):
     255    """
     256        checks if p is some data type corresponding to some singular ``poly```.
     257       
     258        EXAMPLE::
     259            sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural
     260            sage: from sage.matrix.constructor  import Matrix
     261            sage: from sage.libs.singular.function import is_singular_poly_wrapper
     262            sage: c=Matrix(2)
     263            sage: c[0,1]=-1
     264            sage: P = NCPolynomialRing_plural(QQ, 2, 'x,y', c=c, d=Matrix(2))
     265            sage: (x,y)=P.gens()
     266            sage: is_singular_poly_wrapper(x+y)
     267            True
     268       
     269    """
     270    return PY_TYPE_CHECK(p, MPolynomial_libsingular) or PY_TYPE_CHECK(p,  NCPolynomial_plural)
     271
     272def all_singular_poly_wrapper(s):
    234273    """
    235274    Tests for a sequence ``s``, whether it consists of
    236275    singular polynomials.
    237276   
    238277    EXAMPLE::
    239278       
    240         sage: from sage.libs.singular.function import all_polynomials
     279        sage: from sage.libs.singular.function import all_singular_poly_wrapper
    241280        sage: P.<x,y,z> = QQ[]
    242         sage: all_polynomials([x+1, y])
     281        sage: all_singular_poly_wrapper([x+1, y])
    243282        True
    244         sage: all_polynomials([x+1, y, 1])
     283        sage: all_singular_poly_wrapper([x+1, y, 1])
    245284        False
    246285    """
    247286    for p in s:
    248         if not isinstance(p, MPolynomial_libsingular):
     287        if not is_singular_poly_wrapper(p):
    249288            return False
    250289    return True
    251290
     291cdef poly* access_singular_poly(p) except <poly*> -1:
     292    """
     293        Get the raw ``poly`` pointer from a wrapper object
     294        EXAMPLE::
     295            # sage: P.<a,b,c> = PolynomialRing(QQ)
     296            #  sage: p=a+b
     297            #  sage: from sage.libs.singular.function import access_singular_poly
     298            #  sage: access_singular_poly(p)
     299    """
     300    if PY_TYPE_CHECK(p, MPolynomial_libsingular):
     301        return (<MPolynomial_libsingular> p)._poly
     302    else:
     303        if PY_TYPE_CHECK(p, NCPolynomial_plural):
     304            return (<NCPolynomial_plural> p)._poly
     305    raise ValueError("not a singular polynomial wrapper")
     306
     307cdef ring* access_singular_ring(r) except <ring*> -1:
     308    """
     309        Get the singular ``ring`` pointer from a
     310        wrapper object.
     311       
     312        EXAMPLE::
     313            # sage: P.<x,y,z> = QQ[]
     314            # sage: from sage.libs.singular.function import access_singular_ring
     315            # sage: access_singular_ring(P)
     316
     317    """
     318    if PY_TYPE_CHECK(r, MPolynomialRing_libsingular):
     319        return (<MPolynomialRing_libsingular> r )._ring
     320    if PY_TYPE_CHECK(r, NCPolynomialRing_plural):
     321        return (<NCPolynomialRing_plural> r )._ring
     322    raise ValueError("not a singular polynomial ring wrapper")
     323 
     324cdef poly* copy_sage_polynomial_into_singular_poly(p):
     325    return p_Copy(access_singular_poly(p), access_singular_ring(p.parent()))
     326
    252327def all_vectors(s):
    253328    """
    254329    Checks if a sequence ``s`` consists of free module
     
    269344        False
    270345    """
    271346    for p in s:
    272         if not (isinstance(p, FreeModuleElement_generic_dense)\
    273             and isinstance(p.parent().base_ring(), MPolynomialRing_libsingular)):
     347        if not (PY_TYPE_CHECK(p, FreeModuleElement_generic_dense)\
     348            and is_sage_wrapper_for_singular_ring(p.parent().base_ring())):
    274349            return False
    275350    return True
    276351
    277352
    278353
     354
     355
     356
     357
    279358cdef class Converter(SageObject):
    280359    """
    281360    A :class:`Converter` interfaces between Sage objects and Singular
     
    305384        cdef leftv *v
    306385        self.args = NULL
    307386        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
     387        if ring is not None:
     388            self._singular_ring = access_singular_ring(ring)
    312389       
    313390        from  sage.matrix.matrix_mpolynomial_dense import Matrix_mpolynomial_dense
    314391        from sage.matrix.matrix_integer_dense import Matrix_integer_dense
     392        from sage.matrix.matrix_generic_dense import Matrix_generic_dense
    315393        for a in args:
    316             if PY_TYPE_CHECK(a, MPolynomial_libsingular) or \
    317                     PY_TYPE_CHECK(a, NCPolynomial_plural):
     394            if is_singular_poly_wrapper(a):
    318395                v = self.append_polynomial(a)
    319396
    320             elif PY_TYPE_CHECK(a, MPolynomialRing_libsingular) or \
    321                     PY_TYPE_CHECK(a, NCPolynomialRing_plural):
     397            elif is_sage_wrapper_for_singular_ring(a):
    322398                v = self.append_ring(a)
    323399
    324400            elif PY_TYPE_CHECK(a, MPolynomialIdeal) or \
     
    336412
    337413            elif PY_TYPE_CHECK(a, Matrix_integer_dense):
    338414                v = self.append_intmat(a)
    339 
     415           
     416            elif PY_TYPE_CHECK(a, Matrix_generic_dense) and\
     417                is_sage_wrapper_for_singular_ring(a.parent().base_ring()):
     418                self.append_matrix(a)
     419           
    340420            elif PY_TYPE_CHECK(a, Resolution):
    341421                v = self.append_resolution(a)
    342422
    343423            elif PY_TYPE_CHECK(a, FreeModuleElement_generic_dense)\
    344                 and isinstance(
    345                     a.parent().base_ring(),
    346                     MPolynomialRing_libsingular):
     424                and is_sage_wrapper_for_singular_ring(
     425                    a.parent().base_ring()):
    347426                v = self.append_vector(a)
    348427               
    349428            # as output ideals get converted to sequences
     
    351430            # this means, that Singular lists should not be converted to Sequences,
    352431            # as we do not want ambiguities
    353432            elif PY_TYPE_CHECK(a, Sequence)\
    354                 and all_polynomials(a):
     433                and all_singular_poly_wrapper(a):
    355434                v = self.append_ideal(ring.ideal(a))
    356435            elif PY_TYPE_CHECK(a, Sequence)\
    357436                and all_vectors(a):
     
    480559        ncols = mat.ncols
    481560        nrows = mat.nrows
    482561        result = Matrix(self._sage_ring, nrows, ncols)
    483         #FIXME: NCPolynomial
    484         cdef MPolynomial_libsingular p
    485562        for i in xrange(nrows):
    486563            for j in xrange(ncols):
    487                 p = MPolynomial_libsingular(self._sage_ring)
    488                 p._poly = mat.m[i*ncols+j]
     564                p = new_sage_polynomial(self._sage_ring, mat.m[i*ncols+j])
    489565                mat.m[i*ncols+j]=NULL
    490566                result[i,j] = p
    491567        return result
     
    527603                else:
    528604                    previous = p_iter
    529605                    p_iter = pNext(p_iter)
    530             result.append(new_MP(self._sage_ring, first))
     606           
     607            result.append(new_sage_polynomial(self._sage_ring, first))
    531608        return free_module(result)
    532609         
    533610    cdef object to_sage_module_element_sequence_destructive( self, ideal *i):
     
    544621        #cdef MPolynomialRing_libsingular sage_ring = self._ring
    545622        cdef int j
    546623        cdef int rank=i.rank
    547         free_module = self._sage_ring**rank       
     624        free_module = self._sage_ring ** rank       
    548625        l = []
    549626
    550627        for j from 0 <= j < IDELEMS(i):
     
    578655        Append the polynomial ``p`` to the list.
    579656        """
    580657        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)
     658        _p = copy_sage_polynomial_into_singular_poly(p)
     659               
    585660        return self._append(_p, POLY_CMD)
    586661
    587662    cdef leftv *append_ideal(self,  i) except NULL:
     
    617692        cdef number *_n =  sa2si(n, self._singular_ring)
    618693        return self._append(<void *>_n, NUMBER_CMD)
    619694
    620     cdef leftv *append_ring(self, MPolynomialRing_libsingular r) except NULL:
     695    cdef leftv *append_ring(self, r) except NULL:
    621696        """
    622697        Append the ring ``r`` to the list.
    623698        """
    624         #FIXME
    625         cdef ring *_r =  <ring*> r._ring
     699        cdef ring *_r =  access_singular_ring(r)
    626700        _r.ref+=1
    627701        return self._append(<void *>_r, RING_CMD)
    628702
    629703    cdef leftv *append_matrix(self, mat) except NULL:
    630         #FIXME
    631704       
    632705        sage_ring = mat.base_ring()
    633         cdef ring *r=<ring*> (<MPolynomialRing_libsingular> sage_ring)._ring
     706        cdef ring *r=<ring*> access_singular_ring(sage_ring)
    634707
    635708        cdef poly *p
    636709        ncols = mat.ncols()
     
    639712        for i in xrange(nrows):
    640713            for j in xrange(ncols):
    641714                #FIXME
    642                 p = p_Copy(
    643                     (<MPolynomial_libsingular> mat[i,j])._poly, r)
     715                p = copy_sage_polynomial_into_singular_poly(mat[i,j])
    644716                _m.m[ncols*i+j]=p
    645717        return self._append(_m, MATRIX_CMD)
    646718
     
    10781150
    10791151        return prefix + get_docstring(self._name)
    10801152
    1081     cdef MPolynomialRing_libsingular common_ring(self, tuple args, ring=None):
     1153    cdef common_ring(self, tuple args, ring=None):
    10821154        """
    10831155        Return the common ring for the argument list ``args``.
    10841156
     
    10991171            if PY_TYPE_CHECK(a, MPolynomialIdeal) or \
    11001172                    PY_TYPE_CHECK(a, NCPolynomialIdeal):
    11011173                ring2 = a.ring()
    1102             elif PY_TYPE_CHECK(a, MPolynomial_libsingular) or \
    1103                     PY_TYPE_CHECK(a, NCPolynomial_plural):
     1174            elif is_singular_poly_wrapper(a):
    11041175                ring2 = a.parent()
    1105             elif PY_TYPE_CHECK(a, MPolynomialRing_libsingular) or \
    1106                     PY_TYPE_CHECK(a, NCPolynomialRing_plural):
     1176            elif is_sage_wrapper_for_singular_ring(a):
    11071177                ring2 = a
    1108             elif PY_TYPE_CHECK(a, int) or PY_TYPE_CHECK(a, long) or PY_TYPE_CHECK(a, basestring):
     1178            elif PY_TYPE_CHECK(a, int) or\
     1179                PY_TYPE_CHECK(a, long) or\
     1180                PY_TYPE_CHECK(a, basestring):
    11091181                continue
    11101182            elif PY_TYPE_CHECK(a, Matrix_integer_dense):
    11111183                continue
     
    11181190            elif PY_TYPE_CHECK(a, Resolution):
    11191191                ring2 = (<Resolution> a).base_ring
    11201192            elif PY_TYPE_CHECK(a, FreeModuleElement_generic_dense)\
    1121                 and PY_TYPE_CHECK(
    1122                     a.parent().base_ring(),
    1123                      MPolynomialRing_libsingular):
     1193                and is_sage_wrapper_for_singular_ring(
     1194                    a.parent().base_ring()):
    11241195                ring2 = a.parent().base_ring()
    11251196            elif ring is not None:
    11261197                a.parent() is ring
     
    11321203                raise ValueError("Rings do not match up.")
    11331204        if ring is None:
    11341205            raise ValueError("Could not detect ring.")
    1135         return <MPolynomialRing_libsingular>ring
     1206        return ring
    11361207
    11371208    def __reduce__(self):
    11381209        """
     
    11681239    global errorreported
    11691240   
    11701241    cdef ring *si_ring
    1171     if isinstance(R, MPolynomialRing_libsingular):
     1242    if PY_TYPE_CHECK(R, MPolynomialRing_libsingular):
    11721243        si_ring = (<MPolynomialRing_libsingular>R)._ring
    11731244    else:
    11741245        si_ring = (<NCPolynomialRing_plural>R)._ring
     
    13991470        <Resolution>
    14001471        sage: singular_list(resolution)
    14011472        [[(-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 
     1473        sage: from sage.rings.polynomial.plural import NCPolynomialRing_plural
     1474        sage: from sage.matrix.constructor  import Matrix
     1475        sage: c=Matrix(2)
     1476        sage: c[0,1]=-1
     1477        sage: P = NCPolynomialRing_plural(QQ, 2, 'x,y', c=c, d=Matrix(2))
     1478        sage: (x,y)=P.gens()
     1479        sage: I= Sequence([x*y,x+y], check=False, immutable=True)#P.ideal(x*y,x+y)
     1480        sage: twostd = singular_function("twostd")
     1481        sage: twostd(I)
     1482        [x + y, y^2]
     1483        sage: M=syz(I)
     1484        doctest...
     1485        sage: M
     1486        [(x + y, x*y)]
     1487        sage: syz(M, ring=P)
     1488        [(0)]
     1489        sage: mres(I, 0)
     1490        <Resolution>
     1491        sage: M=P**3
     1492        sage: v=M((100*x,5*y,10*y*x*y))
     1493        sage: leadcoef(v)
     1494        -10
     1495        sage: v = M([x+y,x*y+y**3,x])
     1496        sage: lead(v)
     1497        (0, y^3)
     1498        sage: jet(v, 2)
     1499        (x + y, x*y, x)
     1500        sage: l = ringlist(P)
     1501        sage: len(l)
     1502        6
     1503        sage: ring(l, ring=P)
     1504        <RingWrap>
     1505        sage: I=twostd(I)
     1506        sage: l[3]=I
     1507        sage: ring(l, ring=P)
     1508        <RingWrap>
    14031509       
    14041510    """
    14051511
  • 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: