@@ -1235,30 +1235,6 @@ cdef class Polynomial(CommutativeAlgebraElement):
1235
1235
return - 2
1236
1236
return result
1237
1237
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
-
1262
1238
def _im_gens_ (self , codomain , im_gens , base_map = None ):
1263
1239
"""
1264
1240
Return the image of this element under the morphism defined by
@@ -1300,27 +1276,122 @@ cdef class Polynomial(CommutativeAlgebraElement):
1300
1276
i -= 1
1301
1277
return result
1302
1278
1303
- def _integer_ (self , ZZ ):
1279
+ def _scalar_conversion (self , R ):
1304
1280
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
1306
1342
1307
1343
sage: K. <x> = Frac( RR['x' ])
1308
1344
sage: ZZ( 2* x/x) # indirect doctest
1309
1345
2
1310
1346
sage: ZZ( x)
1311
1347
Traceback ( most recent call last) :
1312
1348
...
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
1314
1365
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 )
1316
1375
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 ):
1320
1377
"""
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 )
1324
1395
1325
1396
def _rational_ (self ):
1326
1397
r """
@@ -1334,9 +1405,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
1334
1405
...
1335
1406
TypeError: not a constant polynomial
1336
1407
"""
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)
1340
1409
1341
1410
def _symbolic_ (self , R ):
1342
1411
"""
0 commit comments