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,118 @@
from __future__ import annotations
from sympy.core.singleton import S
from sympy.core.basic import Basic
from sympy.strategies.core import (
null_safe, exhaust, memoize, condition,
chain, tryit, do_one, debug, switch, minimize)
from io import StringIO
def posdec(x: int) -> int:
if x > 0:
return x - 1
return x
def inc(x: int) -> int:
return x + 1
def dec(x: int) -> int:
return x - 1
def test_null_safe():
def rl(expr: int) -> int | None:
if expr == 1:
return 2
return None
safe_rl = null_safe(rl)
assert rl(1) == safe_rl(1)
assert rl(3) is None
assert safe_rl(3) == 3
def test_exhaust():
sink = exhaust(posdec)
assert sink(5) == 0
assert sink(10) == 0
def test_memoize():
rl = memoize(posdec)
assert rl(5) == posdec(5)
assert rl(5) == posdec(5)
assert rl(-2) == posdec(-2)
def test_condition():
rl = condition(lambda x: x % 2 == 0, posdec)
assert rl(5) == 5
assert rl(4) == 3
def test_chain():
rl = chain(posdec, posdec)
assert rl(5) == 3
assert rl(1) == 0
def test_tryit():
def rl(expr: Basic) -> Basic:
assert False
safe_rl = tryit(rl, AssertionError)
assert safe_rl(S(1)) == S(1)
def test_do_one():
rl = do_one(posdec, posdec)
assert rl(5) == 4
def rl1(x: int) -> int:
if x == 1:
return 2
return x
def rl2(x: int) -> int:
if x == 2:
return 3
return x
rule = do_one(rl1, rl2)
assert rule(1) == 2
assert rule(rule(1)) == 3
def test_debug():
file = StringIO()
rl = debug(posdec, file)
rl(5)
log = file.getvalue()
file.close()
assert posdec.__name__ in log
assert '5' in log
assert '4' in log
def test_switch():
def key(x: int) -> int:
return x % 3
rl = switch(key, {0: inc, 1: dec})
assert rl(3) == 4
assert rl(4) == 3
assert rl(5) == 5
def test_minimize():
def key(x: int) -> int:
return -x
rl = minimize(inc, dec)
assert rl(4) == 3
rl = minimize(inc, dec, objective=key)
assert rl(4) == 5

View File

@ -0,0 +1,78 @@
from sympy.core.singleton import S
from sympy.strategies.rl import (
rm_id, glom, flatten, unpack, sort, distribute, subs, rebuild)
from sympy.core.basic import Basic
from sympy.core.add import Add
from sympy.core.mul import Mul
from sympy.core.symbol import symbols
from sympy.abc import x
def test_rm_id():
rmzeros = rm_id(lambda x: x == 0)
assert rmzeros(Basic(S(0), S(1))) == Basic(S(1))
assert rmzeros(Basic(S(0), S(0))) == Basic(S(0))
assert rmzeros(Basic(S(2), S(1))) == Basic(S(2), S(1))
def test_glom():
def key(x):
return x.as_coeff_Mul()[1]
def count(x):
return x.as_coeff_Mul()[0]
def newargs(cnt, arg):
return cnt * arg
rl = glom(key, count, newargs)
result = rl(Add(x, -x, 3 * x, 2, 3, evaluate=False))
expected = Add(3 * x, 5)
assert set(result.args) == set(expected.args)
def test_flatten():
assert flatten(Basic(S(1), S(2), Basic(S(3), S(4)))) == \
Basic(S(1), S(2), S(3), S(4))
def test_unpack():
assert unpack(Basic(S(2))) == 2
assert unpack(Basic(S(2), S(3))) == Basic(S(2), S(3))
def test_sort():
assert sort(str)(Basic(S(3), S(1), S(2))) == Basic(S(1), S(2), S(3))
def test_distribute():
class T1(Basic):
pass
class T2(Basic):
pass
distribute_t12 = distribute(T1, T2)
assert distribute_t12(T1(S(1), S(2), T2(S(3), S(4)), S(5))) == \
T2(T1(S(1), S(2), S(3), S(5)), T1(S(1), S(2), S(4), S(5)))
assert distribute_t12(T1(S(1), S(2), S(3))) == T1(S(1), S(2), S(3))
def test_distribute_add_mul():
x, y = symbols('x, y')
expr = Mul(2, Add(x, y), evaluate=False)
expected = Add(Mul(2, x), Mul(2, y))
distribute_mul = distribute(Mul, Add)
assert distribute_mul(expr) == expected
def test_subs():
rl = subs(1, 2)
assert rl(1) == 2
assert rl(3) == 3
def test_rebuild():
expr = Basic.__new__(Add, S(1), S(2))
assert rebuild(expr) == 3

View File

@ -0,0 +1,32 @@
from sympy.strategies.tools import subs, typed
from sympy.strategies.rl import rm_id
from sympy.core.basic import Basic
from sympy.core.singleton import S
def test_subs():
from sympy.core.symbol import symbols
a, b, c, d, e, f = symbols('a,b,c,d,e,f')
mapping = {a: d, d: a, Basic(e): Basic(f)}
expr = Basic(a, Basic(b, c), Basic(d, Basic(e)))
result = Basic(d, Basic(b, c), Basic(a, Basic(f)))
assert subs(mapping)(expr) == result
def test_subs_empty():
assert subs({})(Basic(S(1), S(2))) == Basic(S(1), S(2))
def test_typed():
class A(Basic):
pass
class B(Basic):
pass
rmzeros = rm_id(lambda x: x == S(0))
rmones = rm_id(lambda x: x == S(1))
remove_something = typed({A: rmzeros, B: rmones})
assert remove_something(A(S(0), S(1))) == A(S(1))
assert remove_something(B(S(0), S(1))) == B(S(0))

View File

@ -0,0 +1,84 @@
from sympy.strategies.traverse import (
top_down, bottom_up, sall, top_down_once, bottom_up_once, basic_fns)
from sympy.strategies.rl import rebuild
from sympy.strategies.util import expr_fns
from sympy.core.add import Add
from sympy.core.basic import Basic
from sympy.core.numbers import Integer
from sympy.core.singleton import S
from sympy.core.symbol import Str, Symbol
from sympy.abc import x, y, z
def zero_symbols(expression):
return S.Zero if isinstance(expression, Symbol) else expression
def test_sall():
zero_onelevel = sall(zero_symbols)
assert zero_onelevel(Basic(x, y, Basic(x, z))) == \
Basic(S(0), S(0), Basic(x, z))
def test_bottom_up():
_test_global_traversal(bottom_up)
_test_stop_on_non_basics(bottom_up)
def test_top_down():
_test_global_traversal(top_down)
_test_stop_on_non_basics(top_down)
def _test_global_traversal(trav):
zero_all_symbols = trav(zero_symbols)
assert zero_all_symbols(Basic(x, y, Basic(x, z))) == \
Basic(S(0), S(0), Basic(S(0), S(0)))
def _test_stop_on_non_basics(trav):
def add_one_if_can(expr):
try:
return expr + 1
except TypeError:
return expr
expr = Basic(S(1), Str('a'), Basic(S(2), Str('b')))
expected = Basic(S(2), Str('a'), Basic(S(3), Str('b')))
rl = trav(add_one_if_can)
assert rl(expr) == expected
class Basic2(Basic):
pass
def rl(x):
if x.args and not isinstance(x.args[0], Integer):
return Basic2(*x.args)
return x
def test_top_down_once():
top_rl = top_down_once(rl)
assert top_rl(Basic(S(1.0), S(2.0), Basic(S(3), S(4)))) == \
Basic2(S(1.0), S(2.0), Basic(S(3), S(4)))
def test_bottom_up_once():
bottom_rl = bottom_up_once(rl)
assert bottom_rl(Basic(S(1), S(2), Basic(S(3.0), S(4.0)))) == \
Basic(S(1), S(2), Basic2(S(3.0), S(4.0)))
def test_expr_fns():
expr = x + y**3
e = bottom_up(lambda v: v + 1, expr_fns)(expr)
b = bottom_up(lambda v: Basic.__new__(Add, v, S(1)), basic_fns)(expr)
assert rebuild(b) == e

View File

@ -0,0 +1,92 @@
from sympy.strategies.tree import treeapply, greedy, allresults, brute
from functools import partial, reduce
def inc(x):
return x + 1
def dec(x):
return x - 1
def double(x):
return 2 * x
def square(x):
return x**2
def add(*args):
return sum(args)
def mul(*args):
return reduce(lambda a, b: a * b, args, 1)
def test_treeapply():
tree = ([3, 3], [4, 1], 2)
assert treeapply(tree, {list: min, tuple: max}) == 3
assert treeapply(tree, {list: add, tuple: mul}) == 60
def test_treeapply_leaf():
assert treeapply(3, {}, leaf=lambda x: x**2) == 9
tree = ([3, 3], [4, 1], 2)
treep1 = ([4, 4], [5, 2], 3)
assert treeapply(tree, {list: min, tuple: max}, leaf=lambda x: x + 1) == \
treeapply(treep1, {list: min, tuple: max})
def test_treeapply_strategies():
from sympy.strategies import chain, minimize
join = {list: chain, tuple: minimize}
assert treeapply(inc, join) == inc
assert treeapply((inc, dec), join)(5) == minimize(inc, dec)(5)
assert treeapply([inc, dec], join)(5) == chain(inc, dec)(5)
tree = (inc, [dec, double]) # either inc or dec-then-double
assert treeapply(tree, join)(5) == 6
assert treeapply(tree, join)(1) == 0
maximize = partial(minimize, objective=lambda x: -x)
join = {list: chain, tuple: maximize}
fn = treeapply(tree, join)
assert fn(4) == 6 # highest value comes from the dec then double
assert fn(1) == 2 # highest value comes from the inc
def test_greedy():
tree = [inc, (dec, double)] # either inc or dec-then-double
fn = greedy(tree, objective=lambda x: -x)
assert fn(4) == 6 # highest value comes from the dec then double
assert fn(1) == 2 # highest value comes from the inc
tree = [inc, dec, [inc, dec, [(inc, inc), (dec, dec)]]]
lowest = greedy(tree)
assert lowest(10) == 8
highest = greedy(tree, objective=lambda x: -x)
assert highest(10) == 12
def test_allresults():
# square = lambda x: x**2
assert set(allresults(inc)(3)) == {inc(3)}
assert set(allresults([inc, dec])(3)) == {2, 4}
assert set(allresults((inc, dec))(3)) == {3}
assert set(allresults([inc, (dec, double)])(4)) == {5, 6}
def test_brute():
tree = ([inc, dec], square)
fn = brute(tree, lambda x: -x)
assert fn(2) == (2 + 1)**2
assert fn(-2) == (-2 - 1)**2
assert brute(inc)(1) == 2