def test_wilds_in_wilds(): from sympy import MatrixSymbol, MatMul A = MatrixSymbol('A', n, m) B = MatrixSymbol('B', m, k) pattern = patternify(A*B, 'A', n, m, B) # note that m is in B as well assert deconstruct(pattern) == Compound(MatMul, (Compound(MatrixSymbol, (Variable('A'), Variable(n), Variable(m))), Variable(B)))
def test_non_frankenAdds(): # the is_commutative property used to fail because of Basic.__new__ # This caused is_commutative and str calls to fail expr = x+y*2 rebuilt = construct(deconstruct(expr)) # Ensure that we can run these commands without causing an error str(rebuilt) rebuilt.is_commutative
def test_unify_iter(): expr = Add(1, 2, 3, evaluate=False) a, b, c = map(Symbol, 'abc') pattern = Add(a, c, evaluate=False) assert is_associative(deconstruct(pattern)) assert is_commutative(deconstruct(pattern)) result = list(unify(expr, pattern, {}, (a, c))) expected = [{a: 1, c: Add(2, 3, evaluate=False)}, {a: 1, c: Add(3, 2, evaluate=False)}, {a: 2, c: Add(1, 3, evaluate=False)}, {a: 2, c: Add(3, 1, evaluate=False)}, {a: 3, c: Add(1, 2, evaluate=False)}, {a: 3, c: Add(2, 1, evaluate=False)}, {a: Add(1, 2, evaluate=False), c: 3}, {a: Add(2, 1, evaluate=False), c: 3}, {a: Add(1, 3, evaluate=False), c: 2}, {a: Add(3, 1, evaluate=False), c: 2}, {a: Add(2, 3, evaluate=False), c: 1}, {a: Add(3, 2, evaluate=False), c: 1}] assert iterdicteq(result, expected)
def test_deconstruct(): expr = Basic(1, 2, 3) expected = Compound(Basic, (1, 2, 3)) assert deconstruct(expr) == expected assert deconstruct(1) == 1 assert deconstruct(x) == x assert deconstruct(x, variables=(x,)) == Variable(x) assert deconstruct(Add(1, x, evaluate=False)) == Compound(Add, (1, x)) assert deconstruct(Add(1, x, evaluate=False), variables=(x,)) == \ Compound(Add, (1, Variable(x)))
def test_nested(): expr = Basic(1, Basic(2), 3) cmpd = Compound(Basic, (1, Compound(Basic, (2,)), 3)) assert deconstruct(expr) == cmpd assert construct(cmpd) == expr
def test_is_commutative(): assert is_commutative(deconstruct(x+y)) assert is_commutative(deconstruct(x*y)) assert not is_commutative(deconstruct(x**y))
def test_patternify(): assert deconstruct(patternify(x + y, x)) in ( Compound(Add, (Variable(x), y)), Compound(Add, (y, Variable(x)))) pattern = patternify(x**2 + y**2, x) assert list(unify(pattern, w**2 + y**2, {})) == [{x: w}]
def test_deconstruct(): expr = Basic(1, 2, 3) expected = Compound(Basic, (1, 2, 3)) assert deconstruct(expr) == expected
def test_nested(): expr = Basic(1, Basic(2), 3) cmpd = Compound(Basic, (1, Compound(Basic, (2, )), 3)) assert deconstruct(expr) == cmpd assert construct(cmpd) == expr
def test_patternify(): assert deconstruct(patternify(x + y, x)) in (Compound(Add, (Variable(x), y)), Compound(Add, (y, Variable(x)))) pattern = patternify(x**2 + y**2, x) assert list(unify(pattern, w**2 + y**2, {})) == [{x: w}]