polynomial remainder

1,289 views
Skip to first unread message

Michael Beeson

unread,
Jan 3, 2010, 1:59:54 PM1/3/10
to sage-support
I am just learning Sage. I tried to define a polynomial and then
find the polynomial remainder upon division by the
cyclotomic_polynomial(18), which is 1-x^3+x^6. This is easily
accomplished in Mathematica using the PolynomialRemainder function.
But I could not find the analog of that function in the Sage
documentation.

I expected it to be a method of the Polynomial class. I suspect my
difficulty lies in converting a symbolic expression to a polynomial.
In fact, if I knew how to do that properly I shouldn't even need
PolynomialRemainder. The plan is to create the ring Q(z) where
z is a primitive 18th root of 1, and then enter some expression such
as (z^5-1/z^5 )(z - 1/z) (but much longer) and see it expressed as
a polynomial of degree 5 in z. In Mathematica I just multiplied by a
sufficient power of z and then used PolynomialRemainder (I'm going to
set the result to zero so multiplying by a power of z is fine).

What is the right way to do this in Sage?

bump

unread,
Jan 3, 2010, 3:42:09 PM1/3/10
to sage-support

On Jan 3, 10:59 am, Michael Beeson <profbee...@gmail.com> wrote:
> I am just learning Sage.   I tried to define a polynomial and then
> find the polynomial remainder upon division by the
> cyclotomic_polynomial(18), which is 1-x^3+x^6.    This is easily
> accomplished in Mathematica using the PolynomialRemainder function.
> But I could not find the analog of that function in the Sage
> documentation.

> What is the right way to do this in Sage?

I think this is what you are trying to do:

sage: P.<x> = PolynomialRing(QQ)
sage: f = x^10+2*x^8+3*x+1
sage: f in P
True
sage: g = cyclotomic_polynomial(18); g
x^6 - x^3 + 1
sage: f.quo_rem(g)
(x^4 + 2*x^2 + x, 2*x^5 - 2*x^2 + 2*x + 1)

The first term is the quotient and the second is the remainder. See
sage: f.quo_rem?

for the description of the method.

Daniel Bump

Jason Grout

unread,
Jan 4, 2010, 4:38:16 AM1/4/10
to sage-s...@googlegroups.com

Michael Beeson

unread,
Jan 5, 2010, 3:37:07 AM1/5/10
to sage-support
Thanks, that's a start, but my polynomials have some parameters
a,b,c,...
in the coefficients. In Mathematica you say PolyomialRemainder[f,g,x]
where the
last 'x' names the polynomial variable, so all other variables are
parameters. When
I tried to modify your code by inserting a=var('a') and then
f=a*x^10, it didn't work,
because now f only belongs to sage.symbolic.expression.Expression, and
not
to P. Well, obviously, because I didn't specify that a belongs to
QQ. Is there
a way to do that so that I can work with parameters in the
coefficients of a polynomial?
Various guesses as to what might be the way to do that didn't work.

If there's an easy way that I should have been able to look this up
for myself I would like to know that even more than the specific
answer.

On another subject:
The documentation mentions the "ring" of symbolic expressions. To be
a ring one needs to know when two symbolic expressions are equal. Is
1/0 a symbolic expression and if so is it equal to oo ? How about x-
x and 0? x/x and 1? More generally has anything been written
about the or "a" semantics for Sage?

Michael

Yann

unread,
Jan 5, 2010, 8:44:34 AM1/5/10
to sage-support

On 5 jan, 09:37, Michael Beeson <profbee...@gmail.com> wrote:
> Thanks,  that's a start,  but my polynomials have some parameters
> a,b,c,...
> in the coefficients.  In Mathematica you say PolyomialRemainder[f,g,x]
> where the
> last 'x'  names the polynomial variable, so all other variables are
> parameters.  When
> I tried to modify your code by inserting  a=var('a')  and then
> f=a*x^10,   it didn't work,
> because now f only belongs to sage.symbolic.expression.Expression, and
> not
> to P.  

Is the following what you look for?

sage: R.<x>=PolynomialRing(SR)
sage: var('a')
a
sage: f = a*x^10+2*x^8+3*x+1
sage: g = cyclotomic_polynomial(18)(x)
sage: f.quo_rem(g)
(a*x^4 + 2*x^2 + a*x, 2*x^5 - 2*x^2 + (-a + 3)*x + 1)

Yann

William Stein

unread,
Jan 5, 2010, 9:24:38 AM1/5/10
to sage-support
On Tue, Jan 5, 2010 at 12:37 AM, Michael Beeson <profb...@gmail.com> wrote:
> Thanks,  that's a start,  but my polynomials have some parameters
> a,b,c,...
> in the coefficients.  In Mathematica you say PolyomialRemainder[f,g,x]
> where the
> last 'x'  names the polynomial variable, so all other variables are
> parameters.  When
> I tried to modify your code by inserting  a=var('a')  and then
> f=a*x^10,   it didn't work,
> because now f only belongs to sage.symbolic.expression.Expression, and
> not
> to P.   Well,  obviously, because I didn't specify that a belongs to
> QQ.   Is there
> a way to do that so that I can work with parameters in the
> coefficients of a polynomial?
> Various guesses as to what might be the way to do that didn't work.
>
> If there's an easy way that I should have been able to look this up
> for myself I would like to know that even more than the specific
> answer.
>
> On another subject:
> The documentation mentions the "ring" of symbolic expressions.   To be
> a ring one needs to know when two symbolic expressions are equal.   Is
> 1/0  a symbolic expression and if so is it equal to oo ?

1/0 is an error.

sage: 1/0
Traceback (most recent call last):
...
ZeroDivisionError: Rational division by zero

> How about x-
> x and 0?

sage: bool(x-x == 0)
True

> x/x and 1?

sage: bool(x/x == 1)
True

> More generally has anything been written
> about the or "a" semantics for Sage?

Maybe in the ginac documentation...

>
> Michael
>
>
> On Jan 3, 12:42 pm, bump <b...@match.stanford.edu> wrote:
>> On Jan 3, 10:59 am, Michael Beeson <profbee...@gmail.com> wrote:
>>
>> > I am just learning Sage.   I tried to define a polynomial and then
>> > find the polynomial remainder upon division by the
>> > cyclotomic_polynomial(18), which is 1-x^3+x^6.    This is easily
>> > accomplished in Mathematica using the PolynomialRemainder function.
>> > But I could not find the analog of that function in the Sage
>> > documentation.
>> > What is the right way to do this in Sage?
>>
>> I think this is what you are trying to do:
>>
>> sage: P.<x> = PolynomialRing(QQ)
>> sage: f = x^10+2*x^8+3*x+1
>> sage: f in P
>> True
>> sage: g = cyclotomic_polynomial(18); g
>> x^6 - x^3 + 1
>> sage: f.quo_rem(g)
>> (x^4 + 2*x^2 + x, 2*x^5 - 2*x^2 + 2*x + 1)
>>
>> The first term is the quotient and the second is the remainder. See
>> sage: f.quo_rem?
>>
>> for the description of the method.
>>
>> Daniel Bump
>

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Michael Beeson

unread,
Jan 18, 2013, 2:09:41 PM1/18/13
to sage-s...@googlegroups.com
Sage hangs on the following input:

sage: K.<p,d,e,N> = FractionField(PolynomialRing(QQ,4,'pdeN'))
sage: R.<x> = K[]
sage: a = x-x^-1
sage: b = x^6-x^-6
sage: c = x^7-x^-7
sage: X = p*a + d*b + e*c
sage: F = N*a*b*c - X^2*(x-x^-1)

and also on this closely related input

sage: K.<p,d,e,N> = FractionField(PolynomialRing(QQ,4,'pdeN'))
sage: R.<x>= K[]
sage: a = x-x^-1
sage: b = x^6-x^-6
sage: c = x^7-x^-7
sage: X = p*a + d*b + e*c
sage: F = N*a*b*c
sage: G = X^2*(x-x^-1)
sage: F = F-G

but if the last line is changed to
sage: F = R(x^15*F) - R(x^15*G)

then it works fine.
Reply all
Reply to author
Forward
0 new messages