def _calc_sqrt(pattern_narr, signs, narr, rewrite_rules, output_tempalates): x = get_atom_number(rewrite_rules['x']) if x != None and x > 0 and x.is_integer(): m, n = sqrt_draw(x) if n == 1: rewrite_rules['c'] = gen_atom_number(m) return rewrite_by_alpha(output_tempalates[0], rewrite_rules), True elif m > 1: rewrite_rules['m'] = gen_atom_number(m) rewrite_rules['n'] = gen_atom_number(n) return rewrite_by_alpha(output_tempalates[1], rewrite_rules), True return narr, False
def _collapse_fraction(pattern_narr, signs, narr, rewrite_rules, output_tempalates): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) if b != 0: if a != None and b != None and not (a.is_integer() and b.is_integer()): rewrite_rules['c'] = gen_atom_number(a / b) return rewrite_by_alpha(output_tempalates[0], rewrite_rules), True elif b != None and not b.is_integer(): rewrite_rules['k'] = gen_atom_number(1 / b) rewrite_rules['x'] = rewrite_rules['a'] return rewrite_by_alpha(output_tempalates[1], rewrite_rules), True return narr, False
def _fraction_int_addition(pattern_narr, signs, narr, rewrite_rules, output_tempalate): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) c = get_atom_number(rewrite_rules['c']) if a != None and a.is_integer(): return rewrite_by_alpha(output_tempalate, rewrite_rules), True return narr, False
def _calc_abs(pattern_narr, signs, narr, rewrite_rules, output_tempalate): x = get_atom_number(rewrite_rules['x']) if x != None: c = abs(x) rewrite_rules['c'] = gen_atom_number(c) return rewrite_by_alpha(output_tempalate, rewrite_rules), True return narr, False
def _fraction_addition_same_denom(pattern_narr, signs, narr, rewrite_rules, output_tempalates): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) c = get_atom_number(rewrite_rules['c']) if a != None and b != None: if a.is_integer() and b.is_integer(): a = a * signs[1] b = b * signs[2] z = a + b if c != None and c.is_integer() and c != 0: if (z / c).is_integer(): rewrite_rules['z'] = gen_atom_number(z / c) return rewrite_by_alpha(output_tempalates[0], rewrite_rules), True rewrite_rules['z'] = gen_atom_number(z) return rewrite_by_alpha(output_tempalates[1], rewrite_rules), True return narr, False
def _simplify_fraction(pattern_narr, signs, narr, rewrite_rules, output_tempalates): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) if a != None and b != None and a.is_integer() and b.is_integer(): a, b = a, b gcd = Euclidean_gcd(a, b) if gcd != 0 and gcd != 1: a = a / gcd b = b / gcd if b == 1: rewrite_rules['c'] = gen_atom_number(a) return rewrite_by_alpha(output_tempalates[0], rewrite_rules), True elif b != 0: rewrite_rules['c'] = gen_atom_number(a) rewrite_rules['d'] = gen_atom_number(b) return rewrite_by_alpha(output_tempalates[1], rewrite_rules), True return narr, False
def _calc_mul(pattern_narr, signs, narr, rewrite_rules, output_tempalate): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) if a != None and b != None: c = a * b rewrite_rules['c'] = gen_atom_number(c) return rewrite_by_alpha(output_tempalate, rewrite_rules), True return narr, False
def _canonicalize(pattern_narr, signs, narr, rewrite_rules, output_tempalate): sign, Type = narr[0].get() if Type in expression.commutative_operators(): new_narr, is_applied = expression.canonicalize(narr) if is_applied: rewrite_rules['X'] = new_narr return rewrite_by_alpha(output_tempalate, rewrite_rules), True else: return new_narr, False return narr, False
def _collapse_fraction_add_float(pattern_narr, signs, narr, rewrite_rules, output_tempalate): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) c = get_atom_number(rewrite_rules['c']) if a != None and b != None and c != None: if b != 0 and not c.is_integer(): rewrite_rules['x'] = gen_atom_number(a / b) return rewrite_by_alpha(output_tempalate, rewrite_rules), True return narr, False
def _calc_pow(pattern_narr, signs, narr, rewrite_rules, output_tempalate): a = get_atom_number(rewrite_rules['a']) b = get_atom_number(rewrite_rules['b']) if a != None and b != None: try: c = a ** b if abs(c) > 2000: raise OverflowError rewrite_rules['c'] = gen_atom_number(c) return rewrite_by_alpha(output_tempalate, rewrite_rules), True except OverflowError: pass except ZeroDivisionError: pass return narr, False
def _exact_apply(self, pattern, narr, debug=False): # refuse to apply when rule is not animation compatible in animation mode. if self.animation_mode and pattern not in self.animation: return narr, False # apply pattern transformation to nested array pattern_narr = self.narrs[pattern] is_match, rewrite_rules = test_alpha_equiv(pattern_narr, narr, debug=False) if debug: # Example: # Axiom: (a+b)(a-b) => (a)^{2} - (b)^{2} # pattern => destination # narr: (-xy - z)(-xy + z) # rewrite_rules: # a -> -xy # b -> -z print() if False: print('pattern:', pattern_narr) print('subject:', narr) else: print('pattern:', expression.narr2tex(pattern_narr)) print('subject:', expression.narr2tex(narr)) rich.print('match:', is_match) if is_match: if debug: alpha_prettyprint(rewrite_rules[0]) dest = self.animation[ pattern] if self.animation_mode else self.rules[pattern] dest_narr = [self.narrs[d] for d in dest] if isinstance( dest, list) else self.narrs[dest] if debug: print('dest:', dest) call = self.dp[pattern] if call is not None: # dynamical axiom signs = self.signs[pattern] rewritten_narr, is_applied = call(pattern_narr, signs, narr, rewrite_rules[0], dest_narr) else: rewritten_narr, is_applied = rewrite_by_alpha( dest_narr, rewrite_rules[0]), True if debug: rich.print('applied:', is_applied, end=': ') if False: print(rewritten_narr) else: print(expression.narr2tex(rewritten_narr)) # if a rule with higher priority get applied, later ones are ignored if is_applied: # post processes rewritten_narr = self._fraction_cancel(rewritten_narr) return rewritten_narr, True return narr, False