| 1 | from sage.libs.singular.letterplace_ring import \ |
| 2 | temporary_MPolynomialRing_from_letterplace_ring |
| 3 | from sage.libs.singular.function import singular_function |
| 4 | from sage.algebras.free_algebra import FreeAlgebra |
| 5 | from sage.libs.singular.function import lib, singular_function |
| 6 | from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing |
| 7 | |
| 8 | def 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 | |
| 24 | def 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 |