Example #1
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
Example #2
0
    def __new__(cls, expr, *symbols, **assumptions):
        expr = sympify(expr)
        if not symbols:
            return expr
        symbols = Derivative._symbolgen(*symbols)
        if expr.is_commutative:
            assumptions['commutative'] = True
        evaluate = assumptions.pop('evaluate', False)
        if not evaluate and not isinstance(expr, Derivative):
            symbols = list(symbols)
            if len(symbols) == 0:
                # We make a special case for 0th derivative, because there
                # is no good way to unambiguously print this.
                return expr
            obj = Expr.__new__(cls, expr, *symbols, **assumptions)
            return obj
        unevaluated_symbols = []
        for s in symbols:
            s = sympify(s)
            if not isinstance(s, C.Symbol):
                raise ValueError('Invalid literal: %s is not a valid variable' % s)
            obj = expr._eval_derivative(s)
            if obj is None:
                unevaluated_symbols.append(s)
            elif obj is S.Zero:
                return S.Zero
            else:
                expr = obj

        if not unevaluated_symbols:
            return expr
        return Expr.__new__(cls, expr, *unevaluated_symbols, **assumptions)
Example #3
0
    def __new__(cls, *args, **options):
        if len(args) == 0:
            return cls.identity

        args = map(_sympify, args)
        if len(args) == 1:
            return args[0]

        if not options.pop('evaluate', True):
            obj = Expr.__new__(cls, *args)
            obj.is_commutative = all(a.is_commutative for a in args)
            return obj

        c_part, nc_part, order_symbols = cls.flatten(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))
            obj.is_commutative = not nc_part

        if order_symbols is not None:
            obj = C.Order(obj, *order_symbols)
        return obj
Example #4
0
 def row_select(self, vec):
   """
   Boolean column select lookup
   :param vec: An H2OVec.
   :return: A new H2OVec.
   """
   e = Expr("[", self, vec)
   j = h2o.frame(e.eager())
   e.set_len(j['frames'][0]['rows'])
   return H2OVec(self._name, e)
Example #5
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
Example #6
0
File: power.py Project: 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
Example #7
0
 def __new__(cls, p, q = None):
     if q is None:
         if isinstance(p, basestring):
             p, q = _parse_rational(p)
         elif isinstance(p, Rational):
             return p
         else:
             return Integer(p)
     q = int(q)
     p = int(p)
     if q==0:
         if p==0:
             if _errdict["divide"]:
                 raise ValueError("Indeterminate 0/0")
             else:
                 return S.NaN
         if p<0: return S.NegativeInfinity
         return S.Infinity
     if q<0:
         q = -q
         p = -p
     n = igcd(abs(p), q)
     if n>1:
         p //= n
         q //= n
     if q==1: return Integer(p)
     if p==1 and q==2: return S.Half
     obj = Expr.__new__(cls)
     obj.p = p
     obj.q = q
     #obj._args = (p, q)
     return obj
Example #8
0
    def _eval_evalf(self, prec):
        # Lookup mpmath function based on name
        fname = self.func.__name__
        try:
            if not hasattr(mpmath, fname):
                from sympy.utilities.lambdify import MPMATH_TRANSLATIONS
                fname = MPMATH_TRANSLATIONS[fname]
            func = getattr(mpmath, fname)
        except (AttributeError, KeyError):
            try:
                return C.Real(self._imp_(*self.args), prec)
            except (AttributeError, TypeError):
                return

        # Convert all args to mpf or mpc
        try:
            args = [arg._to_mpmath(prec) for arg in self.args]
        except ValueError:
            return

        # Set mpmath precision and apply. Make sure precision is restored
        # afterwards
        orig = mpmath.mp.prec
        try:
            mpmath.mp.prec = prec
            v = func(*args)
        finally:
            mpmath.mp.prec = orig

        return Expr._from_mpmath(v, prec)
Example #9
0
 def matches(self, expr, repl_dict={}, evaluate=False):
     if self in repl_dict:
         if repl_dict[self] == expr:
             return repl_dict
     elif isinstance(expr, Derivative):
         if len(expr.variables) == len(self.variables):
             return Expr.matches(self, expr, repl_dict, evaluate)
Example #10
0
 def _new(cls, _mpf_, _prec):
     if _mpf_ == mlib.fzero:
         return S.Zero
     obj = Expr.__new__(cls)
     obj._mpf_ = _mpf_
     obj._prec = _prec
     return obj
Example #11
0
    def __new__(cls, expr, variables, point, **assumptions):
        if not ordered_iter(variables, Tuple):
            variables = [variables]
        variables = Tuple(*sympify(variables))

        if uniq(variables) != variables:
            repeated = repeated = [ v for v in set(variables)
                                    if list(variables).count(v) > 1 ]
            raise ValueError('cannot substitute expressions %s more than '
                             'once.' % repeated)

        if not ordered_iter(point, Tuple):
            point = [point]
        point = Tuple(*sympify(point))

        if len(point) != len(variables):
            raise ValueError('Number of point values must be the same as '
                             'the number of variables.')

        # it's necessary to use dummy variables internally
        new_variables = Tuple(*[ arg.as_dummy() if arg.is_Symbol else
            C.Dummy(str(arg)) for arg in variables ])
        expr = sympify(expr).subs(tuple(zip(variables, new_variables)))

        if expr.is_commutative:
            assumptions['commutative'] = True

        obj = Expr.__new__(cls, expr, new_variables, point, **assumptions)
        return obj
Example #12
0
 def __new_stage2__(cls, name, **assumptions):
     if not isinstance(name, basestring):
         raise TypeError("name should be a string, not %s" % repr(type(name)))
     obj = Expr.__new__(cls)
     obj.name = name
     obj._assumptions = StdFactKB(assumptions)
     return obj
Example #13
0
 def __new__(cls, p, q=None):
     if q is None:
         if isinstance(p, Rational):
            return p
         if isinstance(p, basestring):
             try:
                 # we might have a Real
                 neg_pow, digits, expt = decimal.Decimal(p).as_tuple()
                 p = [1, -1][neg_pow] * int("".join(str(x) for x in digits))
                 if expt > 0:
                     rv = Rational(p*Pow(10, expt), 1)
                 return Rational(p, Pow(10, -expt))
             except decimal.InvalidOperation:
                 import re
                 f = re.match(' *([-+]? *[0-9]+)( */ *)([0-9]+)', p)
                 if f:
                     p, _, q = f.groups()
                     return Rational(int(p), int(q))
                 else:
                     raise ValueError('invalid literal: %s' % p)
         elif not isinstance(p, Basic):
             return Rational(S(p))
         q = S.One
     if isinstance(q, Rational):
         p *= q.q
         q = q.p
     if isinstance(p, Rational):
         q *= p.q
         p = p.p
     p = int(p)
     q = int(q)
     if q == 0:
         if p == 0:
             if _errdict["divide"]:
                 raise ValueError("Indeterminate 0/0")
             else:
                 return S.NaN
         if p < 0:
             return S.NegativeInfinity
         return S.Infinity
     if q < 0:
         q = -q
         p = -p
     n = igcd(abs(p), q)
     if n > 1:
         p //= n
         q //= n
     if q == 1:
         return Integer(p)
     if p == 1 and q == 2:
         return S.Half
     obj = Expr.__new__(cls)
     obj.p = p
     obj.q = q
     #obj._args = (p, q)
     return obj
Example #14
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 Expr.__le__(self, other)
Example #15
0
  def __getitem__(self, i):
    """
    Column selection via integer, string(name)
    Column selection via slice returns a subset of the H2OFrame

    :param i: An int, str, slice, H2OVec, or list/tuple
    :return: An H2OVec, an H2OFrame, or scalar depending on the input slice.
    """
    if self._vecs is None or self._vecs == []:
      raise ValueError("Frame Removed")
    if isinstance(i, int):   return self._vecs[i]
    if isinstance(i, str):   return self._find(i)
    # Slice; return a Frame not a Vec
    if isinstance(i, slice): return H2OFrame(vecs=self._vecs[i])
    # Row selection from a boolean Vec
    if isinstance(i, H2OVec):
      self._len_check(i)
      return H2OFrame(vecs=[x.row_select(i) for x in self._vecs])

    # have a list/tuple of numbers or strings
    if isinstance(i, list) or (isinstance(i, tuple) and len(i) != 2):
      vecs = []
      for it in i:
        if isinstance(it, int):    vecs.append(self._vecs[it])
        elif isinstance(it, str):  vecs.append(self._find(it))
        else:                      raise NotImplementedError
      return H2OFrame(vecs=vecs)

    # multi-dimensional slicing via 2-tuple
    if isinstance(i, tuple):
      veckeys = [str(v._expr._data) for v in self._vecs]
      left = Expr(veckeys)
      rite = Expr((i[0], i[1]))
      res = Expr("[", left, rite, length=2)
      if not isinstance(i[0], int) or not isinstance(i[1], int): return res # possible big data
      # small data (single value)
      res.eager()
      if res.is_local(): return res._data
      j = h2o.frame(res._data) # data is remote
      return map(list, zip(*[c['data'] for c in j['frames'][0]['columns'][:]]))[0][0]

    raise NotImplementedError("Slicing by unknown type: "+str(type(i)))
Example #16
0
 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)
Example #17
0
    def _from_args(cls, args, is_commutative=None):
        """Create new instance with already-processed args"""
        if len(args) == 0:
            return cls.identity
        elif len(args) == 1:
            return args[0]

        obj = Expr.__new__(cls, *args)
        if is_commutative is None:
            is_commutative = all(a.is_commutative for a in args)
        obj.is_commutative = is_commutative
        return obj
Example #18
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
Example #19
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 Expr.__le__(self, other)
Example #20
0
    def __new__(cls, *args, **options):

        if len(args) == 0:
            return cls.identity

        args = map(_sympify, args)

        try:
            args = [a for a in args if a is not cls.identity]
        except AttributeError:
            pass

        if len(args) == 0:  # recheck after filtering
            return cls.identity

        if len(args) == 1:
            return args[0]

        if not options.pop("evaluate", True):
            obj = Expr.__new__(cls, *args)
            obj.is_commutative = all(a.is_commutative for a in args)
            return obj

        c_part, nc_part, order_symbols = cls.flatten(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))
            obj.is_commutative = not nc_part

        if order_symbols is not None:
            obj = C.Order(obj, *order_symbols)
        return obj
Example #21
0
    def __new__(cls, variables, expr):
        try:
            variables = Tuple(*variables)
        except TypeError:
            variables = Tuple(variables)
        if len(variables) == 1 and variables[0] == expr:
            return S.IdentityFunction

        #use dummy variables internally, just to be sure
        new_variables = [C.Dummy(arg.name) for arg in variables]
        expr = sympify(expr).subs(tuple(zip(variables, new_variables)))

        obj = Expr.__new__(cls, Tuple(*new_variables), expr)
        return obj
Example #22
0
    def eval(cls,*args):
        obj = Expr.__new__(cls, *args)
        #use dummy variables internally, just to be sure
        nargs = len(args)-1

        expression = args[nargs]
        funargs = [C.Symbol(arg.name, dummy=True) for arg in args[:nargs]]
        #probably could use something like foldl here
        for arg,funarg in zip(args[:nargs],funargs):
            expression = expression.subs(arg,funarg)
        funargs.append(expression)
        obj._args = tuple(funargs)

        return obj
Example #23
0
 def __new__(cls, *args, **assumptions):
     args = (sympify(arg) for arg in args)
     try:
         _args = frozenset(cls._new_args_filter(args))
     except ShortCircuit:
         return cls.zero
     if not _args:
         return cls.identity
     elif len(_args) == 1:
         return set(_args).pop()
     else:
         obj = Expr.__new__(cls, _args, **assumptions)
         obj._argset = _args
         return obj
Example #24
0
File: power.py Project: 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
Example #25
0
File: power.py Project: 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
Example #26
0
    def __new__(cls, i):
        try:
            return _intcache[i]
        except KeyError:
            # We only work with well-behaved integer types. This converts, for
            # example, numpy.int32 instances.
            ival = int(i)
            try:
                obj = _intcache[ival]
            except KeyError:
                obj = Expr.__new__(cls)
                obj.p = ival

            _intcache[i] = obj
            return obj
Example #27
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
Example #28
0
    def __new__(cls, i):
        try:
            return _intcache[i]
        except KeyError:
            # We only work with well-behaved integer types. This converts, for
            # example, numpy.int32 instances.
            ival = int(i)
            if ival == 0: obj = S.Zero
            elif ival == 1: obj = S.One
            elif ival == -1: obj = S.NegativeOne
            else:
                obj = Expr.__new__(cls)
                obj.p = ival

            _intcache[i] = obj
            return obj
Example #29
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 Expr.__lt__(self, other)
Example #30
0
    def doit_numerically(self, z0):
        """
        Evaluate the derivative at z numerically.

        When we can represent derivatives at a point, this should be folded
        into the normal evalf. For now, we need a special method.
        """
        from sympy import mpmath
        from sympy.core.expr import Expr
        if len(self.free_symbols) != 1 or len(self.variables) != 1:
            raise NotImplementedError('partials and higher order derivatives')
        z = list(self.free_symbols)[0]
        def eval(x):
            f0 = self.expr.subs(z, Expr._from_mpmath(x, prec=mpmath.mp.prec))
            f0 = f0.evalf(mlib.libmpf.prec_to_dps(mpmath.mp.prec))
            return f0._to_mpmath(mpmath.mp.prec)
        return Expr._from_mpmath(mpmath.diff(eval, z0._to_mpmath(mpmath.mp.prec)),
                                 mpmath.mp.prec)
Example #31
0
def isLikely(_init, _pred, _new):  # 判断循环的init,pred,new是否可能可以并行
    init = Expr(_init)
    init.parse()
    pred = Expr(_pred)
    pred.parse()
    new = Expr(_new)
    new.parse()
    if len(init.Array_Mdf) == 0 and len(init.Var_Mdf) == 1:
        if len(new.Var_Rdc) == 1 and (
            (new.Var_Rdc[0][1] == "++" or new.Var_Rdc[0][1] == "--") or
            (len(new.var) == 2 and new.fa[0][0] in "+-" and
             (new.var[0] is None or new.var[1] is None))):
            if len(pred.Var_Use) <= 2 and init.Var_Mdf[0] in pred.Var_Use:
                return True
    return False
Example #32
0
def parse_block(begin, end, outer_new, for_father, outer_end):  # 解析BLOCK,获取摘要
    global countfor
    whole = Expr("")
    (values, arrays) = car(begin.env)
    while 1:
        if begin.category != "for":
            whole.merge(begin.expression)
        if begin.category == "for":
            if for_father:
                for_father.son.append(begin)
            else:
                graph.begin.son.append(begin)
            tmp = Expr("")
            exp = begin.succ_inst[0]
            t = 0
            if exp.succ_inst[0] == begin.jump:
                t = 1
            q = 0
            if exp.pred_inst[0] == begin:
                q = 1
            new = exp.pred_inst[q]
            if len(new.pred_inst) > 1:
                tmp.interrupt = True
            for i in range(len(new.pred_inst)):
                if len(new.pred_inst[i].succ_inst) == 1:
                    tmp.merge(
                        parse_block(exp.succ_inst[t], new.pred_inst[i], new,
                                    begin, begin.jump))
            begin.isLikely = isLikely(begin.expression.s, exp.expression.s,
                                      new.expression.s)
            itr = None
            if begin.isLikely:
                itr = begin.expression.Var_Mdf[0]
            begin.expression.merge(tmp)
            if itr:
                while itr in begin.expression.Var_Mdf:
                    begin.expression.Var_Mdf.remove(itr)
                countfor += 1
                for i in range(len(begin.expression.Array_Mdf)):
                    for j in range(len(begin.expression.Array_Mdf[i][1])):
                        if begin.expression.Array_Mdf[i][1][j] == itr:
                            begin.expression.Array_Mdf[i][1][j] += str(
                                countfor) + "@Software"
                for i in range(len(begin.expression.Array_Use)):
                    for j in range(len(begin.expression.Array_Use[i][1])):
                        if begin.expression.Array_Use[i][1][j] == itr:
                            begin.expression.Array_Use[i][1][j] += str(
                                countfor) + "@Software"
            if itr in begin.jump.live.live:
                begin.isLikely = False
            add = isParallel(begin, begin.jump)
            if not (add is False):
                can_parallel.append([begin, add])
            whole.merge(begin.expression)
            begin = begin.jump
        elif begin.category == "if":
            whole.merge(
                parse_block(begin.succ_inst[0], begin.jump, outer_new,
                            for_father, outer_end))
            whole.merge(
                parse_block(begin.succ_inst[1], begin.jump, outer_new,
                            for_father, outer_end))
            begin = begin.jump
        elif begin.category == "while":
            if begin == end:
                break
            t = 0
            if begin.succ_inst[0] == begin.jump:
                t = 1
            whole.merge(
                parse_block(begin.succ_inst[t], begin, begin, for_father,
                            begin.jump))
            begin = begin.jump
        if begin == end:
            break
        if len(begin.succ_inst) > 2:
            print("Error!")
        elif len(begin.succ_inst) > 1:
            whole.interrupt = True
            if graph.end in begin.succ_inst:
                print("return")
                t = 1 - begin.succ_inst.index(graph.end)
                begin = begin.succ_inst[t]
            else:
                t = 0
                if begin.succ_inst[0].expression.s == "":
                    t = 1
                if begin.succ_inst[t].expression.s != "":
                    print("continue")
                    if begin.succ_inst[t] == outer_new:
                        t = 1 - t
                    begin = begin.succ_inst[t]
                else:
                    print("break")
                    begin = begin.succ_inst[t]

        else:
            begin = begin.succ_inst[0]
    for i in range(len(values)):
        tmp = []
        for j in range(len(whole.Var_Rdc)):
            if values[i] != whole.Var_Rdc[j][0]:
                tmp.append(whole.Var_Rdc[j])
        whole.Var_Rdc = tmp
        while values[i] in whole.Var_Use:
            whole.Var_Use.remove(values[i])
        while values[i] in whole.Var_Mdf:
            whole.Var_Mdf.remove(values[i])
    return whole
Example #33
0
 def as_coeff_mul(self, *deps):
     # -2 + 2 * a -> -1, 2-2*a
     if self.args[0].is_Rational and self.args[0].is_negative:
         return S.NegativeOne, (-self, )
     return Expr.as_coeff_mul(self, *deps)
Example #34
0
    def _new_rawargs(self, *args, **kwargs):
        """create new instance of own class with args exactly as provided by caller
           but returning the self class identity if args is empty.

           This is handy when we want to optimize things, e.g.

               >>> from sympy import Mul, symbols, S
               >>> from sympy.abc import x, y
               >>> e = Mul(3, x, y)
               >>> e.args
               (3, x, y)
               >>> Mul(*e.args[1:])
               x*y
               >>> e._new_rawargs(*e.args[1:])  # the same as above, but faster
               x*y

           Note: use this with caution. There is no checking of arguments at
           all. This is best used when you are rebuilding an Add or Mul after
           simply removing one or more terms. If modification which result,
           for example, in extra 1s being inserted (as when collecting an
           expression's numerators and denominators) they will not show up in
           the result but a Mul will be returned nonetheless:

               >>> m = (x*y)._new_rawargs(S.One, x); m
               x
               >>> m == x
               False
               >>> m.is_Mul
               True

           Another issue to be aware of is that the commutativity of the result
           is based on the commutativity of self. If you are rebuilding the
           terms that came from a commutative object then there will be no
           problem, but if self was non-commutative then what you are
           rebuilding may now be commutative.

           Although this routine tries to do as little as possible with the
           input, getting the commutativity right is important, so this level
           of safety is enforced: commutativity will always be recomputed if
           either a) self has no is_commutate attribute or b) self is
           non-commutative and kwarg `reeval=False` has not been passed.

           If you don't have an existing Add or Mul and need one quickly, try
           the following.

               >>> m = object.__new__(Mul)
               >>> m._new_rawargs(x, y)
               x*y

           Note that the commutativity is always computed in this case since
           m doesn't have an is_commutative attribute; reeval is ignored:

               >>> _.is_commutative
               True
               >>> hasattr(m, 'is_commutative')
               False
               >>> m._new_rawargs(x, y, reeval=False).is_commutative
               True

           It is possible to define the commutativity of m. If it's False then
           the new Mul's commutivity will be re-evaluated:

               >>> m.is_commutative = False
               >>> m._new_rawargs(x, y).is_commutative
               True

           But if reeval=False then a non-commutative self can pass along
           its non-commutativity to the result (but at least you have to *work*
           to get this wrong):

               >>> m._new_rawargs(x, y, reeval=False).is_commutative
               False

        """
        if len(args) > 1:
            obj = Expr.__new__(type(self),
                               *args)  # NB no assumptions for Add/Mul

            if (hasattr(self, 'is_commutative') and
                (self.is_commutative or not kwargs.pop('reeval', True))):
                obj.is_commutative = self.is_commutative
            else:
                obj.is_commutative = all(a.is_commutative for a in args)

        elif len(args) == 1:
            obj = args[0]
        else:
            obj = self.identity

        return obj
Example #35
0
 def __new_stage2__(cls, name, commutative=True, **assumptions):
     assert isinstance(name, str), repr(type(name))
     obj = Expr.__new__(cls, **assumptions)
     obj.is_commutative = commutative
     obj.name = name
     return obj
Example #36
0
    def __init__(self,
                 python_obj=None,
                 local_fname=None,
                 remote_fname=None,
                 vecs=None,
                 raw_fname=None):
        """
    Create a new H2OFrame object by passing a file path or a list of H2OVecs.

    If `remote_fname` is not None, then a REST call will be made to import the
    data specified at the location `remote_fname`.  This path is relative to the
    H2O cluster, NOT the local Python process

    If `local_fname` is not None, then the data is not imported into the H2O cluster
    at the time of object creation.

    If `python_obj` is not None, then an attempt to upload the python object to H2O
    will be made. A valid python object has type `list`, or `dict`.

    For more information on the structure of the input for the various native python
    data types ("native" meaning non-H2O), please see the general documentation for
    this object.

    :param python_obj: A "native" python object - list, dict, tuple.
    :param local_fname: A local path to a data source. Data is python-process-local.
    :param remote_fname: A remote path to a data source. Data is cluster-local.
    :param vecs: A list of H2OVec objects.
    :param raw_fname: A raw key resulting from an upload_file.
    :return: An instance of an H2OFrame object.
    """

        self.local_fname = local_fname
        self.remote_fname = remote_fname
        self._vecs = None

        if python_obj is not None:  # avoids the truth value of an array is ambiguous err
            self._upload_python_object(python_obj)
            return

        # Import the data into H2O cluster
        if remote_fname:
            rawkey = h2o.import_file(remote_fname)
            setup = h2o.parse_setup(rawkey)
            parse = h2o.parse(setup, H2OFrame.py_tmp_key())  # create a new key
            cols = parse['columnNames']
            rows = parse['rows']
            veckeys = parse['vecKeys']
            self._vecs = H2OVec.new_vecs(zip(cols, veckeys), rows)
            print "Imported", remote_fname, "into cluster with", rows, "rows and", len(
                cols), "cols"

        # Read data locally into python process
        elif local_fname:
            with open(local_fname, 'rb') as csvfile:
                self._vecs = []
                for name in csvfile.readline().split(','):
                    self._vecs.append(H2OVec(name.rstrip(), Expr([])))
                for row in csv.reader(csvfile):
                    for i, data in enumerate(row):
                        self._vecs[i].append(data)
            print "Imported", local_fname, "into local python process"

        # Construct from an array of Vecs already passed in
        elif vecs:
            vlen = len(vecs[0])
            for v in vecs:
                if not isinstance(v, H2OVec):
                    raise ValueError("Not a list of Vecs")
                if len(v) != vlen:
                    raise ValueError("Vecs not the same size: " + str(vlen) +
                                     " != " + str(len(v)))
            self._vecs = vecs

        elif raw_fname:
            self._handle_raw_fname(raw_fname, None)

        else:
            raise ValueError(
                "Frame made from CSV file or an array of Vecs only")
Example #37
0
 def new_vecs(vecs=None, rows=-1):
     if not vecs: return vecs
     return [
         H2OVec(str(col), Expr(op=veckey['name'], length=rows))
         for idx, (col, veckey) in enumerate(vecs)
     ]
Example #38
0
 def floor(self):
     """
 :return: A lazy Expr representing the Math.floor() of this H2OVec.
 """
     return H2OVec(self._name, Expr("floor", self._expr, None))
Example #39
0
 def mean(self):
     """
 :return: A lazy Expr representing the mean of this H2OVec.
 """
     return Expr("mean", self._expr, None, length=1)
Example #40
0
 def __new_stage2__(cls, name, **assumptions):
     assert isinstance(name, str), repr(type(name))
     obj = Expr.__new__(cls)
     obj.name = name
     obj._assumptions = StdFactKB(assumptions)
     return obj
Example #41
0
from expr import Expr
from copy import deepcopy

six_plus_nine = Expr('+', 6, 9)
six_times_nine = Expr('*', 6, 9)
compound1 = Expr('+', six_times_nine, six_plus_nine)
compound2 = Expr('*', six_times_nine, compound1)
compound3 = Expr('+', compound2, 3)


def test_equality():
    assert six_plus_nine == deepcopy(six_plus_nine)
    assert compound1 == deepcopy(compound1)
    assert compound2 == deepcopy(compound2)
    assert compound3 == deepcopy(compound3)


def test_basic():
    x = six_plus_nine
    y = six_times_nine
    assert six_plus_nine.eval() == 15
    assert six_times_nine.eval() == 54
    assert x == six_plus_nine
    assert y == six_times_nine


def test_recur():
    x = compound1
    y = compound2
    z = compound3
    assert compound1.eval() == 69
Example #42
0
    def __new__(cls, expr, *symbols, **assumptions):
        expr = sympify(expr)
        if not symbols:
            return expr

        # standardize symbols
        symbols = list(sympify(symbols))
        if not symbols[-1].is_Integer:
            symbols.append(S.One)
        symbol_count = []
        all_zero = True
        i = 0
        while i < len(symbols) - 1: # process up to final Integer
            s, count = symbols[i: i+2]
            iwas = i
            if s.is_Symbol:
                if count.is_Symbol:
                    count = 1
                    i += 1
                elif count.is_Integer:
                    count = int(count)
                    i += 2

            if i == iwas: # didn't get an update because of bad input
                raise ValueError('Derivative expects Symbol [, Integer] args but got %s, %s' % (s, count))

            symbol_count.append((s, count))
            if all_zero and not count == 0:
                all_zero = False

        # We make a special case for 0th derivative, because there
        # is no good way to unambiguously print this.
        if all_zero:
            return expr

        evaluate = assumptions.pop('evaluate', False)

        # look for a quick exit if there are symbols that are not in the free symbols
        if evaluate:
            if set(sc[0] for sc in symbol_count
                  ).difference(expr.free_symbols):
                return S.Zero

        # We make a generator so as to only generate a symbol when necessary.
        # If a high order of derivative is requested and the expr becomes 0
        # after a few differentiations, then we won't need the other symbols
        symbolgen = (s for s, count in symbol_count for i in xrange(count))

        if expr.is_commutative:
            assumptions['commutative'] = True

        if (not (hasattr(expr, '_eval_derivative') and
                 evaluate) and
            not isinstance(expr, Derivative)):
            symbols = list(symbolgen)
            obj = Expr.__new__(cls, expr, *symbols, **assumptions)
            return obj

        # compute the derivative now
        unevaluated_symbols = []
        for s in symbolgen:
            obj = expr._eval_derivative(s)
            if obj is None:
                unevaluated_symbols.append(s)
            elif obj is S.Zero:
                return S.Zero
            else:
                expr = obj

        if not unevaluated_symbols:
            return expr

        return Expr.__new__(cls, expr, *unevaluated_symbols, **assumptions)
Example #43
0
 def _evaluate(self, expr: ex.Expr) -> LoxValue:
     return expr.accept(self)
Example #44
0
 def evaluate(self, expr: e.Expr):
     return expr.accept(self)