
    MZd^                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m	Z	 ddl
mZ h d	Zd
dddZi ddddddddddddddddddddddddddddddddddd d!d"d"d#d$d%d&d'd(d)d*d+Zd,d-d,d.d/d/d0Zd1 Zd2 Z G d3 d4e      Z G d5 d6      Z G d7 d8e      Zej*                  D ]  Z eed9ez  e        eD ]  Z eed9ez  e        d: Zd;j3                         Zej7                         D  cg c]  \  } }| evs| |f c}} Z eefi d<d=d>d?d@dAdBdCdDdEdFdGdHZd,d-dIdJdKd/d.dLdMZdN Z  G dO dPe      Z!e!j*                  D ]  Z ee!d9ez  e        eD ]  Z ee!d9ez  e         G dQ dRe      Z"ySc c}} w )Tzy
Python code printers

This module contains Python code printers for plain Python as well as NumPy & SciPy enabled code.
    )defaultdict)chain)S)Mod   )
precedence)CodePrinter>!   asifinisoranddefdelfornottryNoneTrueelifelsefrompasswithFalsebreakclassraisewhileyieldassertexceptglobalimportlambdareturnfinallycontinuenonlocalabsminmax)AbsMinMaxacosacoshasinasinhatanatan2atanhceilingceilcoscosherferfcexpexpm1	factorialfloorgammahypotlgammaloglog10log1plog2sinsinhsqrttantanh)rC   loggammarE   lnrF   rG   rH   rI   rJ   SqrtrL   rM   epiinfnan)Exp1PiEInfinityNaNComplexInfinityc                       j                   |j                  j                     }dj                   j	                  |      dj                   fd|j                  D                    S )Nz{name}({args}), c              3   @   K   | ]  }j                  |        y wN_print.0argselfs     7/usr/lib/python3/dist-packages/sympy/printing/pycode.py	<genexpr>z$_print_known_func.<locals>.<genexpr>E   s     2Y4;;s3C2Y   )nameargs)known_functions	__class____name__format_module_formatjoinri   rd   exprknowns   `  re   _print_known_funcrs   B   sZ      !8!89E""(;(;E(B(,		2Ytyy2Y(Z # \ \    c                 j    | j                   |j                  j                     }| j                  |      S r^   )known_constantsrk   rl   rn   rp   s      re   _print_known_constrw   H   s-      !8!89Eu%%rt   c                   <   e Zd ZdZdZeZdZdZ e	 e
ej                         ej                         D  cg c]  \  }}|d|z   f c}}}             Zej                         D  ci c]  \  }}|d|z    c}}} Zdddd	Z e	ej&                  i d
dddd      Zd-fd	Zd Zd.dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!fdZ"d Z#d Z$d  Z%d! Z&d" Z'd# xZ(xZ)xZ*xZ+xZ,xZ-xZ.Z/d$ Z0d% Z1d& Z2d' Z3d( Z4d) Z5d* Z6d+ Z7d/d,Z8xZ9S c c}}} w c c}}} w )0AbstractPythonCodePrinter_pythoncodePythonNz    zmath.r   r   r   )r   r   r      TFpython3)user_functions	precisioninlinefully_qualified_modulescontractstandardc                    t         |   |       | j                  d   }|)dd l}dj	                  |j
                  j                        }|dk7  rt        d      || _        t        t              | _        t        | j                  fi |xs i j                  di       | _        t        | j                   fi |xs i j                  di       | _        y )Nr   r   zpython{}r}   zOnly Python 3 is supported.r~   user_constants)super__init__	_settingssysrm   version_infomajor
ValueErrorr   r   setmodule_importsdict_kfgetrj   _kcrv   )rd   settingsstdr   rk   s       re   r   z"AbstractPythonCodePrinter.__init__c   s    " nnZ(;##C$4$4$:$:;C):;;)#.  $DHH  #R0D0Db1"  ##DHH  #R0D0Db1"  #rt   c                     |d|S Nz =  )rd   rh   values      re   _declare_number_constz/AbstractPythonCodePrinter._declare_number_constw   s     %((rt   c                 <   |j                  d      }|rAt        |      dkD  r3| j                  dj                  |d d          j	                  |d          | j
                  d   r|S |j                  d      d   j                  d      d   j                  d      d   S )N.r   r   (r   [)splitlenr   ro   addr   )rd   fqnregisterpartss       re   rn   z(AbstractPythonCodePrinter._module_formatz   s    		#E
Qs 4599%)D>>34J99S>!$**3/288=bAArt   c                     |S r^   r   )rd   liness     re   _format_codez&AbstractPythonCodePrinter._format_code   s    rt   c                 $    dj                  |      S )Nz{}rm   )rd   
codestrings     re   _get_statementz(AbstractPythonCodePrinter._get_statement   s    {{:&&rt   c                 $    dj                  |      S )Nz  # {}r   )rd   texts     re   _get_commentz&AbstractPythonCodePrinter._get_comment   s    t$$rt   c                     t        |      dk(  r| j                  |d         S | j                  |      d| j                  ||dd       d| j                  |d         dS )z
        This method expands a fold on binary operations.

        ``functools.reduce`` is an example of a folded operation.

        For example, the expression

        `A + B + C + D`

        is folded into

        `((A + B) + C) + D`
        r   r   r   Nr   r\   ))r   r`   rn   _expand_fold_binary_op)rd   opri   s      re   r   z0AbstractPythonCodePrinter._expand_fold_binary_op   sb     t9>;;tAw'' ##B'++BSb	:DH% rt   c           	          t        |      dk(  r| j                  |d         S t        |      }|dz  }| j                  |      d| j                  |d|       d| j                  ||d       dS )z
        This method expands a reductin on binary operations.

        Notice: this is NOT the same as ``functools.reduce``.

        For example, the expression

        `A + B + C + D`

        is reduced into:

        `(A + B) + (C + D)`
        r   r      r   Nr\   r   )r   r`   rn   _expand_reduce_binary_op)rd   r   ri   NNhalfs        re   r   z2AbstractPythonCodePrinter._expand_reduce_binary_op   st     t9>;;tAw''D	AFE##B'--d6El;--d56l; rt   c                      y)Nzfloat('nan')r   rd   rq   s     re   
_print_NaNz$AbstractPythonCodePrinter._print_NaN       rt   c                      y)Nzfloat('inf')r   r   s     re   _print_Infinityz)AbstractPythonCodePrinter._print_Infinity   r   rt   c                      y)Nzfloat('-inf')r   r   s     re   _print_NegativeInfinityz1AbstractPythonCodePrinter._print_NegativeInfinity   s    rt   c                 $    | j                  |      S r^   )r   r   s     re   _print_ComplexInfinityz0AbstractPythonCodePrinter._print_ComplexInfinity   s    t$$rt   c                 b     t        |       dj                   fd|j                  D         S )Nz{} % {}c              3   B   K   | ]  }j                  |        y wr^   )parenthesize)rb   xPRECrd   s     re   rf   z7AbstractPythonCodePrinter._print_Mod.<locals>.<genexpr>   s     "Q!4#4#4Q#="Qs   )r   rm   ri   rd   rq   r   s   ` @re   
_print_Modz$AbstractPythonCodePrinter._print_Mod   s*    $ 	  "Qtyy"QRSrt   c                 &   g }d}|j                   D ]  }|j                  }|j                  }|dk(  r|j                  d       |j                  d       |j                  | j	                  |             |j                  d       |j                  d       |j                  | j	                  |             |j                  d       |dz  } |d d }|d   dk(  r|d d	 }|j                  d       n|j                  d
       dj                  |      S )Nr   r   r   z if z else r   r   r   z else None) )ri   rq   condappendr`   ro   )rd   rq   resultirc   rQ   cs          re   _print_Piecewisez*AbstractPythonCodePrinter._print_Piecewise   s    99 	CAAAvc"MM#MM$++a.)MM#MM&!MM$++a.)MM(#FA	 ":CR[FMM#MM-(wwvrt   c                     ddddddd}|j                   |v rT| j                  |j                        }| j                  |j                        }dj	                  |j                   ||	      S t
        |   |      S )
z.Relational printer for Equality and Unequalityequal	not_equalless
less_equalgreatergreater_equal)z==z!=<z<=>z>=z({lhs} {op} {rhs}))r   lhsrhs)rel_opr`   r   r   rm   r   _print_Relational)rd   rq   r   r   r   rk   s        re   r   z+AbstractPythonCodePrinter._print_Relational   s|     !
 ;;"++dhh'C++dhh'C'..$++3C.PPw(..rt   c                 N    ddl m} | j                  |j                  |            S )Nr   )	Piecewise)$sympy.functions.elementary.piecewiser   r`   rewrite)rd   rq   r   s      re   
_print_ITEz$AbstractPythonCodePrinter._print_ITE   s    B{{4<<	233rt   c                       fd|j                   D        }dj                   j                  |j                        dj	                  |            S )Nc              3      K   | ]I  \  }}}d j                  j                  |      j                  |      j                  |             K yw)zfor {i} in range({a}, {b}+1))r   abN)rm   r`   )rb   r   r   r   rd   s       re   rf   z7AbstractPythonCodePrinter._print_Sum.<locals>.<genexpr>   sR      (
 1a	 +11++a.++a.++a. 2 "(s   AAz"(builtins.sum({function} {loops})) )functionloops)limitsrm   r`   r   ro   )rd   rq   r   s   `  re   
_print_Sumz$AbstractPythonCodePrinter._print_Sum   sM    (
  ;;( 4::[[/((5/ ; # 	#rt   c                      y)N1jr   r   s     re   _print_ImaginaryUnitz.AbstractPythonCodePrinter._print_ImaginaryUnit  s    rt   c                     |j                   \  }}dj                  | j                  |      | j                  |            S )Nz(1 if {a} == {b} else 0))r   r   )ri   rm   r`   )rd   rq   r   r   s       re   _print_KroneckerDeltaz/AbstractPythonCodePrinter._print_KroneckerDelta  s=    yy1)00AA 1 
 	
rt   c                     |j                   j                  }| j                  j                  ||      }|d| j	                  |j                               dS )Nr   r   )rk   rl   rj   r   r`   tolist)rd   rq   rh   funcs       re   _print_MatrixBasez+AbstractPythonCodePrinter._print_MatrixBase  sC    ~~&&##''d3T[[]!;<<rt   c                 $    | j                  |      S r^   )r   r   s     re   <lambda>z"AbstractPythonCodePrinter.<lambda>  s    411$7 rt   c                     dj                  |j                  d      D cg c]  }| j                  |z    c}      S c c}w )N
)ro   r   tab)rd   r   lines      re   _indent_codestringz,AbstractPythonCodePrinter._indent_codestring  s2    yyj6F6Ft6LMd$((T/MNNMs   <c                 N    dj                   fd|j                  D              }dj                   j                  |j                        dj                  |j
                  D cg c]  } j                  |j                         c}       j                  |            S c c}w )Nr   c              3   @   K   | ]  }j                  |        y wr^   r_   ra   s     re   rf   zFAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<genexpr>!  s     >s$++c*>rg   z def {name}({parameters}):
{body}r\   )rh   
parametersbody)ro   r   rm   r`   rh   r   symbolr   )rd   fdr   vars   `   re   _print_FunctionDefinitionz3AbstractPythonCodePrinter._print_FunctionDefinition   s|    yy>bgg>?299RWW%yyR]]!Sc$++cjj"9!ST((. : 
 	
!Ss   $"B"c                      dj                   fd|j                  D              }dj                   j                  |j                         j                  |            S )Nr   c              3   @   K   | ]  }j                  |        y wr^   r_   ra   s     re   rf   z9AbstractPythonCodePrinter._print_While.<locals>.<genexpr>)  s     ?s$++c*?rg   zwhile {cond}:
{body})r   r   )ro   r   rm   r`   	conditionr   )rd   whlr   s   `  re   _print_Whilez&AbstractPythonCodePrinter._print_While(  sQ    yy?chh?@&--S]]+((. . 
 	
rt   c                     | j                  |j                  j                        d| j                  |j                  j                        S r   )r`   variabler  r   )rd   decls     re   _print_Declarationz,AbstractPythonCodePrinter._print_Declaration/  s8    KK,,-KK++,
 	
rt   c                 F    |j                   \  }d| j                  |      z  S )Nz	return %s)ri   r`   )rd   retrc   s      re   _print_Returnz'AbstractPythonCodePrinter._print_Return5  s!    xxT[[---rt   c                 *    dj                   fd|j                  D              }|j                  d k7  r+dj                   j	                  |j                        |      }|j
                  d k7  r!|d j	                  |j
                        z  z  }d|z  S )Nr\   c              3   @   K   | ]  }j                  |        y wr^   r_   ra   s     re   rf   z9AbstractPythonCodePrinter._print_Print.<locals>.<genexpr>:  s     LSC 0Lrg   z	{} % ({})z	, file=%sz	print(%s))ro   
print_argsformat_stringrm   r`   file)rd   prntr  s   `  re   _print_Printz&AbstractPythonCodePrinter._print_Print9  s    YYLDOOLM
%$++D../=J99+DII(>>>JZ''rt   c                     t        |j                        dk(  r| j                  d      S t        |j                        dk(  r| j                  d      S | j                  |j                        S )Nstdoutz
sys.stdoutstderrz
sys.stderr)strrh   rn   r`   )rd   strms     re   _print_Streamz'AbstractPythonCodePrinter._print_StreamC  sW    tyy>X%&&|44^x'&&|44;;tyy))rt   c                      y)Nr   r   )rd   rc   s     re   _print_NoneTokenz*AbstractPythonCodePrinter._print_NoneTokenK  s    rt   c                 2   t        |      }|j                  t        j                  k(  rA|s?| j	                  |      }| j                  |j                        }dj                  ||      S |j                  r|s|j                   t        j                  u rV| j	                  |      }| j                  t        j                        }| j                  |j                        }| d| d| dS |j                  t        j                  u rD| j                  t        j                        }| j                  |j                  |d      }| d| S | j                  |j                  |d      }| j                  |j                  |d      }	dj                  ||	      S )	a  Printing helper function for ``Pow``

        Notes
        =====

        This preprocesses the ``sqrt`` as math formatter and prints division

        Examples
        ========

        >>> from sympy import sqrt
        >>> from sympy.printing.pycode import PythonCodePrinter
        >>> from sympy.abc import x

        Python code printer automatically looks up ``math.sqrt``.

        >>> printer = PythonCodePrinter()
        >>> printer._hprint_Pow(sqrt(x), rational=True)
        'x**(1/2)'
        >>> printer._hprint_Pow(sqrt(x), rational=False)
        'math.sqrt(x)'
        >>> printer._hprint_Pow(1/sqrt(x), rational=True)
        'x**(-1/2)'
        >>> printer._hprint_Pow(1/sqrt(x), rational=False)
        '1/math.sqrt(x)'
        >>> printer._hprint_Pow(1/x, rational=False)
        '1/x'
        >>> printer._hprint_Pow(1/x, rational=True)
        'x**(-1)'

        Using sqrt from numpy or mpmath

        >>> printer._hprint_Pow(sqrt(x), sqrt='numpy.sqrt')
        'numpy.sqrt(x)'
        >>> printer._hprint_Pow(sqrt(x), sqrt='mpmath.sqrt')
        'mpmath.sqrt(x)'

        See Also
        ========

        sympy.printing.str.StrPrinter._print_Pow
        z{func}({arg}))r   rc   /r   r   F)strictz{}**{})r   r>   r   Halfrn   r`   baserm   is_commutativeOneNegativeOner   )
rd   rq   rationalrK   r   r   rc   numbase_strexp_strs
             re   _hprint_Powz%AbstractPythonCodePrinter._hprint_PowN  sY   V $88qvvh&&t,D++dii(C"))t)==xyAFF"**40kk!%%(kk$)),avQse1--xx1==(kk!%%(''		4'Fau~% $$TYYU$C##DHHd5#Ax11rt   r^   )T)Fz	math.sqrt):rl   
__module____qualname__printmethodlanguage_kwreserved_wordsmodulesr   r   r   _known_functionsitems_known_functions_mathr   _known_constants_mathr   
_operatorsr	   _default_settingsr   r   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   _print_SparseRepMatrix_print_MutableSparseMatrix_print_ImmutableSparseMatrix_print_Matrix_print_DenseMatrix_print_MutableDenseMatrix_print_ImmutableMatrix_print_ImmutableDenseMatrixr   r  r	  r  r  r  r  r  r,  __classcell__)rb   kvrk   s   000@re   ry   ry   M   s   KHNG
C
u &;&A&A&CDDda!Wq[	D C %:$?$?$A
B
BDAq1gai<
BCd59J%% $#()B'%.2%T./ 4	#
= 	88 8"8$8 	8 		8
 	"8 	#O


.(*@2s 	E
Bs   D)Dry   c                   T    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)ArrayPrinterc                 B    ddl m} 	  ||      S # t        $ r |cY S w xY w)Nr   )convert_indexed_to_array)4sympy.tensor.array.expressions.from_indexed_to_arrayrH  	Exception)rd   indexedrH  s      re   	_arrayifyzArrayPrinter._arrayify  s)    a	+G44 	N	s    c           	         | j                         }d}d}|D ci c]  }|D ]  }|t        |        }}}g }	|D ]T  }
g }t        |
      D ]1  }||v r|j                  ||          n|j                  |       |dz  }3 |	j                  |       V i }g }g }|	D ]Y  }|D ]M  }||vrt	        |      }|||<   n||   }||z  }||v r||vs+|j                  |       =|j                  |       O |dz  }[ |d d }|||fS c c}}w )Nr   r   r   ,r   ) _get_letter_generator_for_einsumr,   ranger   next)rd   subrankscontraction_indicesletterscontraction_stringcounterr   jdindicesrank_arglindicesmappingletters_freeletters_dumls                   re   _get_einsum_stringzArrayPrinter._get_einsum_string  s_   779 3?1Q?QAY?Q??  	%HH8_ a<OOAgJ/OOG,1 NN8$	%  	&A +G#WA!"GAJ
A"a'"6+#**1- ''*+ #%	& 04!<<<; @s   D c              #      K   t        dd      D ]  }t        |        t        dd      D ]  }t        |        t        d      w)Na   {   A   [   zout of letters)rP  chrr   )rd   r   s     re   rO  z-ArrayPrinter._get_letter_generator_for_einsum  sN     r3 	Aa&L	r2 	Aa&L	)**s   AA
c                    | j                         }dj                  |j                  D cg c]3  }dj                  t        |      D cg c]  }t	        |       c}      5 c}}      }| j                  | j                  dz   | j                  z         d|ddj                  |j                  D cg c]  }| j                  |       c}      dS c c}w c c}}w c c}w )NrN  r   r   ("", r\   r   )
rO  ro   rR  rP  rQ  rn   _module_einsumri   r`   )rd   rq   rT  r   rW  rU  rc   s          re   _print_ArrayTensorProductz&ArrayPrinter._print_ArrayTensorProduct  s    779 XX[_[h[h&iVWrwwuQx/P!W/P'Q&ij##DLL3$6$EF"		tyyA4;;s+AB
 	
 0Q&i Bs   C
C	C
3C C
c           
         ddl m} |j                  }|j                  }t	        ||      rHdj                  |j                  D cg c]  }d| j                  |      z   c}      }|j                  }n'| j                  |      }t        |j                        g}| j                  ||      \  }}	}
|s| j                  |      S t	        ||      r<dj                  |j                  D cg c]  }d| j                  |      z   c}      }n| j                  |      }| j                  | j                  dz   | j                  z         ddj                  |dj                  t!        |	                  d	|d
S c c}w c c}w )Nr   ArrayTensorProductrN  z%sr   rh  {}->{}r   ri  r   )0sympy.tensor.array.expressions.array_expressionsro  rq   rS  
isinstancero   ri   r`   rR  r   shaper`  rn   rj  rk  rm   sorted)rd   rq   ro  r$  rS  rc   elemsranksrU  r]  r^  s              re   _print_ArrayContractionz$ArrayPrinter._print_ArrayContraction  s?   Wyy"66d./HH499MCddkk#&67MNEMMEKK%E_%E8<8O8OPUWj8k5L+";;t$$d./HH499MCddkk#&67MNEKK%Es 2T\\ ABOO.|8L0MN
 	
 N Ns   E8(E=c           	      ,   ddl m} t        |j                        }t	        |j
                  |      r-|j
                  j                  }|j
                  j                  }n|j                  }|j
                  g}| j                  ||      \  }}}|D 	cg c]  }	| j                  |	       }}	| j                  | j                  dz   | j                  z         ddj                  |dj                  ||z               ddj                  |      d	S c c}	w )
Nr   rn  r   rh  rp  r   ri  r\   r   )rq  ro  listdiagonal_indicesrr  rq   rR  ri   r`  r`   rn   rj  rk  rm   ro   )
rd   rq   ro  rz  rR  ru  diagonal_stringr]  r^  r   s
             re   _print_ArrayDiagonalz!ArrayPrinter._print_ArrayDiagonal  s    W 5 56dii!34yy))HIINNE}}HYYKE595L5LXWg5h2{)./AQ//s 2T\\ ABOOORWW\+5M-NOIIe
 	
 0s   Dc                     | j                  | j                  dz   | j                  z         d| j                  |j                        d| j                  |j
                  j                        dS )Nr   r   r\   r   )rn   rj  
_transposer`   rq   permutation
array_formr   s     re   _print_PermuteDimszArrayPrinter._print_PermuteDims  sT    s 2T__ DEKK		"KK((334
 	
rt   c                 n    | j                  | j                  dz   | j                  z   |j                        S )Nr   )r   rj  _addri   r   s     re   _print_ArrayAddzArrayPrinter._print_ArrayAdd  s+    **4<<#+=		+I499UUrt   c                     | j                  | j                  dz   | j                  z         ddj                  t	        | j
                  |j                              dS Nr   z((rN  z,)))rn   rj  _onesro   mapr`   ri   r   s     re   _print_OneArrayzArrayPrinter._print_OneArray  sG    c 1DJJ >?HHSTYY/0
 	
rt   c                     | j                  | j                  dz   | j                  z         ddj                  t	        | j
                  |j                              dS r  )rn   rj  _zerosro   r  r`   ri   r   s     re   _print_ZeroArrayzArrayPrinter._print_ZeroArray  sG    c 1DKK ?@HHSTYY/0
 	
rt   c                     | j                  | j                  |j                              }| j                  | j                  |j                              }|d|S r   )r`   rL  r   r   )rd   rq   r   r   s       re   _print_AssignmentzArrayPrinter._print_Assignment  sE     kk$..23kk$..23 #''rt   c                 $    | j                  |      S r^   )_print_ArraySymbolr   s     re   _print_IndexedBasezArrayPrinter._print_IndexedBase  s    &&t,,rt   N)rl   r-  r.  rL  r`  rO  rl  rw  r|  r  r  r  r  r  r  r   rt   re   rF  rF    s@    !=F+

4
"
V

(-rt   rF  c                        e Zd Zd Zd Zd Zd	dZd Zd Zd Z	 fdZ
ej                  Zej                  Zej                  Zej                  Z xZS )
PythonCodePrinterc                 ~    dj                  | j                  d      | j                  |j                  d               S )Nz"(0.0 if {e} == 0 else {f}(1, {e}))zmath.copysignr   )frQ   rm   rn   r`   ri   rd   rQ   s     re   _print_signzPythonCodePrinter._print_sign  s=    3::!!/2dkk!&&)6L ; N 	Nrt   c                 v    t        |      }| j                  d   | j                  |j                  d   |      z   S )Nr   r   )r   r8  r   ri   r   s      re   
_print_NotzPythonCodePrinter._print_Not  s5    $u%(9(9$))A,(MMMrt   c                     |j                   d   }|j                   dd  }dj                  t        |      dj                  |D cg c]  }| j	                  |       c}            S c c}w )Nr   r   z{}[{}]r\   )ri   rm   r  ro   r`   )rd   rq   r$  indexinds        re   _print_Indexedz PythonCodePrinter._print_Indexed#  sT    yy|		!"s4y$))QV4W#T[[5E4W*XYY4Ws   A&c                 (    | j                  ||      S )N)r(  r,  rd   rq   r(  s      re   
_print_PowzPythonCodePrinter._print_Pow(  s    x88rt   c                 N    dj                  |j                  |j                        S )Nz{}/{})rm   pqr   s     re   _print_Rationalz!PythonCodePrinter._print_Rational+  s    ~~dffdff--rt   c                 $    | j                  |      S r^   r  r   s     re   _print_HalfzPythonCodePrinter._print_Half.  s    ##D))rt   c                 R    | j                  t        |j                  d   d            S )Nr   r   )r   r   ri   r   s     re   _print_fraczPythonCodePrinter._print_frac1  s     s499Q<344rt   c                    t         |   |      }|| j                  v r=| j                  d   rd}t	        |j                  |            || j                  d   z   S d|v r"|j                  dd      j                  dd      S |S )Nerror_on_reservedzVThis expression includes the symbol "{}" which is a reserved keyword in this language.reserved_word_suffix{r   })r   _print_Symbolr2  r   r   rm   replace)rd   rq   rh   msgrk   s       re   r  zPythonCodePrinter._print_Symbol4  s    w$T*4&&&~~12< D!122$..)?@@@D[<<R(00b99Krt   F)rl   r-  r.  r  r  r  r  r  r  r  r  r	   _print_not_supported_print_lowergamma_print_uppergamma_print_fresnelc_print_fresnelsrB  )rk   s   @re   r  r    s^    NNZ
9.*5 $88#88!66O!66Ort   r  z	_print_%sc                 6    t        |      j                  |       S )a)   Converts an expr to a string of Python code

    Parameters
    ==========

    expr : Expr
        A SymPy expression.
    fully_qualified_modules : bool
        Whether or not to write out full module names of functions
        (``math.sin`` vs. ``sin``). default: ``True``.
    standard : str or None, optional
        Only 'python3' (default) is supported.
        This parameter may be removed in the future.

    Examples
    ========

    >>> from sympy import pycode, tan, Symbol
    >>> pycode(tan(Symbol('x')) + 1)
    'math.tan(x) + 1'

    )r  doprint)rq   r   s     re   pycoder  P  s    . X&..t44rt   z
log1p log2betafracfresnelcfresnelssignrN   hypermeijergbesseljbesselybesselibesselk)r  r  r  r  r  rN   r  r  r  r  r  r  phieulercatalanninf)rU   rV   GoldenRatio
EulerGammaCatalanrY   rX   NegativeInfinityc                     g }g }| j                   D ]F  }t        |      dk(  r|\  }}}nt        d      |j                  |       |j                  ||f       H ||fS )z helper function for _print_Integral that
        - accepts an Integral expression
        - returns a tuple of
           - a list variables of integration
           - a list of tuples of the upper and lower limits of integration
       z%Only definite integrals are supported)r   r   NotImplementedErrorr   )integral_exprintegration_varsr   integration_rangeintegration_varlower_limitupper_limits          re   _unpack_integral_limitsr    sw     F*11 2 !Q&8I5O[+%&MNN0{K012 V##rt   c                   H   e Zd ZdZdZdZ e eej                         e
j                         D  cg c]  \  }}|d|z   f c}}}             Zej                         D  ci c]  \  }}|d|z    c}}} Zd Zd Zd Zd Zd	 Zd
 Zd ZddZd Zyc c}}} w c c}}} w )MpmathPrinterzH
    Lambda printer for mpmath which maintains precision for floats
    _mpmathcodezPython with mpmathzmpmath.c                     t        t        t        t        |j                                    }dj                  | j                  d      |      S )Nz{func}({args})
mpmath.mpf)r   ri   )r  tupler  int_mpf_rm   rn   )rd   rQ   ri   s      re   _print_FloatzMpmathPrinter._print_Float  s@     5S!''*+,&&D,?,?,MTX&YYrt   c                     dj                  | j                  d      | j                  |j                        | j                  |j                              S )Nz{func}({p})/{func}({q})r  )r   r  r  )rm   rn   r`   r  r  r  s     re   r  zMpmathPrinter._print_Rational  sH    (//$$\2kk!##kk!## 0 
 	
rt   c                 $    | j                  |      S r^   r  r  s     re   r  zMpmathPrinter._print_Half  s    ##A&&rt   c                     dj                  | j                  d      | j                  |j                  d         | j                  |j                  d         | j                  d            S )Nz{}({}, {}, {})mpmath.gammaincr   r   z
mpmath.infr  r  s     re   r  zMpmathPrinter._print_uppergamma  sZ    && 12KKq	"KKq	"-	/ 	/rt   c                     dj                  | j                  d      | j                  |j                  d         | j                  |j                  d               S )Nz{}({}, 0, {})r  r   r   r  r  s     re   r  zMpmathPrinter._print_lowergamma  sL    %% 12KKq	"KKq	"$ 	$rt   c                 |    dj                  | j                  d      | j                  |j                  d               S )Nz{0}({1})/{0}(2)z
mpmath.logr   r  r  s     re   _print_log2zMpmathPrinter._print_log2  s8     ''-t{{166!9/EG 	Grt   c                 |    dj                  | j                  d      | j                  |j                  d               S )Nz{}({})zmpmath.log1pr   r  r  s     re   _print_log1pzMpmathPrinter._print_log1p  s6    /QVVAY1GI 	Irt   c                 *    | j                  ||d      S )Nzmpmath.sqrtr(  rK   r  r  s      re   r  zMpmathPrinter._print_Pow  s    xmLLrt   c           	          t        |      \  }}dj                   j                  d      dj                  t	         j
                  |             j                  |j                  d         dj                   fd|D                    S )Nz{}(lambda {}: {}, {})zmpmath.quadr\   r   c              3   b   K   | ]&  }d t        t        j                  |            z   ( yw)z(%s, %s)N)r  r  r`   )rb   r_  rd   s     re   rf   z0MpmathPrinter._print_Integral.<locals>.<genexpr>  s&     Ra*uSa-@'AARs   ,/)r  rm   rn   ro   r  r`   ri   )rd   rQ   r  r   s   `   re   _print_IntegralzMpmathPrinter._print_Integral  st    #:1#= &&--##M2		#dkk+;<=AFF1I&		R6RR	T 	Trt   Nr  )rl   r-  r.  __doc__r/  r0  r   r   r4  r5  _known_functions_mpmathr   _known_constants_mpmathr   r  r  r  r  r  r  r  r  r  )rb   rC  rD  s   000re   r  r    s      K#H
u (?(E(E(GHH1!Y]	H C '>&C&C&E
F
Fda1ik>
FCZ
'/$GIMTa 	I
Fs   B$Br  c                       e Zd ZdZd ZddZy)SymPyPrinterzPython with SymPyc                      |j                   j                  xs d} j                  ||rdndz   |j                   j                  z         ddj	                   fd|j
                  D              dS )Nr   r   r   r\   c              3   @   K   | ]  }j                  |        y wr^   r_   ra   s     re   rf   z/SymPyPrinter._print_Function.<locals>.<genexpr>  s     %L3dkk#&6%Lrg   r   )r   r-  rn   rl   ro   ri   )rd   rq   mods   `  re   _print_FunctionzSymPyPrinter._print_Function  s_    ii""(b..sScb/IDIIL^L^/^_99%L$))%LMO 	Ort   c                 *    | j                  ||d      S )Nz
sympy.sqrtr  r  r  s      re   r  zSymPyPrinter._print_Pow  s    xlKKrt   Nr  )rl   r-  r.  r0  r  r  r   rt   re   r  r    s    "HO
Lrt   r  N)#r  collectionsr   	itertoolsr   
sympy.corer   sympy.core.modr   r   codeprinterr	   r1  r4  r6  r7  rs   rw   ry   rF  r  r   rC  setattrr  r   _not_in_mpmathr5  
_in_mpmathr   r  r  r  r  r  )rC  rD  s   00re   <module>r     s  
 $    " $  

FW F W	
 F W W v 
5 F 
5 F 
5 W   W!" W#$ 
; B 
	 \&
A2 A2H
E- E-P-71 -7` 
		 CA{Q0ABC 
 DA{Q0BCD54 ##%!6!<!<!>ZA!>BYq!fZ
z .   
	 $&AT% ATH 
		 ?AM;?,=>? 
! @AM;?,>?@
L, 
Ls [s   <F 	F 