<s> from sympy import sin , cos , atan2 , log , exp , gamma , conjugate , sqrt , factorial , Integral , Piecewise , Add , diff , symbols , S , Float , Dummy , Eq <EOL> from sympy import Catalan , EulerGamma , E , GoldenRatio , I , pi <EOL> from sympy import Function , Rational , Integer , Lambda <EOL> from sympy . core . relational import Relational <EOL> from sympy . logic . boolalg import And , Or , Not , Equivalent , Xor <EOL> from sympy . printing . fcode import fcode , FCodePrinter <EOL> from sympy . tensor import IndexedBase , Idx <EOL> from sympy . utilities . lambdify import implemented_function <EOL> from sympy . utilities . pytest import raises <EOL> from sympy . core . compatibility import range <EOL> from sympy . matrices import Matrix , MatrixSymbol <EOL> def test_printmethod ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> class nint ( Function ) : <EOL> def _fcode ( self , printer ) : <EOL> return "<STR_LIT>" % printer . _print ( self . args [ <NUM_LIT:0> ] ) <EOL> assert fcode ( nint ( x ) ) == "<STR_LIT>" <EOL> def test_fcode_Pow ( ) : <EOL> x , y = symbols ( '<STR_LIT>' ) <EOL> n = symbols ( '<STR_LIT:n>' , integer = True ) <EOL> assert fcode ( x ** <NUM_LIT:3> ) == "<STR_LIT>" <EOL> assert fcode ( x ** ( y ** <NUM_LIT:3> ) ) == "<STR_LIT>" <EOL> assert fcode ( <NUM_LIT:1> / ( sin ( x ) * <NUM_LIT> ) ** ( x - y ** x ) / ( x ** <NUM_LIT:2> + y ) ) == "<STR_LIT>" <EOL> assert fcode ( sqrt ( x ) ) == '<STR_LIT>' <EOL> assert fcode ( sqrt ( n ) ) == '<STR_LIT>' <EOL> assert fcode ( x ** <NUM_LIT:0.5> ) == '<STR_LIT>' <EOL> assert fcode ( sqrt ( x ) ) == '<STR_LIT>' <EOL> assert fcode ( sqrt ( <NUM_LIT:10> ) ) == '<STR_LIT>' <EOL> assert fcode ( x ** - <NUM_LIT:1.0> ) == '<STR_LIT>' <EOL> assert fcode ( x ** - <NUM_LIT> , '<STR_LIT:y>' , source_format = '<STR_LIT>' ) == '<STR_LIT>' <EOL> assert fcode ( x ** Rational ( <NUM_LIT:3> , <NUM_LIT:7> ) ) == '<STR_LIT>' <EOL> def test_fcode_Rational ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert fcode ( Rational ( <NUM_LIT:3> , <NUM_LIT:7> ) ) == "<STR_LIT>" <EOL> assert fcode ( Rational ( <NUM_LIT> , <NUM_LIT:9> ) ) == "<STR_LIT>" <EOL> assert fcode ( Rational ( <NUM_LIT:3> , - <NUM_LIT:7> ) ) == "<STR_LIT>" <EOL> assert fcode ( Rational ( - <NUM_LIT:3> , - <NUM_LIT:7> ) ) == "<STR_LIT>" <EOL> assert fcode ( x + Rational ( <NUM_LIT:3> , <NUM_LIT:7> ) ) == "<STR_LIT>" <EOL> assert fcode ( Rational ( <NUM_LIT:3> , <NUM_LIT:7> ) * x ) == "<STR_LIT>" <EOL> def test_fcode_Integer ( ) : <EOL> assert fcode ( Integer ( <NUM_LIT> ) ) == "<STR_LIT>" <EOL> assert fcode ( Integer ( - <NUM_LIT:1> ) ) == "<STR_LIT>" <EOL> def test_fcode_Float ( ) : <EOL> assert fcode ( Float ( <NUM_LIT> ) ) == "<STR_LIT>" <EOL> assert fcode ( Float ( - <NUM_LIT> ) ) == "<STR_LIT>" <EOL> def test_fcode_functions ( ) : <EOL> x , y = symbols ( '<STR_LIT>' ) <EOL> assert fcode ( sin ( x ) ** cos ( y ) ) == "<STR_LIT>" <EOL> def test_fcode_functions_with_integers ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert fcode ( x * log ( <NUM_LIT:10> ) ) == "<STR_LIT>" <EOL> assert fcode ( x * log ( <NUM_LIT:10> ) ) == "<STR_LIT>" <EOL> assert fcode ( x * log ( S ( <NUM_LIT:10> ) ) ) == "<STR_LIT>" <EOL> assert fcode ( log ( S ( <NUM_LIT:10> ) ) ) == "<STR_LIT>" <EOL> assert fcode ( exp ( <NUM_LIT:10> ) ) == "<STR_LIT>" <EOL> assert fcode ( x * log ( log ( <NUM_LIT:10> ) ) ) == "<STR_LIT>" <EOL> assert fcode ( x * log ( log ( S ( <NUM_LIT:10> ) ) ) ) == "<STR_LIT>" <EOL> def test_fcode_NumberSymbol ( ) : <EOL> p = FCodePrinter ( ) <EOL> assert fcode ( Catalan ) == '<STR_LIT>' <EOL> assert fcode ( EulerGamma ) == '<STR_LIT>' <EOL> assert fcode ( E ) == '<STR_LIT>' <EOL> assert fcode ( GoldenRatio ) == '<STR_LIT>' <EOL> assert fcode ( pi ) == '<STR_LIT>' <EOL> assert fcode ( <EOL> pi , precision = <NUM_LIT:5> ) == '<STR_LIT>' <EOL> assert fcode ( Catalan , human = False ) == ( set ( <EOL> [ ( Catalan , p . _print ( Catalan . evalf ( <NUM_LIT:15> ) ) ) ] ) , set ( [ ] ) , '<STR_LIT>' ) <EOL> assert fcode ( EulerGamma , human = False ) == ( set ( [ ( EulerGamma , p . _print ( <EOL> EulerGamma . evalf ( <NUM_LIT:15> ) ) ) ] ) , set ( [ ] ) , '<STR_LIT>' ) <EOL> assert fcode ( E , human = False ) == ( <EOL> set ( [ ( E , p . _print ( E . evalf ( <NUM_LIT:15> ) ) ) ] ) , set ( [ ] ) , '<STR_LIT>' ) <EOL> assert fcode ( GoldenRatio , human = False ) == ( set ( [ ( GoldenRatio , p . _print ( <EOL> GoldenRatio . evalf ( <NUM_LIT:15> ) ) ) ] ) , set ( [ ] ) , '<STR_LIT>' ) <EOL> assert fcode ( pi , human = False ) == ( <EOL> set ( [ ( pi , p . _print ( pi . evalf ( <NUM_LIT:15> ) ) ) ] ) , set ( [ ] ) , '<STR_LIT>' ) <EOL> assert fcode ( pi , precision = <NUM_LIT:5> , human = False ) == ( <EOL> set ( [ ( pi , p . _print ( pi . evalf ( <NUM_LIT:5> ) ) ) ] ) , set ( [ ] ) , '<STR_LIT>' ) <EOL> def test_fcode_complex ( ) : <EOL> assert fcode ( I ) == "<STR_LIT>" <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert fcode ( <NUM_LIT:4> * I ) == "<STR_LIT>" <EOL> assert fcode ( <NUM_LIT:3> + <NUM_LIT:4> * I ) == "<STR_LIT>" <EOL> assert fcode ( <NUM_LIT:3> + <NUM_LIT:4> * I + x ) == "<STR_LIT>" <EOL> assert fcode ( I * x ) == "<STR_LIT>" <EOL> assert fcode ( <NUM_LIT:3> + <NUM_LIT:4> * I - x ) == "<STR_LIT>" <EOL> x = symbols ( '<STR_LIT:x>' , imaginary = True ) <EOL> assert fcode ( <NUM_LIT:5> * x ) == "<STR_LIT>" <EOL> assert fcode ( I * x ) == "<STR_LIT>" <EOL> assert fcode ( <NUM_LIT:3> + x ) == "<STR_LIT>" <EOL> def test_implicit ( ) : <EOL> x , y = symbols ( '<STR_LIT>' ) <EOL> assert fcode ( sin ( x ) ) == "<STR_LIT>" <EOL> assert fcode ( atan2 ( x , y ) ) == "<STR_LIT>" <EOL> assert fcode ( conjugate ( x ) ) == "<STR_LIT>" <EOL> def test_not_fortran ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> g = Function ( '<STR_LIT:g>' ) <EOL> assert fcode ( <EOL> gamma ( x ) ) == "<STR_LIT>" <EOL> assert fcode ( Integral ( sin ( x ) ) ) == "<STR_LIT>" <EOL> assert fcode ( g ( x ) ) == "<STR_LIT>" <EOL> def test_user_functions ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert fcode ( sin ( x ) , user_functions = { "<STR_LIT>" : "<STR_LIT>" } ) == "<STR_LIT>" <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert fcode ( <EOL> gamma ( x ) , user_functions = { "<STR_LIT>" : "<STR_LIT>" } ) == "<STR_LIT>" <EOL> g = Function ( '<STR_LIT:g>' ) <EOL> assert fcode ( g ( x ) , user_functions = { "<STR_LIT:g>" : "<STR_LIT>" } ) == "<STR_LIT>" <EOL> n = symbols ( '<STR_LIT:n>' , integer = True ) <EOL> assert fcode ( <EOL> factorial ( n ) , user_functions = { "<STR_LIT>" : "<STR_LIT>" } ) == "<STR_LIT>" <EOL> def test_inline_function ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> g = implemented_function ( '<STR_LIT:g>' , Lambda ( x , <NUM_LIT:2> * x ) ) <EOL> assert fcode ( g ( x ) ) == "<STR_LIT>" <EOL> g = implemented_function ( '<STR_LIT:g>' , Lambda ( x , <NUM_LIT:2> * pi / x ) ) <EOL> assert fcode ( g ( x ) ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> A = IndexedBase ( '<STR_LIT:A>' ) <EOL> i = Idx ( '<STR_LIT:i>' , symbols ( '<STR_LIT:n>' , integer = True ) ) <EOL> g = implemented_function ( '<STR_LIT:g>' , Lambda ( x , x * ( <NUM_LIT:1> + x ) * ( <NUM_LIT:2> + x ) ) ) <EOL> assert fcode ( g ( A [ i ] ) , assign_to = A [ i ] ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_assign_to ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert fcode ( sin ( x ) , assign_to = "<STR_LIT:s>" ) == "<STR_LIT>" <EOL> def test_line_wrapping ( ) : <EOL> x , y = symbols ( '<STR_LIT>' ) <EOL> assert fcode ( ( ( x + y ) ** <NUM_LIT:10> ) . expand ( ) , assign_to = "<STR_LIT>" ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> e = [ x ** i for i in range ( <NUM_LIT:11> ) ] <EOL> assert fcode ( Add ( * e ) ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_fcode_precedence ( ) : <EOL> x , y = symbols ( "<STR_LIT>" ) <EOL> assert fcode ( And ( x < y , y < x + <NUM_LIT:1> ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( x < y , y < x + <NUM_LIT:1> ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( x < y , y < x + <NUM_LIT:1> , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( x < y , y < x + <NUM_LIT:1> ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> def test_fcode_Logical ( ) : <EOL> x , y , z = symbols ( "<STR_LIT>" ) <EOL> assert fcode ( Not ( x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( x , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( x , Not ( y ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Not ( x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Not ( x ) , Not ( y ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Not ( And ( x , y ) , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( x , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( x , Not ( y ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Not ( x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Not ( x ) , Not ( y ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Not ( Or ( x , y ) , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Or ( y , z ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Or ( z , x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Or ( x , y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( And ( y , z ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( And ( z , x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( And ( x , y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( x , y , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( x , y , Not ( z ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( x , Not ( y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Not ( x ) , y , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Not ( And ( x , y , z ) , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( x , y , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( x , y , Not ( z ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( x , Not ( y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Not ( x ) , y , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Not ( Or ( x , y , z ) , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> def test_fcode_Xlogical ( ) : <EOL> x , y , z = symbols ( "<STR_LIT>" ) <EOL> assert fcode ( Xor ( x , y , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( x , Not ( y ) , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Not ( x ) , y , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Not ( x ) , Not ( y ) , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Not ( Xor ( x , y , evaluate = False ) , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( x , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( x , Not ( y ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Not ( x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Not ( x ) , Not ( y ) ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Not ( Equivalent ( x , y ) , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( And ( y , z ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( And ( z , x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( And ( x , y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Equivalent ( y , z ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Equivalent ( z , x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Equivalent ( x , y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Or ( y , z ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Or ( z , x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Or ( x , y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Equivalent ( y , z ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Equivalent ( z , x ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Equivalent ( x , y ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Xor ( y , z , evaluate = False ) , x ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Xor ( z , x , evaluate = False ) , y ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Equivalent ( Xor ( x , y , evaluate = False ) , z ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Equivalent ( y , z ) , x , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Equivalent ( z , x ) , y , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Equivalent ( x , y ) , z , evaluate = False ) , <EOL> source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( And ( y , z ) , x , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( And ( z , x ) , y , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( And ( x , y ) , z , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Xor ( y , z , evaluate = False ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Xor ( z , x , evaluate = False ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( And ( Xor ( x , y , evaluate = False ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Or ( y , z ) , x , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Or ( z , x ) , y , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Or ( x , y ) , z , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Xor ( y , z , evaluate = False ) , x ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Xor ( z , x , evaluate = False ) , y ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Or ( Xor ( x , y , evaluate = False ) , z ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( x , y , z , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( x , y , Not ( z ) , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( x , Not ( y ) , z , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Xor ( Not ( x ) , y , z , evaluate = False ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> def test_fcode_Relational ( ) : <EOL> x , y = symbols ( "<STR_LIT>" ) <EOL> assert fcode ( Relational ( x , y , "<STR_LIT>" ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Relational ( x , y , "<STR_LIT>" ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Relational ( x , y , "<STR_LIT>" ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Relational ( x , y , "<STR_LIT>" ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Relational ( x , y , "<STR_LIT:>>" ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> assert fcode ( Relational ( x , y , "<STR_LIT:<>" ) , source_format = "<STR_LIT>" ) == "<STR_LIT>" <EOL> def test_fcode_Piecewise ( ) : <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> expr = Piecewise ( ( x , x < <NUM_LIT:1> ) , ( x ** <NUM_LIT:2> , True ) ) <EOL> raises ( NotImplementedError , lambda : fcode ( expr ) ) <EOL> code = fcode ( expr , standard = <NUM_LIT> ) <EOL> expected = "<STR_LIT>" <EOL> assert code == expected <EOL> assert fcode ( Piecewise ( ( x , x < <NUM_LIT:1> ) , ( x ** <NUM_LIT:2> , True ) ) , assign_to = "<STR_LIT>" ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> a = cos ( x ) / x <EOL> b = sin ( x ) / x <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> a = diff ( a , x ) <EOL> b = diff ( b , x ) <EOL> expected = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> code = fcode ( Piecewise ( ( a , x < <NUM_LIT:0> ) , ( b , True ) ) , assign_to = "<STR_LIT>" ) <EOL> assert code == expected <EOL> code = fcode ( Piecewise ( ( x , x < <NUM_LIT:1> ) , ( x ** <NUM_LIT:2> , x > <NUM_LIT:1> ) , ( sin ( x ) , True ) ) , standard = <NUM_LIT> ) <EOL> expected = "<STR_LIT>" <EOL> assert code == expected <EOL> expr = Piecewise ( ( x , x < <NUM_LIT:1> ) , ( x ** <NUM_LIT:2> , x > <NUM_LIT:1> ) , ( sin ( x ) , x > <NUM_LIT:0> ) ) <EOL> raises ( ValueError , lambda : fcode ( expr ) ) <EOL> def test_wrap_fortran ( ) : <EOL> printer = FCodePrinter ( ) <EOL> lines = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> wrapped_lines = printer . _wrap_fortran ( lines ) <EOL> expected_lines = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for line in wrapped_lines : <EOL> assert len ( line ) <= <NUM_LIT> <EOL> for w , e in zip ( wrapped_lines , expected_lines ) : <EOL> assert w == e <EOL> assert len ( wrapped_lines ) == len ( expected_lines ) <EOL> def test_wrap_fortran_keep_d0 ( ) : <EOL> printer = FCodePrinter ( ) <EOL> lines = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> expected = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> assert printer . _wrap_fortran ( lines ) == expected <EOL> def test_settings ( ) : <EOL> raises ( TypeError , lambda : fcode ( S ( <NUM_LIT:4> ) , method = "<STR_LIT>" ) ) <EOL> def test_free_form_code_line ( ) : <EOL> x , y = symbols ( '<STR_LIT>' ) <EOL> assert fcode ( cos ( x ) + sin ( y ) , source_format = '<STR_LIT>' ) == "<STR_LIT>" <EOL> def test_free_form_continuation_line ( ) : <EOL> x , y = symbols ( '<STR_LIT>' ) <EOL> result = fcode ( ( ( cos ( x ) + sin ( y ) ) ** ( <NUM_LIT:7> ) ) . expand ( ) , source_format = '<STR_LIT>' ) <EOL> expected = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> assert result == expected <EOL> def test_free_form_comment_line ( ) : <EOL> printer = FCodePrinter ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> lines = [ "<STR_LIT>" ] <EOL> expected = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> assert printer . _wrap_fortran ( lines ) == expected <EOL> def test_loops ( ) : <EOL> n , m = symbols ( '<STR_LIT>' , integer = True ) <EOL> A = IndexedBase ( '<STR_LIT:A>' ) <EOL> x = IndexedBase ( '<STR_LIT:x>' ) <EOL> y = IndexedBase ( '<STR_LIT:y>' ) <EOL> i = Idx ( '<STR_LIT:i>' , m ) <EOL> j = Idx ( '<STR_LIT>' , n ) <EOL> expected = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> code = fcode ( A [ i , j ] * x [ j ] , assign_to = y [ i ] , source_format = '<STR_LIT>' ) <EOL> assert ( code == expected % { '<STR_LIT>' : '<STR_LIT>' } or <EOL> code == expected % { '<STR_LIT>' : '<STR_LIT>' } or <EOL> code == expected % { '<STR_LIT>' : '<STR_LIT>' } or <EOL> code == expected % { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_dummy_loops ( ) : <EOL> i , m = symbols ( '<STR_LIT>' , integer = True , cls = Dummy ) <EOL> x = IndexedBase ( '<STR_LIT:x>' ) <EOL> y = IndexedBase ( '<STR_LIT:y>' ) <EOL> i = Idx ( i , m ) <EOL> expected = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) % { '<STR_LIT>' : i . label . dummy_index , '<STR_LIT>' : m . dummy_index } <EOL> code = fcode ( x [ i ] , assign_to = y [ i ] , source_format = '<STR_LIT>' ) <EOL> assert code == expected <EOL> def test_fcode_Indexed_without_looking_for_contraction ( ) : <EOL> len_y = <NUM_LIT:5> <EOL> y = IndexedBase ( '<STR_LIT:y>' , shape = ( len_y , ) ) <EOL> x = IndexedBase ( '<STR_LIT:x>' , shape = ( len_y , ) ) <EOL> Dy = IndexedBase ( '<STR_LIT>' , shape = ( len_y - <NUM_LIT:1> , ) ) <EOL> i = Idx ( '<STR_LIT:i>' , len_y - <NUM_LIT:1> ) <EOL> e = Eq ( Dy [ i ] , ( y [ i + <NUM_LIT:1> ] - y [ i ] ) / ( x [ i + <NUM_LIT:1> ] - x [ i ] ) ) <EOL> code0 = fcode ( e . rhs , assign_to = e . lhs , contract = False ) <EOL> assert code0 . endswith ( '<STR_LIT>' ) <EOL> def test_derived_classes ( ) : <EOL> class MyFancyFCodePrinter ( FCodePrinter ) : <EOL> _default_settings = FCodePrinter . _default_settings . copy ( ) <EOL> printer = MyFancyFCodePrinter ( ) <EOL> x = symbols ( '<STR_LIT:x>' ) <EOL> assert printer . doprint ( sin ( x ) , "<STR_LIT>" ) == "<STR_LIT>" <EOL> def test_indent ( ) : <EOL> codelines = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> expected = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> p = FCodePrinter ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> result = p . indent_code ( codelines ) <EOL> assert result == expected <EOL> def test_Matrix_printing ( ) : <EOL> x , y , z = symbols ( '<STR_LIT>' ) <EOL> mat = Matrix ( [ x * y , Piecewise ( ( <NUM_LIT:2> + x , y > <NUM_LIT:0> ) , ( y , True ) ) , sin ( z ) ] ) <EOL> A = MatrixSymbol ( '<STR_LIT:A>' , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> assert fcode ( mat , A ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> expr = Piecewise ( ( <NUM_LIT:2> * A [ <NUM_LIT:2> , <NUM_LIT:0> ] , x > <NUM_LIT:0> ) , ( A [ <NUM_LIT:2> , <NUM_LIT:0> ] , True ) ) + sin ( A [ <NUM_LIT:1> , <NUM_LIT:0> ] ) + A [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> assert fcode ( expr , standard = <NUM_LIT> ) == ( <EOL> "<STR_LIT>" ) <EOL> q = MatrixSymbol ( '<STR_LIT:q>' , <NUM_LIT:5> , <NUM_LIT:1> ) <EOL> M = MatrixSymbol ( '<STR_LIT:M>' , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> m = Matrix ( [ [ sin ( q [ <NUM_LIT:1> , <NUM_LIT:0> ] ) , <NUM_LIT:0> , cos ( q [ <NUM_LIT:2> , <NUM_LIT:0> ] ) ] , <EOL> [ q [ <NUM_LIT:1> , <NUM_LIT:0> ] + q [ <NUM_LIT:2> , <NUM_LIT:0> ] , q [ <NUM_LIT:3> , <NUM_LIT:0> ] , <NUM_LIT:5> ] , <EOL> [ <NUM_LIT:2> * q [ <NUM_LIT:4> , <NUM_LIT:0> ] / q [ <NUM_LIT:1> , <NUM_LIT:0> ] , sqrt ( q [ <NUM_LIT:0> , <NUM_LIT:0> ] ) + <NUM_LIT:4> , <NUM_LIT:0> ] ] ) <EOL> assert fcode ( m , M ) == ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) </s>
