
    Ed _                        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                    t           fd|j                                      S )Nz{name}({args}), c                 .                         |           S N_printargselfs    5lib/python3.11/site-packages/sympy/printing/pycode.py<lambda>z#_print_known_func.<locals>.<lambda>E   s    $++cBRBR     )nameargs)known_functions	__class____name__format_module_formatjoinmaprh   rc   exprknowns   `  rd   _print_known_funcrs   B   sl     !89E""(;(;E(B(B(,		#6R6R6R6RTXT]2^2^(_(_ # a a arf   c                 Z    | j         |j        j                 }|                     |          S r^   )known_constantsrj   rk   rm   rp   s      rd   _print_known_constrv   H   s(     !89Eu%%%rf   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. .0kvs      rd   
<listcomp>z$AbstractPythonCodePrinter.<listcomp>U   s%    DDDda!Wq[	DDDrf   c                      i | ]\  }}|d |z   S r|   r}   r~   s      rd   
<dictcomp>z$AbstractPythonCodePrinter.<dictcomp>W   s"    
B
B
BDAq1gai
B
B
Brf   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sysrl   version_infomajor
ValueErrorr   r   setmodule_importsdict_kfgetri   _kcru   )rc   settingsstdr   rj   s       rd   r   z"AbstractPythonCodePrinter.__init__c   s   """ nZ( 	<JJJ##C$4$:;;C) 	<:;;;)#..  $DH  #  #R0D0Db1" 1"  #  ##DH  #  #R0D0Db1" 1"  #  #rf   c                     |d|S Nz = r}   )rc   rg   values      rd   _declare_number_constz/AbstractPythonCodePrinter._declare_number_constw   s     DD%%((rf   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   rn   addr   )rc   fqnregisterpartss       rd   rm   z(AbstractPythonCodePrinter._module_formatz   s    		# 	EE

Q 	Ess 4 4599%)DDD>34 	BJ99S>>!$**3//288==bAArf   c                     |S r^   r}   )rc   liness     rd   _format_codez&AbstractPythonCodePrinter._format_code   s    rf   c                 ,    d                     |          S )Nz{}rl   rc   
codestrings     rd   _get_statementz(AbstractPythonCodePrinter._get_statement   s    {{:&&&rf   c                 ,    d                     |          S )Nz  # {}r   )rc   texts     rd   _get_commentz&AbstractPythonCodePrinter._get_comment   s    t$$$rf   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`   rm   _expand_fold_binary_op)rc   oprh   s      rd   r   z0AbstractPythonCodePrinter._expand_fold_binary_op   s     t99> 	;;tAw''' ##B''''++BSbS	::::DH%%%% rf   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`   rm   _expand_reduce_binary_op)rc   r   rh   NNhalfs        rd   r   z2AbstractPythonCodePrinter._expand_reduce_binary_op   s     t99> 		;;tAw'''D		AFE##B''''--d6E6l;;;;--d566l;;;; rf   c                     dS )Nzfloat('nan')r}   rc   rq   s     rd   
_print_NaNz$AbstractPythonCodePrinter._print_NaN       ~rf   c                     dS )Nzfloat('inf')r}   r   s     rd   _print_Infinityz)AbstractPythonCodePrinter._print_Infinity   r   rf   c                     dS )Nzfloat('-inf')r}   r   s     rd   _print_NegativeInfinityz1AbstractPythonCodePrinter._print_NegativeInfinity   s    rf   c                 ,    |                      |          S r^   )r   r   s     rd   _print_ComplexInfinityz0AbstractPythonCodePrinter._print_ComplexInfinity   s    t$$$rf   c                 f     t          |           dj        t           fd|j                   S )Nz{} % {}c                 0                         |           S r^   )parenthesize)xPRECrc   s    rd   re   z6AbstractPythonCodePrinter._print_Mod.<locals>.<lambda>   s    0A0A!T0J0J rf   )r   rl   ro   rh   rc   rq   r   s   ` @rd   
_print_Modz$AbstractPythonCodePrinter._print_Mod   s:    $ 	 #&J&J&J&J&JDI"V"VWXrf   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) )rh   rq   condappendr`   rn   )rc   rq   resultirb   rQ   cs          rd   _print_Piecewisez*AbstractPythonCodePrinter._print_Piecewise   s6   9 	 	CAAAv #c"""MM#MM$++a..)))MM#MM&!!!MM$++a..)))MM(###FAA": 	)CRC[FMM#MM-(((wwvrf   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   rl   r   _print_Relational)rc   rq   r   r   r   rj   s        rd   r   z+AbstractPythonCodePrinter._print_Relational   s     !
 
 ;" 	Q++dh''C++dh''C'..$+3C.PPPww((...rf   c                 ^    ddl m} |                     |                    |                    S )Nr   )	Piecewise)$sympy.functions.elementary.piecewiser   r`   rewrite)rc   rq   r   s      rd   
_print_ITEz$AbstractPythonCodePrinter._print_ITE   s2    BBBBBB{{4<<	22333rf   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)rl   r`   )r   r   r   r   rc   s       rd   	<genexpr>z7AbstractPythonCodePrinter._print_Sum.<locals>.<genexpr>   s{       ( (
 1a	 +11++a..++a..++a.. 2 " "( ( ( ( ( (rf   z"(builtins.sum({function} {loops})) )functionloops)limitsrl   r`   r   rn   )rc   rq   r   s   `  rd   
_print_Sumz$AbstractPythonCodePrinter._print_Sum   sl    ( ( ( (
  ;( ( ( 4::[[//((5// ; # # 	#rf   c                     dS )N1jr}   r   s     rd   _print_ImaginaryUnitz.AbstractPythonCodePrinter._print_ImaginaryUnit  s    trf   c                     |j         \  }}d                    |                     |          |                     |                    S )Nz(1 if {a} == {b} else 0))r   r   )rh   rl   r`   )rc   rq   r   r   s       rd   _print_KroneckerDeltaz/AbstractPythonCodePrinter._print_KroneckerDelta  sD    y1)00AA 1 
 
 	
rf   c                     |j         j        }| j                            ||          }|d|                     |                                          dS )Nr   r   )rj   rk   ri   r   r`   tolist)rc   rq   rg   funcs       rd   _print_MatrixBasez+AbstractPythonCodePrinter._print_MatrixBase  sM    ~&#''d3344T[[]]!;!;!;!;<<rf   c                 ,    |                      |          S r^   )r   r   s     rd   re   z"AbstractPythonCodePrinter.<lambda>  s    411$77 rf   c                 l     d                      fd|                    d          D                       S )N
c                 $    g | ]}j         |z   S r}   )tab)r   linerc   s     rd   r   z@AbstractPythonCodePrinter._indent_codestring.<locals>.<listcomp>  s    MMMd$(T/MMMrf   )rn   r   r   s   ` rd   _indent_codestringz,AbstractPythonCodePrinter._indent_codestring  s8    yyMMMMj6F6Ft6L6LMMMNNNrf   c                 (    d                     t           fd|j                            }d                                         |j                  d                      fd|j        D                                            |                    S )Nr  c                 .                         |           S r^   r_   ra   s    rd   re   zEAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<lambda>!      S)9)9 rf   z def {name}({parameters}):
{body}r\   c                 D    g | ]}                     |j                  S r}   )r`   symbol)r   varrc   s     rd   r   zGAbstractPythonCodePrinter._print_FunctionDefinition.<locals>.<listcomp>$  s'    !S!S!Sc$++cj"9"9!S!S!Srf   )rg   
parametersbody)rn   ro   r  rl   r`   rg   r  r  )rc   fdr  s   `  rd   _print_FunctionDefinitionz3AbstractPythonCodePrinter._print_FunctionDefinition   s    yy999927CCDD299RW%%yy!S!S!S!SR]!S!S!STT((.. : 
 
 	
rf   c                      d                     t           fd|j                            }d                                         |j                                       |                    S )Nr  c                 .                         |           S r^   r_   ra   s    rd   re   z8AbstractPythonCodePrinter._print_While.<locals>.<lambda>)  r	  rf   zwhile {cond}:
{body})r   r  )rn   ro   r  rl   r`   	conditionr  )rc   whlr  s   `  rd   _print_Whilez&AbstractPythonCodePrinter._print_While(  si    yy999938DDEE&--S]++((.. . 
 
 	
rf   c                     |                      |j        j                  d|                      |j        j                  S r   )r`   variabler  r   )rc   decls     rd   _print_Declarationz,AbstractPythonCodePrinter._print_Declaration/  s=    KK,----KK+,,,
 	
rf   c                 D    |j         \  }d|                     |          z  S )Nz	return %s)rh   r`   )rc   retrb   s      rd   _print_Returnz'AbstractPythonCodePrinter._print_Return5  s"    xT[[----rf   c                 ,    d                     t           fd|j                            }|j        d k    r.d                                         |j                  |          }|j        d k    r |d                     |j                  z  z  }d|z  S )Nr\   c                 .                         |           S r^   r_   ra   s    rd   re   z8AbstractPythonCodePrinter._print_Print.<locals>.<lambda>:  s    t{{3/?/? rf   z	{} % ({})z	, file=%sz	print(%s))rn   ro   
print_argsformat_stringrl   r`   file)rc   prntr  s   `  rd   _print_Printz&AbstractPythonCodePrinter._print_Print9  s    YYs#?#?#?#?QQRR
% 	=$++D.//= =J9 	?+DI(>(>>>JZ''rf   c                     t          |j                  dk    r|                     d          S t          |j                  dk    r|                     d          S |                     |j                  S )Nstdoutz
sys.stdoutstderrz
sys.stderr)strrg   rm   r`   )rc   strms     rd   _print_Streamz'AbstractPythonCodePrinter._print_StreamC  sh    ty>>X% 	*&&|444^^x' 	*&&|444;;ty)))rf   c                     dS )Nr   r}   )rc   rb   s     rd   _print_NoneTokenz*AbstractPythonCodePrinter._print_NoneTokenK  s    vrf   	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   rb   /r   r   F)strictz{}**{})r   r>   r   Halfrm   r`   baserl   is_commutativeOneNegativeOner   )
rc   rq   rationalrK   r   r   rb   numbase_strexp_strs
             rd   _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1rf   r^   )T)Fr,  ):rk   
__module____qualname__printmethodlanguage_kwreserved_wordsmodulesr  r   r   _known_functionsitems_known_functions_mathr   _known_constants_mathr   
_operatorsr	   _default_settingsr   r   rm   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+  r9  __classcell__rj   s   @rd   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      % % %Y Y Y  ./ / / / / 4 4 4	# 	# 	#  
 
 
= = = 	878 8"8$8 	8 		8
 	"8 	#O O O
 
 

 
 

 
 
. . .( ( (* * *  @2 @2 @2 @2 @2 @2 @2 @2rf   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.conv_indexed_to_arrayrT  	Exception)rc   indexedrT  s      rd   	_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,   )r   r   js      rd   r   z3ArrayPrinter._get_einsum_string.<locals>.<dictcomp>  s-    ???1Q??QA????rf   r   ,r   ) _get_letter_generator_for_einsumranger   next)rc   subrankscontraction_indicesletterscontraction_stringcounterdindicesrank_arglindicesr   mappingletters_freeletters_dumr[  ls                   rd   _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'"6 ++ .#**1--- ''****#%/4!<<<rf   c              #      K   t          dd          D ]}t          |          V  t          dd          D ]}t          |          V  t          d          )Na   {   A   [   zout of letters)r^  chrr   )rc   r   s     rd   r]  z-ArrayPrinter._get_letter_generator_for_einsum  se      r3 	 	Aa&&LLLLr2 	 	Aa&&LLLL)***rf   c                                                       d                    fd|j        D                       }                      j        dz    j        z             d|dd                     fd|j        D                       dS )	Nr\  c                 l    g | ]0}d                      fdt          |          D                       1S )r   c                 .    g | ]}t                    S r}   )r_  )r   r[  rb  s     rd   r   zEArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>  s    /P/P/P!W/P/P/Prf   )rn   r^  )r   r   rb  s     rd   r   z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>  sA    &i&i&iVWrww/P/P/P/PuQxx/P/P/P'Q'Q&i&i&irf   r   ("", r\   c                 :    g | ]}                     |          S r}   r_   r   rb   rc   s     rd   r   z:ArrayPrinter._print_ArrayTensorProduct.<locals>.<listcomp>  s%    AAA4;;s++AAArf   r   )r]  rn   r`  rm   _module_einsumrh   )rc   rq   rc  rb  s   `  @rd   _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
 	
rf   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   ArrayTensorProductr\  c                 @    g | ]}d                      |          z  S z%sr_   rz  s     rd   r   z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>  *    MMMCddkk#&6&67MMMrf   c                 @    g | ]}d                      |          z  S r  r_   rz  s     rd   r   z8ArrayPrinter._print_ArrayContraction.<locals>.<listcomp>  r  rf   r   rw  {}->{}r   rx  r   )0sympy.tensor.array.expressions.array_expressionsr  rq   ra  
isinstancern   rh   r`  r`   r   shaperm  rm   r{  r|  rl   sorted)
rc   rq   r  r1  ra  elemsranksrc  rj  rk  s
   `         rd   _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
 	
rf   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_   )r   r   rc   s     rd   r   z5ArrayPrinter._print_ArrayDiagonal.<locals>.<listcomp>  s#    ///AQ///rf   r   rw  r  r   rx  r\   r   )r  r  listdiagonal_indicesr  rq   r`  rh   rm  rm   r{  r|  rl   rn   )	rc   rq   r  r  r`  r  diagonal_stringrj  rk  s	   `        rd   _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
 	
rf   c                     |                      | j        dz   | j        z             d|                     |j                  d|                     |j        j                  dS )Nr   r   r\   r   )rm   r{  
_transposer`   rq   permutation
array_formr   s     rd   _print_PermuteDimszArrayPrinter._print_PermuteDims  sc    s 2T_ DEEEEKK	""""KK(34444
 	
rf   c                 X    |                      | j        dz   | j        z   |j                  S )Nr   )r   r{  _addrh   r   s     rd   _print_ArrayAddzArrayPrinter._print_ArrayAdd  s(    **4<#+=	+I49UUUrf   c                     |                      | j        dz   | j        z             dd                    t	          | j        |j                            dS Nr   z((r\  z,)))rm   r{  _onesrn   ro   r`   rh   r   s     rd   _print_OneArrayzArrayPrinter._print_OneArray  sT    c 1DJ >????HHSTY//0000
 	
rf   c                     |                      | j        dz   | j        z             dd                    t	          | j        |j                            dS r  )rm   r{  _zerosrn   ro   r`   rh   r   s     rd   _print_ZeroArrayzArrayPrinter._print_ZeroArray  sT    c 1DK ?@@@@HHSTY//0000
 	
rf   c                     |                      |                     |j                            }|                      |                     |j                            }|d|S r   )r`   rX  r   r   )rc   rq   r   r   s       rd   _print_AssignmentzArrayPrinter._print_Assignment  sS     kk$..2233kk$..2233 SS##''rf   c                 ,    |                      |          S r^   )_print_ArraySymbolr   s     rd   _print_IndexedBasezArrayPrinter._print_IndexedBase  s    &&t,,,rf   N)rk   r:  r;  rX  rm  r]  r}  r  r  r  r  r  r  r  r  r}   rf   rd   rR  rR    s          != != !=F+ + +
 
 

 
 
4
 
 
"
 
 
V V V
 
 

 
 
( ( (- - - - -rf   rR  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   rl   rm   r`   rh   rc   rQ   s     rd   _print_signzPythonCodePrinter._print_sign  sG    3::!!/22dkk!&)6L6L ; N N 	Nrf   c                 ~    t          |          }| j        d         |                     |j        d         |          z   S )Nr   r   )r   rE  r   rh   r   s      rd   
_print_NotzPythonCodePrinter._print_Not  s7    $u%(9(9$)A,(M(MMMrf   c                      |j         d         }|j         dd          }d                    t          |          d                     fd|D                                 S )Nr   r   z{}[{}]r\   c                 :    g | ]}                     |          S r}   r_   )r   indrc   s     rd   r   z4PythonCodePrinter._print_Indexed.<locals>.<listcomp>&  s%    4W4W4W#T[[5E5E4W4W4Wrf   )rh   rl   r'  rn   )rc   rq   r1  indexs   `   rd   _print_Indexedz PythonCodePrinter._print_Indexed#  sY    y|	!""s4yy$))4W4W4W4WQV4W4W4W*X*XYYYrf   Fc                 0    |                      ||          S )N)r5  r9  rc   rq   r5  s      rd   
_print_PowzPythonCodePrinter._print_Pow(  s    x888rf   c                 B    d                     |j        |j                  S )Nz{}/{})rl   pqr   s     rd   _print_Rationalz!PythonCodePrinter._print_Rational+  s    ~~dfdf---rf   c                 ,    |                      |          S r^   r  r   s     rd   _print_HalfzPythonCodePrinter._print_Half.  s    ##D)))rf   c                 ^    |                      t          |j        d         d                    S )Nr   r   )r   r   rh   r   s     rd   _print_fraczPythonCodePrinter._print_frac1  s$    s49Q<33444rf   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   rl   replace)rc   rq   rg   msgrj   s       rd   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Krf   F)rk   r:  r;  r  r  r  r  r  r  r  r  r	   _print_not_supported_print_lowergamma_print_uppergamma_print_fresnelc_print_fresnelsrO  rP  s   @rd   r  r    s        N N NN N NZ Z Z
9 9 9 9. . .* * *5 5 5     $8#8!6O!6OOOOOrf   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     rd   pycoder  P  s     . X&&..t444rf   z
log1p log2c                 .    g | ]\  }}|t           v||fS r}   )_not_in_mpmathr~   s      rd   r   r   k  s*    ZZZA!>BYZq!fZZZrf   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          rd   _unpack_integral_limitsr    s     F*1 2 2 !!Q& 	O8I5O[++%&MNNN000{K01111V##rf   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      rd   r   zMpmathPrinter.<listcomp>  s%    HHH1!Y]	HHHrf   c                      i | ]\  }}|d |z   S r  r}   r~   s      rd   r   zMpmathPrinter.<dictcomp>  s"    
F
F
Fda1ik
F
F
Frf   c                     t          t          t          t          |j                                      }d                    |                     d          |          S )Nz{func}({args})
mpmath.mpf)r   rh   )r'  tuplero   int_mpf_rl   rm   )rc   rQ   rh   s      rd   _print_FloatzMpmathPrinter._print_Float  sM     5S!'**++,,&&D,?,?,M,MTX&YYYrf   c                     d                     |                     d          |                     |j                  |                     |j                            S )Nz{func}({p})/{func}({q})r  )r   r  r  )rl   rm   r`   r  r  r  s     rd   r  zMpmathPrinter._print_Rational  sR    (//$$\22kk!#kk!# 0 
 
 	
rf   c                 ,    |                      |          S r^   r  r  s     rd   r  zMpmathPrinter._print_Half  s    ##A&&&rf   c                     d                     |                     d          |                     |j        d                   |                     |j        d                   |                     d                    S )Nz{}({}, {}, {})mpmath.gammaincr   r   z
mpmath.infr  r  s     rd   r  zMpmathPrinter._print_uppergamma  sg    && 122KKq	""KKq	""--	/ / 	/rf   c                     d                     |                     d          |                     |j        d                   |                     |j        d                             S )Nz{}({}, 0, {})r  r   r   r  r  s     rd   r  zMpmathPrinter._print_lowergamma  sV    %% 122KKq	""KKq	""$ $ 	$rf   c                     d                     |                     d          |                     |j        d                             S )Nz{0}({1})/{0}(2)
mpmath.logr   r  r  s     rd   _print_log2zMpmathPrinter._print_log2  sB     ''--t{{16!9/E/EG G 	Grf   c                     d                     |                     d          |                     |j        d                             S )Nz{}({}+1)r  r   r  r  s     rd   _print_log1pzMpmathPrinter._print_log1p  sB      --t{{16!9/E/EG G 	Grf   Fc                 2    |                      ||d          S )Nzmpmath.sqrtr5  rK   r  r  s      rd   r  zMpmathPrinter._print_Pow  s    xmLLLrf   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  ro   r`   )r   rl  rc   s     rd   r   z0MpmathPrinter._print_Integral.<locals>.<genexpr>  s<      RRa*uSa-@-@'A'AARRRRRRrf   )r  rl   rm   rn   ro   r`   rh   )rc   rQ   r  r   s   `   rd   _print_IntegralzMpmathPrinter._print_Integral  s    #:1#=#= &&--##M22		#dk+;<<==AF1I&&		RRRR6RRRRR	T T 	Trf   Nr  )rk   r:  r;  __doc__r<  r=  r   r   rA  rB  _known_functions_mpmathr   _known_constants_mpmathr   r  r  r  r  r  r  r  r  r  r}   rf   rd   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G G GM M M MT T T T Trf   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                    t           fd|j                            dS )Nr   r   r   r\   c                 .                         |           S r^   r_   ra   s    rd   re   z.SymPyPrinter._print_Function.<locals>.<lambda>  s    T[[5E5E rf   r   )r   r:  rm   rk   rn   ro   rh   )rc   rq   mods   `  rd   _print_FunctionzSymPyPrinter._print_Function  sx    i"(b..sS6Hccb/IDIL^/^____99S)E)E)E)Ety%Q%QRRRRT 	Trf   Fc                 2    |                      ||d          S )Nz
sympy.sqrtr	  r  r  s      rd   r  zSymPyPrinter._print_Pow  s    xlKKKrf   Nr  )rk   r:  r;  r=  r  r  r}   rf   rd   r  r    sE        "HT T T
L L L L L Lrf   r  N)#r  collectionsr   	itertoolsr   
sympy.corer   sympy.core.modr   r   codeprinterr	   r>  rA  rC  rD  rs   rv   rx   rR  r  r   r   setattrr  r   r  rB  
_in_mpmathr   r  r  r  r  r  r}   rf   rd   <module>r     s{   
 $ # # # # #                   " " " " " " $ $ $ $ $ $     

FW F W	
 F W W v 
5 F 
5 F 
5 W   W!" W#$ 
;   B 
	  a a a& & &
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rf   