예제 #1
0
파일: rules.py 프로젝트: bzhan/holpy
 def get_zero_point(self, e):
     abs_expr = e.body.getAbs()
     zero_point = []
     for a in abs_expr:
         arg = a.args[0]
         zeros = solveset(expr.sympy_style(arg), expr.sympy_style(e.var), Interval(sympy_style(e.lower), sympy_style(e.upper), left_open = True, right_open = True))
         zero_point += zeros
     return holpy_style(zero_point[0])
예제 #2
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)
예제 #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

        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)
예제 #4
0
파일: rules.py 프로젝트: bzhan/holpy
 def check_zero_point(self, e):
     integrals = e.separate_integral()
     if not integrals:
         return False
     abs_info = []
     for i, j in integrals:
         abs_expr = i.getAbs()
         abs_info += [(a, i) for a in abs_expr]
     zero_point = []
     for a, i in abs_info:
         arg = a.args[0]
         zeros = solveset(expr.sympy_style(arg), expr.sympy_style(i.var), Interval(sympy_style(i.lower), sympy_style(i.upper), left_open = True, right_open = True))
         zero_point += zeros
     return len(zero_point) > 0
예제 #5
0
파일: rules.py 프로젝트: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)

        rule_fun, _ = expr.trigFun[self.rule_name]
        sympy_result = rule_fun(expr.sympy_style(e))
        result = expr.holpy_style(sympy_result)
        return result
예제 #6
0
파일: rules.py 프로젝트: bzhan/holpy
    def eval(self, e):
        if e.ty == INTEGRAL:
            raise NotImplementedError
        if isinstance(e, str):
            e = parser.parse_expr(e)

        if expand_multinomial(expr.sympy_style(self.new_expr.normalize()).simplify()) != \
           expand_multinomial(expr.sympy_style(e.normalize()).simplify()):
            raise AssertionError("Rewriting by equation failed")

        # if self.denom is None:
        #     if self.new_expr.normalize() != e.normalize():
        #         raise AssertionError("Rewriting by equation failed")
        # else:
        #     if (self.new_expr * self.denom).normalize() != (e * self.denom).normalize():
        #         raise AssertionError("Rewriting by equation failed")
        return self.new_expr
예제 #7
0
파일: rules.py 프로젝트: bzhan/holpy
    def eval(self, e):
        if isinstance(e, str):
            e = parser.parse_expr(e)


        if e.ty == OP and e.op != "/" and not (e.ty == OP and e.op == "*" and e.args[1].ty == OP and e.args[1].op == "^"\
            and (e.args[1].args[1].ty == OP and len(e.args[1].args[1]) == 1 or e.args[1].args[1].ty == CONST and e.args[1].args[1].val < 0)):
            return e
            
        result = apart(expr.sympy_style(e))
        return parser.parse_expr(str(result).replace("**","^"))
예제 #8
0
파일: rules.py 프로젝트: bzhan/holpy
    def eval(self, e):
        """
        Parameters:
        e: Expr, the integral on which to perform substitution.

        Returns:
        The new integral e', and stores in self.f the parameter used to
        specify the substitution.

        """
        if isinstance(e, str):
            e = parser.parse_expr(e)


        var_name = parser.parse_expr(self.var_name)
        var_subst = self.var_subst
        dfx = expr.deriv(e.var, var_subst)
        body = holpy_style(sympy_style(e.body/dfx))
        body_subst = body.replace_trig(var_subst, var_name)
        if body_subst == body:
            body_subst = body.replace_trig(var_subst, var_name)
        lower = var_subst.subst(e.var, e.lower).normalize()
        upper = var_subst.subst(e.var, e.upper).normalize()
        if parser.parse_expr(e.var) not in body_subst.findVar():
            # Substitution is able to clear all x in original integrand
            # print('Substitution: case 1')
            self.f = body_subst
            if sympy_style(lower) <= sympy_style(upper):
                return Integral(self.var_name, lower, upper, body_subst).normalize()
            else:
                return Integral(self.var_name, upper, lower, Op("-", body_subst)).normalize()
        else:
            # Substitution is unable to clear x, need to solve for x
            # print('Substitution: case 2')
            gu = solvers.solve(expr.sympy_style(var_subst - var_name), expr.sympy_style(e.var))
            if gu == []:  # sympy can't solve the equation
                return e
            gu = gu[-1] if isinstance(gu, list) else gu
            gu = expr.holpy_style(gu)
            c = e.body.replace_trig(parser.parse_expr(e.var), gu)
            new_problem_body = (e.body.replace_trig(parser.parse_expr(e.var), gu)*expr.deriv(str(var_name), gu))
            lower = holpy_style(sympy_style(var_subst).subs(sympy_style(e.var), sympy_style(e.lower)))
            upper = holpy_style(sympy_style(var_subst).subs(sympy_style(e.var), sympy_style(e.upper)))
            self.f = new_problem_body
            if sympy_style(lower) < sympy_style(upper):
                return Integral(self.var_name, lower, upper, new_problem_body).normalize()
            else:
                return Integral(self.var_name, upper, lower, Op("-", new_problem_body)).normalize()