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