<s> from __future__ import print_function , division <EOL> from sympy import pi , I <EOL> from sympy . core . singleton import S <EOL> from sympy . core import Dummy , sympify <EOL> from sympy . core . function import Function , ArgumentIndexError <EOL> from sympy . functions import assoc_legendre <EOL> from sympy . functions . elementary . trigonometric import sin , cos , cot <EOL> from sympy . functions . combinatorial . factorials import factorial <EOL> from sympy . functions . elementary . complexes import Abs <EOL> from sympy . functions . elementary . exponential import exp <EOL> from sympy . functions . elementary . miscellaneous import sqrt <EOL> _x = Dummy ( "<STR_LIT:x>" ) <EOL> class Ynm ( Function ) : <EOL> r"""<STR_LIT>""" <EOL> @ classmethod <EOL> def eval ( cls , n , m , theta , phi ) : <EOL> n , m , theta , phi = [ sympify ( x ) for x in ( n , m , theta , phi ) ] <EOL> if m . could_extract_minus_sign ( ) : <EOL> m = - m <EOL> return S . NegativeOne ** m * exp ( - <NUM_LIT:2> * I * m * phi ) * Ynm ( n , m , theta , phi ) <EOL> if theta . could_extract_minus_sign ( ) : <EOL> theta = - theta <EOL> return Ynm ( n , m , theta , phi ) <EOL> if phi . could_extract_minus_sign ( ) : <EOL> phi = - phi <EOL> return exp ( - <NUM_LIT:2> * I * m * phi ) * Ynm ( n , m , theta , phi ) <EOL> def _eval_expand_func ( self , ** hints ) : <EOL> n , m , theta , phi = self . args <EOL> rv = ( sqrt ( ( <NUM_LIT:2> * n + <NUM_LIT:1> ) / ( <NUM_LIT:4> * pi ) * factorial ( n - m ) / factorial ( n + m ) ) * <EOL> exp ( I * m * phi ) * assoc_legendre ( n , m , cos ( theta ) ) ) <EOL> return rv . subs ( sqrt ( - cos ( theta ) ** <NUM_LIT:2> + <NUM_LIT:1> ) , sin ( theta ) ) <EOL> def fdiff ( self , argindex = <NUM_LIT:4> ) : <EOL> if argindex == <NUM_LIT:1> : <EOL> raise ArgumentIndexError ( self , argindex ) <EOL> elif argindex == <NUM_LIT:2> : <EOL> raise ArgumentIndexError ( self , argindex ) <EOL> elif argindex == <NUM_LIT:3> : <EOL> n , m , theta , phi = self . args <EOL> return ( m * cot ( theta ) * Ynm ( n , m , theta , phi ) + <EOL> sqrt ( ( n - m ) * ( n + m + <NUM_LIT:1> ) ) * exp ( - I * phi ) * Ynm ( n , m + <NUM_LIT:1> , theta , phi ) ) <EOL> elif argindex == <NUM_LIT:4> : <EOL> n , m , theta , phi = self . args <EOL> return I * m * Ynm ( n , m , theta , phi ) <EOL> else : <EOL> raise ArgumentIndexError ( self , argindex ) <EOL> def _eval_rewrite_as_polynomial ( self , n , m , theta , phi ) : <EOL> return self . expand ( func = True ) <EOL> def _eval_rewrite_as_sin ( self , n , m , theta , phi ) : <EOL> return self . rewrite ( cos ) <EOL> def _eval_rewrite_as_cos ( self , n , m , theta , phi ) : <EOL> from sympy . simplify import simplify , trigsimp <EOL> term = simplify ( self . expand ( func = True ) ) <EOL> term = term . xreplace ( { Abs ( sin ( theta ) ) : sin ( theta ) } ) <EOL> return simplify ( trigsimp ( term ) ) <EOL> def _eval_conjugate ( self ) : <EOL> n , m , theta , phi = self . args <EOL> return S . NegativeOne ** m * self . func ( n , - m , theta , phi ) <EOL> def as_real_imag ( self , deep = True , ** hints ) : <EOL> n , m , theta , phi = self . args <EOL> re = ( sqrt ( ( <NUM_LIT:2> * n + <NUM_LIT:1> ) / ( <NUM_LIT:4> * pi ) * factorial ( n - m ) / factorial ( n + m ) ) * <EOL> cos ( m * phi ) * assoc_legendre ( n , m , cos ( theta ) ) ) <EOL> im = ( sqrt ( ( <NUM_LIT:2> * n + <NUM_LIT:1> ) / ( <NUM_LIT:4> * pi ) * factorial ( n - m ) / factorial ( n + m ) ) * <EOL> sin ( m * phi ) * assoc_legendre ( n , m , cos ( theta ) ) ) <EOL> return ( re , im ) <EOL> def _eval_evalf ( self , prec ) : <EOL> from mpmath import mp , workprec <EOL> from sympy import Expr <EOL> n = self . args [ <NUM_LIT:0> ] . _to_mpmath ( prec ) <EOL> m = self . args [ <NUM_LIT:1> ] . _to_mpmath ( prec ) <EOL> theta = self . args [ <NUM_LIT:2> ] . _to_mpmath ( prec ) <EOL> phi = self . args [ <NUM_LIT:3> ] . _to_mpmath ( prec ) <EOL> with workprec ( prec ) : <EOL> res = mp . spherharm ( n , m , theta , phi ) <EOL> return Expr . _from_mpmath ( res , prec ) <EOL> def _sage_ ( self ) : <EOL> import sage . all as sage <EOL> return sage . spherical_harmonic ( self . args [ <NUM_LIT:0> ] . _sage_ ( ) , <EOL> self . args [ <NUM_LIT:1> ] . _sage_ ( ) , <EOL> self . args [ <NUM_LIT:2> ] . _sage_ ( ) , <EOL> self . args [ <NUM_LIT:3> ] . _sage_ ( ) ) <EOL> def Ynm_c ( n , m , theta , phi ) : <EOL> r"""<STR_LIT>""" <EOL> from sympy import conjugate <EOL> return conjugate ( Ynm ( n , m , theta , phi ) ) <EOL> class Znm ( Function ) : <EOL> r"""<STR_LIT>""" <EOL> @ classmethod <EOL> def eval ( cls , n , m , theta , phi ) : <EOL> n , m , th , ph = [ sympify ( x ) for x in ( n , m , theta , phi ) ] <EOL> if m . is_positive : <EOL> zz = ( Ynm ( n , m , th , ph ) + Ynm_c ( n , m , th , ph ) ) / sqrt ( <NUM_LIT:2> ) <EOL> return zz <EOL> elif m . is_zero : <EOL> return Ynm ( n , m , th , ph ) <EOL> elif m . is_negative : <EOL> zz = ( Ynm ( n , m , th , ph ) - Ynm_c ( n , m , th , ph ) ) / ( sqrt ( <NUM_LIT:2> ) * I ) <EOL> return zz </s>
