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

Commit 4c7a3c3

Browse files
committedMar 14, 2021
more scalar conversions for polynomials
1 parent ee49837 commit 4c7a3c3

File tree

1 file changed

+106
-37
lines changed

1 file changed

+106
-37
lines changed
 

‎src/sage/rings/polynomial/polynomial_element.pyx

+106-37
Original file line numberDiff line numberDiff line change
@@ -1235,30 +1235,6 @@ cdef class Polynomial(CommutativeAlgebraElement):
12351235
return -2
12361236
return result
12371237

1238-
def __float__(self):
1239-
"""
1240-
EXAMPLES::
1241-
1242-
sage: P = PolynomialRing(ZZ, 'x')([1])
1243-
sage: float(P)
1244-
1.0
1245-
"""
1246-
if self.degree() > 0:
1247-
raise TypeError("cannot coerce nonconstant polynomial to float")
1248-
return float(self.get_coeff_c(0))
1249-
1250-
def __int__(self):
1251-
"""
1252-
EXAMPLES::
1253-
1254-
sage: P = PolynomialRing(ZZ, 'x')([3])
1255-
sage: int(P)
1256-
3
1257-
"""
1258-
if self.degree() > 0:
1259-
raise TypeError("cannot coerce nonconstant polynomial to int")
1260-
return int(self.get_coeff_c(0))
1261-
12621238
def _im_gens_(self, codomain, im_gens, base_map=None):
12631239
"""
12641240
Return the image of this element under the morphism defined by
@@ -1300,27 +1276,122 @@ cdef class Polynomial(CommutativeAlgebraElement):
13001276
i -= 1
13011277
return result
13021278

1303-
def _integer_(self, ZZ):
1279+
def _scalar_conversion(self, R):
13041280
r"""
1305-
EXAMPLES::
1281+
Generic conversion of constant polynomial to the ring ``R``.
1282+
1283+
Ideally such conversions should go through
1284+
:class:`ConstantPolynomialSection`. However, it does not work when
1285+
there are additional conversions involved.
1286+
1287+
EXAMPLES::
1288+
1289+
sage: a = QQ['x'](1/5)
1290+
sage: QQ(a)
1291+
1/5
1292+
sage: AA(a)
1293+
1/5
1294+
sage: QQbar(a)
1295+
1/5
1296+
sage: RDF(a)
1297+
0.2
1298+
sage: CDF(a)
1299+
0.2
1300+
sage: RR(a)
1301+
0.200000000000000
1302+
sage: CC(a)
1303+
0.200000000000000
1304+
sage: RBF(a)
1305+
[0.2000000000000000 +/- 4.45e-17]
1306+
sage: CBF(a)
1307+
[0.2000000000000000 +/- 4.45e-17]
1308+
sage: RIF(a)
1309+
0.2000000000000000?
1310+
sage: CIF(a)
1311+
0.2000000000000000?
1312+
sage: float(a)
1313+
0.2
1314+
sage: complex(a)
1315+
(0.2+0j)
1316+
1317+
sage: b = AA['x'](AA(2/3).sqrt())
1318+
sage: AA(b)
1319+
0.8164965809277260?
1320+
sage: RR(b)
1321+
0.816496580927726
1322+
sage: RBF(b)
1323+
[0.816496580927726 +/- 2.44e-16]
1324+
sage: RIF(b)
1325+
0.8164965809277260?
1326+
sage: float(b)
1327+
0.816496580927726
1328+
1329+
sage: c = QQbar['x'](QQbar(-2/5).sqrt())
1330+
sage: QQbar(c)
1331+
0.6324555320336758?*I
1332+
sage: CDF(c)
1333+
0.6324555320336758*I
1334+
sage: CC(c)
1335+
0.632455532033676*I
1336+
sage: CBF(c)
1337+
[0.632455532033676 +/- 3.96e-16]*I
1338+
sage: CIF(c)
1339+
0.6324555320336758?*I
1340+
sage: complex(c)
1341+
0.6324555320336758j
13061342
13071343
sage: K.<x> = Frac(RR['x'])
13081344
sage: ZZ(2*x/x) # indirect doctest
13091345
2
13101346
sage: ZZ(x)
13111347
Traceback (most recent call last):
13121348
...
1313-
TypeError: cannot coerce nonconstant polynomial
1349+
TypeError: cannot convert nonconstant polynomial
1350+
"""
1351+
if self.degree() > 0:
1352+
raise TypeError("cannot convert nonconstant polynomial")
1353+
return R(self.get_coeff_c(0))
1354+
1355+
_real_double_ = _scalar_conversion
1356+
_complex_double_ = _scalar_conversion
1357+
_mpfr_ = _scalar_conversion
1358+
_complex_mpfr_ = _scalar_conversion
1359+
_real_mpfi_ = _scalar_conversion
1360+
_complex_mpfi_ = _scalar_conversion
1361+
_arb_ = _scalar_conversion
1362+
_acb_ = _scalar_conversion
1363+
_integer_ = _scalar_conversion
1364+
_algebraic_ = _scalar_conversion
13141365

1315-
.. NOTE::
1366+
def __int__(self):
1367+
"""
1368+
EXAMPLES::
1369+
1370+
sage: P = PolynomialRing(ZZ, 'x')([3])
1371+
sage: int(P)
1372+
3
1373+
"""
1374+
return self._scalar_conversion(int)
13161375

1317-
The original example has been moved to :meth:`section` of
1318-
:class:`sage.categories.map.FormalCompositeMap` by :trac:`27081`
1319-
since coercion doesn't need :meth:`_integer_` for it, any more.
1376+
def __float__(self):
13201377
"""
1321-
if self.degree() > 0:
1322-
raise TypeError("cannot coerce nonconstant polynomial")
1323-
return ZZ(self.get_coeff_c(0))
1378+
EXAMPLES::
1379+
1380+
sage: P = PolynomialRing(ZZ, 'x')([1])
1381+
sage: float(P)
1382+
1.0
1383+
"""
1384+
return self._scalar_conversion(float)
1385+
1386+
def __complex__(self):
1387+
r"""
1388+
EXAMPLES::
1389+
1390+
sage: p = PolynomialRing(QQbar, 'x')(1+I)
1391+
sage: complex(p)
1392+
(1+1j)
1393+
"""
1394+
return self._scalar_conversion(complex)
13241395

13251396
def _rational_(self):
13261397
r"""
@@ -1334,9 +1405,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
13341405
...
13351406
TypeError: not a constant polynomial
13361407
"""
1337-
if self.degree() > 0:
1338-
raise TypeError("not a constant polynomial")
1339-
return sage.rings.rational.Rational(self.get_coeff_c(0))
1408+
return self._scalar_conversion(sage.rings.rational.Rational)
13401409

13411410
def _symbolic_(self, R):
13421411
"""

0 commit comments

Comments
 (0)
This repository has been archived.