<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from sympy import S , Expr , Mul , Integer <EOL> from sympy . core . compatibility import u <EOL> from sympy . printing . pretty . stringpict import prettyForm <EOL> from sympy . physics . quantum . operator import Operator <EOL> from sympy . physics . quantum . dagger import Dagger <EOL> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> class AntiCommutator ( Expr ) : <EOL> """<STR_LIT>""" <EOL> is_commutative = False <EOL> def __new__ ( cls , A , B ) : <EOL> r = cls . eval ( A , B ) <EOL> if r is not None : <EOL> return r <EOL> obj = Expr . __new__ ( cls , A , B ) <EOL> return obj <EOL> @ classmethod <EOL> def eval ( cls , a , b ) : <EOL> if not ( a and b ) : <EOL> return S . Zero <EOL> if a == b : <EOL> return Integer ( <NUM_LIT:2> ) * a ** <NUM_LIT:2> <EOL> if a . is_commutative or b . is_commutative : <EOL> return Integer ( <NUM_LIT:2> ) * a * b <EOL> ca , nca = a . args_cnc ( ) <EOL> cb , ncb = b . args_cnc ( ) <EOL> c_part = ca + cb <EOL> if c_part : <EOL> return Mul ( Mul ( * c_part ) , cls ( Mul . _from_args ( nca ) , Mul . _from_args ( ncb ) ) ) <EOL> if a . compare ( b ) == <NUM_LIT:1> : <EOL> return cls ( b , a ) <EOL> def doit ( self , ** hints ) : <EOL> """<STR_LIT>""" <EOL> A = self . args [ <NUM_LIT:0> ] <EOL> B = self . args [ <NUM_LIT:1> ] <EOL> if isinstance ( A , Operator ) and isinstance ( B , Operator ) : <EOL> try : <EOL> comm = A . _eval_anticommutator ( B , ** hints ) <EOL> except NotImplementedError : <EOL> try : <EOL> comm = B . _eval_anticommutator ( A , ** hints ) <EOL> except NotImplementedError : <EOL> comm = None <EOL> if comm is not None : <EOL> return comm . doit ( ** hints ) <EOL> return ( A * B + B * A ) . doit ( ** hints ) <EOL> def _eval_adjoint ( self ) : <EOL> return AntiCommutator ( Dagger ( self . args [ <NUM_LIT:0> ] ) , Dagger ( self . args [ <NUM_LIT:1> ] ) ) <EOL> def _sympyrepr ( self , printer , * args ) : <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , printer . _print ( <EOL> self . args [ <NUM_LIT:0> ] ) , printer . _print ( self . args [ <NUM_LIT:1> ] ) <EOL> ) <EOL> def _sympystr ( self , printer , * args ) : <EOL> return "<STR_LIT>" % ( self . args [ <NUM_LIT:0> ] , self . args [ <NUM_LIT:1> ] ) <EOL> def _pretty ( self , printer , * args ) : <EOL> pform = printer . _print ( self . args [ <NUM_LIT:0> ] , * args ) <EOL> pform = prettyForm ( * pform . right ( ( prettyForm ( u'<STR_LIT:U+002C>' ) ) ) ) <EOL> pform = prettyForm ( * pform . right ( ( printer . _print ( self . args [ <NUM_LIT:1> ] , * args ) ) ) ) <EOL> pform = prettyForm ( * pform . parens ( left = '<STR_LIT:{>' , right = '<STR_LIT:}>' ) ) <EOL> return pform <EOL> def _latex ( self , printer , * args ) : <EOL> return "<STR_LIT>" % tuple ( [ <EOL> printer . _print ( arg , * args ) for arg in self . args ] ) </s>
