Changeset 7508:0e1318c724ad
- Timestamp:
- 12/02/07 12:17:20 (5 years ago)
- Branch:
- default
- Parents:
- 7505:2a3f3513a11b (diff), 7507:de39c2037850 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Location:
- sage/rings
- Files:
-
- 4 edited
-
number_field/number_field.py (modified) (2 diffs)
-
number_field/number_field.py (modified) (8 diffs)
-
polynomial/polynomial_element.pyx (modified) (4 diffs)
-
polynomial/polynomial_element.pyx (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/rings/number_field/number_field.py
r7478 r7508 1356 1356 return False 1357 1357 1358 def pari_polynomial(self ):1358 def pari_polynomial(self, name='x'): 1359 1359 """ 1360 1360 PARI polynomial corresponding to polynomial that defines 1361 this field. This is always a polynomial in the variable "x". 1361 this field. By default, this is a polynomial in the 1362 variable "x". 1362 1363 1363 1364 EXAMPLES: … … 1365 1366 sage: k.<a> = NumberField(y^2 - 3/2*y + 5/3) 1366 1367 sage: k.pari_polynomial() 1367 x^2 - 3/2*x + 5/3 1368 x^2 - 3/2*x + 5/3 1369 sage: k.pari_polynomial('a') 1370 a^2 - 3/2*a + 5/3 1368 1371 """ 1369 1372 try: 1370 return self.__pari_polynomial 1373 if (self.__pari_polynomial_var == name): 1374 return self.__pari_polynomial 1375 else: 1376 self.__pari_polynomial = self.__pari_polynomial(name) 1377 self.__pari_polynomial_var = name 1378 return self.__pari_polynomial 1371 1379 except AttributeError: 1372 1380 poly = self.polynomial() 1373 with localvars(poly.parent(), 'x'):1381 with localvars(poly.parent(), name): 1374 1382 self.__pari_polynomial = poly._pari_() 1383 self.__pari_polynomial_var = name 1375 1384 return self.__pari_polynomial 1376 1385 -
sage/rings/number_field/number_field.py
r7506 r7508 2118 2118 """ 2119 2119 return infinity.infinity 2120 2120 2121 2121 def polynomial_ntl(self): 2122 2122 """ … … 2227 2227 return self.__regulator 2228 2228 2229 def residue_field(self, prime, name = None, check = False):2229 def residue_field(self, prime, names = None, check = False): 2230 2230 """ 2231 2231 Return the residue field of this number field at a given prime, ie $O_K / p O_K$. … … 2233 2233 INPUT: 2234 2234 prime -- a prime ideal of the maximal order in this number field. 2235 name -- the name of the variable in the residue field2235 names -- the name of the variable in the residue field 2236 2236 check -- whether or not to check the primality of prime. 2237 2237 OUTPUT: … … 2243 2243 sage: P = K.ideal(61).factor()[0][0] 2244 2244 sage: K.residue_field(P) 2245 Residue field of Fractional ideal (-2*a^2 + 1)2245 Residue field in abar of Fractional ideal (-2*a^2 + 1) 2246 2246 """ 2247 2247 import sage.rings.residue_field 2248 return sage.rings.residue_field.ResidueField(prime )2248 return sage.rings.residue_field.ResidueField(prime, names = names) 2249 2249 2250 2250 def signature(self): … … 3688 3688 return K 3689 3689 3690 def absolute_polynomial_ntl(self): 3691 """ 3692 Return defining polynomial of this number field 3693 as a pair, an ntl polynomial and a denominator. 3694 3695 This is used mainly to implement some internal arithmetic. 3696 3697 EXAMPLES: 3698 sage: NumberField(x^2 + (2/3)*x - 9/17,'a').polynomial_ntl() 3699 ([-27 34 51], 51) 3700 """ 3701 try: 3702 return (self.__abs_polynomial_ntl, self.__abs_denominator_ntl) 3703 except AttributeError: 3704 self.__abs_denominator_ntl = ntl.ZZ() 3705 den = self.absolute_polynomial().denominator() 3706 self.__abs_denominator_ntl.set_from_sage_int(ZZ(den)) 3707 self.__abs_polynomial_ntl = ntl.ZZX((self.absolute_polynomial()*den).list()) 3708 return (self.__abs_polynomial_ntl, self.__abs_denominator_ntl) 3709 3690 3710 def absolute_polynomial(self): 3691 3711 r""" … … 3762 3782 To: Complex Field with 58 bits of precision 3763 3783 Defn: a |--> -0.62996052494743676 - 1.0911236359717214*I 3764 b |--> -0.00000000000000019428902930940239 + 1.0000000000000000*I, 3765 ... 3784 b |--> -1.9428902930940239e-16 + 1.0000000000000000*I, Relative number field morphism: 3785 ... 3786 From: Number Field in a with defining polynomial x^3 - 2 over its base field 3766 3787 To: Complex Field with 58 bits of precision 3767 3788 Defn: a |--> 1.2599210498948731 3768 3789 b |--> -0.99999999999999999*I] 3769 3790 sage: f[0](a)^3 3770 2.0000000000000002 - 0.00000000000000086389229103644993*I3791 2.0000000000000002 - 8.6389229103644993e-16*I 3771 3792 sage: f[0](b)^2 3772 -1.0000000000000001 - 0.00000000000000038857805861880480*I3793 -1.0000000000000001 - 3.8857805861880480e-16*I 3773 3794 sage: f[0](a+b) 3774 3795 -0.62996052494743693 - 0.091123635971721295*I … … 4416 4437 sage: C.complex_embeddings() 4417 4438 [Ring morphism: 4418 From: Cyclotomic Field of order 4 and degree 24419 To: Complex Field with 53 bits of precision4420 Defn: zeta4 |--> 6.12323399573677e-17 + 1.00000000000000*I, Ring morphism:4421 From: Cyclotomic Field of order 4 and degree 24422 To: Complex Field with 53 bits of precision4423 Defn: zeta4 |--> -0.000000000000000183697019872103- 1.00000000000000*I]4439 From: Cyclotomic Field of order 4 and degree 2 4440 To: Complex Field with 53 bits of precision 4441 Defn: zeta4 |--> 6.12323399573677e-17 + 1.00000000000000*I, Ring morphism: 4442 From: Cyclotomic Field of order 4 and degree 2 4443 To: Complex Field with 53 bits of precision 4444 Defn: zeta4 |--> -1.83697019872103e-16 - 1.00000000000000*I] 4424 4445 """ 4425 4446 CC = sage.rings.complex_field.ComplexField(prec) … … 4616 4637 parts = -b/(2*a), (Dpoly/D).sqrt()/(2*a) 4617 4638 self._NumberField_generic__gen = self._element_class(self, parts) 4639 4640 4641 def coerce_map_from_impl(self, S): 4642 """ 4643 EXAMPLES: 4644 sage: K.<a> = QuadraticField(-3) 4645 sage: f = K.coerce_map_from(QQ); f 4646 Natural morphism: 4647 From: Rational Field 4648 To: Number Field in a with defining polynomial x^2 + 3 4649 sage: f(3/5) 4650 3/5 4651 sage: parent(f(3/5)) is K 4652 True 4653 """ 4654 if S is QQ: 4655 return number_field_element_quadratic.Q_to_quadratic_field_element(self) 4656 else: 4657 return NumberField_absolute.coerce_map_from_impl(self, S) 4658 4659 4618 4660 4619 4661 def discriminant(self, v=None): -
sage/rings/polynomial/polynomial_element.pyx
r7504 r7508 1523 1523 sage: expand(F) 1524 1524 T^6 + 10/7*T^5 + (-867/49)*T^4 + (-76/245)*T^3 + 3148/35*T^2 + (-25944/245)*T + 48771/1225 1525 1526 sage: f = x^2 - 1/3 ; K.<a> = NumberField(f) ; A.<T> = K[] ; g = A(x^2-1) 1527 sage: g.factor() 1528 (T - 1) * (T + 1) 1529 1530 sage: h = A(3*x^2-1) ; h.factor() 1531 (3) * (T - a) * (T + a) 1532 1533 sage: h = A(x^2-1/3) ; h.factor() 1534 (T - a) * (T + a) 1525 1535 1526 1536 Over the real double field: … … 1593 1603 G = None 1594 1604 1595 from sage.rings.number_field.all import is_NumberField, is_RelativeNumberField 1605 from sage.rings.number_field.all import is_NumberField, \ 1606 is_RelativeNumberField, NumberField 1596 1607 from sage.rings.finite_field import is_FiniteField 1597 1608 … … 1616 1627 return Factorization(v, from_M(F.unit())) 1617 1628 1618 1619 elif is_NumberField(R) or is_FiniteField(R): 1629 elif is_FiniteField(R): 1620 1630 v = [x._pari_("a") for x in self.list()] 1621 1631 f = pari(v).Polrev() 1622 1632 G = list(f.factor()) 1633 1634 1635 elif is_NumberField(R): 1636 if (R.defining_polynomial().denominator() == 1) and \ 1637 (self.denominator() == 1): 1638 v = [ x._pari_("a") for x in self.list() ] 1639 f = pari(v).Polrev() 1640 Rpari = R.pari_nf() 1641 if (Rpari.variable() != "a"): 1642 Rpari = Rpari.copy() 1643 Rpari[0] = Rpari[0]("a") 1644 Rpari[6] = [ x("a") for x in Rpari[6] ] 1645 G = list(Rpari.nffactor(f)) 1646 1647 else: 1648 1649 Rdenom = R.defining_polynomial().denominator() 1650 1651 new_Rpoly = (R.defining_polynomial() * Rdenom).change_variable_name("a") 1652 1653 Rpari, Rdiff = new_Rpoly._pari_().nfinit(3) 1654 1655 AZ = polynomial_ring.PolynomialRing(QQ,'z') 1656 Raux = NumberField(AZ(Rpari[0]),'alpha') 1657 1658 S, gSRaux, fRauxS = Raux.change_generator(Raux(Rdiff)) 1659 1660 phi_RS = R.Hom(S)([S.gen(0)]) 1661 phi_SR = S.Hom(R)([R.gen(0)]) 1662 1663 unit = self.leading_coefficient() 1664 temp_f = self * 1/unit 1665 1666 v = [ gSRaux(phi_RS(x))._pari_("a") for x in temp_f.list() ] 1667 f = pari(v).Polrev() 1668 1669 pari_factors = Rpari.nffactor(f) 1670 1671 factors = [ ( self.parent([ phi_SR(fRauxS(Raux(pari_factors[0][i][j]))) 1672 for j in range(len(pari_factors[0][i])) ]) , 1673 int(pari_factors[1][i]) ) 1674 for i in range(pari_factors.nrows()) ] 1675 1676 return Factorization(factors, unit) 1677 1623 1678 1624 1679 elif is_RealField(R): … … 3031 3086 sage: R(4).is_irreducible() 3032 3087 True 3088 3089 TESTS: 3090 sage: F.<t> = NumberField(x^2-5) 3091 sage: Fx.<xF> = PolynomialRing(F) 3092 sage: f = Fx([2*t - 5, 5*t - 10, 3*t - 6, -t, -t + 2, 1]) 3093 sage: f.is_irreducible() 3094 False 3095 sage: f = Fx([2*t - 3, 5*t - 10, 3*t - 6, -t, -t + 2, 1]) 3096 sage: f.is_irreducible() 3097 True 3033 3098 """ 3034 3099 if self.is_zero(): -
sage/rings/polynomial/polynomial_element.pyx
r7507 r7508 36 36 import sage.rings.complex_field 37 37 import sage.rings.fraction_field_element 38 from sage.rings.infinity importinfinity38 import sage.rings.infinity as infinity 39 39 #import sage.misc.misc as misc 40 40 from sage.misc.sage_eval import sage_eval … … 74 74 75 75 cdef object is_AlgebraicRealField 76 cdef object is_AlgebraicField 77 cdef object is_AlgebraicField_common 78 cdef object NumberField_quadratic 79 cdef object is_ComplexIntervalField 76 80 77 81 cdef void late_import(): 78 82 # A hack to avoid circular imports. 79 83 global is_AlgebraicRealField 84 global is_AlgebraicField 85 global is_AlgebraicField_common 86 global NumberField_quadratic 87 global is_ComplexIntervalField 80 88 81 89 if is_AlgebraicRealField is not None: 82 90 return 83 91 84 import sage.rings.algebraic_real 85 is_AlgebraicRealField = sage.rings.algebraic_real.is_AlgebraicRealField 92 import sage.rings.qqbar 93 is_AlgebraicRealField = sage.rings.qqbar.is_AlgebraicRealField 94 is_AlgebraicField = sage.rings.qqbar.is_AlgebraicField 95 is_AlgebraicField_common = sage.rings.qqbar.is_AlgebraicField_common 96 import sage.rings.number_field.number_field 97 NumberField_quadratic = sage.rings.number_field.number_field.NumberField_quadratic 98 import sage.rings.complex_interval_field 99 is_ComplexIntervalField = sage.rings.complex_interval_field.is_ComplexIntervalField 86 100 87 101 cdef class Polynomial(CommutativeAlgebraElement): … … 525 539 526 540 def _integer_(self): 541 r""" 542 EXAMPLES: 543 sage: k = GF(47) 544 sage: R.<x> = PolynomialRing(k) 545 sage: ZZ(R(45)) 546 45 547 sage: ZZ(3*x + 45) 548 Traceback (most recent call last): 549 ... 550 TypeError: cannot coerce nonconstant polynomial 551 """ 527 552 if self.degree() > 0: 528 553 raise TypeError, "cannot coerce nonconstant polynomial" 529 554 return sage.rings.integer.Integer(self[0]) 555 556 def _rational_(self): 557 r""" 558 EXAMPLES: 559 sage: R.<x> = PolynomialRing(QQ) 560 sage: QQ(R(45/4)) 561 45/4 562 sage: QQ(3*x + 45) 563 Traceback (most recent call last): 564 ... 565 TypeError: cannot coerce nonconstant polynomial 566 """ 567 if self.degree() > 0: 568 raise TypeError, "cannot coerce nonconstant polynomial" 569 return sage.rings.rational.Rational(self[0]) 530 570 531 571 def __invert__(self): … … 1042 1082 sage: f = y^10 - 1.393493*y + 0.3 1043 1083 sage: f._mul_karatsuba(f) 1044 1.00000000000000*y^20 - 2.78698600000000*y^11 + 0.600000000000000*y^10 + 0.000000000000000111022302462516*y^8 - 0.000000000000000111022302462516*y^6 - 0.000000000000000111022302462516*y^3 + 1.94182274104900*y^2 - 0.836095800000000*y + 0.09000000000000001084 1.00000000000000*y^20 - 2.78698600000000*y^11 + 0.600000000000000*y^10 + 1.11022302462516e-16*y^8 - 1.11022302462516e-16*y^6 - 1.11022302462516e-16*y^3 + 1.94182274104900*y^2 - 0.836095800000000*y + 0.0900000000000000 1045 1085 sage: f._mul_fateman(f) 1046 1086 1.00000000000000*y^20 - 2.78698600000000*y^11 + 0.600000000000000*y^10 + 1.94182274104900*y^2 - 0.836095800000000*y + 0.0900000000000000 … … 1964 2004 +Infinity 1965 2005 """ 1966 return infinity 2006 return infinity.infinity 1967 2007 1968 2008 def padded_list(self, n=None): … … 2350 2390 at the end of the docstring about this. 2351 2391 2352 If the output ring is a RealIntervalField of a given 2353 precision, then the answer will always be correct (or an 2354 exception will be raised, if a case is not implemented). Each 2355 root will be contained in one of the returned intervals, 2356 and the intervals will be disjoint. (The returned intervals 2357 may be of higher precision than the specified output ring.) 2392 If the output ring is a RealIntervalField or 2393 ComplexIntervalField of a given precision, then the answer 2394 will always be correct (or an exception will be raised, if a 2395 case is not implemented). Each root will be contained in one 2396 of the returned intervals, and the intervals will be disjoint. 2397 (The returned intervals may be of higher precision than the 2398 specified output ring.) 2358 2399 2359 2400 At the end of this docstring (after the examples) is a description … … 2425 2466 sage: f = x^10 - 2*(5*x-1)^2 2426 2467 sage: f.roots(multiplicities=False) 2427 [-1.6772670339941..., 0.19995479628 5..., 0.200045306115..., 1.5763035161844...]2468 [-1.6772670339941..., 0.19995479628..., 0.200045306115..., 1.5763035161844...] 2428 2469 2429 2470 sage: x = CC['x'].0 … … 2440 2481 [(I, 3), (-2^(1/4), 1), (2^(1/4), 1), (1, 1)] 2441 2482 2442 A n example where the base ring doesn't have a factorization2443 algorithm (yet). Note that this is currently done via naive2444 enumeration, so could be very slow:2483 A couple of examples where the base ring doesn't have a 2484 factorization algorithm (yet). Note that this is currently 2485 done via naive enumeration, so could be very slow: 2445 2486 sage: R = Integers(6) 2446 2487 sage: S.<x> = R['x'] … … 2452 2493 sage: p.roots(multiplicities=False) 2453 2494 [1, 5] 2495 sage: R = Integers(9) 2496 sage: A = PolynomialRing(R, 'y') 2497 sage: y = A.gen() 2498 sage: f = 10*y^2 - y^3 - 9 2499 sage: f.roots(multiplicities=False) 2500 [0, 1, 3, 6] 2454 2501 2455 2502 An example over the complex double field (where root finding … … 2511 2558 sage: f.roots(ring=RIF, multiplicities=False) 2512 2559 [[-0.618033988749894848204586834365642 .. -0.618033988749894848204586834365629], [0.999999999999999999999999999999987 .. 1.00000000000000000000000000000003], [1.61803398874989484820458683436561 .. 1.61803398874989484820458683436565]] 2560 2561 Examples using complex root isolation: 2562 sage: x = polygen(ZZ) 2563 sage: p = x^5 - x - 1 2564 sage: p.roots() 2565 [] 2566 sage: p.roots(ring=CIF) 2567 [([1.1673039782614185 .. 1.1673039782614188], 1), ([0.18123244446987518 .. 0.18123244446987558] + [1.0839541013177103 .. 1.0839541013177110]*I, 1), ([0.181232444469875... .. 0.1812324444698755...] - [1.083954101317710... .. 1.0839541013177110]*I, 1), ([-0.76488443360058489 .. -0.76488443360058455] + [0.35247154603172609 .. 0.35247154603172643]*I, 1), ([-0.76488443360058489 .. -0.76488443360058455] - [0.35247154603172609 .. 0.35247154603172643]*I, 1)] 2568 sage: p.roots(ring=ComplexIntervalField(200)) 2569 [([1.1673039782614186842560458998548421807205603715254890391400816 .. 1.1673039782614186842560458998548421807205603715254890391400829], 1), ([0.18123244446987538390180023778112063996871646618462304743773153 .. 0.18123244446987538390180023778112063996871646618462304743773341] + [1.0839541013177106684303444929807665742736402431551156543011306 .. 1.0839541013177106684303444929807665742736402431551156543011344]*I, 1), ([0.18123244446987538390180023778112063996871646618462304743773153 .. 0.18123244446987538390180023778112063996871646618462304743773341] - [1.0839541013177106684303444929807665742736402431551156543011306 .. 1.0839541013177106684303444929807665742736402431551156543011344]*I, 1), ([-0.76488443360058472602982318770854173032899665194736756700777454 .. -0.76488443360058472602982318770854173032899665194736756700777...] + [0.35247154603172624931794709140258105439420648082424733283769... .. 0.35247154603172624931794709140258105439420648082424733283769...]*I, 1), ([-0.76488443360058472602982318770854173032899665194736756700777454 .. -0.76488443360058472602982318770854173032899665194736756700777204] - [0.35247154603172624931794709140258105439420648082424733283769122 .. 0.35247154603172624931794709140258105439420648082424733283769341]*I, 1)] 2570 sage: rts = p.roots(ring=QQbar); rts 2571 [([1.1673039782614185 .. 1.1673039782614188], 1), ([0.18123244446987538 .. 0.18123244446987541] + [1.0839541013177105 .. 1.0839541013177108]*I, 1), ([0.18123244446987538 .. 0.18123244446987541] - [1.0839541013177105 .. 1.0839541013177108]*I, 1), ([-0.76488443360058478 .. -0.76488443360058466] + [0.35247154603172620 .. 0.35247154603172626]*I, 1), ([-0.76488443360058478 .. -0.76488443360058466] - [0.35247154603172620 .. 0.35247154603172626]*I, 1)] 2572 sage: p.roots(ring=AA) 2573 [([1.1673039782614185 .. 1.1673039782614188], 1)] 2574 sage: p = (x - rts[1][0])^2 * (x^2 + x + 1) 2575 sage: p.roots(ring=QQbar) 2576 [([-0.50000000000000012 .. -0.49999999999999994] + [0.86602540378443859 .. 0.86602540378443871]*I, 1), ([-0.50000000000000000 .. -0.50000000000000000] - [0.86602540378443859 .. 0.86602540378443871]*I, 1), ([0.18123244446987538 .. 0.18123244446987541] + [1.0839541013177105 .. 1.0839541013177108]*I, 2)] 2577 sage: p.roots(ring=CIF) 2578 [([-0.50000000000000012 .. -0.49999999999999994] + [0.86602540378443859 .. 0.86602540378443871]*I, 1), ([-0.50000000000000000 .. -0.50000000000000000] - [0.86602540378443859 .. 0.86602540378443871]*I, 1), ([0.18123244446987538 .. 0.18123244446987541] + [1.0839541013177105 .. 1.0839541013177108]*I, 2)] 2579 2580 Note that coefficients in a number field with defining polynomial 2581 x^2 + 1 are considered to be Gaussian rationals (with the generator 2582 mapping to +I), if you ask for complex roots. 2583 2584 sage: K.<im> = NumberField(x^2 + 1) 2585 sage: y = polygen(K) 2586 sage: p = y^4 - 2 - im 2587 sage: p.roots(ring=CC) 2588 [(-1.2146389322441... - 0.14142505258239...*I, 1), (-0.14142505258239... + 1.2146389322441...*I, 1), (0.14142505258239... - 1.2146389322441...*I, 1), (1.2146389322441... + 0.14142505258239...*I, 1)] 2589 sage: p = p^2 * (y^2 - 2) 2590 sage: p.roots(ring=CIF) 2591 [([-1.4142135623730952 .. -1.4142135623730949], 1), ([1.4142135623730949 .. 1.4142135623730952], 1), ([-1.2146389322441827 .. -1.2146389322441821] - [0.1414250525823937... .. 0.1414250525823939...]*I, 2), ([-0.14142505258239399 .. -0.14142505258239376] + [1.2146389322441821 .. 1.2146389322441827]*I, 2), ([0.14142505258239376 .. 0.14142505258239399] - [1.2146389322441821 .. 1.2146389322441827]*I, 2), ([1.2146389322441821 .. 1.2146389322441827] + [0.14142505258239376 .. 0.14142505258239399]*I, 2)] 2513 2592 2514 2593 There are many combinations of floating-point input and output … … 2542 2621 2543 2622 Note that we can find the roots of a polynomial with 2544 algebraic realcoefficients:2623 algebraic coefficients: 2545 2624 2546 2625 sage: rt2 = sqrt(AA(2)) … … 2558 2637 Algorithms used: 2559 2638 2560 For brevity, we will use RR to mean any RealField of any precision; 2561 similarly for CC and CIF. 2639 For brevity, we will use RR to mean any RealField of any 2640 precision; similarly for RIF, CC, and CIF. Since Sage has no 2641 specific implementation of Gaussian rationals (or of number 2642 fields with embedding, at all), when we refer to Gaussian 2643 rationals below we will accept any number field with defining 2644 polynomial x^2+1, mapping the field generator to +I. 2562 2645 2563 2646 We call the base ring of the polynomial K, and the ring given … … 2578 2661 this method gives.) 2579 2662 2580 If L is floating-point and K is not, then we attempt to 2581 change the polynomial ring to L (using .change_ring()) 2582 (or, if L is complex, to the corresponding real field). 2583 Then we use either Pari or numpy as specified above. 2663 If L is QQbar or CIF, and K is ZZ, QQ, AA, QQbar, or the 2664 Gaussian rationals, then the root isolation algorithm 2665 sage.rings.polynomial.complex_roots.complex_roots() is used. 2666 (You can call complex_roots() directly to get more control 2667 than this method gives.) 2668 2669 If L is AA and K is QQbar or the Gaussian rationals, then 2670 complex_roots() is used (as above) to find roots in QQbar, 2671 then these roots are filtered to select only the real roots. 2672 2673 If L is floating-point and K is not, then we attempt to change 2674 the polynomial ring to L (using .change_ring()) (or, if L is 2675 complex and K is not, to the corresponding real field). Then 2676 we use either Pari or numpy as specified above. 2584 2677 2585 2678 For all other cases where K is different than L, we just use 2586 2679 .change_ring(L) and proceed as below. 2587 2680 2588 The next method is to attempt to factor the polynomial. 2589 If this succeeds, then for every degree-one factor a*x+b, we 2590 add -b/a as a root. 2591 2592 If factoring over K is not implemented, and K is finite, then 2593 we find the roots by enumerating all elements of K and checking 2594 whether the polynomial evaluates to zero at that value. 2681 The next method, which is used if K is an integral domain, is 2682 to attempt to factor the polynomial. If this succeeds, then 2683 for every degree-one factor a*x+b, we add -b/a as a root (as 2684 long as this quotient is actually in the desired ring). 2685 2686 If factoring over K is not implemented (or K is not an 2687 integral domain), and K is finite, then we find the roots by 2688 enumerating all elements of K and checking whether the 2689 polynomial evaluates to zero at that value. 2595 2690 2596 2691 … … 2618 2713 if L is None: L = K 2619 2714 2715 late_import() 2716 2620 2717 input_fp = (is_RealField(K) 2621 2718 or is_ComplexField(K) … … 2630 2727 output_complex = (is_ComplexField(L) 2631 2728 or is_ComplexDoubleField(L)) 2729 input_gaussian = (isinstance(K, NumberField_quadratic) 2730 and list(K.polynomial()) == [1, 0, 1]) 2632 2731 2633 2732 if input_fp and output_fp: … … 2689 2788 return rts 2690 2789 2691 late_import() 2692 2693 if L != K or is_AlgebraicRealField(L): 2694 # So far, the only "special" implementation is for K exact 2695 # and L either AA or a real interval field. 2790 if L != K or is_AlgebraicField_common(L): 2791 # So far, the only "special" implementations are for real 2792 # and complex root isolation. 2696 2793 if (is_IntegerRing(K) or is_RationalField(K) 2697 2794 or is_AlgebraicRealField(K)) and \ … … 2724 2821 return [rt for (rt, mult) in rts] 2725 2822 2726 if output_fp and output_complex: 2823 if (is_IntegerRing(K) or is_RationalField(K) 2824 or is_AlgebraicField_common(K) or input_gaussian) and \ 2825 (is_ComplexIntervalField(L) or is_AlgebraicField_common(L)): 2826 2827 from sage.rings.polynomial.complex_roots import complex_roots 2828 2829 if is_ComplexIntervalField(L): 2830 rts = complex_roots(self, min_prec=L.prec()) 2831 elif is_AlgebraicField(L): 2832 rts = complex_roots(self, retval='algebraic') 2833 else: 2834 rts = complex_roots(self, retval='algebraic_real') 2835 2836 if multiplicities: 2837 return rts 2838 else: 2839 return [rt for (rt, mult) in rts] 2840 2841 if output_fp and output_complex and not input_gaussian: 2727 2842 # If we want the complex roots, and the input is not 2728 # floating point, we convert to a real polynomial. 2729 # I think this works for now, because there are no 2730 # non-floating-point complex types in Sage. Hopefully 2731 # someday we will have Gaussian integers and rationals, 2732 # and this will have to change then. 2843 # floating point, we convert to a real polynomial 2844 # (except when the input coefficients are Gaussian rationals). 2733 2845 if is_ComplexDoubleField(L): 2734 2846 real_field = RDF … … 2741 2853 2742 2854 try: 2743 rts = self.factor() 2855 if K.is_integral_domain(): 2856 rts = self.factor() 2857 else: 2858 raise NotImplementedError 2744 2859 except NotImplementedError: 2745 2860 if K.is_finite(): … … 2875 2990 2876 2991 if not self: 2877 return infinity 2878 2879 if p is infinity :2992 return infinity.infinity 2993 2994 if p is infinity.infinity: 2880 2995 return -self.degree() 2881 2996 … … 3126 3241 3127 3242 coeffs = self.coeffs() 3128 if p == infinity :3243 if p == infinity.infinity: 3129 3244 return RR(max([abs(i) for i in coeffs])) 3130 3245
Note: See TracChangeset
for help on using the changeset viewer.
