@@ -2230,30 +2230,76 @@ def clear_denominators(poly):
2230
2230
(2, x + 3)
2231
2231
sage: clear_denominators(x^2 + x/2 + 1/4)
2232
2232
(2, x^2 + x + 1)
2233
- """
2234
2233
2235
- # This algorithm factors the polynomial denominators.
2236
- # We should check the size of the denominators and switch to
2237
- # an alternate, less precise algorithm if we decide factoring
2238
- # would be too slow.
2234
+ TESTS::
2235
+
2236
+ sage: R.<y> = QQ[]
2237
+ sage: coefficients_as_integer_ratios = [
2238
+ ....: (-2774600080567517563395913264491323241652779066919616441429094563840,
2239
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2240
+ ....: (-24216324060414384566983400245979288839929814383090701293489050615808,
2241
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2242
+ ....: (325579773864372490083706670433410006284520887405882567940047555526656,
2243
+ ....: 180143564412823751787837643000565238870031999674661705128541236331583133845246252269971),
2244
+ ....: (-86736048492777879473586471630941922517134071457946320753641122078523392,
2245
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2246
+ ....: (-2338058278498910195688689352766977573607428722429118859280880481590329344,
2247
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2248
+ ....: (105830270645785996318880019945503938356315302592627229453391693256551317504,
2249
+ ....: 1381100660498315430373421929671000164670245330839073072652149478542137359480221267403111),
2250
+ ....: (1110926147990548796149597141538460730252912439930561079348611699181798425600,
2251
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2252
+ ....: (-89705438380888704653335165590083767769953879654958783855317882966200828559360,
2253
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2254
+ ....: (1151092895747371986483047191334923516591005329489629755485810229546333821625856,
2255
+ ....: 1381100660498315430373421929671000164670245330839073072652149478542137359480221267403111),
2256
+ ....: (24725641793859400310483886670136079788266826658111372723121573233077840328938576,
2257
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2258
+ ....: (-31051495080139473677925068000403254349133134904365702868216464107777210775457136,
2259
+ ....: 153455628944257270041491325519000018296693925648785896961349942060237484386691251933679),
2260
+ ....: (9431591461895130351865642769482226964622378075329823505708119342634182162193000560,
2261
+ ....: 4143301981494946291120265789013000494010735992517219217956448435626412078440663802209333),
2262
+ ....: (1721694880863483428337378731387732043714427651970488363462560317808769716807148992,
2263
+ ....: 153455628944257270041491325519000018296693925648785896961349942060237484386691251933679),
2264
+ ....: (255327752077837584624694974814916395144764296822788813014081161094149724325120096,
2265
+ ....: 27080405107810106477910233915117650287651869232138687699061754481218379597651397400061),
2266
+ ....: (238105337335596176836773151768694069523377650990453522899627157538495252117232992338,
2267
+ ....: 27080405107810106477910233915117650287651869232138687699061754481218379597651397400061),
2268
+ ....: (1255826892296350234297164500548658984205287902407560187136301197703464130999349114638,
2269
+ ....: 14336685057075938723599535602121108975815695475838128781856222960645024492874269211797),
2270
+ ....: (1, 1)]
2271
+ sage: p = R(coefficients_as_integer_ratios)
2272
+ sage: a = QQbar.polynomial_root(
2273
+ ....: AA.common_polynomial(p),
2274
+ ....: CIF(RIF(-RR(0.036151142425748496), -RR(0.036151142425748489)),
2275
+ ....: RIF(-RR(0.011298617187916445), -RR(0.011298617187916443))))
2276
+ sage: a.exactify()
2277
+ sage: a
2278
+ -0.03615114242574849? - 0.011298617187916444?*I
2279
+ """
2239
2280
2240
2281
d = poly .denominator ()
2241
2282
if d == 1 :
2242
2283
return d , poly
2243
2284
deg = poly .degree ()
2244
- factors = {}
2245
- for i in range (deg ):
2246
- d = poly [i ].denominator ()
2247
- df = factor (d )
2248
- for f , e in df :
2249
- oe = 0
2250
- if f in factors :
2251
- oe = factors [f ]
2252
- min_e = (e + (deg - i ) - 1 ) // (deg - i )
2253
- factors [f ] = max (oe , min_e )
2254
- change = 1
2255
- for f , e in factors .items ():
2256
- change = change * f ** e
2285
+ denoms = [c .denominator () for c in poly ]
2286
+ if all (d .nbits () < 128 for d in denoms ):
2287
+ # Factor the polynomial denominators.
2288
+ factors = {}
2289
+ for i , d in enumerate (denoms ):
2290
+ df = factor (d )
2291
+ for f , e in df :
2292
+ oe = 0
2293
+ if f in factors :
2294
+ oe = factors [f ]
2295
+ min_e = (e + (deg - i ) - 1 ) // (deg - i )
2296
+ factors [f ] = max (oe , min_e )
2297
+ change = 1
2298
+ for f , e in factors .items ():
2299
+ change = change * f ** e
2300
+ else :
2301
+ # Factoring would be too slow.
2302
+ change = poly .monic ().denominator ()
2257
2303
poly = poly * (change ** deg )
2258
2304
poly = poly (poly .parent ().gen () / change )
2259
2305
return change , poly
0 commit comments