ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 30 Jun 2013 09:55:51 -0500Powers of irreducible polynomialshttps://ask.sagemath.org/question/10296/powers-of-irreducible-polynomials/I'm working on a WeBWorK project to demonstrate the relatively new WeBWorK-Sage connectivity. We'd like to demonstrate some math problems that one could program in WeBWorK using Sage more cleanly than without Sage.
To that end I'm trying the following: WeBWorK would display a randomly generated polynomial in Z[x]. Let's say it could be degree 2, 3, or 4 with integer coefficients from [-10,10]. A student has to factor it completely.
I'm able to send the student's answer to Sage as a string, properly formatted for Sage. It might be '(x^2+1)*(2*x+1)' or '2*(x^2+3*x+2)*(x+2)^2'. But how can I evaluate whether or not the answer is fully factored? (For the moment, I'm not concerned about factoring out integers.)
So far, I have done this, where poly is the string passed to Sage:
<pre>
<code>
factored = True
if (poly).operator()!=operator.mul:
if (poly).polynomial(base_ring=QQ).is_irreducible():
factored = True
else:
factored = False
else:
factors = (poly).operands()
for factor in factors:
factored = factored and factor.polynomial(base_ring=QQ).is_irreducible()
print factored
</code>
</pre>
And factored is True if all of the factors in poly were irreducible, and False otherwise. This is not what I want since I want something like '(x+1)^2*(x+2)' to count as factored. Is there a quick test for whether or not a polynomial is a power of an irreducible? Or a constant multiple of such a thing?
Sat, 29 Jun 2013 15:20:00 -0500https://ask.sagemath.org/question/10296/powers-of-irreducible-polynomials/Answer by vdelecroix for <p>I'm working on a WeBWorK project to demonstrate the relatively new WeBWorK-Sage connectivity. We'd like to demonstrate some math problems that one could program in WeBWorK using Sage more cleanly than without Sage. </p>
<p>To that end I'm trying the following: WeBWorK would display a randomly generated polynomial in Z[x]. Let's say it could be degree 2, 3, or 4 with integer coefficients from [-10,10]. A student has to factor it completely.</p>
<p>I'm able to send the student's answer to Sage as a string, properly formatted for Sage. It might be '(x^2+1)<em>(2</em>x+1)' or '2<em>(x^2+3</em>x+2)*(x+2)^2'. But how can I evaluate whether or not the answer is fully factored? (For the moment, I'm not concerned about factoring out integers.)</p>
<p>So far, I have done this, where poly is the string passed to Sage:</p>
<pre><code>
factored = True
if (poly).operator()!=operator.mul:
if (poly).polynomial(base_ring=QQ).is_irreducible():
factored = True
else:
factored = False
else:
factors = (poly).operands()
for factor in factors:
factored = factored and factor.polynomial(base_ring=QQ).is_irreducible()
print factored
</code>
</pre>
<p>And factored is True if all of the factors in poly were irreducible, and False otherwise. This is not what I want since I want something like '(x+1)^2*(x+2)' to count as factored. Is there a quick test for whether or not a polynomial is a power of an irreducible? Or a constant multiple of such a thing?</p>
https://ask.sagemath.org/question/10296/powers-of-irreducible-polynomials/?answer=15163#post-id-15163For your very last question:
sage: R.<x> = PolynomialRing(QQ, 'x')
sage: P = (1/2*x+3)^5
sage: P.factor()
(1/32) * (x + 6)^5
sage: len(P.factor())
1
sage: Q = P * (3*x+2)
sage: len(Q.factor())
2
This is because you may think of the result of .factor() as a unit together with a list of pairs (factor, multiplicity).Sun, 30 Jun 2013 09:55:51 -0500https://ask.sagemath.org/question/10296/powers-of-irreducible-polynomials/?answer=15163#post-id-15163