<s> from __future__ import print_function , division <EOL> from sympy . core import Add , Mul , Pow , S <EOL> from sympy . core . basic import Basic <EOL> from sympy . core . compatibility import default_sort_key , string_types <EOL> from sympy . core . function import Lambda <EOL> from sympy . core . mul import _keep_coeff <EOL> from sympy . core . relational import Relational <EOL> from sympy . core . symbol import Symbol <EOL> from sympy . printing . str import StrPrinter <EOL> from sympy . printing . precedence import precedence <EOL> from sympy . core . sympify import _sympify , sympify <EOL> class AssignmentError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Assignment ( Relational ) : <EOL> """<STR_LIT>""" <EOL> rel_op = '<STR_LIT>' <EOL> __slots__ = [ ] <EOL> def __new__ ( cls , lhs , rhs = <NUM_LIT:0> , ** assumptions ) : <EOL> from sympy . matrices . expressions . matexpr import ( <EOL> MatrixElement , MatrixSymbol ) <EOL> from sympy . tensor . indexed import Indexed <EOL> lhs = _sympify ( lhs ) <EOL> rhs = _sympify ( rhs ) <EOL> assignable = ( Symbol , MatrixSymbol , MatrixElement , Indexed ) <EOL> if not isinstance ( lhs , assignable ) : <EOL> raise TypeError ( "<STR_LIT>" % type ( lhs ) ) <EOL> lhs_is_mat = hasattr ( lhs , '<STR_LIT>' ) and not isinstance ( lhs , Indexed ) <EOL> rhs_is_mat = hasattr ( rhs , '<STR_LIT>' ) and not isinstance ( rhs , Indexed ) <EOL> if lhs_is_mat : <EOL> if not rhs_is_mat : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif lhs . shape != rhs . shape : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif rhs_is_mat and not lhs_is_mat : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return Relational . __new__ ( cls , lhs , rhs , ** assumptions ) <EOL> Relational . ValidRelationOperator [ '<STR_LIT>' ] = Assignment <EOL> class CodePrinter ( StrPrinter ) : <EOL> """<STR_LIT>""" <EOL> _operators = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:!>' , <EOL> } <EOL> _default_settings = { '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT:_>' } <EOL> def __init__ ( self , settings = None ) : <EOL> super ( CodePrinter , self ) . __init__ ( settings = settings ) <EOL> self . reserved_words = set ( ) <EOL> def doprint ( self , expr , assign_to = None ) : <EOL> """<STR_LIT>""" <EOL> from sympy . matrices . expressions . matexpr import MatrixSymbol <EOL> if isinstance ( assign_to , string_types ) : <EOL> if expr . is_Matrix : <EOL> assign_to = MatrixSymbol ( assign_to , * expr . shape ) <EOL> else : <EOL> assign_to = Symbol ( assign_to ) <EOL> elif not isinstance ( assign_to , ( Basic , type ( None ) ) ) : <EOL> raise TypeError ( "<STR_LIT>" . format ( <EOL> type ( self ) . __name__ , type ( assign_to ) ) ) <EOL> if assign_to : <EOL> expr = Assignment ( assign_to , expr ) <EOL> else : <EOL> expr = sympify ( expr ) <EOL> self . _not_supported = set ( ) <EOL> self . _number_symbols = set ( ) <EOL> lines = self . _print ( expr ) . splitlines ( ) <EOL> if self . _settings [ "<STR_LIT>" ] : <EOL> frontlines = [ ] <EOL> if len ( self . _not_supported ) > <NUM_LIT:0> : <EOL> frontlines . append ( self . _get_comment ( <EOL> "<STR_LIT>" . format ( self . language ) ) ) <EOL> for expr in sorted ( self . _not_supported , key = str ) : <EOL> frontlines . append ( self . _get_comment ( type ( expr ) . __name__ ) ) <EOL> for name , value in sorted ( self . _number_symbols , key = str ) : <EOL> frontlines . append ( self . _declare_number_const ( name , value ) ) <EOL> lines = frontlines + lines <EOL> lines = self . _format_code ( lines ) <EOL> result = "<STR_LIT:\n>" . join ( lines ) <EOL> else : <EOL> lines = self . _format_code ( lines ) <EOL> result = ( self . _number_symbols , self . _not_supported , <EOL> "<STR_LIT:\n>" . join ( lines ) ) <EOL> del self . _not_supported <EOL> del self . _number_symbols <EOL> return result <EOL> def _doprint_loops ( self , expr , assign_to = None ) : <EOL> if self . _settings . get ( '<STR_LIT>' , True ) : <EOL> from sympy . tensor import get_contraction_structure <EOL> indices = self . _get_expression_indices ( expr , assign_to ) <EOL> dummies = get_contraction_structure ( expr ) <EOL> else : <EOL> indices = [ ] <EOL> dummies = { None : ( expr , ) } <EOL> openloop , closeloop = self . _get_loop_opening_ending ( indices ) <EOL> if None in dummies : <EOL> text = StrPrinter . doprint ( self , Add ( * dummies [ None ] ) ) <EOL> else : <EOL> text = StrPrinter . doprint ( self , <NUM_LIT:0> ) <EOL> lhs_printed = self . _print ( assign_to ) <EOL> lines = [ ] <EOL> if text != lhs_printed : <EOL> lines . extend ( openloop ) <EOL> if assign_to is not None : <EOL> text = self . _get_statement ( "<STR_LIT>" % ( lhs_printed , text ) ) <EOL> lines . append ( text ) <EOL> lines . extend ( closeloop ) <EOL> for d in dummies : <EOL> if isinstance ( d , tuple ) : <EOL> indices = self . _sort_optimized ( d , expr ) <EOL> openloop_d , closeloop_d = self . _get_loop_opening_ending ( <EOL> indices ) <EOL> for term in dummies [ d ] : <EOL> if term in dummies and not ( [ list ( f . keys ( ) ) for f in dummies [ term ] ] <EOL> == [ [ None ] for f in dummies [ term ] ] ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" ) <EOL> else : <EOL> if assign_to is None : <EOL> raise AssignmentError ( <EOL> "<STR_LIT>" ) <EOL> if term . has ( assign_to ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> lines . extend ( openloop ) <EOL> lines . extend ( openloop_d ) <EOL> text = "<STR_LIT>" % ( lhs_printed , StrPrinter . doprint ( <EOL> self , assign_to + term ) ) <EOL> lines . append ( self . _get_statement ( text ) ) <EOL> lines . extend ( closeloop_d ) <EOL> lines . extend ( closeloop ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def _get_expression_indices ( self , expr , assign_to ) : <EOL> from sympy . tensor import get_indices <EOL> rinds , junk = get_indices ( expr ) <EOL> linds , junk = get_indices ( assign_to ) <EOL> if linds and not rinds : <EOL> rinds = linds <EOL> if rinds != linds : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % expr ) <EOL> return self . _sort_optimized ( rinds , assign_to ) <EOL> def _sort_optimized ( self , indices , expr ) : <EOL> from sympy . tensor . indexed import Indexed <EOL> if not indices : <EOL> return [ ] <EOL> score_table = { } <EOL> for i in indices : <EOL> score_table [ i ] = <NUM_LIT:0> <EOL> arrays = expr . atoms ( Indexed ) <EOL> for arr in arrays : <EOL> for p , ind in enumerate ( arr . indices ) : <EOL> try : <EOL> score_table [ ind ] += self . _rate_index_position ( p ) <EOL> except KeyError : <EOL> pass <EOL> return sorted ( indices , key = lambda x : score_table [ x ] ) <EOL> def _rate_index_position ( self , p ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _get_statement ( self , codestring ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _get_comment ( self , text ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _declare_number_const ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _format_code ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _get_loop_opening_ending ( self , indices ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _print_Assignment ( self , expr ) : <EOL> from sympy . functions . elementary . piecewise import Piecewise <EOL> from sympy . matrices . expressions . matexpr import MatrixSymbol <EOL> from sympy . tensor . indexed import IndexedBase <EOL> lhs = expr . lhs <EOL> rhs = expr . rhs <EOL> if isinstance ( expr . rhs , Piecewise ) : <EOL> expressions = [ ] <EOL> conditions = [ ] <EOL> for ( e , c ) in rhs . args : <EOL> expressions . append ( Assignment ( lhs , e ) ) <EOL> conditions . append ( c ) <EOL> temp = Piecewise ( * zip ( expressions , conditions ) ) <EOL> return self . _print ( temp ) <EOL> elif isinstance ( lhs , MatrixSymbol ) : <EOL> lines = [ ] <EOL> for ( i , j ) in self . _traverse_matrix_indices ( lhs ) : <EOL> temp = Assignment ( lhs [ i , j ] , rhs [ i , j ] ) <EOL> code0 = self . _print ( temp ) <EOL> lines . append ( code0 ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> elif self . _settings [ "<STR_LIT>" ] and ( lhs . has ( IndexedBase ) or <EOL> rhs . has ( IndexedBase ) ) : <EOL> return self . _doprint_loops ( rhs , lhs ) <EOL> else : <EOL> lhs_code = self . _print ( lhs ) <EOL> rhs_code = self . _print ( rhs ) <EOL> return self . _get_statement ( "<STR_LIT>" % ( lhs_code , rhs_code ) ) <EOL> def _print_Symbol ( self , expr ) : <EOL> name = super ( CodePrinter , self ) . _print_Symbol ( expr ) <EOL> if name in self . reserved_words : <EOL> if self . _settings [ '<STR_LIT>' ] : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise ValueError ( msg . format ( name ) ) <EOL> return name + self . _settings [ '<STR_LIT>' ] <EOL> else : <EOL> return name <EOL> def _print_Function ( self , expr ) : <EOL> if expr . func . __name__ in self . known_functions : <EOL> cond_func = self . known_functions [ expr . func . __name__ ] <EOL> func = None <EOL> if isinstance ( cond_func , str ) : <EOL> func = cond_func <EOL> else : <EOL> for cond , func in cond_func : <EOL> if cond ( * expr . args ) : <EOL> break <EOL> if func is not None : <EOL> return "<STR_LIT>" % ( func , self . stringify ( expr . args , "<STR_LIT:U+002CU+0020>" ) ) <EOL> elif hasattr ( expr , '<STR_LIT>' ) and isinstance ( expr . _imp_ , Lambda ) : <EOL> return self . _print ( expr . _imp_ ( * expr . args ) ) <EOL> else : <EOL> return self . _print_not_supported ( expr ) <EOL> def _print_NumberSymbol ( self , expr ) : <EOL> self . _number_symbols . add ( ( expr , <EOL> self . _print ( expr . evalf ( self . _settings [ "<STR_LIT>" ] ) ) ) ) <EOL> return str ( expr ) <EOL> def _print_Dummy ( self , expr ) : <EOL> return "<STR_LIT>" % ( expr . name , expr . dummy_index ) <EOL> def _print_Catalan ( self , expr ) : <EOL> return self . _print_NumberSymbol ( expr ) <EOL> def _print_EulerGamma ( self , expr ) : <EOL> return self . _print_NumberSymbol ( expr ) <EOL> def _print_GoldenRatio ( self , expr ) : <EOL> return self . _print_NumberSymbol ( expr ) <EOL> def _print_Exp1 ( self , expr ) : <EOL> return self . _print_NumberSymbol ( expr ) <EOL> def _print_Pi ( self , expr ) : <EOL> return self . _print_NumberSymbol ( expr ) <EOL> def _print_And ( self , expr ) : <EOL> PREC = precedence ( expr ) <EOL> return ( "<STR_LIT>" % self . _operators [ '<STR_LIT>' ] ) . join ( self . parenthesize ( a , PREC ) <EOL> for a in sorted ( expr . args , key = default_sort_key ) ) <EOL> def _print_Or ( self , expr ) : <EOL> PREC = precedence ( expr ) <EOL> return ( "<STR_LIT>" % self . _operators [ '<STR_LIT>' ] ) . join ( self . parenthesize ( a , PREC ) <EOL> for a in sorted ( expr . args , key = default_sort_key ) ) <EOL> def _print_Xor ( self , expr ) : <EOL> if self . _operators . get ( '<STR_LIT>' ) is None : <EOL> return self . _print_not_supported ( expr ) <EOL> PREC = precedence ( expr ) <EOL> return ( "<STR_LIT>" % self . _operators [ '<STR_LIT>' ] ) . join ( self . parenthesize ( a , PREC ) <EOL> for a in expr . args ) <EOL> def _print_Equivalent ( self , expr ) : <EOL> if self . _operators . get ( '<STR_LIT>' ) is None : <EOL> return self . _print_not_supported ( expr ) <EOL> PREC = precedence ( expr ) <EOL> return ( "<STR_LIT>" % self . _operators [ '<STR_LIT>' ] ) . join ( self . parenthesize ( a , PREC ) <EOL> for a in expr . args ) <EOL> def _print_Not ( self , expr ) : <EOL> PREC = precedence ( expr ) <EOL> return self . _operators [ '<STR_LIT>' ] + self . parenthesize ( expr . args [ <NUM_LIT:0> ] , PREC ) <EOL> def _print_Mul ( self , expr ) : <EOL> prec = precedence ( expr ) <EOL> c , e = expr . as_coeff_Mul ( ) <EOL> if c < <NUM_LIT:0> : <EOL> expr = _keep_coeff ( - c , e ) <EOL> sign = "<STR_LIT:->" <EOL> else : <EOL> sign = "<STR_LIT>" <EOL> a = [ ] <EOL> b = [ ] <EOL> if self . order not in ( '<STR_LIT>' , '<STR_LIT:none>' ) : <EOL> args = expr . as_ordered_factors ( ) <EOL> else : <EOL> args = Mul . make_args ( expr ) <EOL> for item in args : <EOL> if item . is_commutative and item . is_Pow and item . exp . is_Rational and item . exp . is_negative : <EOL> if item . exp != - <NUM_LIT:1> : <EOL> b . append ( Pow ( item . base , - item . exp , evaluate = False ) ) <EOL> else : <EOL> b . append ( Pow ( item . base , - item . exp ) ) <EOL> else : <EOL> a . append ( item ) <EOL> a = a or [ S . One ] <EOL> a_str = [ self . parenthesize ( x , prec ) for x in a ] <EOL> b_str = [ self . parenthesize ( x , prec ) for x in b ] <EOL> if len ( b ) == <NUM_LIT:0> : <EOL> return sign + '<STR_LIT:*>' . join ( a_str ) <EOL> elif len ( b ) == <NUM_LIT:1> : <EOL> return sign + '<STR_LIT:*>' . join ( a_str ) + "<STR_LIT:/>" + b_str [ <NUM_LIT:0> ] <EOL> else : <EOL> return sign + '<STR_LIT:*>' . join ( a_str ) + "<STR_LIT>" % '<STR_LIT:*>' . join ( b_str ) <EOL> def _print_not_supported ( self , expr ) : <EOL> self . _not_supported . add ( expr ) <EOL> return self . emptyPrinter ( expr ) <EOL> _print_Basic = _print_not_supported <EOL> _print_ComplexInfinity = _print_not_supported <EOL> _print_Derivative = _print_not_supported <EOL> _print_dict = _print_not_supported <EOL> _print_ExprCondPair = _print_not_supported <EOL> _print_GeometryEntity = _print_not_supported <EOL> _print_Infinity = _print_not_supported <EOL> _print_Integral = _print_not_supported <EOL> _print_Interval = _print_not_supported <EOL> _print_AccumulationBounds = _print_not_supported <EOL> _print_Limit = _print_not_supported <EOL> _print_list = _print_not_supported <EOL> _print_Matrix = _print_not_supported <EOL> _print_ImmutableMatrix = _print_not_supported <EOL> _print_MutableDenseMatrix = _print_not_supported <EOL> _print_MatrixBase = _print_not_supported <EOL> _print_DeferredVector = _print_not_supported <EOL> _print_NaN = _print_not_supported <EOL> _print_NegativeInfinity = _print_not_supported <EOL> _print_Normal = _print_not_supported <EOL> _print_Order = _print_not_supported <EOL> _print_PDF = _print_not_supported <EOL> _print_RootOf = _print_not_supported <EOL> _print_RootsOf = _print_not_supported <EOL> _print_RootSum = _print_not_supported <EOL> _print_Sample = _print_not_supported <EOL> _print_SparseMatrix = _print_not_supported <EOL> _print_tuple = _print_not_supported <EOL> _print_Uniform = _print_not_supported <EOL> _print_Unit = _print_not_supported <EOL> _print_Wild = _print_not_supported <EOL> _print_WildFunction = _print_not_supported </s>
