Ticket #10981: trac_10981.patch

File trac_10981.patch, 3.0 KB (added by was, 10 years ago)
  • sage/rings/qqbar.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1314160579 25200
    # Node ID 86072d5911b8b1263f875533202d80a912402569
    # Parent  5fc756b95966cbb64c7f6f06e1ac5dbade9126d9
    trac 10981 -- buggy arithmetic in AA, and other issues
    
    diff --git a/sage/rings/qqbar.py b/sage/rings/qqbar.py
    a b  
    473473    [42, None, None, -13, None, None, None]
    474474    sage: convert_test_all(QQ)
    475475    [42, 22/7, None, -13, 89/55, None, None]
     476
     477TESTS::
     478
     479Verify that trac 10981 is fixed::
     480
     481    sage: x = AA['x'].gen()
     482    sage: P = 1/(1+x^4)
     483    sage: P.partial_fraction_decomposition()
     484    (0, [(-0.3535533905932738?*x + 1/2)/(x^2 - 1.414213562373095?*x + 1), (0.3535533905932738?*x + 1/2)/(x^2 + 1.414213562373095?*x + 1)])
    476485"""
    477486
    478487import sage.rings.ring
     
    33833392
    33843393class AlgebraicReal(AlgebraicNumber_base):
    33853394    def __init__(self, x):
     3395        """
     3396        Create an algebraic real from x, possibly taking the real part of x.
     3397       
     3398        TESTS::
     3399
     3400        Both of the following examples, from trac 11728, trigger
     3401        taking the real part below.  This is necessary because
     3402        sometimes a very small (e.g., 1e-17) complex part appears in a
     3403        complex interval used to create an AlgebraicReal.::
     3404
     3405            sage: a = QQbar((-1)^(1/4)); b = AA(a^3-a); t = b.as_number_field_element()
     3406            sage: b*1
     3407            -1.414213562373095?
     3408        """
    33863409        AlgebraicNumber_base.__init__(self, AA, x)
     3410        self._ensure_real()
     3411
     3412    def _ensure_real(self):
     3413        """
     3414        This is used internally by some methods to check if
     3415        self._value is a complex interval, and if so, take the real
     3416        part.
     3417
     3418        EXAMPLES::
     3419       
     3420            sage: a = QQbar((-1)^(1/4)); b = AA(a^3-a); b._value
     3421            -1.4142135623730950488?
     3422            sage: b._value = a._value; b._value
     3423            0.70710678118654752440084436210484903929? + 0.70710678118654752440084436210484903929?*I
     3424            sage: b._ensure_real()
     3425            sage: b._value
     3426            0.70710678118654752440084436210484903929?
     3427            sage: type(b._value)
     3428            <type 'sage.rings.real_mpfi.RealIntervalFieldElement'>
     3429        """
     3430        if is_ComplexIntervalFieldElement(self._value):
     3431            self._value = self._value.real()
     3432
     3433    def _more_precision(self):
     3434        """
     3435        Recompute the interval bounding this number with higher-precision
     3436        interval arithmetic.
     3437
     3438        TESTS::
     3439
     3440        We have to ensure after doing this that self._value is still
     3441        real which isn't the case without calling _ensure_real (see
     3442        trac 11728)::
     3443       
     3444            sage: P = AA[x](1+x^4); a1,a2 = P.factor()[0][0],P.factor()[1][0]; a1*a2
     3445            x^4 + 1.000000000000000?
     3446            sage: a1,a2
     3447            (x^2 - 1.414213562373095?*x + 1, x^2 + 1.414213562373095?*x + 1)
     3448            sage: a1*a2
     3449            x^4 + 1
     3450        """
     3451        AlgebraicNumber_base._more_precision(self)
     3452        self._ensure_real()
    33873453
    33883454    def __reduce__(self):
    33893455        """