Ticket #7797: trac_7797-letterplace.3.patch

File trac_7797-letterplace.3.patch, 4.5 KB (added by PolyBoRi, 9 years ago)
  • new file sage/libs/singular/letterplace.py

    # HG changeset patch
    # User Michael Brickenstein <brickenstein@mfo.de>
    # Date 1262206690 -3600
    # Node ID 8af359e49f41055ea226c2561d8551354c6db0bb
    # Parent  2f30153147fc9655b3d1610b199ced51586f88d0
    Add basic interface to LETTERPLACE from Singular.
    
    diff --git a/sage/libs/singular/letterplace.py b/sage/libs/singular/letterplace.py
    new file mode 100644
    - +  
     1from sage.libs.singular.letterplace_ring import \
     2        temporary_MPolynomialRing_from_letterplace_ring
     3from sage.libs.singular.function import singular_function
     4from sage.algebras.free_algebra import FreeAlgebra
     5from sage.libs.singular.function import lib, singular_function
     6from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
     7
     8def iter_free_monomial(monomial):
     9    """
     10    EXAMPLES::
     11
     12        sage: from sage.libs.singular.letterplace import iter_free_monomial
     13        sage: F = FreeMonoid(3,'x,y,z')
     14        sage: F.gens()
     15        (x, y, z)
     16        sage: x, y, z = F.gens()
     17        sage: list(iter_free_monomial(x^2*y*z*y^2))
     18        [x, x, y, z, y, y]
     19    """
     20    for (gen, exponent) in monomial:
     21        for i in xrange(exponent):
     22            yield gen
     23
     24def freegb(ideal, deg_bound):
     25    """
     26    EXAMPLES::
     27
     28        sage: from sage.libs.singular.letterplace import freegb
     29        sage: F.<x,y,z> = FreeAlgebra(QQ, 3); F
     30        Free Algebra on 3 generators (x, y, z) over Rational Field
     31        sage: l=[2*x*z*x+y*x*y, 3*x*y+x*z]
     32        sage: freegb(l, 10)
     33        [3*y*x*z^7*y + y*x*z^8, 3*y*x*z^6*y + y*x*z^7, y*x*z^6*x*z + 314928*y^2*x*z^2*x^5, 3*y*x*z^5*y + y*x*z^6, y*x*z^5*x*z - 17496*y^2*x*z^2*x^4, 3*y*x*z^4*y + y*x*z^5, y*x*z^4*x*z + 972*y^2*x*z^2*x^3, 3*y*x*z^4*x^2*z*y + y*x*z^4*x^2*z^2, 3*y*x*z^3*y + y*x*z^4, y*x*z^3*x*z - 54*y^2*x*z^2*x^2, 3*y*x*z^3*x^2*z^2*y + y*x*z^3*x^2*z^3, 3*y*x*z^3*x^2*z*y + y*x*z^3*x^2*z^2, 3*y*x*z^3*x^3*z*y + y*x*z^3*x^3*z^2, 3*y*x*z^2*y + y*x*z^3, y*x*z^2*x*z + 3*y^2*x*z^2*x, 3*y*x*z^2*x^2*z^3*y + y*x*z^2*x^2*z^4, 3*y*x*z^2*x^2*z^2*y + y*x*z^2*x^2*z^3, y*x*z^2*x^2*z^2*x*z + 3*y^2*x*z^2*x^2*z^2*x, 3*y*x*z^2*x^2*z*y + y*x*z^2*x^2*z^2, 3*y*x*z^2*x^3*z^2*y + y*x*z^2*x^3*z^3, 3*y*x*z^2*x^3*z*y + y*x*z^2*x^3*z^2, 3*y*x*z^2*x^4*z*y + y*x*z^2*x^4*z^2, 3*y*x*z*y + y*x*z^2, x*z*y^6*x*z - 7776*y*x*z^2*x^6, x*z*y^5*x*z - 1296*y*x*z^2*x^5, x*z*y^4*x*z - 216*y*x*z^2*x^4, x*z*y^3*x*z - 36*y*x*z^2*x^3, x*z*y^2*x*z - 6*y*x*z^2*x^2, x*z*y*x*z - y*x*z^2*x, 6*x*z*x - y*x*z, 3*x*y + x*z]
     34    """
     35    lib("freegb.lib")
     36    free_algebra=ideal[0].parent()
     37    base_ring = free_algebra.base_ring()
     38    gens = free_algebra.gens()
     39    variable_names = [str(g) for g in gens]
     40    pre_letter_place_ring=PolynomialRing(base_ring, variable_names)
     41    make_letter_place_ring = singular_function("makeLetterplaceRing")
     42    ring_wrap = make_letter_place_ring(10, ring=pre_letter_place_ring)
     43   
     44    (to_letter_place, from_letter_place) = \
     45        temporary_MPolynomialRing_from_letterplace_ring(ring_wrap,
     46        base_ring, gens,  deg_bound)
     47   
     48    singular_ring = iter(from_letter_place.keys()).next().parent()
     49   
     50    polynomial_list=[]
     51    for p in ideal:
     52        sum_p = 0
     53        for term in p:
     54            (coef, monomial) = term
     55            m = 1
     56            for (i,v) in enumerate(iter_free_monomial(monomial)):
     57                m=m*to_letter_place[v][i]
     58            m=m*coef
     59            sum_p = sum_p+m
     60        polynomial_list.append(sum_p)
     61   
     62   
     63    free_gbasis=singular_function("freeGBasis")
     64    from sage.libs.singular.option import opt_ctx
     65   
     66    singular_system=singular_function("system")
     67   
     68    #letter place needs these options
     69    with opt_ctx(redTail = True,
     70        redSB = True):   
     71        gb = singular_system("freegb",
     72            singular_ring.ideal(polynomial_list),
     73            deg_bound,
     74            len(gens))
     75
     76    result = []
     77    for p in gb:
     78        sum_p=free_algebra(0)
     79        for (coef, monomial) in p:
     80            m=free_algebra(1)
     81            index_generator_list=[]
     82            for v in monomial.variables():
     83                #there exist no exponent > 1 in the result freegb
     84                (index, alg_gen) = from_letter_place[v]
     85                index_generator_list.append((index, alg_gen))
     86            index_generator_list = sorted(index_generator_list,
     87                    key=lambda x: x[0])
     88           
     89            for (index, alg_gen) in index_generator_list:
     90                m = m * alg_gen
     91            m=coef * m
     92            sum_p = sum_p + m
     93        result.append(sum_p)
     94    return result