Ejemplo n.º 1
0
 def __new__(cls, lhs, rhs, rop=None, **assumptions):
     lhs = Basic.sympify(lhs)
     rhs = Basic.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
Ejemplo n.º 2
0
 def __new__(cls, a, b, **assumptions):
     a = Basic.sympify(a)
     b = Basic.sympify(b)
     if isinstance(b, Basic.Zero):
         return S.One
     if isinstance(b, Basic.One):
         return a
     obj = a._eval_power(b)
     if obj is None:
         obj = Basic.__new__(cls, a, b, **assumptions)
     return obj
Ejemplo n.º 3
0
 def __add__(self, other):
     other = Basic.sympify(other)
     if isinstance(other, NaN) or isinstance(self, NaN):
         return S.NaN
     if isinstance(other, Number):
         return Real(self.num + other._as_decimal())
     return Number.__add__(self, other)
Ejemplo n.º 4
0
 def __le__(self, other):
     other = Basic.sympify(other)
     if self is other: return True
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return self.evalf()<=other
     return RelMeths.__le__(self, other)
Ejemplo n.º 5
0
    def __new__(cls, expr, *symbols, **assumptions):
        expr = Basic.sympify(expr)
        if not symbols: return expr
        symbols = map(Basic.sympify, symbols)

        if not assumptions.get("evaluate", True):
            obj = Basic.__new__(cls, expr, *symbols)
            return obj

        for s in symbols:
            assert isinstance(s, Basic.Symbol),`s`
            if not expr.has(s):
                return Basic.Zero()

        unevaluated_symbols = []
        for s in symbols:
            obj = expr._eval_derivative(s)
            if obj is None:
                unevaluated_symbols.append(s)
            else:
                expr = obj

        if not unevaluated_symbols:
            return expr
        return Basic.__new__(cls, expr, *unevaluated_symbols)
Ejemplo n.º 6
0
    def matches(pattern, expr, repl_dict={}, evaluate=False):
        Basic.matches.__doc__
        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 = Basic.sympify(expr)
        b, e = expr.as_base_exp()

        # special case, pattern = 1 and expr.exp can match to 0
        if isinstance(expr, Basic.One):
            d = repl_dict.copy()
            d = pattern.exp.matches(Basic.Integer(0), 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
Ejemplo n.º 7
0
 def __le__(self, other):
     other = Basic.sympify(other)
     if isinstance(other, NumberSymbol):
         return other.__gt__(self)
     if other.is_comparable: other = other.evalf()
     if isinstance(other, Number):
         return bool(self._as_decimal()<=other._as_decimal())
     return RelMeths.__le__(self, other)
Ejemplo n.º 8
0
 def __ne__(self, other):
     other = Basic.sympify(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(self._as_decimal()!=other._as_decimal())
     return RelMeths.__ne__(self, other)
Ejemplo n.º 9
0
 def __mul__(self, other):
     other = Basic.sympify(other)
     if isinstance(other, NaN) or isinstance(self, NaN):
         return S.NaN
     if isinstance(other, Real):
         return Real(self._as_decimal() * other.num)
     if isinstance(other, Rational):
         return Rational(self.p * other.p, self.q * other.q)
     return Number.__mul__(self, other)
Ejemplo n.º 10
0
 def __new__(cls, name=None, exclude=None, **assumptions):
     assumptions["dummy"] = True
     if name is None:
         name = "W%s" % (Symbol.dummycount + 1)
     obj = Symbol.__new__(cls, name, **assumptions)
     if exclude is None:
         obj.exclude = None
     else:
         obj.exclude = [Basic.sympify(x) for x in exclude]
     return obj
Ejemplo n.º 11
0
 def __le__(self, other):
     other = Basic.sympify(other)
     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(self._as_decimal()<=other._as_decimal())
         return bool(self.p * other.q <= self.q * other.p)
     return RelMeths.__le__(self, other)
Ejemplo n.º 12
0
 def __eq__(self, other):
     other = Basic.sympify(other)
     if isinstance(other, NumberSymbol):
         if other.is_irrational: return False
         return other.__eq__(self)
     from sympy.core.function import FunctionClass
     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(self._as_decimal()==other._as_decimal())
         return bool(self.p==other.p and self.q==other.q)
     return RelMeths.__eq__(self, other)
Ejemplo n.º 13
0
 def __lt__(self, other):
     other = Basic.sympify(other)
     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 RelMeths.__lt__(self, other)
Ejemplo n.º 14
0
def diff(f, x, times = 1, evaluate=True):
    """Differentiate f with respect to x

    It's just a wrapper to unify .diff() and the Derivative class,
    it's interface is similar to that of integrate()

    see http://documents.wolfram.com/v5/Built-inFunctions/AlgebraicComputation/Calculus/D.html
    """
    f = Basic.sympify(f)
    if evaluate == True:
        for i in range(0,times):
            f = f.diff(x)
        return f
    else:
        return Derivative(f, x, evaluate=evaluate)
Ejemplo n.º 15
0
 def __new__(cls, expr, *args):
     expr = Basic.sympify(expr)
     args = tuple(map(Basic.sympify, args))
     #if isinstance(expr, Apply):
     #    if expr[:]==args:
     #        return expr.func
     dummy_args = []
     for a in args:
         if not isinstance(a, Basic.Symbol):
             raise TypeError("%s %s-th argument must be Symbol instance (got %r)" \
                             % (cls.__name__, len(dummy_args)+1,a))
         d = a.as_dummy()
         expr = expr.subs(a, d)
         dummy_args.append(d)
     obj = Basic.__new__(cls, expr, *dummy_args, **expr._assumptions)
     return obj
Ejemplo n.º 16
0
 def __add__(self, other):
     other = Basic.sympify(other)
     if isinstance(other, NaN) or isinstance(self, NaN):
         return S.NaN
     if isinstance(other, Real):
         return Real(self._as_decimal() + other.num)
     if isinstance(other, Rational):
         if self.is_unbounded:
             if other.is_bounded:
                 return self
             elif self==other:
                 return self
         else:
             if other.is_unbounded:
                 return other
         return Rational(self.p * other.q + self.q * other.p, self.q * other.q)
     return Number.__add__(self, other)
Ejemplo n.º 17
0
 def __new__(cls, *args, **assumptions):
     if len(args)==0:
         return cls.identity()
     if len(args)==1:
         return Basic.sympify(args[0])
     c_part, nc_part, lambda_args, order_symbols = cls.flatten(map(Basic.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:
         assumptions['commutative'] = not nc_part
         obj = Basic.__new__(cls, *(c_part + nc_part), **assumptions)
     if order_symbols is not None:
         obj = Basic.Order(obj, *order_symbols)
     if lambda_args is not None:
         obj = Basic.Lambda(obj, *lambda_args)
     return obj
Ejemplo n.º 18
0
 def matches(pattern, expr, repl_dict={}, evaluate=False):
     expr = Basic.sympify(expr)
     if pattern.is_commutative and expr.is_commutative:
         return AssocOp._matches_commutative(pattern, expr, repl_dict, evaluate)
     # todo for commutative parts, until then use the default matches method for non-commutative products
     return Basic.matches(pattern, expr, repl_dict, evaluate)
Ejemplo n.º 19
0
 def __rsub__(self, other):
     return Basic.sympify(other).__sub__(self)
Ejemplo n.º 20
0
 def taylor_term(self, n, x, *previous_terms): # of (1+x)**e
     if n<0: return S.Zero
     x = Basic.sympify(x)
     return Basic.Binomial(self.exp, n) * x**n
Ejemplo n.º 21
0
 def __rmul__(self, other):
     return Basic.sympify(other).__mul__(self)
Ejemplo n.º 22
0
 def __rpow__(self, other):
     return Basic.sympify(other).__pow__(self)
Ejemplo n.º 23
0
 def __div__(self, other):
     return self * (Basic.sympify(other) ** Basic.Integer(-1))
Ejemplo n.º 24
0
 def __rdiv__(self, other):
     return Basic.sympify(other).__div__(self)
Ejemplo n.º 25
0
 def __ne__(self, other):
     other = Basic.sympify(other)
     if self is other: return False
     if isinstance(other, Number) and self.is_irrational: return True
     return RelMeths.__ne__(self, other)
Ejemplo n.º 26
0
 def __sub__(self, other):
     return self + (-Basic.sympify(other))
Ejemplo n.º 27
0
 def __ge__(self, other):
     return Basic.sympify(other).__le__(self)
Ejemplo n.º 28
0
 def __mul__(self, other):
     other = Basic.sympify(other)
     if isinstance(other, Number):
         return Real(self.num * other._as_decimal())
     return Number.__mul__(self, other)
Ejemplo n.º 29
0
 def __new__(cls, start, end, **assumptions):
     start = Basic.sympify(start)
     end = Basic.sympify(end)
     return Basic.__new__(cls, start, end, **assumptions)
Ejemplo n.º 30
0
 def __radd__(self, other):
     return Basic.sympify(other).__add__(self)