trac #16682: fix random_element in polynomial ring
trac #16682: the zero polynomial has degree -1
trac #16682: merge 6.3.rc0
trac #16682: input check
trac #16682: better random element
trac #16682: fix doctests
trac #16682: merge sage 6.3
trac #16881: fix factorization
trac #16682: AssertionError -> ValueError
Trac #16682: Fix random_element of polynomial ring

The degree is not the one expected (as there is no check that the
dominant coefficient is nonzero):
{{{
sage: R = PolynomialRing(GF(2),'z')
sage: R.random_element(degree=5).degree()
3
}}}

This was found in #16681

URL: http://trac.sagemath.org/16682
Reported by: vdelecroix
Ticket author(s): Vincent Delecroix
Reviewer(s): Volker Braun
trac #16881: fix documentation issue
Trac #16681: Random doctest failures in sage/rings/algebraic_closure_finite_field.py

This is caused by #15390
{{{
sage -t --long src/sage/rings/algebraic_closure_finite_field.py
**********************************************************************
File "src/sage/rings/algebraic_closure_finite_field.py", line 940, in sa
ge.rings.algebraic_closure_finite_field.AlgebraicClosureFiniteField_gene
ric._factor_univariate_polynomial
Failed example:
    for d in xrange(10):
        p = R.random_element(degree=randint(2,8))
        assert p.factor().prod() == p
Exception raised:
    Traceback (most recent call last):
      File "/Users/buildslave-
sage/slave/sage_git/build/local/lib/python2.7/site-
packages/sage/doctest/forker.py", line 480, in _run
        self.execute(example, compiled, test.globs)
      File "/Users/buildslave-
sage/slave/sage_git/build/local/lib/python2.7/site-
packages/sage/doctest/forker.py", line 839, in execute
        exec compiled in globs
      File "<doctest sage.rings.algebraic_closure_finite_field.Algebraic
ClosureFiniteField_generic._factor_univariate_polynomial[4]>", line 3,
in <module>
        assert p.factor().prod() == p
    AssertionError
**********************************************************************
1 item had failures:
   1 of   6 in sage.rings.algebraic_closure_finite_field.AlgebraicClosur
eFiniteField_generic._factor_univariate_polynomial
}}}

First of all, you should use a form of assert that gives some minimal
information when it can fail, e.g. in the form below. The following
fails occasionally and makes it easy enough to reproduce:
{{{
sage: K = GF(3).algebraic_closure()
sage: R = PolynomialRing(K, 'T')
sage: T = R.gen()
sage: for d in xrange(1000):
....:         p = R.random_element(degree=8)
....:         assert p.factor().prod() == p, p.factor()
....:
------------------------------------------------------------------------
---
AssertionError                            Traceback (most recent call
last)
<ipython-input-22-f31a401fb53b> in <module>()
      1 for d in xrange(Integer(1000)):
      2         p = R.random_element(degree=Integer(8))
----> 3         assert p.factor().prod() == p, p.factor()
      4

AssertionError: T * (T + 1) * (T + z5 + 1) * (T + z5^3 + 1) * (T +
2*z5^4 + z5) * (T + 2*z5^4 + 2*z5^2) * (T + 2*z5^4 + 2*z5^3 + z5^2 + z5)
}}}

On a related note, a random element can be zero:
{{{
sage: K = GF(3).algebraic_closure()
sage: R = PolynomialRing(K, 'T')
sage: T = R.gen()
sage: for d in xrange(1000):
....:         p = R.random_element(degree=randint(2,8))
....:         assert p.factor().prod() == p, p
....:
------------------------------------------------------------------------
---
ValueError                                Traceback (most recent call
last)
<ipython-input-15-db4aacba08d5> in <module>()
      1 for d in xrange(Integer(1000)):
      2         p =
R.random_element(degree=randint(Integer(2),Integer(8)))
----> 3         assert p.factor().prod() == p, p
      4

/home/release/Sage/local/lib/python2.7/site-
packages/sage/rings/polynomial/polynomial_element.so in
sage.rings.polynomial.polynomial_element.Polynomial.factor
(build/cythonized/sage/rings/polynomial/polynomial_element.c:23530)()

ValueError: factorization of 0 not defined
}}}

URL: http://trac.sagemath.org/16681
Reported by: vbraun
Ticket author(s): Vincent Delecroix
Reviewer(s): Volker Braun