Changeset 7450:766af69fc0fe
- Timestamp:
- 12/01/07 10:06:53 (5 years ago)
- Branch:
- default
- Parents:
- 7448:925ec067ebdd (diff), 7449:2fcb657f9f75 (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. - Files:
-
- 2 edited
-
sage/rings/polynomial/polynomial_element.pyx (modified) (1 diff)
-
sage/rings/polynomial/polynomial_element.pyx (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/rings/polynomial/polynomial_element.pyx
r7433 r7450 533 533 534 534 def _integer_(self): 535 r""" 536 EXAMPLES: 537 sage: k = GF(47) 538 sage: R.<x> = PolynomialRing(k) 539 sage: ZZ(R(45)) 540 45 541 sage: ZZ(3*x + 45) 542 Traceback (most recent call last): 543 ... 544 TypeError: cannot coerce nonconstant polynomial 545 """ 535 546 if self.degree() > 0: 536 547 raise TypeError, "cannot coerce nonconstant polynomial" 537 548 return sage.rings.integer.Integer(self[0]) 549 550 def _rational_(self): 551 r""" 552 EXAMPLES: 553 sage: R.<x> = PolynomialRing(QQ) 554 sage: QQ(R(45/4)) 555 45/4 556 sage: QQ(3*x + 45) 557 Traceback (most recent call last): 558 ... 559 TypeError: cannot coerce nonconstant polynomial 560 """ 561 if self.degree() > 0: 562 raise TypeError, "cannot coerce nonconstant polynomial" 563 return sage.rings.rational.Rational(self[0]) 538 564 539 565 def __invert__(self): -
sage/rings/polynomial/polynomial_element.pyx
r7449 r7450 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 NumberField_quadratic 77 cdef object is_ComplexIntervalField 76 78 77 79 cdef void late_import(): 78 80 # A hack to avoid circular imports. 79 81 global is_AlgebraicRealField 82 global NumberField_quadratic 83 global is_ComplexIntervalField 80 84 81 85 if is_AlgebraicRealField is not None: 82 86 return 83 87 84 import sage.rings.algebraic_real 85 is_AlgebraicRealField = sage.rings.algebraic_real.is_AlgebraicRealField 88 import sage.rings.qqbar 89 is_AlgebraicRealField = sage.rings.qqbar.is_AlgebraicRealField 90 import sage.rings.number_field.number_field 91 NumberField_quadratic = sage.rings.number_field.number_field.NumberField_quadratic 92 import sage.rings.complex_interval_field 93 is_ComplexIntervalField = sage.rings.complex_interval_field.is_ComplexIntervalField 86 94 87 95 cdef class Polynomial(CommutativeAlgebraElement): … … 1935 1943 +Infinity 1936 1944 """ 1937 return infinity 1945 return infinity.infinity 1938 1946 1939 1947 def padded_list(self, n=None): … … 2321 2329 at the end of the docstring about this. 2322 2330 2323 If the output ring is a RealIntervalField of a given 2324 precision, then the answer will always be correct (or an 2325 exception will be raised, if a case is not implemented). Each 2326 root will be contained in one of the returned intervals, 2327 and the intervals will be disjoint. (The returned intervals 2328 may be of higher precision than the specified output ring.) 2331 If the output ring is a RealIntervalField or 2332 ComplexIntervalField of a given precision, then the answer 2333 will always be correct (or an exception will be raised, if a 2334 case is not implemented). Each root will be contained in one 2335 of the returned intervals, and the intervals will be disjoint. 2336 (The returned intervals may be of higher precision than the 2337 specified output ring.) 2329 2338 2330 2339 At the end of this docstring (after the examples) is a description … … 2396 2405 sage: f = x^10 - 2*(5*x-1)^2 2397 2406 sage: f.roots(multiplicities=False) 2398 [-1.6772670339941..., 0.19995479628 5..., 0.200045306115..., 1.5763035161844...]2407 [-1.6772670339941..., 0.19995479628..., 0.200045306115..., 1.5763035161844...] 2399 2408 2400 2409 sage: x = CC['x'].0 … … 2482 2491 sage: f.roots(ring=RIF, multiplicities=False) 2483 2492 [[-0.618033988749894848204586834365642 .. -0.618033988749894848204586834365629], [0.999999999999999999999999999999987 .. 1.00000000000000000000000000000003], [1.61803398874989484820458683436561 .. 1.61803398874989484820458683436565]] 2493 2494 Examples using complex root isolation: 2495 sage: x = polygen(ZZ) 2496 sage: p = x^5 - x - 1 2497 sage: p.roots() 2498 [] 2499 sage: p.roots(ring=CIF) 2500 [([1.1673039782614185 .. 1.1673039782614188], 1), ([0.18123244446987518 .. 0.18123244446987558] + [1.0839541013177103 .. 1.0839541013177110]*I, 1), ([0.18123244446987518 .. 0.18123244446987558] - [1.0839541013177103 .. 1.0839541013177110]*I, 1), ([-0.76488443360058489 .. -0.76488443360058455] + [0.35247154603172609 .. 0.35247154603172643]*I, 1), ([-0.76488443360058489 .. -0.76488443360058455] - [0.35247154603172609 .. 0.35247154603172643]*I, 1)] 2501 sage: p.roots(ring=ComplexIntervalField(200)) 2502 [([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)] 2503 2504 Note that coefficients in a number field with defining polynomial 2505 x^2 + 1 are considered to be Gaussian rationals (with the generator 2506 mapping to +I), if you ask for complex roots. 2507 2508 sage: K.<im> = NumberField(x^2 + 1) 2509 sage: y = polygen(K) 2510 sage: p = y^4 - 2 - im 2511 sage: p.roots(ring=CC) 2512 [(-1.2146389322441... - 0.14142505258239...*I, 1), (-0.14142505258239... + 1.2146389322441...*I, 1), (0.14142505258239... - 1.2146389322441...*I, 1), (1.2146389322441... + 0.14142505258239...*I, 1)] 2513 sage: p = p^2 * (y^2 - 2) 2514 sage: p.roots(ring=CIF) 2515 [([-1.4142135623730952 .. -1.4142135623730949], 1), ([1.4142135623730949 .. 1.4142135623730952], 1), ([-1.2146389322441827 .. -1.2146389322441821] - [0.14142505258239376 .. 0.14142505258239399]*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)] 2484 2516 2485 2517 There are many combinations of floating-point input and output … … 2529 2561 Algorithms used: 2530 2562 2531 For brevity, we will use RR to mean any RealField of any precision; 2532 similarly for CC and CIF. 2563 For brevity, we will use RR to mean any RealField of any 2564 precision; similarly for RIF, CC, and CIF. Since Sage has no 2565 specific implementation of Gaussian rationals (or of number 2566 fields with embedding, at all), when we refer to Gaussian 2567 rationals below we will accept any number field with defining 2568 polynomial x^2+1, mapping the field generator to +I. 2533 2569 2534 2570 We call the base ring of the polynomial K, and the ring given … … 2549 2585 this method gives.) 2550 2586 2551 If L is floating-point and K is not, then we attempt to 2552 change the polynomial ring to L (using .change_ring()) 2553 (or, if L is complex, to the corresponding real field). 2554 Then we use either Pari or numpy as specified above. 2587 If L is CIF, and K is ZZ, QQ, AA, or the Gaussian rationals, 2588 then the root isolation algorithm 2589 sage.rings.polynomial.complex_roots.complex_roots() is used. 2590 (You can call complex_roots() directly to get more control than 2591 this method gives.) 2592 2593 If L is floating-point and K is not, then we attempt to change 2594 the polynomial ring to L (using .change_ring()) (or, if L is 2595 complex and K is not, to the corresponding real field). Then 2596 we use either Pari or numpy as specified above. 2555 2597 2556 2598 For all other cases where K is different than L, we just use … … 2559 2601 The next method is to attempt to factor the polynomial. 2560 2602 If this succeeds, then for every degree-one factor a*x+b, we 2561 add -b/a as a root. 2603 add -b/a as a root (as long as this quotient is actually in 2604 the desired ring). 2562 2605 2563 2606 If factoring over K is not implemented, and K is finite, then … … 2589 2632 if L is None: L = K 2590 2633 2634 late_import() 2635 2591 2636 input_fp = (is_RealField(K) 2592 2637 or is_ComplexField(K) … … 2601 2646 output_complex = (is_ComplexField(L) 2602 2647 or is_ComplexDoubleField(L)) 2648 input_gaussian = (isinstance(K, NumberField_quadratic) 2649 and list(K.polynomial()) == [1, 0, 1]) 2603 2650 2604 2651 if input_fp and output_fp: … … 2660 2707 return rts 2661 2708 2662 late_import()2663 2664 2709 if L != K or is_AlgebraicRealField(L): 2665 # So far, the only "special" implementation is for K exact2666 # and L either AA or a real interval field.2710 # So far, the only "special" implementations are for real 2711 # and complex root isolation. 2667 2712 if (is_IntegerRing(K) or is_RationalField(K) 2668 2713 or is_AlgebraicRealField(K)) and \ … … 2695 2740 return [rt for (rt, mult) in rts] 2696 2741 2697 if output_fp and output_complex: 2742 if (is_IntegerRing(K) or is_RationalField(K) 2743 or is_AlgebraicRealField(K) or input_gaussian) and \ 2744 (is_ComplexIntervalField(L)): 2745 2746 from sage.rings.polynomial.complex_roots import complex_roots 2747 2748 rts = complex_roots(self, min_prec=L.prec()) 2749 2750 if multiplicities: 2751 return rts 2752 else: 2753 return [rt for (rt, mult) in rts] 2754 2755 if output_fp and output_complex and not input_gaussian: 2698 2756 # If we want the complex roots, and the input is not 2699 # floating point, we convert to a real polynomial. 2700 # I think this works for now, because there are no 2701 # non-floating-point complex types in Sage. Hopefully 2702 # someday we will have Gaussian integers and rationals, 2703 # and this will have to change then. 2757 # floating point, we convert to a real polynomial 2758 # (except when the input coefficients are Gaussian rationals). 2704 2759 if is_ComplexDoubleField(L): 2705 2760 real_field = RDF … … 2846 2901 2847 2902 if not self: 2848 return infinity 2849 2850 if p is infinity :2903 return infinity.infinity 2904 2905 if p is infinity.infinity: 2851 2906 return -self.degree() 2852 2907 … … 3087 3142 3088 3143 coeffs = self.coeffs() 3089 if p == infinity :3144 if p == infinity.infinity: 3090 3145 return RR(max([abs(i) for i in coeffs])) 3091 3146
Note: See TracChangeset
for help on using the changeset viewer.
