Ask Your Question
1

Error computing the primitive part of a monovariate integer polynomial

asked 2018-11-12 17:35:29 +0100

Jsevillamol gravatar image

Consider the following code:

primitive_part = lambda f : f / f.content()

# Primitive Euclidean Algorithm
def primitive_euclides(f,g):
    r0 = f; r1 = g; 

    assert(f.degree() >= g.degree())

    while r1 != 0:
        print(r1)
        delta = r0.degree() - r1.degree() + 1
        aux = ((r1.leading_coefficient())^delta*r0) % r1
        aux = primitive_part(aux)
        r0, r1 = r1, aux

    return r0 #TODO: normal form

# Example: Z[x]
ZX = PolynomialRing(ZZ, 'x')
f = ZX(x^8+2*x^7+4*x^6-2*x^5-3*x^4+4*x^3+9*x^2-2*x-5)
g = ZX(5*x^5-2*x^4-13*x^3-11*x^2+7*x+6)
r = primitive_euclides(f,g)
print(r)

When I execute this code, I get the following output:

5*x^5 - 2*x^4 - 13*x^3 - 11*x^2 + 7*x + 6
15201*x^4 + 25424*x^3 + 10223*x^2 - 15201*x - 10223
Error in lines 15-15
Traceback (most recent call last):
  File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
    flags=compile_flags) in namespace, locals
  File "", line 1, in <module>
  File "", line 8, in primitive_euclides
  File "", line 1, in <lambda>
  File "sage/rings/polynomial/polynomial_element.pyx", line 2237, in sage.rings.polynomial.polynomial_element.Polynomial.__div__ (build/cythonized/sage/rings/polynomial/polynomial_element.c:24826)
    return PyNumber_TrueDivide(left, right)
  File "sage/rings/polynomial/polynomial_element.pyx", line 2233, in sage.rings.polynomial.polynomial_element.Polynomial.__truediv__ (build/cythonized/sage/rings/polynomial/polynomial_element.c:24752)
    return wrapperdescr_fastcall(RingElement.__truediv__,
  File "sage/cpython/wrapperdescr.pyx", line 102, in sage.cpython.wrapperdescr.wrapperdescr_fastcall (build/cythonized/sage/cpython/wrapperdescr.c:1472)
    return slotdef.wrapper(self, args, slotwrapper.d_wrapped)
  File "sage/structure/element.pyx", line 1724, in sage.structure.element.Element.__truediv__ (build/cythonized/sage/structure/element.c:13086)
    return coercion_model.bin_op(left, right, truediv)
  File "sage/structure/coerce.pyx", line 1228, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10935)
    raise bin_op_exception(op, x, y)
TypeError: unsupported operand parent(s) for /: 'Univariate Polynomial Ring in x over Rational Field' and 'Monoid of ideals of Rational Field'

However, when I try isolating the error as follows:

r0 = ZX(5*x^5 - 2*x^4 - 13*x^3 - 11*x^2 + 7*x + 6)
r1 = ZX(15201*x^4 + 25424*x^3 + 10223*x^2 - 15201*x - 10223)

delta = r0.degree() - r1.degree() + 1
aux = ((r1.leading_coefficient())^delta*r0) % r1
aux = primitive_part(aux)
aux

I get the output I expected, x^3 + x^2 - 1

What is going on? How do I fix this?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-11-12 20:21:25 +0100

tmonteil gravatar image

If you add some debugging print, you will notice that arent(aux) is first Univariate Polynomial Ring in x over Integer Ring but then Univariate Polynomial Ring in x over Rational Field, because you made a division, and though the resulting coefficient remain integers, they now belong to the rational field, see:

sage: parent(2)
Integer Ring
sage: 4/2
2
sage: parent(4/2)
Rational Field

So what you have to do is to convert the result back into ZX:

sage: primitive_part = lambda f : ZX(f / f.content())

Or, since ZX is the parent of f, you can do more generically:

sage: primitive_part = lambda f : parent(f)(f / f.content())
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-11-12 17:35:29 +0100

Seen: 302 times

Last updated: Nov 12 '18