<s> from sympy . external import import_module <EOL> from sympy . utilities . pytest import raises , SKIP <EOL> from sympy . core . compatibility import range <EOL> theano = import_module ( '<STR_LIT>' ) <EOL> if theano : <EOL> import numpy as np <EOL> ts = theano . scalar <EOL> tt = theano . tensor <EOL> xt , yt , zt = [ tt . scalar ( name , '<STR_LIT>' ) for name in '<STR_LIT>' ] <EOL> else : <EOL> disabled = True <EOL> import sympy <EOL> from sympy import S <EOL> sy = sympy <EOL> from sympy . abc import x , y , z <EOL> from sympy . printing . theanocode import ( theano_code , dim_handling , <EOL> theano_function ) <EOL> def fgraph_of ( * exprs ) : <EOL> """<STR_LIT>""" <EOL> outs = list ( map ( theano_code , exprs ) ) <EOL> ins = theano . gof . graph . inputs ( outs ) <EOL> ins , outs = theano . gof . graph . clone ( ins , outs ) <EOL> return theano . gof . FunctionGraph ( ins , outs ) <EOL> def theano_simplify ( fgraph ) : <EOL> """<STR_LIT>""" <EOL> mode = theano . compile . get_default_mode ( ) . excluding ( "<STR_LIT>" ) <EOL> fgraph = fgraph . clone ( ) <EOL> mode . optimizer . optimize ( fgraph ) <EOL> return fgraph <EOL> def theq ( a , b ) : <EOL> """<STR_LIT>""" <EOL> astr = theano . printing . debugprint ( a , file = '<STR_LIT:str>' ) <EOL> bstr = theano . printing . debugprint ( b , file = '<STR_LIT:str>' ) <EOL> if not astr == bstr : <EOL> print ( ) <EOL> print ( astr ) <EOL> print ( bstr ) <EOL> return astr == bstr <EOL> def test_symbol ( ) : <EOL> xt = theano_code ( x ) <EOL> assert isinstance ( xt , ( tt . TensorVariable , ts . ScalarVariable ) ) <EOL> assert xt . name == x . name <EOL> assert theano_code ( x , broadcastables = { x : ( False , ) } ) . broadcastable == ( False , ) <EOL> assert theano_code ( x , broadcastables = { x : ( False , ) } ) . name == x . name <EOL> def test_add ( ) : <EOL> expr = x + y <EOL> comp = theano_code ( expr ) <EOL> assert comp . owner . op == theano . tensor . add <EOL> comp = theano_code ( expr , broadcastables = { x : ( False , ) , y : ( False , ) } ) <EOL> assert comp . broadcastable == ( False , ) <EOL> comp = theano_code ( expr , broadcastables = { x : ( False , True ) , y : ( False , False ) } ) <EOL> assert comp . broadcastable == ( False , False ) <EOL> def test_trig ( ) : <EOL> assert theq ( theano_code ( sympy . sin ( x ) ) , tt . sin ( xt ) ) <EOL> assert theq ( theano_code ( sympy . tan ( x ) ) , tt . tan ( xt ) ) <EOL> def test_many ( ) : <EOL> expr = sy . exp ( x ** <NUM_LIT:2> + sy . cos ( y ) ) * sy . log ( <NUM_LIT:2> * z ) <EOL> comp = theano_code ( expr ) <EOL> expected = tt . exp ( xt ** <NUM_LIT:2> + tt . cos ( yt ) ) * tt . log ( <NUM_LIT:2> * zt ) <EOL> def test_dtype ( ) : <EOL> assert theano_code ( x , dtypes = { x : '<STR_LIT>' } ) . type . dtype == '<STR_LIT>' <EOL> assert theano_code ( x , dtypes = { x : '<STR_LIT>' } ) . type . dtype == '<STR_LIT>' <EOL> assert theano_code ( x + <NUM_LIT:1> , dtypes = { x : '<STR_LIT>' } ) . type . dtype == '<STR_LIT>' <EOL> assert theano_code ( x + y , dtypes = { x : '<STR_LIT>' , y : '<STR_LIT>' } ) . type . dtype == '<STR_LIT>' <EOL> def test_MatrixSymbol ( ) : <EOL> X = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> Xt = theano_code ( X ) <EOL> assert isinstance ( Xt , tt . TensorVariable ) <EOL> assert Xt . broadcastable == ( False , False ) <EOL> def test_MatMul ( ) : <EOL> X = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> Y = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> Z = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> expr = X * Y * Z <EOL> assert isinstance ( theano_code ( expr ) . owner . op , tt . Dot ) <EOL> def test_Transpose ( ) : <EOL> X = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> assert isinstance ( theano_code ( X . T ) . owner . op , tt . DimShuffle ) <EOL> def test_MatAdd ( ) : <EOL> X = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> Y = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> Z = sympy . MatrixSymbol ( '<STR_LIT:X>' , <NUM_LIT:4> , <NUM_LIT:4> ) <EOL> expr = X + Y + Z <EOL> assert isinstance ( theano_code ( expr ) . owner . op , tt . Elemwise ) <EOL> def test_symbols_are_created_once ( ) : <EOL> expr = x ** x <EOL> comp = theano_code ( expr ) <EOL> assert theq ( comp , xt ** xt ) <EOL> def test_dim_handling ( ) : <EOL> assert dim_handling ( [ x ] , dim = <NUM_LIT:2> ) == { x : ( False , False ) } <EOL> assert dim_handling ( [ x , y ] , dims = { x : <NUM_LIT:1> , y : <NUM_LIT:2> } ) == { x : ( False , True ) , <EOL> y : ( False , False ) } <EOL> assert dim_handling ( [ x ] , broadcastables = { x : ( False , ) } ) == { x : ( False , ) } <EOL> def test_Rationals ( ) : <EOL> assert theq ( theano_code ( sympy . Integer ( <NUM_LIT:2> ) / <NUM_LIT:3> ) , tt . true_div ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> assert theq ( theano_code ( S . Half ) , tt . true_div ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def test_Integers ( ) : <EOL> assert theano_code ( sympy . Integer ( <NUM_LIT:3> ) ) == <NUM_LIT:3> <EOL> def test_factorial ( ) : <EOL> n = sympy . Symbol ( '<STR_LIT:n>' ) <EOL> assert theano_code ( sympy . factorial ( n ) ) <EOL> def test_Derivative ( ) : <EOL> simp = lambda expr : theano_simplify ( fgraph_of ( expr ) ) <EOL> assert theq ( simp ( theano_code ( sy . Derivative ( sy . sin ( x ) , x , evaluate = False ) ) ) , <EOL> simp ( theano . grad ( tt . sin ( xt ) , xt ) ) ) <EOL> def test_theano_function_simple ( ) : <EOL> f = theano_function ( [ x , y ] , [ x + y ] ) <EOL> assert f ( <NUM_LIT:2> , <NUM_LIT:3> ) == <NUM_LIT:5> <EOL> def test_theano_function_numpy ( ) : <EOL> f = theano_function ( [ x , y ] , [ x + y ] , dim = <NUM_LIT:1> , <EOL> dtypes = { x : '<STR_LIT>' , y : '<STR_LIT>' } ) <EOL> assert np . linalg . norm ( f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ) - np . asarray ( [ <NUM_LIT:4> , <NUM_LIT:6> ] ) ) < <NUM_LIT> <EOL> f = theano_function ( [ x , y ] , [ x + y ] , dtypes = { x : '<STR_LIT>' , y : '<STR_LIT>' } , <EOL> dim = <NUM_LIT:1> ) <EOL> xx = np . arange ( <NUM_LIT:3> ) . astype ( '<STR_LIT>' ) <EOL> yy = <NUM_LIT:2> * np . arange ( <NUM_LIT:3> ) . astype ( '<STR_LIT>' ) <EOL> assert np . linalg . norm ( f ( xx , yy ) - <NUM_LIT:3> * np . arange ( <NUM_LIT:3> ) ) < <NUM_LIT> <EOL> def test_theano_function_kwargs ( ) : <EOL> import numpy as np <EOL> f = theano_function ( [ x , y , z ] , [ x + y ] , dim = <NUM_LIT:1> , on_unused_input = '<STR_LIT:ignore>' , <EOL> dtypes = { x : '<STR_LIT>' , y : '<STR_LIT>' , z : '<STR_LIT>' } ) <EOL> assert np . linalg . norm ( f ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) - np . asarray ( [ <NUM_LIT:4> , <NUM_LIT:6> ] ) ) < <NUM_LIT> <EOL> f = theano_function ( [ x , y , z ] , [ x + y ] , <EOL> dtypes = { x : '<STR_LIT>' , y : '<STR_LIT>' , z : '<STR_LIT>' } , <EOL> dim = <NUM_LIT:1> , on_unused_input = '<STR_LIT:ignore>' ) <EOL> xx = np . arange ( <NUM_LIT:3> ) . astype ( '<STR_LIT>' ) <EOL> yy = <NUM_LIT:2> * np . arange ( <NUM_LIT:3> ) . astype ( '<STR_LIT>' ) <EOL> zz = <NUM_LIT:2> * np . arange ( <NUM_LIT:3> ) . astype ( '<STR_LIT>' ) <EOL> assert np . linalg . norm ( f ( xx , yy , zz ) - <NUM_LIT:3> * np . arange ( <NUM_LIT:3> ) ) < <NUM_LIT> <EOL> def test_slice ( ) : <EOL> assert theano_code ( slice ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) ) == slice ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> assert str ( theano_code ( slice ( <NUM_LIT:1> , x , <NUM_LIT:3> ) , dtypes = { x : '<STR_LIT>' } ) ) == str ( slice ( <NUM_LIT:1> , xt , <NUM_LIT:3> ) ) <EOL> def test_MatrixSlice ( ) : <EOL> n = sympy . Symbol ( '<STR_LIT:n>' , integer = True ) <EOL> X = sympy . MatrixSymbol ( '<STR_LIT:X>' , n , n ) <EOL> Y = X [ <NUM_LIT:1> : <NUM_LIT:2> : <NUM_LIT:3> , <NUM_LIT:4> : <NUM_LIT:5> : <NUM_LIT:6> ] <EOL> Yt = theano_code ( Y ) <EOL> from theano . scalar import Scalar <EOL> from theano import Constant <EOL> s = Scalar ( '<STR_LIT>' ) <EOL> assert tuple ( Yt . owner . op . idx_list ) == ( slice ( s , s , s ) , slice ( s , s , s ) ) <EOL> assert Yt . owner . inputs [ <NUM_LIT:0> ] == theano_code ( X ) <EOL> assert [ i . equals ( j ) for i , j in zip ( Yt . owner . inputs [ <NUM_LIT:1> : ] , [ <EOL> Constant ( s , <NUM_LIT:1> ) , <EOL> Constant ( s , <NUM_LIT:2> ) , <EOL> Constant ( s , <NUM_LIT:3> ) , <EOL> Constant ( s , <NUM_LIT:4> ) , <EOL> Constant ( s , <NUM_LIT:5> ) , <EOL> Constant ( s , <NUM_LIT:6> ) , <EOL> ] ) ] <EOL> k = sympy . Symbol ( '<STR_LIT:k>' ) <EOL> kt = theano_code ( k , dtypes = { k : '<STR_LIT>' } ) <EOL> start , stop , step = <NUM_LIT:4> , k , <NUM_LIT:2> <EOL> Y = X [ start : stop : step ] <EOL> Yt = theano_code ( Y , dtypes = { n : '<STR_LIT>' , k : '<STR_LIT>' } ) <EOL> def test_BlockMatrix ( ) : <EOL> n = sympy . Symbol ( '<STR_LIT:n>' , integer = True ) <EOL> A = sympy . MatrixSymbol ( '<STR_LIT:A>' , n , n ) <EOL> B = sympy . MatrixSymbol ( '<STR_LIT:B>' , n , n ) <EOL> C = sympy . MatrixSymbol ( '<STR_LIT:C>' , n , n ) <EOL> D = sympy . MatrixSymbol ( '<STR_LIT:D>' , n , n ) <EOL> At , Bt , Ct , Dt = map ( theano_code , ( A , B , C , D ) ) <EOL> Block = sympy . BlockMatrix ( [ [ A , B ] , [ C , D ] ] ) <EOL> Blockt = theano_code ( Block ) <EOL> solutions = [ tt . join ( <NUM_LIT:0> , tt . join ( <NUM_LIT:1> , At , Bt ) , tt . join ( <NUM_LIT:1> , Ct , Dt ) ) , <EOL> tt . join ( <NUM_LIT:1> , tt . join ( <NUM_LIT:0> , At , Ct ) , tt . join ( <NUM_LIT:0> , Bt , Dt ) ) ] <EOL> assert any ( theq ( Blockt , solution ) for solution in solutions ) <EOL> @ SKIP <EOL> def test_BlockMatrix_Inverse_execution ( ) : <EOL> k , n = <NUM_LIT:2> , <NUM_LIT:4> <EOL> dtype = '<STR_LIT>' <EOL> A = sympy . MatrixSymbol ( '<STR_LIT:A>' , n , k ) <EOL> B = sympy . MatrixSymbol ( '<STR_LIT:B>' , n , n ) <EOL> inputs = A , B <EOL> output = B . I * A <EOL> cutsizes = { A : [ ( n // <NUM_LIT:2> , n // <NUM_LIT:2> ) , ( k // <NUM_LIT:2> , k // <NUM_LIT:2> ) ] , <EOL> B : [ ( n // <NUM_LIT:2> , n // <NUM_LIT:2> ) , ( n // <NUM_LIT:2> , n // <NUM_LIT:2> ) ] } <EOL> cutinputs = [ sympy . blockcut ( i , * cutsizes [ i ] ) for i in inputs ] <EOL> cutoutput = output . subs ( dict ( zip ( inputs , cutinputs ) ) ) <EOL> dtypes = dict ( zip ( inputs , [ dtype ] * len ( inputs ) ) ) <EOL> f = theano_function ( inputs , [ output ] , dtypes = dtypes , cache = { } ) <EOL> fblocked = theano_function ( inputs , [ sympy . block_collapse ( cutoutput ) ] , <EOL> dtypes = dtypes , cache = { } ) <EOL> ninputs = [ np . random . rand ( * x . shape ) . astype ( dtype ) for x in inputs ] <EOL> ninputs = [ np . arange ( n * k ) . reshape ( A . shape ) . astype ( dtype ) , <EOL> np . eye ( n ) . astype ( dtype ) ] <EOL> ninputs [ <NUM_LIT:1> ] += np . ones ( B . shape ) * <NUM_LIT> <EOL> assert np . allclose ( f ( * ninputs ) , fblocked ( * ninputs ) , rtol = <NUM_LIT> ) <EOL> def test_DenseMatrix ( ) : <EOL> t = sy . Symbol ( '<STR_LIT>' ) <EOL> for MatrixType in [ sy . Matrix , sy . ImmutableMatrix ] : <EOL> X = MatrixType ( [ [ sy . cos ( t ) , - sy . sin ( t ) ] , [ sy . sin ( t ) , sy . cos ( t ) ] ] ) <EOL> tX = theano_code ( X ) <EOL> assert isinstance ( tX , tt . TensorVariable ) <EOL> assert tX . owner . op == tt . join <EOL> def test_AppliedUndef ( ) : <EOL> t = sy . Symbol ( '<STR_LIT:t>' ) <EOL> f = sy . Function ( '<STR_LIT:f>' ) <EOL> ft = theano_code ( f ( t ) ) <EOL> assert isinstance ( ft , tt . TensorVariable ) <EOL> assert ft . name == '<STR_LIT>' <EOL> def test_bad_keyword_args_raise_error ( ) : <EOL> raises ( Exception , lambda : theano_function ( [ x ] , [ x + <NUM_LIT:1> ] , foobar = <NUM_LIT:3> ) ) <EOL> def test_cache ( ) : <EOL> sx = sy . Symbol ( '<STR_LIT:x>' ) <EOL> cache = { } <EOL> tx = theano_code ( sx , cache = cache ) <EOL> assert theano_code ( sx , cache = cache ) is tx <EOL> assert theano_code ( sx , cache = { } ) is not tx <EOL> def test_Piecewise ( ) : <EOL> xt , yt = theano_code ( x ) , theano_code ( y ) <EOL> expr = sy . Piecewise ( ( <NUM_LIT:0> , x < <NUM_LIT:0> ) , ( x , x < <NUM_LIT:2> ) , ( <NUM_LIT:1> , True ) ) <EOL> result = theano_code ( expr ) <EOL> assert result . owner . op == tt . switch <EOL> expected = tt . switch ( xt < <NUM_LIT:0> , <NUM_LIT:0> , tt . switch ( xt < <NUM_LIT:2> , xt , <NUM_LIT:1> ) ) <EOL> assert theq ( result , expected ) <EOL> expr = sy . Piecewise ( ( x , x < <NUM_LIT:0> ) ) <EOL> result = theano_code ( expr ) <EOL> expected = tt . switch ( xt < <NUM_LIT:0> , xt , np . nan ) <EOL> assert theq ( result , expected ) <EOL> expr = sy . Piecewise ( ( <NUM_LIT:0> , sy . And ( x > <NUM_LIT:0> , x < <NUM_LIT:2> ) ) , ( x , sy . Or ( x > <NUM_LIT:2> , x < <NUM_LIT:0> ) ) ) <EOL> result = theano_code ( expr ) <EOL> expected = tt . switch ( tt . and_ ( xt > <NUM_LIT:0> , xt < <NUM_LIT:2> ) , <NUM_LIT:0> , tt . switch ( tt . or_ ( xt > <NUM_LIT:2> , xt < <NUM_LIT:0> ) , xt , np . nan ) ) <EOL> assert theq ( result , expected ) <EOL> def test_Relationals ( ) : <EOL> xt , yt = theano_code ( x ) , theano_code ( y ) <EOL> assert theq ( theano_code ( x > y ) , xt > yt ) <EOL> assert theq ( theano_code ( x < y ) , xt < yt ) <EOL> assert theq ( theano_code ( x >= y ) , xt >= yt ) <EOL> assert theq ( theano_code ( x <= y ) , xt <= yt ) </s>
