Ticket #12950: trac_12950-pynac_infinities.patch

File trac_12950-pynac_infinities.patch, 7.0 KB (added by burcin, 8 years ago)
  • sage/libs/ginac/decl.pxi

    # HG changeset patch
    # User Volker Braun <vbraun@stp.dias.ie>
    # Date 1308339384 25200
    # Node ID 765a4b54b75306e2e72c2a44c751025eb06b8ec3
    # Parent  d84392d4da573c5dd59d339214b0a9fa1f750580
    Changes for the modified pynac infinities.
    
    diff --git a/sage/libs/ginac/decl.pxi b/sage/libs/ginac/decl.pxi
    a b  
    1818    void ginac_pyinit_Float(object)
    1919    void ginac_pyinit_I(object)
    2020   
     21    # forward declaration of GEx
     22    ctypedef struct GEx "ex"
     23
    2124    ctypedef struct GBasic "basic":
    2225        unsigned int gethash()
    2326        int compare(GBasic other)
     
    2528    ctypedef struct GConstant "constant":
    2629        unsigned get_serial()
    2730
     31    ctypedef struct GInfinity "infinity":
     32        bint is_unsigned_infinity()
     33        bint is_plus_infinity()
     34        bint is_minus_infinity()
     35        GEx get_direction()
     36        GEx conjugate()
     37        GEx real_part()
     38        GEx imag_part()
    2839
    2940    ctypedef struct GSymbol "symbol":
    3041        unsigned get_domain()
     
    4354    ctypedef struct GExMap "exmap":
    4455        void insert(GExPair e)
    4556
    46     # forward declaration of GEx
    47     ctypedef struct GEx "ex"
    48 
    4957    ctypedef struct GExListIter "GiNaC::lst::const_iterator":
    5058        void inc "operator++" ()
    5159        GEx obj "operator*" ()
     
    161169    GEx g_Pi "Pi"
    162170    GEx g_Catalan "Catalan"
    163171    GEx g_Euler "Euler"
    164     GEx g_UnsignedInfinity "UnsignedInfinity"
    165     GEx g_Infinity "Infinity"
    166     GEx g_mInfinity "-Infinity"
    167172
    168173    GConstant* GConstant_construct(void *mem, char* name, char* texname, unsigned domain)
    169174    bint is_a_constant "is_a<constant>" (GEx e)
     
    171176    GConstant* GConstant_construct_str "Construct_p<constant, char*>" \
    172177            (void *mem, char* name) except +
    173178
     179    # Infinities
     180    bint is_a_infinity "is_a<infinity>" (GEx e)
     181    GEx g_UnsignedInfinity "UnsignedInfinity"
     182    GEx g_Infinity "Infinity"
     183    GEx g_mInfinity "-Infinity"
     184    GInfinity ex_to_infinity "ex_to<infinity>" (GEx e)
    174185
    175186    # I is not a constant, but a numeric object
    176187    # we declare it here for easy reference
  • sage/symbolic/expression.pxd

    diff --git a/sage/symbolic/expression.pxd b/sage/symbolic/expression.pxd
    a b  
    1010    cpdef object _convert(self, R)
    1111    cpdef bint is_polynomial(self, var)
    1212    cpdef bint is_relational(self)
     13    cpdef bint is_infinity(self)
    1314    cpdef object pyobject(self)
    1415    cpdef Expression _subs_expr(self, expr)
    1516   
  • sage/symbolic/expression.pyx

    diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
    a b  
    145145from sage.symbolic.function import get_sfunction_from_serial, SymbolicFunction
    146146from sage.rings.rational import Rational  # Used for sqrt.
    147147from sage.misc.derivative import multi_derivative
    148 from sage.rings.infinity import AnInfinity
     148from sage.rings.infinity import AnInfinity, infinity, minus_infinity, unsigned_infinity
    149149from sage.misc.decorators import rename_keyword
    150150from sage.misc.misc import deprecated_function_alias
    151151
     
    203203cdef class Expression(CommutativeRingElement):
    204204    cpdef object pyobject(self):
    205205        """
    206         Get the underlying Python object corresponding to this
    207         expression, assuming this expression is a single numerical
    208         value.   Otherwise, a TypeError is raised.
     206        Get the underlying Python object.
     207
     208        OUTPUT:
     209
     210        The python object corresponding to this expression, assuming
     211        this expression is a single numerical value. Otherwise, a
     212        TypeError is raised.
    209213
    210214        EXAMPLES::
    211215       
     
    217221            -17/3
    218222            sage: a.pyobject() is b
    219223            True
     224           
     225        TESTS::
     226       
     227            sage: SR(oo).pyobject()
     228            +Infinity
     229            sage: SR(-oo).pyobject()
     230            -Infinity
     231            sage: SR(unsigned_infinity).pyobject()
     232            Infinity
     233            sage: SR(I*oo).pyobject()
     234            Traceback (most recent call last):
     235            ...
     236            ValueError: Python infinity cannot have complex phase.
    220237        """
    221238        cdef GConstant* c
    222239        if is_a_constant(self._gobj):
    223240            from sage.symbolic.constants import constants_name_table
    224241            return constants_name_table[GEx_to_str(&self._gobj)]
     242
     243        if is_a_infinity(self._gobj):
     244            if (ex_to_infinity(self._gobj).is_unsigned_infinity()): return unsigned_infinity
     245            if (ex_to_infinity(self._gobj).is_plus_infinity()):     return infinity
     246            if (ex_to_infinity(self._gobj).is_minus_infinity()):    return minus_infinity
     247            raise TypeError('Python infinity cannot have complex phase.')
     248           
    225249        if not is_a_numeric(self._gobj):
    226250            raise TypeError, "self must be a numeric expression"
    227251        return py_object_from_numeric(self._gobj)
     
    16601684        """
    16611685        return is_a_relational(self._gobj)
    16621686
     1687    cpdef bint is_infinity(self):
     1688        """
     1689        Return True if self is a infinite expression.
     1690
     1691        EXAMPLES::
     1692       
     1693            sage: SR(oo).is_infinity()
     1694            True
     1695            sage: x.is_infinity()
     1696            False
     1697        """
     1698        return is_a_infinity(self._gobj)
     1699
    16631700    def left_hand_side(self):
    16641701        """
    16651702        If self is a relational expression, return the left hand side
     
    18591896            sage: bool(x != y) # The same comment as above applies here as well
    18601897            True
    18611898            sage: forget()
     1899
     1900        Comparisons of infinities::
     1901
     1902            sage: assert( (1+I)*oo == (2+2*I)*oo )
     1903            sage: assert( SR(unsigned_infinity) == SR(unsigned_infinity) )
     1904            sage: assert( SR(I*oo) == I*oo )
     1905            sage: assert( SR(-oo) <= SR(oo) )
     1906            sage: assert( SR(oo) >= SR(-oo) )
     1907            sage: assert( SR(oo) != SR(-oo) )
     1908            sage: assert( sqrt(2)*oo != I*oo )
    18621909        """
    18631910        if self.is_relational():
    1864             #If both the left hand side and right hand side are wrappers
    1865             #around Sage objects, then we should do the comparison directly
    1866             #with those objects
     1911            # constants are wrappers around Sage objects, compare directly
    18671912            if is_a_constant(self._gobj.lhs()) and is_a_constant(self._gobj.rhs()):
    18681913                return self.operator()(self.lhs().pyobject(), self.rhs().pyobject())
    18691914
    1870             res = relational_to_bool(self._gobj)
    1871             if res:
     1915            pynac_result = relational_to_bool(self._gobj)
     1916
     1917            # pynac is guaranteed to give the correct answer for comparing infinities
     1918            if is_a_infinity(self._gobj.lhs()) or is_a_infinity(self._gobj.rhs()):
     1919                return pynac_result
     1920
     1921            if pynac_result:
    18721922                if self.operator() == operator.ne: # this hack is necessary to catch the case where the operator is != but is False because of assumptions made
    18731923                    m = self._maxima_()
    18741924                    s = m.parent()._eval_line('is (notequal(%s,%s))'%(repr(m.lhs()),repr(m.rhs())))