예제 #1
0
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            try:
                rop_cls = Relational.ValidRelationOperator[rop]
            except KeyError:
                msg = "Invalid relational operator symbol: '%r'"
                raise ValueError(msg % repr(rop))
        if lhs.is_number and rhs.is_number and (rop_cls in (Equality, Unequality) or lhs.is_real and rhs.is_real):
            diff = lhs - rhs
            know = diff.equals(0, failing_expression=True)
            if know is True:  # exclude failing expression case
                Nlhs = S.Zero
            elif know is False:
                from sympy import sign

                Nlhs = sign(diff.n(1))
            else:
                Nlhs = None
                lhs = know
                rhs = S.Zero
            if Nlhs is not None:
                return rop_cls._eval_relation(Nlhs, S.Zero)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #2
0
    def __new__(cls, start, end, left_open=False, right_open=False):

        start = _sympify(start)
        end = _sympify(end)

        # Only allow real intervals (use symbols with 'is_real=True').
        if not start.is_real or not end.is_real:
            raise ValueError("Only real intervals are supported")

        # Make sure that the created interval will be valid.
        if end.is_comparable and start.is_comparable:
            if end < start:
                return S.EmptySet

        if end == start and (left_open or right_open):
            return S.EmptySet
        if end == start and not (left_open or right_open):
            return FiniteSet(end)

        # Make sure infinite interval end points are open.
        if start == S.NegativeInfinity:
            left_open = True
        if end == S.Infinity:
            right_open = True

        return Basic.__new__(cls, start, end, left_open, right_open)
예제 #3
0
파일: sets.py 프로젝트: Grahack/geophar
    def __new__(cls, start, end, left_open=False, right_open=False):

        start = _sympify(start)
        end = _sympify(end)

        # Only allow real intervals (use symbols with 'is_real=True').
        if not start.is_real or not end.is_real:
            raise ValueError("Only real intervals are supported")

        # Make sure that the created interval will be valid.
        if end.is_comparable and start.is_comparable:
            if end < start:
                return S.EmptySet

        if end == start and (left_open or right_open):
            return S.EmptySet
        if end == start and not (left_open or right_open):
            return FiniteSet(end)

        # Make sure infinite interval end points are open.
        if start == S.NegativeInfinity:
            left_open = True
        if end == S.Infinity:
            right_open = True

        return Basic.__new__(cls, start, end, left_open, right_open)
예제 #4
0
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            try:
                rop_cls = Relational.ValidRelationOperator[rop]
            except KeyError:
                msg = "Invalid relational operator symbol: '%r'"
                raise ValueError(msg % repr(rop))
        if (lhs.is_number and rhs.is_number
                and (rop_cls in (Equality, Unequality)
                     or lhs.is_real and rhs.is_real)):
            diff = lhs - rhs
            know = diff.equals(0, failing_expression=True)
            if know is True:  # exclude failing expression case
                Nlhs = S.Zero
            elif know is False:
                from sympy import sign
                Nlhs = sign(diff.n(2))
            else:
                Nlhs = None
                lhs = know
                rhs = S.Zero
            if Nlhs is not None:
                return rop_cls._eval_relation(Nlhs, S.Zero)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #5
0
파일: basic.py 프로젝트: BDGLunde/sympy
    def compare_pretty(a, b):
        """
        Is a > b in the sense of ordering in printing?

        ::

          yes ..... return 1
          no ...... return -1
          equal ... return 0

        Strategy:

        It uses Basic.compare as a fallback, but improves it in many cases,
        like ``x**3``, ``x**4``, ``O(x**3)`` etc. In those simple cases, it just parses the
        expression and returns the "sane" ordering such as::

          1 < x < x**2 < x**3 < O(x**4) etc.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import Basic, Number
        >>> Basic._compare_pretty(x, x**2)
        -1
        >>> Basic._compare_pretty(x**2, x**2)
        0
        >>> Basic._compare_pretty(x**3, x**2)
        1
        >>> Basic._compare_pretty(Number(1, 2), Number(1, 3))
        1
        >>> Basic._compare_pretty(Number(0), Number(-1))
        1

        """
        try:
            a = _sympify(a)
        except SympifyError:
            pass

        try:
            b = _sympify(b)
        except SympifyError:
            pass

        # both objects are non-SymPy
        if (not isinstance(a, Basic)) and (not isinstance(b, Basic)):
            return cmp(a, b)

        if not isinstance(a, Basic):
            return -1  # other < sympy

        if not isinstance(b, Basic):
            return +1  # sympy > other

        # now both objects are from SymPy, so we can proceed to usual comparison
        return cmp(a.sort_key(), b.sort_key())
예제 #6
0
파일: basic.py 프로젝트: Kimay/sympy
    def compare_pretty(a, b):
        """
        Is a > b in the sense of ordering in printing?

        ::

          yes ..... return 1
          no ...... return -1
          equal ... return 0

        Strategy:

        It uses Basic.compare as a fallback, but improves it in many cases,
        like x**3, x**4, O(x**3) etc. In those simple cases, it just parses the
        expression and returns the "sane" ordering such as::

          1 < x < x**2 < x**3 < O(x**4) etc.

        Examples
        ========

        >>> from sympy.abc import x
        >>> from sympy import Basic, Number
        >>> Basic._compare_pretty(x, x**2)
        -1
        >>> Basic._compare_pretty(x**2, x**2)
        0
        >>> Basic._compare_pretty(x**3, x**2)
        1
        >>> Basic._compare_pretty(Number(1, 2), Number(1, 3))
        1
        >>> Basic._compare_pretty(Number(0), Number(-1))
        1

        """
        try:
            a = _sympify(a)
        except SympifyError:
            pass

        try:
            b = _sympify(b)
        except SympifyError:
            pass

        # both objects are non-SymPy
        if (not isinstance(a, Basic)) and (not isinstance(b, Basic)):
            return cmp(a,b)

        if not isinstance(a, Basic):
            return -1   # other < sympy

        if not isinstance(b, Basic):
            return +1   # sympy > other

        # now both objects are from SymPy, so we can proceed to usual comparison
        return cmp(a.sort_key(), b.sort_key())
예제 #7
0
 def __new__(cls, lhs, rhs, rop=None, **assumptions):
     lhs = _sympify(lhs)
     rhs = _sympify(rhs)
     if cls is not Relational:
         rop_cls = cls
     else:
         rop_cls, swap = Relational.get_relational_class(rop)
         if swap: lhs, rhs = rhs, lhs
     obj = Basic.__new__(rop_cls, lhs, rhs, **assumptions)
     return obj
예제 #8
0
 def __new__(cls, lhs, rhs, rop=None, **assumptions):
     lhs = _sympify(lhs)
     rhs = _sympify(rhs)
     if cls is not Relational:
         rop_cls = cls
     else:
         rop_cls, swap = Relational.get_relational_class(rop)
         if swap: lhs, rhs = rhs, lhs
     obj = Basic.__new__(rop_cls, lhs, rhs, **assumptions)
     return obj
예제 #9
0
 def __new__(cls, lhs, rhs, rop=None, **assumptions):
     lhs = _sympify(lhs)
     rhs = _sympify(rhs)
     if cls is not Relational:
         rop_cls = cls
     else:
         rop_cls, swap = Relational.get_relational_class(rop)
         if swap: lhs, rhs = rhs, lhs
     if lhs.is_real and lhs.is_number and rhs.is_real and rhs.is_number:
         return rop_cls._eval_relation(lhs.evalf(), rhs.evalf())
     else:
         obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
         return obj
예제 #10
0
 def __new__(cls, lhs, rhs, rop=None, **assumptions):
     lhs = _sympify(lhs)
     rhs = _sympify(rhs)
     if cls is not Relational:
         rop_cls = cls
     else:
         rop_cls, swap = Relational.get_relational_class(rop)
         if swap: lhs, rhs = rhs, lhs
     if lhs.is_real and lhs.is_number and rhs.is_real and rhs.is_number:
         return rop_cls._eval_relation(lhs.evalf(), rhs.evalf())
     else:
         obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
         return obj
예제 #11
0
파일: power.py 프로젝트: rkern/sympy-rkern
 def __new__(cls, a, b, **assumptions):
     a = _sympify(a)
     b = _sympify(b)
     if assumptions.get("evaluate") is False:
         return Basic.__new__(cls, a, b, **assumptions)
     if b is S.Zero:
         return S.One
     if b is S.One:
         return a
     obj = a._eval_power(b)
     if obj is None:
         obj = Basic.__new__(cls, a, b, **assumptions)
         obj.is_commutative = a.is_commutative and b.is_commutative
     return obj
예제 #12
0
 def __new__(cls, b, e, **assumptions):
     b = _sympify(b)
     e = _sympify(e)
     if assumptions.get("evaluate") is False:
         return Basic.__new__(cls, b, e, **assumptions)
     if e is S.Zero:
         return S.One
     if e is S.One:
         return b
     obj = b._eval_power(e)
     if obj is None:
         obj = Basic.__new__(cls, b, e, **assumptions)
         obj.is_commutative = b.is_commutative and e.is_commutative
     return obj
예제 #13
0
 def __new__(cls, a, b, **assumptions):
     a = _sympify(a)
     b = _sympify(b)
     if assumptions.get('evaluate') is False:
         return Basic.__new__(cls, a, b, **assumptions)
     if b is S.Zero:
         return S.One
     if b is S.One:
         return a
     obj = a._eval_power(b)
     if obj is None:
         obj = Basic.__new__(cls, a, b, **assumptions)
         obj.is_commutative = (a.is_commutative and b.is_commutative)
     return obj
예제 #14
0
파일: power.py 프로젝트: laudehenri/rSymPy
 def __new__(cls, b, e, **assumptions):
     b = _sympify(b)
     e = _sympify(e)
     if assumptions.get('evaluate') is False:
         return Basic.__new__(cls, b, e, **assumptions)
     if e is S.Zero:
         return S.One
     if e is S.One:
         return b
     obj = b._eval_power(e)
     if obj is None:
         obj = Basic.__new__(cls, b, e, **assumptions)
         obj.is_commutative = (b.is_commutative and e.is_commutative)
     return obj
예제 #15
0
파일: power.py 프로젝트: Aang/sympy
 def __new__(cls, b, e, **assumptions):
     b = _sympify(b)
     e = _sympify(e)
     if assumptions.pop('evaluate', True):
         if e is S.Zero:
             return S.One
         elif e is S.One:
             return b
         else:
             obj = b._eval_power(e)
             if obj is not None:
                 return obj
     obj = Expr.__new__(cls, b, e, **assumptions)
     obj.is_commutative = (b.is_commutative and e.is_commutative)
     return obj
예제 #16
0
 def __new__(cls, b, e, **assumptions):
     b = _sympify(b)
     e = _sympify(e)
     if assumptions.pop('evaluate', True):
         if e is S.Zero:
             return S.One
         elif e is S.One:
             return b
         else:
             obj = b._eval_power(e)
             if obj is not None:
                 return obj
     obj = Expr.__new__(cls, b, e, **assumptions)
     obj.is_commutative = (b.is_commutative and e.is_commutative)
     return obj
예제 #17
0
 def __new__(cls, b, e, evaluate=True):
     b = _sympify(b)
     e = _sympify(e)
     if evaluate:
         if e is S.Zero:
             return S.One
         elif e is S.One:
             return b
         else:
             obj = b._eval_power(e)
             if obj is not None:
                 return obj
     obj = Expr.__new__(cls, b, e)
     obj.is_commutative = (b.is_commutative and e.is_commutative)
     return obj
예제 #18
0
파일: power.py 프로젝트: itsrg/sympy
 def __new__(cls, b, e, evaluate=True):
     b = _sympify(b)
     e = _sympify(e)
     if evaluate:
         if e is S.Zero:
             return S.One
         elif e is S.One:
             return b
         else:
             obj = b._eval_power(e)
             if obj is not None:
                 return obj
     obj = Expr.__new__(cls, b, e)
     obj.is_commutative = (b.is_commutative and e.is_commutative)
     return obj
예제 #19
0
파일: relational.py 프로젝트: vchekan/sympy
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            try:
                rop_cls = Relational.ValidRelationOperator[ rop ]
            except KeyError:
                msg = "Invalid relational operator symbol: '%r'"
                raise ValueError(msg % repr(rop))

        diff = S.NaN
        if isinstance(lhs, Expr) and isinstance(rhs, Expr):
            diff = lhs - rhs
        if not (diff is S.NaN or diff.has(Symbol)):
            know = diff.equals(0, failing_expression=True)
            if know is True:  # exclude failing expression case
                diff = S.Zero
            elif know is False:
                diff = diff.n()
        if rop_cls is Equality:
            if (lhs == rhs) is True or (diff == S.Zero) is True:
                return True
            elif diff is S.NaN:
                pass
            elif diff.is_Number or diff.is_Float:
                return False
            elif lhs.is_real is not rhs.is_real and \
                lhs.is_real is not None and \
                   rhs.is_real is not None:
                return False
        elif rop_cls is Unequality:
            if (lhs == rhs) is True or (diff == S.Zero) is True:
                return False
            elif diff is S.NaN:
                pass
            elif diff.is_Number or diff.is_Float:
                return True
            elif lhs.is_real is not rhs.is_real and \
                lhs.is_real is not None and \
                   rhs.is_real is not None:
                return True
        elif diff.is_Number and diff.is_real:
            return rop_cls._eval_relation(diff, S.Zero)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #20
0
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            try:
                rop_cls = cls.ValidRelationOperator[ rop ]
            except KeyError:
                msg = "Invalid relational operator symbol: '%r'"
                raise ValueError(msg % repr(rop))

        diff = S.NaN
        if isinstance(lhs, Expr) and isinstance(rhs, Expr):
            diff = lhs - rhs
        if not (diff is S.NaN or diff.has(Symbol)):
            know = diff.equals(0, failing_expression=True)
            if know is True:  # exclude failing expression case
                diff = S.Zero
            elif know is False:
                diff = diff.n()
        if rop_cls is Equality:
            if (lhs == rhs) is True or (diff == S.Zero) is True:
                return True
            elif diff is S.NaN:
                pass
            elif diff.is_Number or diff.is_Float:
                return False
            elif lhs.is_real is not rhs.is_real and \
                lhs.is_real is not None and \
                   rhs.is_real is not None:
                return False
        elif rop_cls is Unequality:
            if (lhs == rhs) is True or (diff == S.Zero) is True:
                return False
            elif diff is S.NaN:
                pass
            elif diff.is_Number or diff.is_Float:
                return True
            elif lhs.is_real is not rhs.is_real and \
                lhs.is_real is not None and \
                   rhs.is_real is not None:
                return True
        elif diff.is_Number and diff.is_real:
            return rop_cls._eval_relation(diff, S.Zero)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #21
0
    def __new__(cls, *args, **assumptions):
        if assumptions.get('evaluate') is False:
            args = map(_sympify, args)
            obj = Expr.__new__(cls, *args, **assumptions)
            obj.is_commutative = all(arg.is_commutative for arg in args)
            return obj
        if len(args)==0:
            return cls.identity
        if len(args)==1:
            return _sympify(args[0])
        c_part, nc_part, order_symbols = cls.flatten(map(_sympify, args))
        if len(c_part) + len(nc_part) <= 1:
            if c_part:
                obj = c_part[0]
            elif nc_part:
                obj = nc_part[0]
            else:
                obj = cls.identity
        else:
            obj = Expr.__new__(cls, *(c_part + nc_part), **assumptions)
            obj.is_commutative = not nc_part

        if order_symbols is not None:
            obj = C.Order(obj, *order_symbols)
        return obj
예제 #22
0
파일: basic.py 프로젝트: goodok/sympy
    def __eq__(self, other):
        """a == b  -> Compare two symbolic trees and see whether they are equal

           this is the same as:

             a.compare(b) == 0

           but faster
        """

        if type(self) is not type(other):
            # issue 3001 a**1.0 == a like a**2.0 == a**2
            while isinstance(self, C.Pow) and self.exp == 1:
                self = self.base
            while isinstance(other, C.Pow) and other.exp == 1:
                other = other.base
            try:
                other = _sympify(other)
            except SympifyError:
                return False    # sympy != other

            if type(self) is not type(other):
                return False

        # type(self) == type(other)
        st = self._hashable_content()
        ot = other._hashable_content()

        return st == ot and self._assume_type_keys == other._assume_type_keys
예제 #23
0
    def matches(self, expr, repl_dict={}, old=False):
        expr = _sympify(expr)

        # special case, pattern = 1 and expr.exp can match to 0
        if expr is S.One:
            d = repl_dict.copy()
            d = self.exp.matches(S.Zero, d)
            if d is not None:
                return d

        b, e = expr.as_base_exp()

        # special case number
        sb, se = self.as_base_exp()
        if sb.is_Symbol and se.is_Integer and expr:
            if e.is_rational:
                return sb.matches(b**(e/se), repl_dict)
            return sb.matches(expr**(1/se), repl_dict)

        d = repl_dict.copy()
        d = self.base.matches(b, d)
        if d is None:
            return None

        d = self.exp.xreplace(d).matches(e, d)
        if d is None:
            return Expr.matches(self, expr, repl_dict)
        return d
예제 #24
0
파일: power.py 프로젝트: Maihj/sympy
    def matches(self, expr, repl_dict={}, old=False):
        expr = _sympify(expr)

        # special case, pattern = 1 and expr.exp can match to 0
        if expr is S.One:
            d = repl_dict.copy()
            d = self.exp.matches(S.Zero, d)
            if d is not None:
                return d

        b, e = expr.as_base_exp()

        # special case number
        sb, se = self.as_base_exp()
        if sb.is_Symbol and se.is_Integer and expr:
            if e.is_rational:
                return sb.matches(b**(e/se), repl_dict)
            return sb.matches(expr**(1/se), repl_dict)

        d = repl_dict.copy()
        d = self.base.matches(b, d)
        if d is None:
            return None

        d = self.exp.xreplace(d).matches(e, d)
        if d is None:
            return Expr.matches(self, expr, repl_dict)
        return d
예제 #25
0
    def matches(pattern, expr, repl_dict={}, evaluate=False):
        if evaluate:
            pat = pattern
            for old,new in repl_dict.items():
                pat = pat.subs(old, new)
            if pat!=pattern:
                return pat.matches(expr, repl_dict)

        expr = _sympify(expr)
        b, e = expr.as_base_exp()

        # special case, pattern = 1 and expr.exp can match to 0
        if expr is S.One:
            d = repl_dict.copy()
            d = pattern.exp.matches(S.Zero, d, evaluate=False)
            if d is not None:
                return d

        d = repl_dict.copy()
        d = pattern.base.matches(b, d, evaluate=False)
        if d is None:
            return None

        d = pattern.exp.matches(e, d, evaluate=True)
        if d is None:
            return Basic.matches(pattern, expr, repl_dict, evaluate)
        return d
예제 #26
0
파일: basic.py 프로젝트: BDGLunde/sympy
    def __eq__(self, other):
        """a == b  -> Compare two symbolic trees and see whether they are equal

           this is the same as:

             a.compare(b) == 0

           but faster
        """

        if type(self) is not type(other):
            # issue 3001 a**1.0 == a like a**2.0 == a**2
            while isinstance(self, C.Pow) and self.exp == 1:
                self = self.base
            while isinstance(other, C.Pow) and other.exp == 1:
                other = other.base
            try:
                other = _sympify(other)
            except SympifyError:
                return False  # sympy != other

            if type(self) is not type(other):
                return False

        return self._hashable_content() == other._hashable_content()
예제 #27
0
파일: power.py 프로젝트: rkern/sympy-rkern
    def matches(pattern, expr, repl_dict={}, evaluate=False):
        if evaluate:
            pat = pattern
            for old, new in repl_dict.items():
                pat = pat.subs(old, new)
            if pat != pattern:
                return pat.matches(expr, repl_dict)

        expr = _sympify(expr)
        b, e = expr.as_base_exp()

        # special case, pattern = 1 and expr.exp can match to 0
        if expr is S.One:
            d = repl_dict.copy()
            d = pattern.exp.matches(S.Zero, d, evaluate=False)
            if d is not None:
                return d

        d = repl_dict.copy()
        d = pattern.base.matches(b, d, evaluate=False)
        if d is None:
            return None

        d = pattern.exp.matches(e, d, evaluate=True)
        if d is None:
            return Basic.matches(pattern, expr, repl_dict, evaluate)
        return d
예제 #28
0
    def __ne__(self, other):
        try:
            other = _sympify(other)
        except SympifyError:
            return True     # sympy != other
        if self is other: return False
        if isinstance(other, Number) and self.is_irrational: return True

        return True     # NumberSymbol != non(Number|self)
예제 #29
0
 def __mul__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return NotImplemented
     if isinstance(other, Number):
         rhs, prec = other._as_mpf_op(self._prec)
         return Real._new(mlib.mpf_mul(self._mpf_, rhs, prec, rnd), prec)
     return Number.__mul__(self, other)
예제 #30
0
파일: numbers.py 프로젝트: gnulinooks/sympy
    def __ne__(self, other):
        try:
            other = _sympify(other)
        except SympifyError:
            return True     # sympy != other
        if self is other: return False
        if isinstance(other, Number) and self.is_irrational: return True

        return True     # NumberSymbol != non(Number|self)
예제 #31
0
파일: numbers.py 프로젝트: gnulinooks/sympy
 def __mul__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return NotImplemented
     if isinstance(other, Number):
         rhs, prec = other._as_mpf_op(self._prec)
         return Real._new(mlib.mpf_mul(self._mpf_, rhs, prec, rnd), prec)
     return Number.__mul__(self, other)
예제 #32
0
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            rop_cls, swap = Relational.get_relational_class(rop)
            if swap: lhs, rhs = rhs, lhs
        if lhs.is_real and lhs.is_number and rhs.is_real and rhs.is_number:
            # Just becase something is a number, doesn't mean you can evalf it.
            Nlhs = lhs.evalf()
            if Nlhs.is_Number:
                # S.Zero.evalf() returns S.Zero, so test Number instead of Float
                Nrhs = rhs.evalf()
                if Nrhs.is_Number:
                    return rop_cls._eval_relation(Nlhs, Nrhs)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #33
0
파일: relational.py 프로젝트: 101man/sympy
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            rop_cls, swap = Relational.get_relational_class(rop)
            if swap: lhs, rhs = rhs, lhs
        if lhs.is_number and rhs.is_number and lhs.is_real and rhs.is_real:
            # Just becase something is a number, doesn't mean you can evalf it.
            Nlhs = lhs.evalf()
            if Nlhs.is_Number:
                # S.Zero.evalf() returns S.Zero, so test Number instead of Float
                Nrhs = rhs.evalf()
                if Nrhs.is_Number:
                    return rop_cls._eval_relation(Nlhs, Nrhs)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #34
0
 def __le__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  --> not <=
     if self is other: return True
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return self.evalf()<=other
     return Basic.__le__(self, other)
예제 #35
0
파일: numbers.py 프로젝트: gnulinooks/sympy
 def __le__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  --> not <=
     if self is other: return True
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return self.evalf()<=other
     return Basic.__le__(self, other)
예제 #36
0
파일: numbers.py 프로젝트: NO2/sympy
    def gcdex(a, b):
        """Extended Euclidean Algorithm. """
        if isinstance(b, (int, long)):
            return tuple(map(Integer, igcdex(int(a), b)))
        else:
            b = _sympify(b)

            if b.is_Integer:
                return tuple(map(Integer, igcdex(int(a), int(b))))
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #37
0
파일: numbers.py 프로젝트: goriccardo/sympy
    def lcm(a, b):
        """Returns least common multiple of input arguments. """
        if type(b) is int:
            return Integer(ilcm(int(a), b))
        else:
            b = _sympify(b)

            if b.is_Integer:
                return Integer(ilcm(int(a), int(b)))
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #38
0
파일: numbers.py 프로젝트: goriccardo/sympy
    def gcd(a, b):
        """Returns greates common divisor of input arguments. """
        if type(b) is int:
            return Integer(igcd(int(a), b))
        else:
            b = _sympify(b)

            if b.is_Integer:
                return Integer(igcd(int(a), int(b)))
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #39
0
파일: numbers.py 프로젝트: goriccardo/sympy
    def gcdex(a, b):
        """Extended Euclidean Algorithm. """
        if type(b) is int:
            return tuple(map(Integer, igcdex(int(a), b)))
        else:
            b = _sympify(b)

            if b.is_Integer:
                return tuple(map(Integer, igcdex(int(a), int(b))))
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #40
0
파일: numbers.py 프로젝트: NO2/sympy
 def __le__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  -->  ! <=
     if isinstance(other, NumberSymbol):
         return other.__gt__(self)
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return bool(mlib.mpf_le(self._mpf_, other._as_mpf_val(self._prec)))
     return Expr.__le__(self, other)
예제 #41
0
파일: numbers.py 프로젝트: NO2/sympy
    def gcd(a, b):
        """Returns greates common divisor of input arguments. """
        if isinstance(b, (int, long)):
            return Integer(igcd(int(a), b))
        else:
            b = _sympify(b)

            if b.is_Integer:
                return Integer(igcd(int(a), int(b)))
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #42
0
파일: numbers.py 프로젝트: NO2/sympy
    def lcm(a, b):
        """Returns least common multiple of input arguments. """
        if isinstance(b, (int, long)):
            return Integer(ilcm(int(a), b))
        else:
            b = _sympify(b)

            if b.is_Integer:
                return Integer(ilcm(int(a), int(b)))
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #43
0
파일: numbers.py 프로젝트: gnulinooks/sympy
 def __le__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  -->  ! <=
     if isinstance(other, NumberSymbol):
         return other.__gt__(self)
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return bool(mlib.mpf_le(self._mpf_, other._as_mpf_val(self._prec)))
     return Basic.__le__(self, other)
예제 #44
0
파일: numbers.py 프로젝트: gnulinooks/sympy
 def __add__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return NotImplemented
     if (other is S.NaN) or (self is NaN):
         return S.NaN
     if isinstance(other, Number):
         rhs, prec = other._as_mpf_op(self._prec)
         return Real._new(mlib.mpf_add(self._mpf_, rhs, prec, rnd), prec)
     return Number.__add__(self, other)
예제 #45
0
 def __add__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return NotImplemented
     if (other is S.NaN) or (self is NaN):
         return S.NaN
     if isinstance(other, Number):
         rhs, prec = other._as_mpf_op(self._prec)
         return Real._new(mlib.mpf_add(self._mpf_, rhs, prec, rnd), prec)
     return Number.__add__(self, other)
예제 #46
0
 def __ne__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return True     # sympy != other
     if isinstance(other, NumberSymbol):
         if other.is_irrational: return True
         return other.__ne__(self)
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return bool(not mlib.mpf_eq(self._mpf_, other._as_mpf_val(self._prec)))
     return True     # Real != non-Number
예제 #47
0
 def __new__(cls, b, e, evaluate=True):
     # don't optimize "if e==0; return 1" here; it's better to handle that
     # in the calling routine so this doesn't get called
     b = _sympify(b)
     e = _sympify(e)
     if evaluate:
         if e is S.Zero:
             return S.One
         elif e is S.One:
             return b
         elif S.NaN in (b, e):
             if b is S.One:  # already handled e == 0 above
                 return S.One
             return S.NaN
         else:
             obj = b._eval_power(e)
             if obj is not None:
                 return obj
     obj = Expr.__new__(cls, b, e)
     obj.is_commutative = (b.is_commutative and e.is_commutative)
     return obj
예제 #48
0
파일: numbers.py 프로젝트: gnulinooks/sympy
 def __ne__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return True     # sympy != other
     if isinstance(other, NumberSymbol):
         if other.is_irrational: return True
         return other.__ne__(self)
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return bool(not mlib.mpf_eq(self._mpf_, other._as_mpf_val(self._prec)))
     return True     # Real != non-Number
예제 #49
0
파일: numbers.py 프로젝트: Aang/sympy
 def __ne__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return True     # sympy != other
     if isinstance(other, NumberSymbol):
         if other.is_irrational: return True
         return other.__ne__(self)
     if isinstance(other, FunctionClass): #cos as opposed to cos(x)
         return True
     if isinstance(other, Number):
         return bool(not mlib.mpf_eq(self._mpf_, other._as_mpf_val(self._prec)))
     return True     # Real != non-Number
예제 #50
0
파일: numbers.py 프로젝트: goriccardo/sympy
    def cofactors(a, b):
        """Returns GCD and cofactors of input arguments. """
        if type(b) is int:
            gcd = Integer(igcd(int(a), b))
            return gcd, a//gcd, Integer(b)//gcd
        else:
            b = _sympify(b)

            if b.is_Integer:
                gcd = Integer(igcd(int(a), int(b)))
                return gcd, a//gcd, b//gcd
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #51
0
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            try:
                rop_cls = Relational.ValidRelationOperator[ rop ]
            except KeyError:
                msg = "Invalid relational operator symbol: '%r'"
                raise ValueError(msg % repr(rop))
        if lhs.is_number and rhs.is_number and lhs.is_real and rhs.is_real:
            # Just becase something is a number, doesn't mean you can evalf it.
            Nlhs = lhs.evalf()
            if Nlhs.is_Number:
                # S.Zero.evalf() returns S.Zero, so test Number instead of Float
                Nrhs = rhs.evalf()
                if Nrhs.is_Number:
                    return rop_cls._eval_relation(Nlhs, Nrhs)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #52
0
 def __le__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  -->  not <=
     if isinstance(other, NumberSymbol):
         return other.__gt__(self)
     if other.is_comparable and not isinstance(other, Rational): other = other.evalf()
     if isinstance(other, Number):
         if isinstance(other, Real):
             return bool(mlib.mpf_le(self._as_mpf_val(other._prec), other._mpf_))
         return bool(self.p * other.q <= self.q * other.p)
     return Basic.__le__(self, other)
예제 #53
0
파일: numbers.py 프로젝트: NO2/sympy
    def cofactors(a, b):
        """Returns GCD and cofactors of input arguments. """
        if isinstance(b, (int, long)):
            gcd = Integer(igcd(int(a), b))
            return gcd, a//gcd, Integer(b)//gcd
        else:
            b = _sympify(b)

            if b.is_Integer:
                gcd = Integer(igcd(int(a), int(b)))
                return gcd, a//gcd, b//gcd
            else:
                raise ValueError("expected an integer, got %s" % b)
예제 #54
0
파일: numbers.py 프로젝트: gnulinooks/sympy
 def __le__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  -->  not <=
     if isinstance(other, NumberSymbol):
         return other.__gt__(self)
     if other.is_comparable and not isinstance(other, Rational): other = other.evalf()
     if isinstance(other, Number):
         if isinstance(other, Real):
             return bool(mlib.mpf_le(self._as_mpf_val(other._prec), other._mpf_))
         return bool(self.p * other.q <= self.q * other.p)
     return Basic.__le__(self, other)
예제 #55
0
    def __new__(cls, lhs, rhs, rop=None, **assumptions):
        lhs = _sympify(lhs)
        rhs = _sympify(rhs)
        if cls is not Relational:
            rop_cls = cls
        else:
            try:
                rop_cls = Relational.ValidRelationOperator[ rop ]
            except KeyError:
                msg = "Invalid relational operator symbol: '%r'"
                raise ValueError(msg % repr(rop))
        if lhs.is_number and rhs.is_number and (rop in ('==', '!=' ) or
        lhs.is_real and rhs.is_real):
            # Just because something is a number, doesn't mean you can evalf it.
            Nlhs = lhs.evalf() if not lhs.is_Atom else lhs
            if Nlhs.is_Atom:
                Nrhs = rhs.evalf() if not rhs.is_Atom else rhs
                if Nrhs.is_Atom:
                    return rop_cls._eval_relation(Nlhs, Nrhs)

        obj = Expr.__new__(rop_cls, lhs, rhs, **assumptions)
        return obj
예제 #56
0
파일: numbers.py 프로젝트: NO2/sympy
 def __eq__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy != other  -->  not ==
     if isinstance(other, NumberSymbol):
         if other.is_irrational: return False
         return other.__eq__(self)
     if isinstance(other, FunctionClass): #cos as opposed to cos(x)
         return False
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return bool(mlib.mpf_eq(self._mpf_, other._as_mpf_val(self._prec)))
     return False    # Real != non-Number
예제 #57
0
    def __new__(cls, b, e, evaluate=True):
        from sympy.functions.elementary.exponential import exp_polar

        # don't optimize "if e==0; return 1" here; it's better to handle that
        # in the calling routine so this doesn't get called
        b = _sympify(b)
        e = _sympify(e)
        if evaluate:
            if e is S.Zero:
                return S.One
            elif e is S.One:
                return b
            elif S.NaN in (b, e):
                if b is S.One:  # already handled e == 0 above
                    return S.One
                return S.NaN
            else:
                # recognize base as E
                if not e.is_Atom and b is not S.Exp1 and b.func is not exp_polar:
                    from sympy import numer, denom, log, sign, im, factor_terms
                    c, ex = factor_terms(e, sign=False).as_coeff_Mul()
                    den = denom(ex)
                    if den.func is log and den.args[0] == b:
                        return S.Exp1**(c * numer(ex))
                    elif den.is_Add:
                        s = sign(im(b))
                        if s.is_Number and s and den == \
                                log(-factor_terms(b, sign=False)) + s*S.ImaginaryUnit*S.Pi:
                            return S.Exp1**(c * numer(ex))

                obj = b._eval_power(e)
                if obj is not None:
                    return obj
        obj = Expr.__new__(cls, b, e)
        obj.is_commutative = (b.is_commutative and e.is_commutative)
        return obj
예제 #58
0
    def __eq__(self, other):
        try:
            other = _sympify(other)
        except SympifyError:
            return False    # sympy != other  -->  not ==
        if isinstance(other, NumberSymbol):
            if other.is_irrational: return False
            return other.__eq__(self)
        if isinstance(self, Number) and isinstance(other, FunctionClass):
            return False
        if other.is_comparable and not isinstance(other, Rational): other = other.evalf()
        if isinstance(other, Number):
            if isinstance(other, Real):
                return bool(mlib.mpf_eq(self._as_mpf_val(other._prec), other._mpf_))
            return bool(self.p==other.p and self.q==other.q)

        return False    # Rational != non-Number
예제 #59
0
 def __lt__(self, other):
     try:
         other = _sympify(other)
     except SympifyError:
         return False    # sympy > other  --> not <
     if self is other: return False
     if isinstance(other, Number):
         approx = self.approximation_interval(other.__class__)
         if approx is not None:
             l,u = approx
             if other < l: return False
             if other > u: return True
         return self.evalf()<other
     if other.is_comparable:
         other = other.evalf()
         return self.evalf()<other
     return Basic.__lt__(self, other)