Ticket #11431: trac11431_singular_sage_documentation.patch

File trac11431_singular_sage_documentation.patch, 11.1 KB (added by SimonKing, 10 years ago)

Show examples of .sage() to the singular interface tutorial. Fix some details of the previous patch.

  • sage/interfaces/singular.py

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1307513795 -7200
    # Node ID a17f68134769b084687b171b3a92d56bff0c75e8
    # Parent  5687b1bb99be4e1154eb175ddce5b3b9273e6cee
    #11431: Show examples of .sage() to the singular interface tutorial. Fix some details of the previous patch.
    
    diff --git a/sage/interfaces/singular.py b/sage/interfaces/singular.py
    a b  
    1919
    2020- Martin Albrecht (2006-05-18): added sage_poly.
    2121
     22- Simon King (2011-06-06): Make conversion from Singular to Sage more flexible.
     23
    2224Introduction
    2325------------
    2426
     
    4345#. ``singular.eval(expr)`` - Evaluation of arbitrary
    4446   Singular expressions, with the result returned as a string.
    4547
     48Of course, there are polynomial rings and ideals in Sage as well
     49(often based on a C-library interface to Singular). One can convert
     50an object in the Singular interpreter interface to Sage by the
     51method ``sage()``.
     52
    4653
    4754Tutorial
    4855--------
     
    8794
    8895::
    8996
    90     sage: R.<x, y> = PolynomialRing(QQ,2)
    91     sage: g = eval(f.sage_polystring()); g
     97    sage: g = f.sage(); g
    9298    9*x^16 - 18*x^13*y^2 - 9*x^12*y^3 + 9*x^10*y^4 - 18*x^11*y^2 + 36*x^8*y^4 + 18*x^7*y^5 - 18*x^5*y^6 + 9*x^6*y^4 - 18*x^3*y^6 - 9*x^2*y^7 + 9*y^8
    93     sage: eval(F[1][2].sage_polystring())
     99    sage: F[1][2].sage()
    94100    x^6 - 2*x^3*y^2 - x^2*y^3 + y^4
     101    sage: g.parent()
     102    Multivariate Polynomial Ring in x, y over Rational Field
    95103
    96104This example illustrates polynomial GCD's::
    97105
     
    126134    x0*x2^3-x1^2*x2^2+x1*x2^3,
    127135    x0*x1-x0*x2-x1*x2,
    128136    x0^2*x2-x0*x2^2-x1*x2^2
     137    sage: I2.sage()
     138    Ideal (x1^2*x2^2, x0*x2^3 - x1^2*x2^2 + x1*x2^3, x0*x1 - x0*x2 - x1*x2, x0^2*x2 - x0*x2^2 - x1*x2^2) of Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
     139
    129140
    130141This example illustrates moving a polynomial from one ring to
    131142another. It also illustrates calling a method of an object with an
     
    158169    6*x+6*x*y^2-2*y^3,6*x^2*y-6*x*y^2,  0,
    159170    6*x^2*y-6*x*y^2,  6*y+2*x^3-6*x^2*y,0,
    160171    0,                0,                2
    161     sage: H.det()
     172    sage: H.sage()
     173    [6*x + 6*x*y^2 - 2*y^3     6*x^2*y - 6*x*y^2                     0]
     174    [    6*x^2*y - 6*x*y^2 6*y + 2*x^3 - 6*x^2*y                     0]
     175    [                    0                     0                     2]
     176    sage: H.det()   # This is a polynomial in Singular
    162177    72*x*y+24*x^4-72*x^3*y+72*x*y^3-24*y^4-48*x^4*y^2+64*x^3*y^3-48*x^2*y^4
     178    sage: H.det().sage()   # This is the corresponding polynomial in Sage
     179    72*x*y + 24*x^4 - 72*x^3*y + 72*x*y^3 - 24*y^4 - 48*x^4*y^2 + 64*x^3*y^3 - 48*x^2*y^4
    163180
    164181The 1x1 and 2x2 minors::
    165182
     
    13291346        """
    13301347        return str(self).replace('^','**')
    13311348
    1332     def sage_basering(self):
     1349    def sage_global_ring(self):
    13331350        """
    13341351        Return the current basering in Singular as a polynomial ring or quotient ring.
    13351352
     
    13371354
    13381355            sage: singular.eval('ring r1 = (9,x),(a,b,c,d,e,f),(M((1,2,3,0)),wp(2,3),lp)')
    13391356            'ring r1 = (9,x),(a,b,c,d,e,f),(M((1,2,3,0)),wp(2,3),lp);'
    1340             sage: R = singular('r1').sage_basering()
     1357            sage: R = singular('r1').sage_global_ring()
    13411358            sage: R
    13421359            Multivariate Polynomial Ring in a, b, c, d, e, f over Finite Field in x of size 3^2
    13431360            sage: R.term_order()
     
    13521369
    13531370            sage: singular.eval('ring r2 = (0,x),(a,b,c),dp')
    13541371            'ring r2 = (0,x),(a,b,c),dp;'
    1355             sage: singular('r2').sage_basering()
     1372            sage: singular('r2').sage_global_ring()
    13561373            Multivariate Polynomial Ring in a, b, c over Fraction Field of Univariate Polynomial Ring in x over Rational Field
    13571374
    13581375        ::
     
    13611378            'ring r3 = (3,z),(a,b,c),dp;'
    13621379            sage: singular.eval('minpoly = 1+z+z2+z3+z4')
    13631380            'minpoly = 1+z+z2+z3+z4;'
    1364             sage: singular('r3').sage_basering()
     1381            sage: singular('r3').sage_global_ring()
    13651382            Multivariate Polynomial Ring in a, b, c over Univariate Quotient Polynomial Ring in z over Finite Field of size 3 with modulus z^4 + z^3 + z^2 + z + 1
    13661383
     1384        Real and complex fields in both Singular and Sage are defined with a precision.
     1385        The precision in Singular is given in terms of digits, but in Sage it is given
     1386        in terms of bits. So, the digit precision is internally converted to a reasonable
     1387        bit precision::
     1388
     1389            sage: singular.eval('ring r4 = (real,20),(a,b,c),dp')
     1390            'ring r4 = (real,20),(a,b,c),dp;'
     1391            sage: singular('r4').sage_global_ring()
     1392            Multivariate Polynomial Ring in a, b, c over Real Field with 70 bits of precision
     1393
    13671394        The case of complex coefficients is not fully supported, yet, since
    13681395        the generator of a complex field in Sage is always called "I"::
    13691396
    1370             sage: singular.eval('ring r4 = (complex,15,j),(a,b,c),dp')
    1371             'ring r4 = (complex,15,j),(a,b,c),dp;'
    1372             sage: R = singular('r4').sage_basering(); R
     1397            sage: singular.eval('ring r5 = (complex,15,j),(a,b,c),dp')
     1398            'ring r5 = (complex,15,j),(a,b,c),dp;'
     1399            sage: R = singular('r5').sage_global_ring(); R
    13731400            Multivariate Polynomial Ring in a, b, c over Complex Field with 54 bits of precision
    13741401            sage: R.base_ring()('j')
    13751402            Traceback (most recent call last):
     
    13801407
    13811408        In our last example, the base ring is a quotient ring::
    13821409
    1383             sage: singular.eval('ring r5 = (9,a), (x,y,z),lp')
    1384             'ring r5 = (9,a), (x,y,z),lp;'
     1410            sage: singular.eval('ring r6 = (9,a), (x,y,z),lp')
     1411            'ring r6 = (9,a), (x,y,z),lp;'
    13851412            sage: Q = singular('std(ideal(x^2,x+y^2+z^3))', type='qring')
    1386             sage: Q.sage_basering()
     1413            sage: Q.sage_global_ring()
    13871414            Quotient of Multivariate Polynomial Ring in x, y, z over Finite Field in a of size 3^2 by the ideal (y^4 - y^2*z^3 + z^6, x + y^2 + z^3)
    13881415
    13891416        AUTHOR:
     
    14031430            from sage.all import QQ
    14041431            br = QQ
    14051432        elif charstr[0]=='real':
    1406             from sage.all import RR
    1407             br = RR
     1433            from sage.all import RealField, ceil, log
     1434            prec = singular.eval('ringlist(basering)[1][2][1]')
     1435            br = RealField(ceil((ZZ(prec)+1)/log(2,10)))
    14081436            is_extension = False
    14091437        elif charstr[0]=='complex':
    14101438            from sage.all import ComplexField, ceil, log
    1411             prec, I = charstr[1].split(',')
     1439            prec = singular.eval('ringlist(basering)[1][2][1]')
     1440            I = singular.eval('ringlist(basering)[1][3]')
    14121441            br = ComplexField(ceil((ZZ(prec)+1)/log(2,10)))
    14131442            is_extension = False
    14141443        else:
     
    14431472       
    14441473        # Now, we form the polynomial ring over BR with the given variables,
    14451474        # using Singular's term order
    1446         from sage.rings.polynomial.term_order import TermOrder_from_Singular
     1475        from sage.rings.polynomial.term_order import termorder_from_singular
    14471476        from sage.all import PolynomialRing
    14481477        if singular.eval('typeof(basering)')=='ring':
    1449             return PolynomialRing(BR, names=singular.eval('varstr(basering)'), order=TermOrder_from_Singular(singular))
    1450         P = PolynomialRing(BR, names=singular.eval('varstr(basering)'), order=TermOrder_from_Singular(singular))
     1478            return PolynomialRing(BR, names=singular.eval('varstr(basering)'), order=termorder_from_singular(singular))
     1479        P = PolynomialRing(BR, names=singular.eval('varstr(basering)'), order=termorder_from_singular(singular))
    14511480        return P.quotient(singular('ringlist(basering)[4]')._sage_(P), names=singular.eval('varstr(basering)'))
    14521481
    14531482    def sage_poly(self, R=None, kcache=None):
     
    14591488       
    14601489       
    14611490        -  ``R`` - (default: None); an optional polynomial ring.
    1462            If it is provided, then you *must* take care it
     1491           If it is provided, then you have to make sure that it
    14631492           matches the current singular ring as, e.g., returned by
    14641493           singular.current_ring(). By default, the output of
    1465            :meth:`sage_basering` is used.
     1494           :meth:`sage_global_ring` is used.
    14661495       
    14671496        -  ``kcache`` - (default: None); an optional dictionary
    14681497           for faster finite field lookups, this is mainly useful for finite
     
    15311560        ring_is_fine = False
    15321561        if R is None:
    15331562            ring_is_fine = True
    1534             R = self.sage_basering()
     1563            R = self.sage_global_ring()
    15351564
    15361565        sage_repr = {}
    15371566        k = R.base_ring()
     
    16431672
    16441673        -  ``R`` - (default: None); an optional ring, over which
    16451674           the resulting matrix is going to be defined.
    1646            By default, the output of :meth:`sage_basering` is used.
     1675           By default, the output of :meth:`sage_global_ring` is used.
    16471676
    16481677        - ``sparse`` - (default: True); determines whether the
    16491678          resulting matrix is sparse or not.
     
    16631692        nrows, ncols = int(self.nrows()),int(self.ncols())
    16641693
    16651694        if R is None:
    1666             R = self.sage_basering()
     1695            R = self.sage_global_ring()
    16671696            A = Matrix(R, nrows, ncols, sparse=sparse)
    16681697            #this is slow
    16691698            for x in range(nrows):
     
    17701799        elif typ == 'string':
    17711800            return repr(self)
    17721801        elif typ == 'ideal':
    1773             R = R or self.sage_basering()
     1802            R = R or self.sage_global_ring()
    17741803            return R.ideal([p.sage_poly(R) for p in self])
    17751804        elif typ in ['ring', 'qring']:
    17761805            br = singular('basering')
    17771806            self.set_ring()
    1778             R = self.sage_basering()
     1807            R = self.sage_global_ring()
    17791808            br.set_ring()
    17801809            return R
    17811810        raise NotImplementedError, "Coercion of this datatype not implemented yet"
  • sage/rings/polynomial/term_order.py

    diff --git a/sage/rings/polynomial/term_order.py b/sage/rings/polynomial/term_order.py
    a b  
    18591859        """           
    18601860        return self._weights is not None
    18611861
    1862 def TermOrder_from_Singular(S):
     1862def termorder_from_singular(S):
    18631863    """
    18641864    Return the Sage term order of the basering in the given Singular interface
    18651865
     
    18771877
    18781878        sage: singular.eval('ring r1 = (9,x),(a,b,c,d,e,f),(M((1,2,3,0)),wp(2,3),lp)')
    18791879        'ring r1 = (9,x),(a,b,c,d,e,f),(M((1,2,3,0)),wp(2,3),lp);'
    1880         sage: from sage.rings.polynomial.term_order import TermOrder_from_Singular
    1881         sage: TermOrder_from_Singular(singular)
     1880        sage: from sage.rings.polynomial.term_order import termorder_from_singular
     1881        sage: termorder_from_singular(singular)
    18821882        Block term order with blocks:
    18831883        (Matrix term order with matrix
    18841884        [1 2]
     
    19101910        else:
    19111911            order.append(TermOrder(inv_singular_name_mapping[blocktype], ZZ(singular.eval("size(%s[2])"%block.name()))))
    19121912    if not order:
    1913         raise ValueError, "Invalid termorder in Singular"
     1913        raise ValueError, "Invalid term order in Singular"
    19141914    out = order.pop(0)
    19151915    while order:
    19161916        out = out + order.pop(0)