I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,74 @@
from sympy.unify.rewrite import rewriterule
from sympy.core.basic import Basic
from sympy.core.singleton import S
from sympy.core.symbol import Symbol
from sympy.functions.elementary.trigonometric import sin
from sympy.abc import x, y
from sympy.strategies.rl import rebuild
from sympy.assumptions import Q
p, q = Symbol('p'), Symbol('q')
def test_simple():
rl = rewriterule(Basic(p, S(1)), Basic(p, S(2)), variables=(p,))
assert list(rl(Basic(S(3), S(1)))) == [Basic(S(3), S(2))]
p1 = p**2
p2 = p**3
rl = rewriterule(p1, p2, variables=(p,))
expr = x**2
assert list(rl(expr)) == [x**3]
def test_simple_variables():
rl = rewriterule(Basic(x, S(1)), Basic(x, S(2)), variables=(x,))
assert list(rl(Basic(S(3), S(1)))) == [Basic(S(3), S(2))]
rl = rewriterule(x**2, x**3, variables=(x,))
assert list(rl(y**2)) == [y**3]
def test_moderate():
p1 = p**2 + q**3
p2 = (p*q)**4
rl = rewriterule(p1, p2, (p, q))
expr = x**2 + y**3
assert list(rl(expr)) == [(x*y)**4]
def test_sincos():
p1 = sin(p)**2 + sin(p)**2
p2 = 1
rl = rewriterule(p1, p2, (p, q))
assert list(rl(sin(x)**2 + sin(x)**2)) == [1]
assert list(rl(sin(y)**2 + sin(y)**2)) == [1]
def test_Exprs_ok():
rl = rewriterule(p+q, q+p, (p, q))
next(rl(x+y)).is_commutative
str(next(rl(x+y)))
def test_condition_simple():
rl = rewriterule(x, x+1, [x], lambda x: x < 10)
assert not list(rl(S(15)))
assert rebuild(next(rl(S(5)))) == 6
def test_condition_multiple():
rl = rewriterule(x + y, x**y, [x,y], lambda x, y: x.is_integer)
a = Symbol('a')
b = Symbol('b', integer=True)
expr = a + b
assert list(rl(expr)) == [b**a]
c = Symbol('c', integer=True)
d = Symbol('d', integer=True)
assert set(rl(c + d)) == {c**d, d**c}
def test_assumptions():
rl = rewriterule(x + y, x**y, [x, y], assume=Q.integer(x))
a, b = map(Symbol, 'ab')
expr = a + b
assert list(rl(expr, Q.integer(b))) == [b**a]

View File

@ -0,0 +1,162 @@
from sympy.core.add import Add
from sympy.core.basic import Basic
from sympy.core.containers import Tuple
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.logic.boolalg import And
from sympy.core.symbol import Str
from sympy.unify.core import Compound, Variable
from sympy.unify.usympy import (deconstruct, construct, unify, is_associative,
is_commutative)
from sympy.abc import x, y, z, n
def test_deconstruct():
expr = Basic(S(1), S(2), S(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_construct():
expr = Compound(Basic, (S(1), S(2), S(3)))
expected = Basic(S(1), S(2), S(3))
assert construct(expr) == expected
def test_nested():
expr = Basic(S(1), Basic(S(2)), S(3))
cmpd = Compound(Basic, (S(1), Compound(Basic, Tuple(2)), S(3)))
assert deconstruct(expr) == cmpd
assert construct(cmpd) == expr
def test_unify():
expr = Basic(S(1), S(2), S(3))
a, b, c = map(Symbol, 'abc')
pattern = Basic(a, b, c)
assert list(unify(expr, pattern, {}, (a, b, c))) == [{a: 1, b: 2, c: 3}]
assert list(unify(expr, pattern, variables=(a, b, c))) == \
[{a: 1, b: 2, c: 3}]
def test_unify_variables():
assert list(unify(Basic(S(1), S(2)), Basic(S(1), x), {}, variables=(x,))) == [{x: 2}]
def test_s_input():
expr = Basic(S(1), S(2))
a, b = map(Symbol, 'ab')
pattern = Basic(a, b)
assert list(unify(expr, pattern, {}, (a, b))) == [{a: 1, b: 2}]
assert list(unify(expr, pattern, {a: 5}, (a, b))) == []
def iterdicteq(a, b):
a = tuple(a)
b = tuple(b)
return len(a) == len(b) and all(x in b for x in a)
def test_unify_commutative():
expr = Add(1, 2, 3, evaluate=False)
a, b, c = map(Symbol, 'abc')
pattern = Add(a, b, c, evaluate=False)
result = tuple(unify(expr, pattern, {}, (a, b, c)))
expected = ({a: 1, b: 2, c: 3},
{a: 1, b: 3, c: 2},
{a: 2, b: 1, c: 3},
{a: 2, b: 3, c: 1},
{a: 3, b: 1, c: 2},
{a: 3, b: 2, c: 1})
assert iterdicteq(result, expected)
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_hard_match():
from sympy.functions.elementary.trigonometric import (cos, sin)
expr = sin(x) + cos(x)**2
p, q = map(Symbol, 'pq')
pattern = sin(p) + cos(p)**2
assert list(unify(expr, pattern, {}, (p, q))) == [{p: x}]
def test_matrix():
from sympy.matrices.expressions.matexpr import MatrixSymbol
X = MatrixSymbol('X', n, n)
Y = MatrixSymbol('Y', 2, 2)
Z = MatrixSymbol('Z', 2, 3)
assert list(unify(X, Y, {}, variables=[n, Str('X')])) == [{Str('X'): Str('Y'), n: 2}]
assert list(unify(X, Z, {}, variables=[n, Str('X')])) == []
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_FiniteSet_commutivity():
from sympy.sets.sets import FiniteSet
a, b, c, x, y = symbols('a,b,c,x,y')
s = FiniteSet(a, b, c)
t = FiniteSet(x, y)
variables = (x, y)
assert {x: FiniteSet(a, c), y: b} in tuple(unify(s, t, variables=variables))
def test_FiniteSet_complex():
from sympy.sets.sets import FiniteSet
a, b, c, x, y, z = symbols('a,b,c,x,y,z')
expr = FiniteSet(Basic(S(1), x), y, Basic(x, z))
pattern = FiniteSet(a, Basic(x, b))
variables = a, b
expected = ({b: 1, a: FiniteSet(y, Basic(x, z))},
{b: z, a: FiniteSet(y, Basic(S(1), x))})
assert iterdicteq(unify(expr, pattern, variables=variables), expected)
def test_and():
variables = x, y
expected = ({x: z > 0, y: n < 3},)
assert iterdicteq(unify((z>0) & (n<3), And(x, y), variables=variables),
expected)
def test_Union():
from sympy.sets.sets import Interval
assert list(unify(Interval(0, 1) + Interval(10, 11),
Interval(0, 1) + Interval(12, 13),
variables=(Interval(12, 13),)))
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_commutative_in_commutative():
from sympy.abc import a,b,c,d
from sympy.functions.elementary.trigonometric import (cos, sin)
eq = sin(3)*sin(4)*sin(5) + 4*cos(3)*cos(4)
pat = a*cos(b)*cos(c) + d*sin(b)*sin(c)
assert next(unify(eq, pat, variables=(a,b,c,d)))

View File

@ -0,0 +1,88 @@
from sympy.unify.core import Compound, Variable, CondVariable, allcombinations
from sympy.unify import core
a,b,c = 'a', 'b', 'c'
w,x,y,z = map(Variable, 'wxyz')
C = Compound
def is_associative(x):
return isinstance(x, Compound) and (x.op in ('Add', 'Mul', 'CAdd', 'CMul'))
def is_commutative(x):
return isinstance(x, Compound) and (x.op in ('CAdd', 'CMul'))
def unify(a, b, s={}):
return core.unify(a, b, s=s, is_associative=is_associative,
is_commutative=is_commutative)
def test_basic():
assert list(unify(a, x, {})) == [{x: a}]
assert list(unify(a, x, {x: 10})) == []
assert list(unify(1, x, {})) == [{x: 1}]
assert list(unify(a, a, {})) == [{}]
assert list(unify((w, x), (y, z), {})) == [{w: y, x: z}]
assert list(unify(x, (a, b), {})) == [{x: (a, b)}]
assert list(unify((a, b), (x, x), {})) == []
assert list(unify((y, z), (x, x), {}))!= []
assert list(unify((a, (b, c)), (a, (x, y)), {})) == [{x: b, y: c}]
def test_ops():
assert list(unify(C('Add', (a,b,c)), C('Add', (a,x,y)), {})) == \
[{x:b, y:c}]
assert list(unify(C('Add', (C('Mul', (1,2)), b,c)), C('Add', (x,y,c)), {})) == \
[{x: C('Mul', (1,2)), y:b}]
def test_associative():
c1 = C('Add', (1,2,3))
c2 = C('Add', (x,y))
assert tuple(unify(c1, c2, {})) == ({x: 1, y: C('Add', (2, 3))},
{x: C('Add', (1, 2)), y: 3})
def test_commutative():
c1 = C('CAdd', (1,2,3))
c2 = C('CAdd', (x,y))
result = list(unify(c1, c2, {}))
assert {x: 1, y: C('CAdd', (2, 3))} in result
assert ({x: 2, y: C('CAdd', (1, 3))} in result or
{x: 2, y: C('CAdd', (3, 1))} in result)
def _test_combinations_assoc():
assert set(allcombinations((1,2,3), (a,b), True)) == \
{(((1, 2), (3,)), (a, b)), (((1,), (2, 3)), (a, b))}
def _test_combinations_comm():
assert set(allcombinations((1,2,3), (a,b), None)) == \
{(((1,), (2, 3)), ('a', 'b')), (((2,), (3, 1)), ('a', 'b')),
(((3,), (1, 2)), ('a', 'b')), (((1, 2), (3,)), ('a', 'b')),
(((2, 3), (1,)), ('a', 'b')), (((3, 1), (2,)), ('a', 'b'))}
def test_allcombinations():
assert set(allcombinations((1,2), (1,2), 'commutative')) ==\
{(((1,),(2,)), ((1,),(2,))), (((1,),(2,)), ((2,),(1,)))}
def test_commutativity():
c1 = Compound('CAdd', (a, b))
c2 = Compound('CAdd', (x, y))
assert is_commutative(c1) and is_commutative(c2)
assert len(list(unify(c1, c2, {}))) == 2
def test_CondVariable():
expr = C('CAdd', (1, 2))
x = Variable('x')
y = CondVariable('y', lambda a: a % 2 == 0)
z = CondVariable('z', lambda a: a > 3)
pattern = C('CAdd', (x, y))
assert list(unify(expr, pattern, {})) == \
[{x: 1, y: 2}]
z = CondVariable('z', lambda a: a > 3)
pattern = C('CAdd', (z, y))
assert list(unify(expr, pattern, {})) == []
def test_defaultdict():
assert next(unify(Variable('x'), 'foo')) == {Variable('x'): 'foo'}