
    Rie^                        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;                                Zd< e                                D             Z eefi d=d>d?d@dAdBdCdDdEdFdGdHdIZd,d-dJdKdLd/d.dMdNZdO Z  G dP dQe          Z!e!j        D ]Z ee!d9ez  e           eD ]Z ee!d9ez  e            G dR dSe          Z"dTS )Uzy
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                                         |          d                     fd|j        D                                 S )Nz{name}({args}), c              3   B   K   | ]}                     |          V  d S N_print.0argselfs     5lib/python3.11/site-packages/sympy/printing/pycode.py	<genexpr>z$_print_known_func.<locals>.<genexpr>E   s/      2Y2Y4;;s3C3C2Y2Y2Y2Y2Y2Y    )nameargs)known_functions	__class____name__format_module_formatjoinri   rd   exprknowns   `  re   _print_known_funcrs   B   sk     !89E""(;(;E(B(B(,		2Y2Y2Y2Yty2Y2Y2Y(Z(Z # \ \ \rg   c                 Z    | j         |j        j                 }|                     |          S r^   )known_constantsrk   rl   rn   rp   s      re   _print_known_constrv   H   s(     !89Eu%%%rg   c            	           e Zd ZdZdZeZdZdZ e	 e
e                                d e                                D                                 Zd e                                D             Zddd	d
Z e	ej        i ddddd          Zd/ fd	Zd Zd0d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, Z7d1d.Z8 xZ9S )2AbstractPythonCodePrinter_pythoncodePythonNz    c                 "    g | ]\  }}|d |z   fS zmath. rb   kvs      re   
<listcomp>z$AbstractPythonCodePrinter.<listcomp>U   s%    DDDda!Wq[	DDDrg   c                      i | ]\  }}|d |z   S r|   r}   r~   s      re   
<dictcomp>z$AbstractPythonCodePrinter.<dictcomp>W   s"    
B
B
BDAq1gai
B
B
Brg   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                  }|dk    rt          d          || _        t          t                    | _        t          | j        fi |pi                     di           | _        t          | j        fi |pi                     di           | _        d S )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   _kcru   )rd   settingsstdr   rk   s       re   r   z"AbstractPythonCodePrinter.__init__c   s    """ nZ(;JJJ##C$4$:;;C):;;;)#..  $DH  #  #R0D0Db1" 1"  #  ##DH  #  #R0D0Db1" 1"  #  #rg   c                     |d|S Nz = r}   )rd   rh   values      re   _declare_number_constz/AbstractPythonCodePrinter._declare_number_constw   s     DD%%((rg   c                    |                     d          }|rTt          |          dk    rA| j        d                    |d d                                                |d                    | j        d         r|S |                     d          d                              d          d                              d          d         S )N.r   r   (r   [)splitlenr   ro   addr   )rd   fqnregisterpartss       re   rn   z(AbstractPythonCodePrinter._module_formatz   s    		# 	EE

Qss 4 4599%)DDD>34 	BJ99S>>!$**3//288==bAArg   c                     |S r^   r}   )rd   liness     re   _format_codez&AbstractPythonCodePrinter._format_code   s    rg   c                 ,    d                     |          S )Nz{}rm   rd   
codestrings     re   _get_statementz(AbstractPythonCodePrinter._get_statement   s    {{:&&&rg   c                 ,    d                     |          S )Nz  # {}r   )rd   texts     re   _get_commentz&AbstractPythonCodePrinter._get_comment   s    t$$$rg   c                    t          |          dk    r|                     |d                   S |                     |          d|                     ||dd                   d|                     |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   s     t99>>;;tAw''' ##B''''++BSbS	::::DH%%%% rg   c           	      .   t          |          dk    r|                     |d                   S t          |          }|dz  }|                     |          d|                     |d|                   d|                     ||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   s     t99>>;;tAw'''D		AFE##B''''--d6E6l;;;;--d566l;;;; rg   c                     dS )Nzfloat('nan')r}   rd   rq   s     re   
_print_NaNz$AbstractPythonCodePrinter._print_NaN       ~rg   c                     dS )Nzfloat('inf')r}   r   s     re   _print_Infinityz)AbstractPythonCodePrinter._print_Infinity   r   rg   c                     dS )Nzfloat('-inf')r}   r   s     re   _print_NegativeInfinityz1AbstractPythonCodePrinter._print_NegativeInfinity   s    rg   c                 ,    |                      |          S r^   )r   r   s     re   _print_ComplexInfinityz0AbstractPythonCodePrinter._print_ComplexInfinity   s    t$$$rg   c                 \     t          |           dj         fd|j        D              S )Nz{} % {}c              3   D   K   | ]}                     |          V  d S r^   )parenthesize)rb   xPRECrd   s     re   rf   z7AbstractPythonCodePrinter._print_Mod.<locals>.<genexpr>   s3      "Q"Q!4#4#4Q#=#="Q"Q"Q"Q"Q"Qrg   )r   rm   ri   rd   rq   r   s   ` @re   
_print_Modz$AbstractPythonCodePrinter._print_Mod   s;    $ 	 "Q"Q"Q"Q"Qty"Q"Q"QRSrg   c                    g }d}|j         D ]}|j        }|j        }|dk    r|                    d           |                    d           |                    |                     |                     |                    d           |                    d           |                    |                     |                     |                    d           |dz  }|d d         }|d         dk    r |d d	         }|                    d           n|                    d
           d                    |          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   s1   9 	 	CAAAvvc"""MM#MM$++a..)))MM#MM&!!!MM$++a..)))MM(###FAA":CRC[FMM#MM-(((wwvrg   c                    ddddddd}|j         |v rQ|                     |j                  }|                     |j                  }d                    |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     !
 
 ;"++dh''C++dh''C'..$+3C.PPPww((...rg   c                 ^    ddl m} |                     |                    |                    S )Nr   )	Piecewise)$sympy.functions.elementary.piecewiser   r`   rewrite)rd   rq   r   s      re   
_print_ITEz$AbstractPythonCodePrinter._print_ITE   s2    BBBBBB{{4<<	22333rg   c                       fd|j         D             }d                                         |j                  d                    |                    S )Nc              3      K   | ]Y\  }}}d                                          |                              |                              |                    V  ZdS )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>   s{       ( (
 1a	 +11++a..++a..++a.. 2 " "( ( ( ( ( (rg   z"(builtins.sum({function} {loops})) )functionloops)limitsrm   r`   r   ro   )rd   rq   r   s   `  re   
_print_Sumz$AbstractPythonCodePrinter._print_Sum   sl    ( ( ( (
  ;( ( ( 4::[[//((5// ; # # 	#rg   c                     dS )N1jr}   r   s     re   _print_ImaginaryUnitz.AbstractPythonCodePrinter._print_ImaginaryUnit  s    trg   c                     |j         \  }}d                    |                     |          |                     |                    S )Nz(1 if {a} == {b} else 0))r   r   )ri   rm   r`   )rd   rq   r   r   s       re   _print_KroneckerDeltaz/AbstractPythonCodePrinter._print_KroneckerDelta  sD    y1)00AA 1 
 
 	
rg   c                     |j         j        }| j                            ||          }|d|                     |                                          dS )Nr   r   )rk   rl   rj   r   r`   tolist)rd   rq   rh   funcs       re   _print_MatrixBasez+AbstractPythonCodePrinter._print_MatrixBase  sM    ~&#''d3344T[[]]!;!;!;!;<<rg   c                 ,    |                      |          S r^   )r   r   s     re   <lambda>z"AbstractPythonCodePrinter.<lambda>  s    411$77 rg   c                 l     d                      fd|                    d          D                       S )N
c                 $    g | ]}j         |z   S r}   )tab)rb   linerd   s     re   r   z@AbstractPythonCodePrinter._indent_codestring.<locals>.<listcomp>  s    MMMd$(T/MMMrg   )ro   r   r   s   ` re   _indent_codestringz,AbstractPythonCodePrinter._indent_codestring  s8    yyMMMMj6F6Ft6L6LMMMNNNrg   c                     d                      fd|j        D                       }d                                         |j                  d                      fd|j        D                                            |                    S )Nr  c              3   B   K   | ]}                     |          V  d S r^   r_   ra   s     re   rf   zFAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<genexpr>!  s/      >>s$++c**>>>>>>rg   z def {name}({parameters}):
{body}r\   c                 D    g | ]}                     |j                  S r}   )r`   symbol)rb   varrd   s     re   r   zGAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<listcomp>$  s'    !S!S!Sc$++cj"9"9!S!S!Srg   )rh   
parametersbody)ro   r  rm   r`   rh   r  r  )rd   fdr  s   `  re   _print_FunctionDefinitionz3AbstractPythonCodePrinter._print_FunctionDefinition   s    yy>>>>bg>>>??299RW%%yy!S!S!S!SR]!S!S!STT((.. : 
 
 	
rg   c                      d                      fd|j        D                       }d                                         |j                                       |                    S )Nr  c              3   B   K   | ]}                     |          V  d S 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(  sj    yy????ch???@@&--S]++((.. . 
 
 	
rg   c                     |                      |j        j                  d|                      |j        j                  S r   )r`   variabler	  r   )rd   decls     re   _print_Declarationz,AbstractPythonCodePrinter._print_Declaration/  s=    KK,----KK+,,,
 	
rg   c                 D    |j         \  }d|                     |          z  S )Nz	return %s)ri   r`   )rd   retrc   s      re   _print_Returnz'AbstractPythonCodePrinter._print_Return5  s"    xT[[----rg   c                 "    d                      fd|j        D                       }|j        d k    r.d                                         |j                  |          }|j        d k    r |d                     |j                  z  z  }d|z  S )Nr\   c              3   B   K   | ]}                     |          V  d S r^   r_   ra   s     re   rf   z9AbstractPythonCodePrinter._print_Print.<locals>.<genexpr>:  s/      LLSC 0 0LLLLL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LLLLDOLLLMM
%%$++D.//= =J9+DI(>(>>>JZ''rg   c                     t          |j                  dk    r|                     d          S t          |j                  dk    r|                     d          S |                     |j                  S )Nstdoutz
sys.stdoutstderrz
sys.stderr)strrh   rn   r`   )rd   strms     re   _print_Streamz'AbstractPythonCodePrinter._print_StreamC  sd    ty>>X%%&&|444^^x''&&|444;;ty)))rg   c                     dS )Nr   r}   )rd   rc   s     re   _print_NoneTokenz*AbstractPythonCodePrinter._print_NoneTokenK  s    vrg   	math.sqrtc                    t          |          }|j        t          j        k    rH|sF|                     |          }|                     |j                  }d                    ||          S |j        r|s|j         t          j        u rY|                     |          }|                     t          j	                  }|                     |j                  }| d| d| dS |j        t          j
        u rC|                     t          j	                  }|                     |j        |d          }| d| S |                     |j        |d          }|                     |j        |d          }	d                    ||	          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  s|   V $8qvh&&t,,D++di((C"))t)=== 		&x 		&yAF""**400kk!%((kk$),,----s----x1=((kk!%((''	4'FF~~~~% $$TYU$CC##DHd5#AAx111rg   r^   )T)Fr*  ):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)  r7  __classcell__rk   s   @re   rx   rx   M   s       KHNG
C
$uu  DD&;&A&A&C&CDDD   C C
B$9$?$?$A$A
B
B
BCd599J% $  # # # # # #() ) )B B B B  ' ' '% % %  .  2      % % %T T T  ./ / / / / 4 4 4	# 	# 	#  
 
 
= = = 	878 8"8$8 	8 		8
 	"8 	#O O O
 
 

 
 

 
 
. . .( ( (* * *  @2 @2 @2 @2 @2 @2 @2 @2rg   rx   c                   V    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dS )ArrayPrinterc                 J    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_arrayrR  	Exception)rd   indexedrR  s      re   	_arrayifyzArrayPrinter._arrayify  sN    aaaaaa	++G444 	 	 	NNN	s   
 ""c                    |                                  }d}d}d |D             }g }|D ]e}g }	t          |          D ]<}
||v r|	                    ||                    n|	                    |           |dz  }=|                    |	           fi }g }g }|D ]e}
|
D ][}||vrt          |          }|||<   n||         }||z  }||v r||vr|                    |           F|                    |           \|dz  }f|d d         }|||fS )Nr   r   c                 8    i | ]}|D ]}|t          |          S r}   )r,   )rb   r   js      re   r   z3ArrayPrinter._get_einsum_string.<locals>.<dictcomp>  s-    ???1Q??QA????rg   r   ,r   ) _get_letter_generator_for_einsumranger   next)rd   subrankscontraction_indicesletterscontraction_stringcounterdindicesrank_arglindicesr   mappingletters_freeletters_dumrY  ls                   re   _get_einsum_stringzArrayPrinter._get_einsum_string  s   7799?? 3???  	% 	%HH8__  a<<OOAgJ////OOG,,,1NN8$$$$ 	& 	&A + +G##WA!"GAJJ
A"a'"66++#**1--- ''****#%/4!<<<rg   c              #      K   t          dd          D ]}t          |          V  t          dd          D ]}t          |          V  t          d          )Na   {   A   [   zout of letters)r\  chrr   )rd   r   s     re   r[  z-ArrayPrinter._get_letter_generator_for_einsum  se      r3 	 	Aa&&LLLLr2 	 	Aa&&LLLL)***rg   c                                                       d                    fd|j        D                       }                      j        dz    j        z             d|dd                     fd|j        D                       dS )	NrZ  c                 l    g | ]0}d                      fdt          |          D                       1S )r   c                 .    g | ]}t                    S r}   )r]  )rb   rY  r`  s     re   r   zEArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>  s    /P/P/P!W/P/P/Prg   )ro   r\  )rb   r   r`  s     re   r   z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>  sA    &i&i&iVWrww/P/P/P/PuQxx/P/P/P'Q'Q&i&i&irg   r   ("", r\   c                 :    g | ]}                     |          S r}   r_   ra   s     re   r   z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>  s%    AAA4;;s++AAArg   r   )r[  ro   r^  rn   _module_einsumri   )rd   rq   ra  r`  s   `  @re   _print_ArrayTensorProductz&ArrayPrinter._print_ArrayTensorProduct  s    7799 XX&i&i&i&i[_[h&i&i&ijj##DL3$6$EFFFF"""		AAAAtyAAABBBB
 	
rg   c           
          ddl m} |j        }|j        }t	          ||          r.d                     fd|j        D                       }|j        }n*                     |          }t          |j
                  g}                     ||          \  }}}	|s                     |          S t	          ||          r'd                     fd|j        D                       }n                     |          }                      j        dz    j        z             dd                    |d	                    t!          |                              d
|dS )Nr   ArrayTensorProductrZ  c                 @    g | ]}d                      |          z  S z%sr_   ra   s     re   r   z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>  *    MMMCddkk#&6&67MMMrg   c                 @    g | ]}d                      |          z  S r  r_   ra   s     re   r   z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>  r  rg   r   ru  {}->{}r   rv  r   )0sympy.tensor.array.expressions.array_expressionsr}  rq   r_  
isinstancero   ri   r^  r`   r   shaperk  rn   rx  ry  rm   sorted)
rd   rq   r}  r/  r_  elemsranksra  rh  ri  s
   `         re   _print_ArrayContractionz$ArrayPrinter._print_ArrayContraction  ss   WWWWWWy"6d.// 	&HHMMMM49MMMNNEMEEKK%%E__%E8<8O8OPUWj8k8k5L+" 	%;;t$$$d.// 	&HHMMMM49MMMNNEEKK%%Es 2T\ ABBBBOO.|8L8L0M0MNNNNEE
 	
rg   c           	          ddl m} t          |j                  }t	          |j        |          r|j        j        }|j        j        }n|j        }|j        g}                     ||          \  }}} fd|D             } 	                     j
        dz    j        z             dd                    |d                    ||z                       dd	                    |          d
S )Nr   r|  c                 :    g | ]}                     |          S r}   r_   )rb   r   rd   s     re   r   z5ArrayPrinter._print_ArrayDiagonal.<locals>.<listcomp>  s#    ///AQ///rg   r   ru  r  r   rv  r\   r   )r  r}  listdiagonal_indicesr  rq   r^  ri   rk  rn   rx  ry  rm   ro   )	rd   rq   r}  r  r^  r  diagonal_stringrh  ri  s	   `        re   _print_ArrayDiagonalz!ArrayPrinter._print_ArrayDiagonal  s   WWWWWW 566di!344 	 y)HINEE}HYKE595L5LXWg5h5h2{///////s 2T\ ABBBBOOORWW\+5M-N-NOOOOIIe
 	
rg   c                     |                      | j        dz   | j        z             d|                     |j                  d|                     |j        j                  dS )Nr   r   r\   r   )rn   rx  
_transposer`   rq   permutation
array_formr   s     re   _print_PermuteDimszArrayPrinter._print_PermuteDims  sc    s 2T_ DEEEEKK	""""KK(34444
 	
rg   c                 X    |                      | j        dz   | j        z   |j                  S )Nr   )r   rx  _addri   r   s     re   _print_ArrayAddzArrayPrinter._print_ArrayAdd  s(    **4<#+=	+I49UUUrg   c                     |                      | j        dz   | j        z             dd                    t	          | j        |j                            dS Nr   z((rZ  z,)))rn   rx  _onesro   mapr`   ri   r   s     re   _print_OneArrayzArrayPrinter._print_OneArray  sT    c 1DJ >????HHSTY//0000
 	
rg   c                     |                      | j        dz   | j        z             dd                    t	          | j        |j                            dS r  )rn   rx  _zerosro   r  r`   ri   r   s     re   _print_ZeroArrayzArrayPrinter._print_ZeroArray  sT    c 1DK ?@@@@HHSTY//0000
 	
rg   c                     |                      |                     |j                            }|                      |                     |j                            }|d|S r   )r`   rV  r   r   )rd   rq   r   r   s       re   _print_AssignmentzArrayPrinter._print_Assignment  sS     kk$..2233kk$..2233 SS##''rg   c                 ,    |                      |          S r^   )_print_ArraySymbolr   s     re   _print_IndexedBasezArrayPrinter._print_IndexedBase  s    &&t,,,rg   N)rl   r8  r9  rV  rk  r[  rz  r  r  r  r  r  r  r  r  r}   rg   re   rP  rP    s          != != !=F+ + +
 
 

 
 
4
 
 
"
 
 
V V V
 
 

 
 
( ( (- - - - -rg   rP  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                     |                     d          |                     |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  sG    3::!!/22dkk!&)6L6L ; N N 	Nrg   c                 ~    t          |          }| j        d         |                     |j        d         |          z   S )Nr   r   )r   rC  r   ri   r   s      re   
_print_NotzPythonCodePrinter._print_Not  s7    $u%(9(9$)A,(M(MMMrg   c                      |j         d         }|j         dd          }d                    t          |          d                     fd|D                                 S )Nr   r   z{}[{}]r\   c                 :    g | ]}                     |          S r}   r_   )rb   indrd   s     re   r   z4PythonCodePrinter._print_Indexed.<locals>.<listcomp>&  s%    4W4W4W#T[[5E5E4W4W4Wrg   )ri   rm   r%  ro   )rd   rq   r/  indexs   `   re   _print_Indexedz PythonCodePrinter._print_Indexed#  sY    y|	!""s4yy$))4W4W4W4WQV4W4W4W*X*XYYYrg   Fc                 0    |                      ||          S )N)r3  r7  rd   rq   r3  s      re   
_print_PowzPythonCodePrinter._print_Pow(  s    x888rg   c                 B    d                     |j        |j                  S )Nz{}/{})rm   pqr   s     re   _print_Rationalz!PythonCodePrinter._print_Rational+  s    ~~dfdf---rg   c                 ,    |                      |          S r^   r  r   s     re   _print_HalfzPythonCodePrinter._print_Half.  s    ##D)))rg   c                 ^    |                      t          |j        d         d                    S )Nr   r   )r   r   ri   r   s     re   _print_fraczPythonCodePrinter._print_frac1  s$    s49Q<33444rg   c                 :   t                                          |          }|| j        v rA| j        d         r$d}t	          |                    |                    || j        d         z   S d|v r*|                    dd                              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_Symbolr=  r   r   rm   replace)rd   rq   rh   msgrk   s       re   r  zPythonCodePrinter._print_Symbol4  s    ww$$T**4&&&~12 3< D!1!1222$.)?@@@D[[<<R((00b999Krg   F)rl   r8  r9  r  r  r  r  r  r  r  r  r	   _print_not_supported_print_lowergamma_print_uppergamma_print_fresnelc_print_fresnelsrM  rN  s   @re   r  r    s        N N NN N NZ Z Z
9 9 9 9. . .* * *5 5 5     $8#8!6O!6OOOOOrg   r  z	_print_%sc                 F    t          |                              |           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44rg   z
log1p log2c                 .    g | ]\  }}|t           v||fS r}   )_not_in_mpmathr~   s      re   r   r   k  s*    ZZZA!>BYBYq!fBYBYBYrg   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 ]W}t          |          dk    r|\  }}}nt          d          |                    |           |                    ||f           X||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    s     F*1 2 2 !!Q&&8I5O[++%&MNNN000{K01111V##rg   c                      e Zd ZdZdZdZ e ee	                                d e
	                                D                                 Zd e	                                D             Zd Zd Zd Zd	 Zd
 Zd Zd ZddZd ZdS )MpmathPrinterzH
    Lambda printer for mpmath which maintains precision for floats
    _mpmathcodezPython with mpmathc                 "    g | ]\  }}|d |z   fS zmpmath.r}   r~   s      re   r   zMpmathPrinter.<listcomp>  s%    HHH1!Y]	HHHrg   c                      i | ]\  }}|d |z   S r  r}   r~   s      re   r   zMpmathPrinter.<dictcomp>  s"    
F
F
Fda1ik
F
F
Frg   c                     t          t          t          t          |j                                      }d                    |                     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  sM     5S!'**++,,&&D,?,?,M,MTX&YYYrg   c                     d                     |                     d          |                     |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  sR    (//$$\22kk!#kk!# 0 
 
 	
rg   c                 ,    |                      |          S r^   r  r  s     re   r  zMpmathPrinter._print_Half  s    ##A&&&rg   c                     d                     |                     d          |                     |j        d                   |                     |j        d                   |                     d                    S )Nz{}({}, {}, {})mpmath.gammaincr   r   z
mpmath.infr  r  s     re   r  zMpmathPrinter._print_uppergamma  sg    && 122KKq	""KKq	""--	/ / 	/rg   c                     d                     |                     d          |                     |j        d                   |                     |j        d                             S )Nz{}({}, 0, {})r  r   r   r  r  s     re   r  zMpmathPrinter._print_lowergamma  sV    %% 122KKq	""KKq	""$ $ 	$rg   c                     d                     |                     d          |                     |j        d                             S )Nz{0}({1})/{0}(2)z
mpmath.logr   r  r  s     re   _print_log2zMpmathPrinter._print_log2  sB     ''--t{{16!9/E/EG G 	Grg   c                     d                     |                     d          |                     |j        d                             S )Nz{}({})zmpmath.log1pr   r  r  s     re   _print_log1pzMpmathPrinter._print_log1p  s@    //QVAY1G1GI I 	Irg   Fc                 2    |                      ||d          S )Nzmpmath.sqrtr3  rK   r  r  s      re   r  zMpmathPrinter._print_Pow  s    xmLLLrg   c           	      D    t          |          \  }}d                                         d          d                    t	           j        |                                         |j        d                   d                     fd|D                                 S )Nz{}(lambda {}: {}, {})zmpmath.quadr\   r   c              3   b   K   | ])}d t          t          j        |                    z  V  *dS )z(%s, %s)N)r  r  r`   )rb   rj  rd   s     re   rf   z0MpmathPrinter._print_Integral.<locals>.<genexpr>  s<      RRa*uSa-@-@'A'AARRRRRRrg   )r  rm   rn   ro   r  r`   ri   )rd   rQ   r  r   s   `   re   _print_IntegralzMpmathPrinter._print_Integral  s    #:1#=#= &&--##M22		#dk+;<<==AF1I&&		RRRR6RRRRR	T T 	Trg   Nr  )rl   r8  r9  __doc__r:  r;  r   r   r?  r@  _known_functions_mpmathr   _known_constants_mpmathr   r  r  r  r  r  r  r  r  r	  r}   rg   re   r  r    s7          K#H
$uu  HH(?(E(E(G(GHHH   C G
F&=&C&C&E&E
F
F
FCZ Z Z
 
 
' ' '/ / /$ $ $G G GI I IM M M MT T T T Trg   r  c                        e Zd ZdZd ZddZdS )SymPyPrinterzPython with SymPyc                      |j         j        pd}                     ||rdndz   |j         j        z             dd                     fd|j        D                       dS )Nr   r   r   r\   c              3   B   K   | ]}                     |          V  d S r^   r_   ra   s     re   rf   z/SymPyPrinter._print_Function.<locals>.<genexpr>  s/      %L%L3dkk#&6&6%L%L%L%L%L%Lrg   r   )r   r8  rn   rl   ro   ri   )rd   rq   mods   `  re   _print_FunctionzSymPyPrinter._print_Function  sy    i"(b..sS6Hccb/IDIL^/^____99%L%L%L%L$)%L%L%LMMMMO 	Org   Fc                 2    |                      ||d          S )Nz
sympy.sqrtr  r  r  s      re   r  zSymPyPrinter._print_Pow  s    xlKKKrg   Nr  )rl   r8  r9  r;  r  r  r}   rg   re   r  r    sE        "HO O O
L L L L L Lrg   r  N)#r
  collectionsr   	itertoolsr   
sympy.corer   sympy.core.modr   r   codeprinterr	   r<  r?  rA  rB  rs   rv   rx   rP  r  r   r   setattrr  r   r  r@  
_in_mpmathr   r  r  r  r  r  r}   rg   re   <module>r     s{   
 $ # # # # #                   " " " " " " $ $ $ $ $ $     

FW F W	
 F W W v 
5 F 
5 F 
5 W   W!" W#$ 
;   B 
	  \ \ \& & &
A2 A2 A2 A2 A2 A2 A2 A2H
E- E- E- E- E- E- E- E-P-7 -7 -7 -7 -71 -7 -7 -7` 
	 C CAG{Q0ABBBB	 D DAG{Q0BCCCC5 5 54 ##%%ZZ!6!<!<!>!>ZZZ
$z  . .    
	 	 $ $ $&AT AT AT AT AT% AT AT ATH 
	 ? ?AGM;?,=>>>>	  @ @AGM;?,>????
L 
L 
L 
L 
L, 
L 
L 
L 
L 
Lrg   