Changeset 7450:766af69fc0fe


Ignore:
Timestamp:
12/01/07 10:06:53 (5 years ago)
Author:
mabshoff@…
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.
Message:

merge

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/rings/polynomial/polynomial_element.pyx

    r7433 r7450  
    533533 
    534534    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        """ 
    535546        if self.degree() > 0: 
    536547            raise TypeError, "cannot coerce nonconstant polynomial" 
    537548        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]) 
    538564 
    539565    def __invert__(self): 
  • sage/rings/polynomial/polynomial_element.pyx

    r7449 r7450  
    3636import sage.rings.complex_field 
    3737import sage.rings.fraction_field_element 
    38 from sage.rings.infinity import infinity 
     38import sage.rings.infinity as infinity 
    3939#import sage.misc.misc as misc 
    4040from sage.misc.sage_eval import sage_eval 
     
    7474 
    7575cdef object is_AlgebraicRealField 
     76cdef object NumberField_quadratic 
     77cdef object is_ComplexIntervalField 
    7678 
    7779cdef void late_import(): 
    7880    # A hack to avoid circular imports. 
    7981    global is_AlgebraicRealField 
     82    global NumberField_quadratic 
     83    global is_ComplexIntervalField 
    8084 
    8185    if is_AlgebraicRealField is not None: 
    8286        return 
    8387 
    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 
    8694 
    8795cdef class Polynomial(CommutativeAlgebraElement): 
     
    19351943            +Infinity 
    19361944        """ 
    1937         return infinity 
     1945        return infinity.infinity 
    19381946 
    19391947    def padded_list(self, n=None): 
     
    23212329        at the end of the docstring about this. 
    23222330 
    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.) 
    23292338         
    23302339        At the end of this docstring (after the examples) is a description 
     
    23962405            sage: f = x^10 - 2*(5*x-1)^2 
    23972406            sage: f.roots(multiplicities=False) 
    2398             [-1.6772670339941..., 0.199954796285..., 0.200045306115..., 1.5763035161844...] 
     2407            [-1.6772670339941..., 0.19995479628..., 0.200045306115..., 1.5763035161844...] 
    23992408 
    24002409            sage: x = CC['x'].0 
     
    24822491            sage: f.roots(ring=RIF, multiplicities=False) 
    24832492            [[-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)] 
    24842516 
    24852517        There are many combinations of floating-point input and output 
     
    25292561        Algorithms used: 
    25302562 
    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. 
    25332569 
    25342570        We call the base ring of the polynomial K, and the ring given 
     
    25492585        this method gives.) 
    25502586 
    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. 
    25552597 
    25562598        For all other cases where K is different than L, we just use 
     
    25592601        The next method is to attempt to factor the polynomial. 
    25602602        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). 
    25622605 
    25632606        If factoring over K is not implemented, and K is finite, then 
     
    25892632        if L is None: L = K 
    25902633         
     2634        late_import() 
     2635 
    25912636        input_fp = (is_RealField(K) 
    25922637                    or is_ComplexField(K)  
     
    26012646        output_complex = (is_ComplexField(L) 
    26022647                          or is_ComplexDoubleField(L)) 
     2648        input_gaussian = (isinstance(K, NumberField_quadratic) 
     2649                          and list(K.polynomial()) == [1, 0, 1]) 
    26032650 
    26042651        if input_fp and output_fp: 
     
    26602707                return rts 
    26612708 
    2662         late_import() 
    2663  
    26642709        if L != K or is_AlgebraicRealField(L): 
    2665             # So far, the only "special" implementation is for K exact 
    2666             # and L either AA or a real interval field. 
     2710            # So far, the only "special" implementations are for real 
     2711            # and complex root isolation. 
    26672712            if (is_IntegerRing(K) or is_RationalField(K) 
    26682713                or is_AlgebraicRealField(K)) and \ 
     
    26952740                    return [rt for (rt, mult) in rts] 
    26962741 
    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: 
    26982756                # 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). 
    27042759                if is_ComplexDoubleField(L): 
    27052760                    real_field = RDF 
     
    28462901 
    28472902        if not self: 
    2848             return infinity 
    2849  
    2850         if p is infinity: 
     2903            return infinity.infinity 
     2904 
     2905        if p is infinity.infinity: 
    28512906            return -self.degree() 
    28522907 
     
    30873142         
    30883143        coeffs = self.coeffs() 
    3089         if p == infinity: 
     3144        if p == infinity.infinity: 
    30903145            return RR(max([abs(i) for i in coeffs])) 
    30913146         
Note: See TracChangeset for help on using the changeset viewer.