<s> from sympy . strategies . tree import treeapply , greedy , allresults , brute <EOL> from sympy . core . compatibility import reduce <EOL> from functools import partial <EOL> def test_treeapply ( ) : <EOL> tree = ( [ <NUM_LIT:3> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> assert treeapply ( tree , { list : min , tuple : max } ) == <NUM_LIT:3> <EOL> add = lambda * args : sum ( args ) <EOL> mul = lambda * args : reduce ( lambda a , b : a * b , args , <NUM_LIT:1> ) <EOL> assert treeapply ( tree , { list : add , tuple : mul } ) == <NUM_LIT> <EOL> def test_treeapply_leaf ( ) : <EOL> assert treeapply ( <NUM_LIT:3> , { } , leaf = lambda x : x ** <NUM_LIT:2> ) == <NUM_LIT:9> <EOL> tree = ( [ <NUM_LIT:3> , <NUM_LIT:3> ] , [ <NUM_LIT:4> , <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> treep1 = ( [ <NUM_LIT:4> , <NUM_LIT:4> ] , [ <NUM_LIT:5> , <NUM_LIT:2> ] , <NUM_LIT:3> ) <EOL> assert treeapply ( tree , { list : min , tuple : max } , leaf = lambda x : x + <NUM_LIT:1> ) == treeapply ( treep1 , { list : min , tuple : max } ) <EOL> def test_treeapply_strategies ( ) : <EOL> from sympy . strategies import chain , minimize <EOL> join = { list : chain , tuple : minimize } <EOL> inc = lambda x : x + <NUM_LIT:1> <EOL> dec = lambda x : x - <NUM_LIT:1> <EOL> double = lambda x : <NUM_LIT:2> * x <EOL> assert treeapply ( inc , join ) == inc <EOL> assert treeapply ( ( inc , dec ) , join ) ( <NUM_LIT:5> ) == minimize ( inc , dec ) ( <NUM_LIT:5> ) <EOL> assert treeapply ( [ inc , dec ] , join ) ( <NUM_LIT:5> ) == chain ( inc , dec ) ( <NUM_LIT:5> ) <EOL> tree = ( inc , [ dec , double ] ) <EOL> assert treeapply ( tree , join ) ( <NUM_LIT:5> ) == <NUM_LIT:6> <EOL> assert treeapply ( tree , join ) ( <NUM_LIT:1> ) == <NUM_LIT:0> <EOL> maximize = partial ( minimize , objective = lambda x : - x ) <EOL> join = { list : chain , tuple : maximize } <EOL> fn = treeapply ( tree , join ) <EOL> assert fn ( <NUM_LIT:4> ) == <NUM_LIT:6> <EOL> assert fn ( <NUM_LIT:1> ) == <NUM_LIT:2> <EOL> def test_greedy ( ) : <EOL> inc = lambda x : x + <NUM_LIT:1> <EOL> dec = lambda x : x - <NUM_LIT:1> <EOL> double = lambda x : <NUM_LIT:2> * x <EOL> tree = [ inc , ( dec , double ) ] <EOL> fn = greedy ( tree , objective = lambda x : - x ) <EOL> assert fn ( <NUM_LIT:4> ) == <NUM_LIT:6> <EOL> assert fn ( <NUM_LIT:1> ) == <NUM_LIT:2> <EOL> tree = [ inc , dec , [ inc , dec , [ ( inc , inc ) , ( dec , dec ) ] ] ] <EOL> lowest = greedy ( tree ) <EOL> assert lowest ( <NUM_LIT:10> ) == <NUM_LIT:8> <EOL> highest = greedy ( tree , objective = lambda x : - x ) <EOL> assert highest ( <NUM_LIT:10> ) == <NUM_LIT:12> <EOL> def test_allresults ( ) : <EOL> inc = lambda x : x + <NUM_LIT:1> <EOL> dec = lambda x : x - <NUM_LIT:1> <EOL> double = lambda x : x * <NUM_LIT:2> <EOL> square = lambda x : x ** <NUM_LIT:2> <EOL> assert set ( allresults ( inc ) ( <NUM_LIT:3> ) ) == { inc ( <NUM_LIT:3> ) } <EOL> assert set ( allresults ( [ inc , dec ] ) ( <NUM_LIT:3> ) ) == { <NUM_LIT:2> , <NUM_LIT:4> } <EOL> assert set ( allresults ( ( inc , dec ) ) ( <NUM_LIT:3> ) ) == { <NUM_LIT:3> } <EOL> assert set ( allresults ( [ inc , ( dec , double ) ] ) ( <NUM_LIT:4> ) ) == { <NUM_LIT:5> , <NUM_LIT:6> } <EOL> def test_brute ( ) : <EOL> inc = lambda x : x + <NUM_LIT:1> <EOL> dec = lambda x : x - <NUM_LIT:1> <EOL> square = lambda x : x ** <NUM_LIT:2> <EOL> tree = ( [ inc , dec ] , square ) <EOL> fn = brute ( tree , lambda x : - x ) <EOL> assert fn ( <NUM_LIT:2> ) == ( <NUM_LIT:2> + <NUM_LIT:1> ) ** <NUM_LIT:2> <EOL> assert fn ( - <NUM_LIT:2> ) == ( - <NUM_LIT:2> - <NUM_LIT:1> ) ** <NUM_LIT:2> <EOL> assert brute ( inc ) ( <NUM_LIT:1> ) == <NUM_LIT:2> </s>
