Commit c6147911 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed ALL! import errors regarding scopes.

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