Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit cd6dd44

Browse files
committed
Added _factor_univariate_polynomial() for QQbar and AA
1 parent 6c85a8f commit cd6dd44

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

src/sage/rings/polynomial/polynomial_element.pyx

-29
Original file line numberDiff line numberDiff line change
@@ -3073,27 +3073,13 @@ cdef class Polynomial(CommutativeAlgebraElement):
30733073
sage: R.<x> = QQbar[]
30743074
sage: (x^8-1).factor()
30753075
(x - 1) * (x - 0.7071067811865475? - 0.7071067811865475?*I) * (x - 0.7071067811865475? + 0.7071067811865475?*I) * (x - I) * (x + I) * (x + 0.7071067811865475? - 0.7071067811865475?*I) * (x + 0.7071067811865475? + 0.7071067811865475?*I) * (x + 1)
3076-
sage: (12*x^2-4).factor()
3077-
(12) * (x - 0.5773502691896258?) * (x + 0.5773502691896258?)
3078-
sage: R(-1).factor()
3079-
-1
3080-
sage: EllipticCurve('11a1').change_ring(QQbar).division_polynomial(5).factor()
3081-
(5) * (x - 16) * (x - 5) * (x - 1.959674775249769?) * (x - 1.427050983124843? - 3.665468789467727?*I) * (x - 1.427050983124843? + 3.665468789467727?*I) * (x + 0.9549150281252629? - 0.8652998037182486?*I) * (x + 0.9549150281252629? + 0.8652998037182486?*I) * (x + 1.927050983124843? - 1.677599044300515?*I) * (x + 1.927050983124843? + 1.677599044300515?*I) * (x + 2.959674775249769?) * (x + 6.545084971874737? - 7.106423590645660?*I) * (x + 6.545084971874737? + 7.106423590645660?*I)
30823076
30833077
Factoring polynomials over the algebraic reals (see
30843078
:trac:`8544`)::
30853079
30863080
sage: R.<x> = AA[]
30873081
sage: (x^8+1).factor()
30883082
(x^2 - 1.847759065022574?*x + 1.000000000000000?) * (x^2 - 0.7653668647301795?*x + 1.000000000000000?) * (x^2 + 0.7653668647301795?*x + 1.000000000000000?) * (x^2 + 1.847759065022574?*x + 1.000000000000000?)
3089-
sage: R(3).factor()
3090-
3
3091-
sage: (12*x^2-4).factor()
3092-
(12) * (x - 0.5773502691896258?) * (x + 0.5773502691896258?)
3093-
sage: (12*x^2+4).factor()
3094-
(12) * (x^2 + 0.3333333333333334?)
3095-
sage: EllipticCurve('11a1').change_ring(AA).division_polynomial(5).factor()
3096-
(5) * (x - 16.00000000000000?) * (x - 5.000000000000000?) * (x - 1.959674775249769?) * (x + 2.959674775249769?) * (x^2 - 2.854101966249685?*x + 15.47213595499958?) * (x^2 + 1.909830056250526?*x + 1.660606461254312?) * (x^2 + 3.854101966249685?*x + 6.527864045000421?) * (x^2 + 13.09016994374948?*x + 93.33939353874569?)
30973083
30983084
TESTS:
30993085
@@ -3350,21 +3336,6 @@ cdef class Polynomial(CommutativeAlgebraElement):
33503336
f = pari(v).Polrev()
33513337
G = list(f.factor())
33523338

3353-
elif is_AlgebraicField(R):
3354-
Rx = self.parent()
3355-
return Factorization([(Rx([-r,1]),e) for r,e in self.roots()],
3356-
unit=self.leading_coefficient())
3357-
3358-
elif is_AlgebraicRealField(R):
3359-
from sage.rings.qqbar import QQbar
3360-
Rx = self.parent()
3361-
rr = self.roots()
3362-
cr = [(r,e) for r,e in self.roots(QQbar) if r.imag()>0]
3363-
return Factorization(
3364-
[(Rx([-r,1]),e) for r,e in rr] +
3365-
[(Rx([r.norm(),-2*r.real(),1]),e) for r,e in cr],
3366-
unit=self.leading_coefficient())
3367-
33683339
elif is_NumberField(R):
33693340
if R.degree() == 1:
33703341
factors = self.change_ring(QQ).factor()

src/sage/rings/qqbar.py

+88
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,54 @@ def polynomial_root(self, poly, interval, multiplicity=1):
959959

960960
return AlgebraicReal(ANRoot(poly, interval, multiplicity))
961961

962+
def _factor_univariate_polynomial(self, f):
963+
"""
964+
Factor the univariate polynomial ``f``.
965+
966+
INPUT:
967+
968+
- ``f`` -- a univariate polynomial defined over the real algebraic field
969+
970+
OUTPUT:
971+
972+
- A factorization of ``f`` over the real algebraic numbers into a unit
973+
and monic irreducible factors
974+
975+
.. NOTE::
976+
977+
This is a helper method for
978+
:meth:`sage.rings.polynomial.polynomial_element.Polynomial.factor`.
979+
980+
TESTS::
981+
982+
sage: R.<x> = AA[]
983+
sage: AA._factor_univariate_polynomial(x)
984+
x
985+
sage: AA._factor_univariate_polynomial(2*x)
986+
(2) * x
987+
sage: AA._factor_univariate_polynomial((x^2 + 1)^2)
988+
(x^2 + 1)^2
989+
sage: AA._factor_univariate_polynomial(x^8 + 1)
990+
(x^2 - 1.847759065022574?*x + 1.000000000000000?) * (x^2 - 0.7653668647301795?*x + 1.000000000000000?) * (x^2 + 0.7653668647301795?*x + 1.000000000000000?) * (x^2 + 1.847759065022574?*x + 1.000000000000000?)
991+
sage: AA._factor_univariate_polynomial(R(3))
992+
3
993+
sage: AA._factor_univariate_polynomial(12*x^2 - 4)
994+
(12) * (x - 0.5773502691896258?) * (x + 0.5773502691896258?)
995+
sage: AA._factor_univariate_polynomial(12*x^2 + 4)
996+
(12) * (x^2 + 0.3333333333333334?)
997+
sage: AA._factor_univariate_polynomial(EllipticCurve('11a1').change_ring(AA).division_polynomial(5))
998+
(5) * (x - 16.00000000000000?) * (x - 5.000000000000000?) * (x - 1.959674775249769?) * (x + 2.959674775249769?) * (x^2 - 2.854101966249685?*x + 15.47213595499958?) * (x^2 + 1.909830056250526?*x + 1.660606461254312?) * (x^2 + 3.854101966249685?*x + 6.527864045000421?) * (x^2 + 13.09016994374948?*x + 93.33939353874569?)
999+
1000+
"""
1001+
rr = f.roots()
1002+
cr = [(r,e) for r,e in f.roots(QQbar) if r.imag()>0]
1003+
1004+
from sage.structure.factorization import Factorization
1005+
return Factorization(
1006+
[(f.parent()([-r,1]),e) for r,e in rr] +
1007+
[(f.parent()([r.norm(),-2*r.real(),1]),e) for r,e in cr],
1008+
unit=f.leading_coefficient())
1009+
9621010
def is_AlgebraicRealField(F):
9631011
r"""
9641012
Check whether ``F`` is an :class:`~AlgebraicRealField` instance. For internal use.
@@ -1355,6 +1403,46 @@ def random_element(self, poly_degree=2, *args, **kwds):
13551403
m = sage.misc.prandom.randint(0, len(roots)-1)
13561404
return roots[m]
13571405

1406+
def _factor_univariate_polynomial(self, f):
1407+
"""
1408+
Factor the univariate polynomial ``f``.
1409+
1410+
INPUT:
1411+
1412+
- ``f`` -- a univariate polynomial defined over the algebraic field
1413+
1414+
OUTPUT:
1415+
1416+
- A factorization of ``f`` over the algebraic numbers into a unit and
1417+
monic irreducible factors
1418+
1419+
.. NOTE::
1420+
1421+
This is a helper method for
1422+
:meth:`sage.rings.polynomial.polynomial_element.Polynomial.factor`.
1423+
1424+
TESTS::
1425+
1426+
sage: R.<x> = QQbar[]
1427+
sage: QQbar._factor_univariate_polynomial(x)
1428+
x
1429+
sage: QQbar._factor_univariate_polynomial(2*x)
1430+
(2) * x
1431+
sage: QQbar._factor_univariate_polynomial((x^2 + 1)^2)
1432+
(x - I)^2 * (x + I)^2
1433+
sage: QQbar._factor_univariate_polynomial(x^8 - 1)
1434+
(x - 1) * (x - 0.7071067811865475? - 0.7071067811865475?*I) * (x - 0.7071067811865475? + 0.7071067811865475?*I) * (x - I) * (x + I) * (x + 0.7071067811865475? - 0.7071067811865475?*I) * (x + 0.7071067811865475? + 0.7071067811865475?*I) * (x + 1)
1435+
sage: QQbar._factor_univariate_polynomial(12*x^2 - 4)
1436+
(12) * (x - 0.5773502691896258?) * (x + 0.5773502691896258?)
1437+
sage: QQbar._factor_univariate_polynomial(R(-1))
1438+
-1
1439+
sage: QQbar._factor_univariate_polynomial(EllipticCurve('11a1').change_ring(QQbar).division_polynomial(5))
1440+
(5) * (x - 16) * (x - 5) * (x - 1.959674775249769?) * (x - 1.427050983124843? - 3.665468789467727?*I) * (x - 1.427050983124843? + 3.665468789467727?*I) * (x + 0.9549150281252629? - 0.8652998037182486?*I) * (x + 0.9549150281252629? + 0.8652998037182486?*I) * (x + 1.927050983124843? - 1.677599044300515?*I) * (x + 1.927050983124843? + 1.677599044300515?*I) * (x + 2.959674775249769?) * (x + 6.545084971874737? - 7.106423590645660?*I) * (x + 6.545084971874737? + 7.106423590645660?*I)
1441+
1442+
"""
1443+
from sage.structure.factorization import Factorization
1444+
return Factorization([(f.parent()([-r,1]),e) for r,e in f.roots()], unit=f.leading_coefficient())
1445+
13581446
def is_AlgebraicField(F):
13591447
r"""
13601448
Check whether ``F`` is an :class:`~AlgebraicField` instance.

0 commit comments

Comments
 (0)