def _simplify(_expr_str): print("đề =",_expr_str) if _expr_str.__len__() > 0: try: _expr = parse_expr(_expr_str) except SyntaxError: return {'success': False, 'msg': "Cú pháp không hợp lệ"} print("đề đã chuyển đổi = ",_expr) min_ex, rules, expr_list = logic_simplify.logic_simplify(_expr) print("min_ex :", min_ex) print("rules :", rules) print("list :", expr_list) steps = [] for ex, r in zip(expr_list, rules): step_detail = { "expr": pretty(ex), "rule": r } steps.append(step_detail) print("step :",steps) answer = { "expr": pretty(_expr), "steps": steps, "min": pretty(min_ex) } return {'success': True, "type": "2", 'answer': answer} return {'success': False, 'msg': "Cú pháp không hợp lệ"}
def test_pretty_unicode_defaults(): use_unicode = pprint_use_unicode(True) assert pretty(Symbol('alpha')) == u'\u03b1' pprint_use_unicode(False) assert pretty(Symbol('alpha')) == 'alpha' pprint_use_unicode(use_unicode)
def _resolve_logic(question): def parse(text: str): tok = re.compile("{(.*)}( )*,( )*(.*)") results1 = tok.findall(text) if results1.__len__() > 0: _premises, _, _, con = results1[0] results2 = _premises.split(",") if results2.__len__() > 0: con = con.strip() pre = list(map(lambda x: x.strip(), results2)) return pre, con return None if question.__len__() < 1: return {'success': False, 'msg': "Cú pháp không hợp lệ"} parse_result = parse(question) if parse_result is None: return {'success': False, 'msg': "Cú pháp không hợp lệ"} premises, conclusion = parse_result if premises.__len__() < 1: return {'success': False, 'msg': "Cần cung cấp giả thuyết!"} if conclusion.__len__() < 1: return {'success': False, 'msg': "Cần cung cấp kết luận!"} _premise_exprs = list() try: _premise_exprs = list(map(lambda x: parse_expr(x), premises)) except SyntaxError: return {'success': False, 'msg': "Cú pháp của giả thuyết không hợp lệ"} try: _conclusion_expr = parse_expr(conclusion) except SyntaxError: return {'success': False, 'msg': "Cú pháp của kết luận không hợp lệ"} resolved, solution = LogicInference.resolve( _premise_exprs, _conclusion_expr) steps = [] for rule_name, rules, fact in solution: pretty_rules = list(map(lambda x: pretty(x), rules)) step_detail = { "rules": pretty_rules, "rule": rule_name, "fact": pretty(fact) } steps.append(step_detail) answer = { "steps": steps, "result": resolved } return {'success': True, "type": "4", 'answer': answer}
def test_pretty_derivatives(): # Simple f_1 = Derivative(log(x), x, evaluate=False) assert pretty(f_1) == 'd \n--(log(x))\ndx ' f_2 = Derivative(log(x), x, evaluate=False) + x assert pretty(f_2) == ' d \nx + --(log(x))\n dx ' # Multiple symbols f_3 = Derivative(log(x) + x**2, x, y, evaluate=False) assert pretty(f_3) == ' 2 \n d / 2 \\\n-----\\x + log(x)/\ndy dx ' f_4 = Derivative(2*x*y, y, x, evaluate=False) + x**2 assert pretty(f_4) == ' 2 \n 2 d \nx + -----(2*x*y)\n dx dy '
def test_pretty_functions(): # Simple assert pretty( (2*x + exp(x)) ) in [' x \ne + 2*x', ' x\n2*x + e '] assert pretty( sqrt(2) ) == ' ___\n\\/ 2 ' assert pretty( sqrt(2+pi) ) == ' ________\n\\/ 2 + pi ' assert pretty(abs(x)) == '|x|' assert pretty(abs(x/(x**2+1))) == '| x |\n|------|\n| 2|\n|1 + x |' # Univariate/Multivariate functions f = Function('f') assert pretty(f(x)) == 'f(x)' assert pretty(f(x, y)) == 'f(x, y)' assert pretty(f(x/(y+1), y)) == ' x \nf(-----, y)\n 1 + y ' # Nesting of square roots assert pretty( sqrt((sqrt(x+1))+1) ) == ' _______________\n / _______ \n\\/ 1 + \\/ 1 + x ' # Function powers assert pretty( sin(x)**2 ) == ' 2 \nsin (x)' # Conjugates a,b = map(Symbol, 'ab')
def _bool(question): if question.__len__() > 0: try: if parse(question) == 0: return {'success': False, 'msg': "Cú pháp không hợp lệ"} else: question, Karf, te_bao_lon, cac_phu_tu_karf,da_thuc_toi_tieu, de_quy = parse(question) except SyntaxError: print("ra try") return {'success': False, 'msg': "Cú pháp không hợp lệ"} answer = { "ques": pretty(question), "karf":Karf, "te_bao_lon": te_bao_lon, "cac_phu_tu_karf":cac_phu_tu_karf, "da_thuc_toi_tieu":da_thuc_toi_tieu, "de_quy":de_quy, } return {'success': True, "type": "1", "answer": answer} return {'success': False, 'msg': "Cú pháp không hợp lệ"}
def test_pretty_integrals(): # Simple f_1 = Integral(log(x), x) assert pretty(f_1) == ' / \n | \n | log(x) dx\n | \n/ ' f_2 = Integral(x**2, x) assert pretty(f_2) == ' / \n | \n | 2 \n | x dx\n | \n/ ' # Double nesting of pow f_3 = Integral(x**(2**x), x) assert pretty(f_3) == ' / \n | \n | / x\\ \n | \\2 / \n | x dx\n | \n/ ' # Definite integrals f_4 = Integral(x**2, (x,1,2)) assert pretty(f_4) == ' 2 \n / \n | \n | 2 \n | x dx\n | \n/ \n1 ' f_5 = Integral(x**2, (x,Rational(1,2),10)) assert pretty(f_5) == ' 10 \n / \n | \n | 2 \n | x dx\n | \n/ \n1/2 ' # Nested integrals f_6 = Integral(x**2*y**2, x,y) assert pretty(f_6) == ' / / \n | | \n | | 2 2 \n | | x *y dx dy\n | | \n/ / '
def do_math(args): return pretty(parse_expr(args), full_prec=False, use_unicode=False)
def upretty(expr): return pretty(expr, True)
def test_pretty_relational(): assert pretty(x == y) == 'x = y' assert pretty(x <= y) == 'x <= y' assert pretty(x > y) == 'y < x' assert pretty(x/(y+1) != y**2) == ' x 2\n----- != y \n1 + y '
def upretty(expr): return pretty(expr, use_unicode=True)
def test_pretty_basic(): # Simple numbers/symbols assert pretty( -Rational(1)/2 ) == '-1/2' assert pretty( -Rational(13)/22 ) == ' 13\n- --\n 22' assert pretty( oo ) == 'oo' # Powers assert pretty( (x**2) ) == ' 2\nx ' assert pretty( 1/x ) == '1\n-\nx' # Sums of terms assert pretty( (x**2 + x + 1)) == ' 2\n1 + x + x ' assert pretty( 1-x ) == '1 - x' assert pretty( 1-2*x ) == '1 - 2*x' assert pretty( 1-Rational(3,2)*y/x ) == ' 3*y\n1 - ---\n 2*x' # Multiplication assert pretty( x/y ) == 'x\n-\ny' assert pretty( -x/y ) == '-x\n--\ny ' assert pretty( (x+2)/y ) == '2 + x\n-----\n y ' assert pretty( (1+x)*y ) in ['(1 + x)*y', 'y*(1 + x)'] # Check for proper placement of negative sign assert pretty( -5*x/(x+10) ) == ' -5*x \n------\n10 + x' assert pretty( 1 - Rational(3,2)*(x+1) ) == ' 3*x\n-1/2 - ---\n 2 '
def test_pretty_unicode(): assert pretty( oo, True ) == u'\u221e' assert pretty( pi, True ) == u'\u03c0' assert pretty( pi+2*x, True ) == u'\u03c0 + 2*x' assert pretty( pi**2+exp(x), True ) == u' 2 x\n\u03c0 + \u212f ' assert pretty( x != y, True ) == u'x \u2260 y'
def _equivalent(question): def parse(text: str): tok = re.compile("{(.*),(.*)}") results = tok.findall(text) if results.__len__() > 0: ex_1, ex_2 = results[0] ex_1 = ex_1.strip() ex_2 = ex_2.strip() return ex_1, ex_2 return None if question.__len__() < 1: return {'success': False, 'msg': "Cú pháp không hợp lệ"} parse_result = parse(question) if parse_result is None: return {'success': False, 'msg': "Cú pháp không hợp lệ"} _expr_str_1, _expr_str_2 = parse_result if _expr_str_1.__len__() < 1 or _expr_str_2.__len__() < 1: return {'success': False, 'msg': "Cú pháp không hợp lệ"} try: _expr_1 = parse_expr(_expr_str_1) except SyntaxError: return {'success': False, 'msg': "Cú pháp không hợp lệ"} try: _expr_2 = parse_expr(_expr_str_2) except SyntaxError: return {'success': False, 'msg': "Cú pháp không hợp lệ"} result, step_1, step_2 = logic_simplify.equivalent(_expr_1, _expr_2) _ex1 = _expr_1 _ex2 = _expr_2 steps_1 = [] if step_1 is not None: _ex1, rules_1, expr_list_1 = step_1 for ex, r in zip(expr_list_1, rules_1): step_detail = { "expr": pretty(ex), "rule": r } steps_1.append(step_detail) steps_2 = [] if step_2 is not None: _ex2, rules_2, expr_list_2 = step_2 for ex, r in zip(expr_list_2, rules_2): step_detail = { "expr": pretty(ex), "rule": r } steps_2.append(step_detail) answer = { "expr_1": pretty(_ex1), "expr_2": pretty(_ex2), "steps_1": steps_1, "steps_2": steps_2, "result": result } return {'success': True, "type": "3", 'answer': answer}
def test_pretty_limits(): assert pretty( limit(x, x, oo, evaluate=False) ) == ' lim x\nx->oo ' assert pretty( limit(x**2, x, 0, evaluate=False) ) == ' 2\nlim x \nx->0 '