Commit 7a9605a7 authored by Taddeus Kroes's avatar Taddeus Kroes

Commenced some chanches to fraction rules (unit tests are still failing).

parent 42662f45
......@@ -9,9 +9,9 @@ from .powers import match_add_exponents, match_subtract_exponents, \
from .numerics import match_add_numerics, match_divide_numerics, \
match_multiply_numerics, match_multiply_zero, match_multiply_one, \
match_raise_numerics
from .fractions import match_constant_division, match_add_constant_fractions, \
match_expand_and_add_fractions, match_multiply_fractions, \
match_divide_fractions, match_equal_fraction_parts
from .fractions import match_constant_division, match_add_fractions, \
match_multiply_fractions, match_divide_fractions, \
match_equal_fraction_parts
from .negation import match_negated_factor, match_negate_polynome, \
match_negated_division
from .sort import match_sort_multiplicants
......@@ -30,12 +30,11 @@ from src.rules.integrals import match_solve_indef, match_constant_integral, \
from src.rules.lineq import match_move_term
RULES = {
OP_ADD: [match_add_numerics, match_add_constant_fractions,
OP_ADD: [match_add_numerics, match_add_fractions,
match_combine_groups, match_add_quadrants,
match_add_logarithms],
OP_MUL: [match_multiply_numerics, match_expand, match_add_exponents,
match_expand_and_add_fractions, match_multiply_zero,
match_negated_factor, match_multiply_one,
match_multiply_zero, match_negated_factor, match_multiply_one,
match_sort_multiplicants, match_multiply_fractions,
match_factor_in_multiplicant],
OP_DIV: [match_subtract_exponents, match_divide_numerics,
......
This diff is collapsed.
from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_MUL, OP_DIV
from ..node import ExpressionNode as N, ExpressionLeaf as L, OP_MUL, OP_DIV, \
OP_ADD, OP_POW, OP_SQRT
def greatest_common_divisor(a, b):
......@@ -132,3 +133,23 @@ def divides(m, n):
Check if m | n (m divides n).
"""
return not divmod(n, m)[1]
def is_numeric_node(node):
"""
Check if a node is numeric.
"""
return node.is_numeric()
def evals_to_numeric(node):
"""
Check if a node will eventually evaluate to a numeric value, by checking if
all leaves are numeric and there are only operators that can be
considerered a constant or will evaluate to one (+, *, /, ^, sqrt).
"""
if node.is_leaf:
return node.is_numeric()
return node.op in (OP_ADD, OP_MUL, OP_DIV, OP_POW, OP_SQRT) \
and all(map(evals_to_numeric, node))
This diff is collapsed.
from src.rules import utils
from src.rules.utils import least_common_multiple, is_fraction, partition, \
find_variables, first_sorted_variable, find_variable, substitute, \
divides
divides, evals_to_numeric
from tests.rulestestcase import tree, RulesTestCase
......@@ -65,3 +65,13 @@ class TestRulesUtils(RulesTestCase):
self.assertTrue(divides(7, 21))
self.assertFalse(divides(4, 2))
self.assertFalse(divides(2, 3))
def test_evals_to_numeric(self):
self.assertTrue(evals_to_numeric(tree('1')))
self.assertFalse(evals_to_numeric(tree('a')))
self.assertTrue(evals_to_numeric(tree('1 + 2')))
self.assertFalse(evals_to_numeric(tree('1 + a')))
self.assertTrue(evals_to_numeric(tree('1 + 2 / 2 * 9')))
self.assertFalse(evals_to_numeric(tree('int 1')))
self.assertFalse(evals_to_numeric(tree('int a')))
self.assertTrue(evals_to_numeric(tree('sqrt 1')))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment