示例#1
0
文件: rules.py 项目: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)

        
        if e.ty != expr.INTEGRAL:
            return e

        if expr.sympy_style(e.upper) <= expr.sympy_style(self.c) or \
        expr.sympy_style(e.lower) >= expr.sympy_style(self.c):
            raise AssertionError("Split region")

        return expr.Integral(e.var, e.lower, self.c, e.body) + expr.Integral(e.var, self.c, e.upper, e.body)
示例#2
0
文件: rules.py 项目: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)

        if e.ty != expr.INTEGRAL:
            return e

        rec = Linearity().eval

        if e.body.is_plus():
            return rec(expr.Integral(e.var, e.lower, e.upper, e.body.args[0])) + \
                   rec(expr.Integral(e.var, e.lower, e.upper, e.body.args[1]))
        elif e.body.is_uminus():
            return -rec(expr.Integral(e.var, e.lower, e.upper, e.body.args[0]))
        elif e.body.is_minus():
            return rec(expr.Integral(e.var, e.lower, e.upper, e.body.args[0])) - \
                   rec(expr.Integral(e.var, e.lower, e.upper, e.body.args[1]))
        elif e.body.is_times():
            factors = decompose_expr_factor(e.body)
            if factors[0].is_constant():
                return factors[0] * rec(expr.Integral(e.var, e.lower, e.upper, 
                            functools.reduce(lambda x, y: x * y, factors[2:], factors[1])))
            else:
                return e
        elif e.body.is_constant() and e.body != Const(1):
            return e.body * expr.Integral(e.var, e.lower, e.upper, Const(1))
        else:
            return e
示例#3
0
文件: rules.py 项目: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)

        if e.ty != expr.INTEGRAL:
            return e

        abs_expr = e.body.getAbs()
        if len(abs_expr) == 0:
            return e
        abs_expr = abs_expr[0]  # only consider the first absolute value

        g, s = abs_expr.args[0].ranges(e.var, e.lower, e.upper) # g: value in abs > 0, s: value in abs < 0
        new_integral = []
        for l, h in g:
            new_integral.append(expr.Integral(e.var, l, h, e.body.replace_trig(abs_expr, abs_expr.args[0])))
        for l, h in s:
            new_integral.append(expr.Integral(e.var, l, h, e.body.replace_trig(abs_expr, Op("-", abs_expr.args[0]))))
        return sum(new_integral[1:], new_integral[0])
示例#4
0
文件: rules.py 项目: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)

        subst_deriv = expr.deriv(self.var_name, self.var_subst) #dx = d(x(u)) = x'(u) *du
        new_e_body = e.body.replace_trig(expr.holpy_style(str(e.var)), self.var_subst) #replace all x with x(u)
        new_e_body = expr.Op("*", new_e_body, subst_deriv) # g(x) = g(x(u)) * x'(u)
        lower = solvers.solve(expr.sympy_style(self.var_subst - e.lower))[0]
        upper = solvers.solve(expr.sympy_style(self.var_subst - e.upper))[0]
        return expr.Integral(self.var_name, expr.holpy_style(lower), expr.holpy_style(upper), new_e_body)
示例#5
0
文件: rules.py 项目: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)

        if e.ty != expr.INTEGRAL:
            return e
        e.body = e.body.normalize()
        du = expr.deriv(e.var, self.u)
        dv = expr.deriv(e.var, self.v)
        udv = (self.u * dv).normalize()
        if udv == e.body:
            return expr.EvalAt(e.var, e.lower, e.upper, (self.u * self.v).normalize()) - \
                   expr.Integral(e.var, e.lower, e.upper, (self.v * du).normalize())
        else:
            print("%s != %s" % (str(udv), str(e.body)))
            raise NotImplementedError("%s != %s" % (str(udv), str(e.body)))
示例#6
0
文件: rules.py 项目: bzhan/holpy
 def eval(self, e):
     rule = self.rule
     if e.ty in (expr.VAR, expr.CONST):
         return rule.eval(e)
     elif e.ty == expr.OP:
         args = [self.eval(arg) for arg in e.args]
         return rule.eval(expr.Op(e.op, *args))
     elif e.ty == expr.FUN:
         args = [self.eval(arg) for arg in e.args]
         return rule.eval(expr.Fun(e.func_name, *args))
     elif e.ty == expr.DERIV:
         return rule.eval(expr.Deriv(e.var, self.eval(e.body)))
     elif e.ty == expr.INTEGRAL:
         return rule.eval(expr.Integral(e.var, self.eval(e.lower), self.eval(e.upper), self.eval(e.body)))
     elif e.ty == expr.EVAL_AT:
         return rule.eval(expr.EvalAt(e.var, self.eval(e.lower), self.eval(e.upper), self.eval(e.body)))
     elif e.ty == expr.LIMIT:
         return rule.eval(expr.Limit(e.var, e.lim, self.eval(e.body)))
     else:
         raise NotImplementedError
示例#7
0
文件: rules.py 项目: bzhan/holpy
 def gen_lim_expr(var, lim, lower, upper):
     return expr.Limit(new_var, lim, expr.Integral(e.var, lower, upper, e.body))
示例#8
0
文件: parser.py 项目: bzhan/holpy
 def integral_expr(self, var, lower, upper, body):
     return expr.Integral(str(var), lower, upper, body)