<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> from sympy . core import Symbol <EOL> from sympy . core . compatibility import iterable <EOL> class MonomialOrder ( object ) : <EOL> """<STR_LIT>""" <EOL> alias = None <EOL> is_global = None <EOL> is_default = False <EOL> def __repr__ ( self ) : <EOL> return self . __class__ . __name__ + "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . alias <EOL> def __call__ ( self , monomial ) : <EOL> raise NotImplementedError <EOL> def __eq__ ( self , other ) : <EOL> return self . __class__ == other . __class__ <EOL> def __hash__ ( self ) : <EOL> return hash ( self . __class__ ) <EOL> def __ne__ ( self , other ) : <EOL> return not ( self == other ) <EOL> class LexOrder ( MonomialOrder ) : <EOL> """<STR_LIT>""" <EOL> alias = '<STR_LIT>' <EOL> is_global = True <EOL> is_default = True <EOL> def __call__ ( self , monomial ) : <EOL> return monomial <EOL> class GradedLexOrder ( MonomialOrder ) : <EOL> """<STR_LIT>""" <EOL> alias = '<STR_LIT>' <EOL> is_global = True <EOL> def __call__ ( self , monomial ) : <EOL> return ( sum ( monomial ) , monomial ) <EOL> class ReversedGradedLexOrder ( MonomialOrder ) : <EOL> """<STR_LIT>""" <EOL> alias = '<STR_LIT>' <EOL> is_global = True <EOL> def __call__ ( self , monomial ) : <EOL> return ( sum ( monomial ) , tuple ( reversed ( [ - m for m in monomial ] ) ) ) <EOL> class ProductOrder ( MonomialOrder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> self . args = args <EOL> def __call__ ( self , monomial ) : <EOL> return tuple ( O ( lamda ( monomial ) ) for ( O , lamda ) in self . args ) <EOL> def __repr__ ( self ) : <EOL> from sympy . core import Tuple <EOL> return self . __class__ . __name__ + repr ( Tuple ( * [ x [ <NUM_LIT:0> ] for x in self . args ] ) ) <EOL> def __str__ ( self ) : <EOL> from sympy . core import Tuple <EOL> return self . __class__ . __name__ + str ( Tuple ( * [ x [ <NUM_LIT:0> ] for x in self . args ] ) ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , ProductOrder ) : <EOL> return False <EOL> return self . args == other . args <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . __class__ , self . args ) ) <EOL> @ property <EOL> def is_global ( self ) : <EOL> if all ( o . is_global is True for o , _ in self . args ) : <EOL> return True <EOL> if all ( o . is_global is False for o , _ in self . args ) : <EOL> return False <EOL> return None <EOL> class InverseOrder ( MonomialOrder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , O ) : <EOL> self . O = O <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:i>" + str ( self . O ) <EOL> def __call__ ( self , monomial ) : <EOL> def inv ( l ) : <EOL> if iterable ( l ) : <EOL> return tuple ( inv ( x ) for x in l ) <EOL> return - l <EOL> return inv ( self . O ( monomial ) ) <EOL> @ property <EOL> def is_global ( self ) : <EOL> if self . O . is_global is True : <EOL> return False <EOL> if self . O . is_global is False : <EOL> return True <EOL> return None <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , InverseOrder ) and other . O == self . O <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . __class__ , self . O ) ) <EOL> lex = LexOrder ( ) <EOL> grlex = GradedLexOrder ( ) <EOL> grevlex = ReversedGradedLexOrder ( ) <EOL> ilex = InverseOrder ( lex ) <EOL> igrlex = InverseOrder ( grlex ) <EOL> igrevlex = InverseOrder ( grevlex ) <EOL> _monomial_key = { <EOL> '<STR_LIT>' : lex , <EOL> '<STR_LIT>' : grlex , <EOL> '<STR_LIT>' : grevlex , <EOL> '<STR_LIT>' : ilex , <EOL> '<STR_LIT>' : igrlex , <EOL> '<STR_LIT>' : igrevlex <EOL> } <EOL> def monomial_key ( order = None , gens = None ) : <EOL> """<STR_LIT>""" <EOL> if order is None : <EOL> order = lex <EOL> if isinstance ( order , Symbol ) : <EOL> order = str ( order ) <EOL> if isinstance ( order , str ) : <EOL> try : <EOL> order = _monomial_key [ order ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" % order ) <EOL> if hasattr ( order , '<STR_LIT>' ) : <EOL> if gens is not None : <EOL> def _order ( expr ) : <EOL> return order ( expr . as_poly ( * gens ) . degree_list ( ) ) <EOL> return _order <EOL> return order <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % order ) <EOL> class _ItemGetter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , seq ) : <EOL> self . seq = tuple ( seq ) <EOL> def __call__ ( self , m ) : <EOL> return tuple ( m [ idx ] for idx in self . seq ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , _ItemGetter ) : <EOL> return False <EOL> return self . seq == other . seq <EOL> def build_product_order ( arg , gens ) : <EOL> """<STR_LIT>""" <EOL> gens2idx = { } <EOL> for i , g in enumerate ( gens ) : <EOL> gens2idx [ g ] = i <EOL> order = [ ] <EOL> for expr in arg : <EOL> name = expr [ <NUM_LIT:0> ] <EOL> var = expr [ <NUM_LIT:1> : ] <EOL> def makelambda ( var ) : <EOL> return _ItemGetter ( gens2idx [ g ] for g in var ) <EOL> order . append ( ( monomial_key ( name ) , makelambda ( var ) ) ) <EOL> return ProductOrder ( * order ) </s>
