Commit c6147911 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed ALL! import errors regarding scopes.

parent 9fee0791
from itertools import product, combinations
from .utils import nary_node
from ..node import OP_ADD, OP_MUL, OP_NEG
from ..node import Scope, OP_ADD, OP_MUL, OP_NEG
from ..possibilities import Possibility as P, MESSAGES
from ..translate import _
......@@ -18,7 +17,7 @@ def match_expand(node):
leaves = []
additions = []
for n in node.get_scope():
for n in Scope(node):
if n.is_leaf() or n.is_op(OP_NEG) and n[0].is_leaf():
leaves.append(n)
elif n.op == OP_ADD:
......@@ -43,15 +42,15 @@ def expand_single(root, args):
"""
a, bc = args
b, c = bc
scope = root.get_scope()
scope = Scope(root)
# Replace 'a' with the new expression
scope[scope.index(a)] = a * b + a * c
scope.remove(a, a * b + a * c)
# Remove the addition
scope.remove(bc)
return nary_node('*', scope)
return scope.as_nary_node()
MESSAGES[expand_single] = _('Expand {1}({2}) to {1}({2[0]}) + {1}({2[1]}).')
......@@ -64,15 +63,15 @@ def expand_double(root, args):
(a + b) * (c + d) -> ac + ad + bc + bd
"""
(a, b), (c, d) = ab, cd = args
scope = root.get_scope()
scope = Scope(root)
# Replace 'b + c' with the new expression
scope[scope.index(ab)] = a * c + a * d + b * c + b * d
# Replace 'a + b' with the new expression
scope.remove(ab, a * c + a * d + b * c + b * d)
# Remove the right addition
scope.remove(cd)
return nary_node('*', scope)
return scope.as_nary_node()
MESSAGES[expand_double] = _('Expand ({1})({2}) to {1[0]}{2[0]} + {1[0]}{2[1]}'
......
from itertools import combinations
from .utils import nary_node, least_common_multiple
from ..node import ExpressionLeaf as L, OP_DIV, OP_ADD, OP_MUL, OP_NEG
from .utils import least_common_multiple
from ..node import ExpressionLeaf as L, Scope, OP_DIV, OP_ADD, OP_MUL, OP_NEG
from ..possibilities import Possibility as P, MESSAGES
from ..translate import _
......@@ -84,7 +84,7 @@ def match_add_constant_fractions(node):
return node.is_op(OP_DIV) or \
(node.is_op(OP_NEG) and node[0].is_op(OP_DIV))
fractions = filter(is_division, node.get_scope())
fractions = filter(is_division, Scope(node))
for a, b in combinations(fractions, 2):
if a.is_op(OP_NEG):
......@@ -117,7 +117,7 @@ def equalize_denominators(root, args):
"""
denom = args[2]
scope = root.get_scope()
scope = Scope(root)
for fraction in args[:2]:
n, d = fraction[0] if fraction.is_op(OP_NEG) else fraction
......@@ -127,11 +127,11 @@ def equalize_denominators(root, args):
n = L(n.value * mult) if n.is_numeric() else L(mult) * n
if fraction.is_op(OP_NEG):
scope[scope.index(fraction)] = -(n / L(d.value * mult))
scope.remove(fraction, -(n / L(d.value * mult)))
else:
scope[scope.index(fraction)] = n / L(d.value * mult)
scope.remove(fraction, n / L(d.value * mult))
return nary_node('+', scope)
return scope.as_nary_node()
MESSAGES[equalize_denominators] = _('Equalize the denominators of division'
......@@ -157,17 +157,15 @@ def add_nominators(root, args):
else:
c = cb[0]
substitution = (a + c) / b
scope = root.get_scope()
scope = Scope(root)
# Replace the left node with the new expression
scope[scope.index(ab)] = substitution
scope.remove(ab, (a + c) / b)
# Remove the right node
scope.remove(cb)
return nary_node('+', scope)
return scope.as_nary_node()
# TODO: convert this to a lambda. Example: 22 / 77 - 28 / 77. the "-" is above
......
from itertools import combinations
from ..node import OP_ADD, OP_MUL, ExpressionNode as Node, \
ExpressionLeaf as Leaf
from ..node import ExpressionNode as Node, ExpressionLeaf as Leaf, Scope, \
OP_ADD, OP_MUL
from ..possibilities import Possibility as P, MESSAGES
from ..translate import _
from .utils import nary_node
def match_combine_groups(node):
......@@ -25,13 +24,13 @@ def match_combine_groups(node):
p = []
groups = []
for n in node.get_scope():
for n in Scope(node):
groups.append((1, n, n))
# Each number multiplication yields a group, multiple occurences of
# the same group can be replaced by a single one
if n.is_op(OP_MUL):
scope = n.get_scope()
scope = Scope(n)
l = len(scope)
for i, sub_node in enumerate(scope):
......@@ -52,18 +51,18 @@ def match_combine_groups(node):
def combine_groups(root, args):
c0, g0, n0, c1, g1, n1 = args
scope = root.get_scope()
scope = Scope(root)
if not isinstance(c0, Leaf):
c0 = Leaf(c0)
# Replace the left node with the new expression
scope[scope.index(n0)] = (c0 + c1) * g0
scope.remove(n0, (c0 + c1) * g0)
# Remove the right node
scope.remove(n1)
return nary_node('+', scope)
return scope.as_nary_node()
MESSAGES[combine_groups] = \
......
from itertools import combinations
from .utils import nary_node
from ..node import ExpressionLeaf as Leaf, OP_DIV, OP_MUL, OP_NEG
from ..node import ExpressionLeaf as Leaf, Scope, nary_node, OP_DIV, OP_MUL, \
OP_NEG
from ..possibilities import Possibility as P, MESSAGES
from ..translate import _
......@@ -28,15 +28,15 @@ def add_numerics(root, args):
else:
c1 = c1.value
scope = root.get_scope()
scope = Scope(root)
# Replace the left node with the new expression
scope[scope.index(n0)] = Leaf(c0 + c1)
scope.remove(n0, Leaf(c0 + c1))
# Remove the right node
scope.remove(n1)
return nary_node('+', scope)
return scope.as_nary_node()
MESSAGES[add_numerics] = _('Combine the constants {1} and {2}, which'
......@@ -119,7 +119,7 @@ def match_multiply_numerics(node):
p = []
numerics = []
for n in node.get_scope():
for n in Scope(node):
if n.is_numeric():
numerics.append((n, n.value))
elif n.is_op(OP_NEG) and n[0].is_numeric():
......@@ -147,7 +147,7 @@ def multiply_numerics(root, args):
else:
substitution = -Leaf(-value)
for n in root.get_scope():
for n in Scope(root):
if hash(n) == hash(n0):
# Replace the left node with the new expression
scope.append(substitution)
......
from itertools import combinations
from ..node import OP_ADD, OP_NEG
from ..node import Scope, OP_ADD, OP_NEG
from ..possibilities import Possibility as P, MESSAGES
from .utils import nary_node
from .numerics import add_numerics
......@@ -32,7 +31,7 @@ def match_combine_polynomes(node, verbose=False):
if verbose: # pragma: nocover
print 'match combine factors:', node
for n in node.get_scope():
for n in Scope(node):
polynome = n.extract_polynome_properties()
if verbose: # pragma: nocover
......@@ -84,16 +83,14 @@ def combine_polynomes(root, args):
else:
power = r ** e
# replacement: (c0 + c1) * a ^ b
# a, b and c are from 'left', d is from 'right'.
replacement = (c0 + c1) * power
scope = root.get_scope()
scope = Scope(root)
# Replace the left node with the new expression
scope[scope.index(n0)] = replacement
# Replace the left node with the new expression:
# (c0 + c1) * a ^ b
# a, b and c are from 'left', d is from 'right'.
scope.remove(n0, (c0 + c1) * power)
# Remove the right node
scope.remove(n1)
return nary_node('+', scope)
return scope.as_nary_node()
from itertools import combinations
from ..node import ExpressionNode as N, ExpressionLeaf as L, \
from ..node import ExpressionNode as N, ExpressionLeaf as L, Scope, \
OP_NEG, OP_MUL, OP_DIV, OP_POW
from ..possibilities import Possibility as P, MESSAGES
from .utils import nary_node
from ..translate import _
......@@ -16,7 +15,7 @@ def match_add_exponents(node):
p = []
powers = {}
for n in node.get_scope():
for n in Scope(node):
if n.is_op(OP_POW):
# Order powers by their roots, e.g. a^p and a^q are put in the same
# list because of the mutual 'a'
......@@ -86,7 +85,7 @@ def match_duplicate_exponent(node):
left, right = node
if left.is_op(OP_MUL):
return [P(node, duplicate_exponent, (left.get_scope(), right))]
return [P(node, duplicate_exponent, (Scope(left), right))]
return []
......@@ -127,15 +126,15 @@ def add_exponents(root, args):
n0, n1 = args
a, p = n0
q = n1[1]
scope = root.get_scope()
scope = Scope(root)
# Replace the left node with the new expression
scope[scope.index(n0)] = a ** (p + q)
scope.remove(n0, a ** (p + q))
# Remove the right node
scope.remove(n1)
return nary_node('*', scope)
return scope.as_nary_node()
MESSAGES[add_exponents] = _('Add the exponents of {1} and {2}, which'
......
from ..node import ExpressionNode as Node
def nary_node(operator, scope):
"""
Create a binary expression tree for an n-ary operator. Takes the operator
and a list of expression nodes as arguments.
"""
return scope[0] if len(scope) == 1 \
else Node(operator, nary_node(operator, scope[:-1]), scope[-1])
def gcd(a, b):
"""
Return greatest common divisor using Euclid's Algorithm.
......
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