# Polynomial Ring in Sage

Hello, I want to use this code in my python script, but it return error:

from sage.all import *
var('x')
R = ZZ['x']
f = 3*x**2 - 3*x**8
g = x**2 - 1
print g.degree(x)
h = f.mod(x**2 - 1)
print h.degree(x)
print f.quo_rem(g)


and error:

2
8
Traceback (most recent call last):
File "degree.py", line 15, in <module>
print f.quo_rem(g)
File "sage/structure/element.pyx", line 438, in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4406)
File "sage/structure/misc.pyx", line 257, in sage.structure.misc.getattr_from_other_class (build/cythonized/sage/structure/misc.c:1631)
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'quo_rem'


What's the problem I can't use this commands in my script? And also note the the result of

h = f.mod(x**2 - 1)
is incorrect!

edit retag close merge delete

Sort by » oldest newest most voted

The problem comes from using a symbolic variable.

This means f and g as defined in your code live in Sage's SymbolicRing.

To work with polynomials, better work in the polynomial ring you define.

Achieve that by defining x as the generator of the polynomial ring R.

from sage.all import *
R = ZZ['x']
x = R.gen()
f = 3*x**2 - 3*x**8
g = x**2 - 1
print g.degree(x)
h = f.mod(x**2 - 1)
print h.degree(x)
print f.quo_rem(g)


Now x is the generator of R and everything works as expected.

more