B
    ™‘[á   ã               @   sl   d Z ddlmZ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 G d	d
„ d
eƒZdd„ ZdS )z½
A Printer for generating executable code.

The most important function here is srepr that returns a string so that the
relation eval(srepr(expr))=expr holds in an appropriate environment.
é    )Úprint_functionÚdivision)ÚAppliedUndefé   )ÚPrinterN)Úrepr_dps)Úrangec               @   sl  e Zd ZdZddiZdd„ Zdd„ ZdPdd	„Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze Z Z Z Z Z Z ZZdd„ Zd d!„ Zd"d#„ ZdQd$d%„Zd&d'„ Zd(d)„ Zd*d+„ Z d,d-„ Z!d.d/„ Z"d0d1„ Z#d2d3„ Z$d4d5„ Z%d6d7„ Z&d8d9„ Z'd:d;„ Z(d<d=„ Z)d>d?„ Z*d@dA„ Z+dBdC„ Z,dDdE„ Z-dFdG„ Z.dHdI„ Z/dJdK„ Z0dLdM„ Z1dNdO„ Z2dS )RÚReprPrinterZ
_sympyreprÚorderNc                s   |  ‡ fdd„|D ƒ¡S )zG
        Prints each item in `args` and joins them with `sep`.
        c                s   g | ]}ˆ   |¡‘qS © )Údoprint)Ú.0Úitem)Úselfr   ú2lib/python3.7/site-packages/sympy/printing/repr.pyú
<listcomp>   s    z'ReprPrinter.reprify.<locals>.<listcomp>)Újoin)r   ÚargsÚsepr   )r   r   Úreprify   s    zReprPrinter.reprifyc             C   s    t |tƒr|S t|dƒr | ¡ S t|dƒrpt|jdƒrpg }x|jD ]}| |  |¡¡ qBW |jjdd 	|¡  S t|dƒr”t|dƒr”d|j
|jf S t|ƒS d	S )
z'
        The fallback printer.
        Ú	__srepr__r   Ú__iter__z(%s)z, Ú
__module__Ú__name__z	<'%s.%s'>N)Ú
isinstanceÚstrÚhasattrr   r   ÚappendÚ_printÚ	__class__r   r   r   )r   ÚexprÚlÚor   r   r   ÚemptyPrinter   s    

zReprPrinter.emptyPrinterc             C   sF   | j ||d}t|ƒ}t| j|ƒ}|dkr8dd |¡ S dd |¡ S )N)r
   éÿ   z
Add(*[%s])z, zAdd(%s))Z_as_ordered_termsÚlenÚmapr   r   )r   r    r
   r   Únargsr   r   r   Ú
_print_Add0   s    zReprPrinter._print_Addc             C   s   |  ¡ S )N)Ú__repr__)r   r    r   r   r   Ú_print_Cycle8   s    zReprPrinter._print_Cyclec                s2   ˆ   |j¡}|dd ‡ fdd„|jD ƒ¡ 7 }|S )Nz(%s)z, c                s   g | ]}ˆ   |¡‘qS r   )r   )r   Úa)r   r   r   r   =   s    z/ReprPrinter._print_Function.<locals>.<listcomp>)r   Úfuncr   r   )r   r    Úrr   )r   r   Ú_print_Function;   s    "zReprPrinter._print_Functionc             C   s   t |tƒrd|j S |jS d S )NzFunction(%r))Ú
issubclassr   r   )r   r    r   r   r   Ú_print_FunctionClass@   s    

z ReprPrinter._print_FunctionClassc             C   s   dS )NzRational(1, 2)r   )r   r    r   r   r   Ú_print_HalfF   s    zReprPrinter._print_Halfc             C   s   t |ƒS )N)r   )r   r    r   r   r   Ú_print_RationalConstantI   s    z#ReprPrinter._print_RationalConstantc             C   s   t |ƒS )N)r   )r   r    r   r   r   Ú_print_AtomicExprL   s    zReprPrinter._print_AtomicExprc             C   s   t |ƒS )N)r   )r   r    r   r   r   Ú_print_NumberSymbolO   s    zReprPrinter._print_NumberSymbolc             C   s
   d|j  S )NzInteger(%i))Úp)r   r    r   r   r   Ú_print_IntegerR   s    zReprPrinter._print_Integerc             C   s   d|   |d¡ S )Nz[%s]z, )r   )r   r    r   r   r   Ú_print_listU   s    zReprPrinter._print_listc             C   s    |j dk|jdkA r>d|jj|  |j ¡|  |j¡|  g ¡f S g }xFt|j ƒD ]8}| g ¡ x(t|jƒD ]}|d  |||f ¡ qhW qNW d|jj|  |¡f S )Nr   z%s(%s, %s, %s)éÿÿÿÿz%s(%s))ZrowsZcolsr   r   r   r   r   )r   r    r!   ÚiÚjr   r   r   Ú_print_MatrixBaseX   s    


zReprPrinter._print_MatrixBasec             C   s   dS )NÚtruer   )r   r    r   r   r   Ú_print_BooleanTruep   s    zReprPrinter._print_BooleanTruec             C   s   dS )NZfalser   )r   r    r   r   r   Ú_print_BooleanFalses   s    zReprPrinter._print_BooleanFalsec             C   s   dS )NÚnanr   )r   r    r   r   r   Ú
_print_NaNv   s    zReprPrinter._print_NaNc             C   s\   |j }| jdkr |j|Ž  ¡ }n|}t|ƒ}t| j|ƒ}|dkrNdd |¡ S dd |¡ S )NÚoldr$   z
Mul(*[%s])z, zMul(%s))r   r
   Z_new_rawargsZas_ordered_factorsr%   r&   r   r   )r   r    r
   Útermsr   r'   r   r   r   Ú
_print_Muly   s    
zReprPrinter._print_Mulc             C   s   d|   |j¡|   |j¡f S )NzRational(%s, %s))r   r5   Úq)r   r    r   r   r   Ú_print_Rational†   s    zReprPrinter._print_Rationalc             C   s   d|j j|j|jf S )Nz
%s(%d, %d))r   r   r5   rD   )r   r    r   r   r   Ú_print_PythonRational‰   s    z!ReprPrinter._print_PythonRationalc             C   s   d|   |j¡|   |j¡f S )NzFraction(%s, %s))r   Ú	numeratorÚdenominator)r   r    r   r   r   Ú_print_FractionŒ   s    zReprPrinter._print_Fractionc             C   s(   t  |jt|jƒ¡}d|jj||jf S )Nz%s('%s', precision=%i))ÚmlibZto_strZ_mpf_r   Z_precr   r   )r   r    r-   r   r   r   Ú_print_Float   s    zReprPrinter._print_Floatc             C   s0   d|   |j¡|   |j¡|   |j¡|   |j¡f S )NzSum2(%s, (%s, %s, %s)))r   Úfr9   r+   Úb)r   r    r   r   r   Ú_print_Sum2“   s    zReprPrinter._print_Sum2c             C   sn   |j j}|jr|j|d< |i kr8d|jj|  |j¡f S dd„ | ¡ D ƒ}d|jj|  |j¡d 	|¡f S d S )NÚdummy_indexz%s(%s)c             S   s   g | ]\}}d ||f ‘qS )z%s=%sr   )r   ÚkÚvr   r   r   r       s    z-ReprPrinter._print_Symbol.<locals>.<listcomp>z
%s(%s, %s)z, )
Z_assumptionsÚ	generatorZis_DummyrO   r   r   r   ÚnameÚitemsr   )r   r    ÚdÚattrr   r   r   Ú_print_Symbol—   s    
zReprPrinter._print_Symbolc             C   s   d|j j|  |j¡f S )Nz%s(%s))r   r   r   rS   )r   r    r   r   r   Ú_print_Predicate¤   s    zReprPrinter._print_Predicatec             C   s   d|j j|j|jf S )Nz
%s(%s, %s))r   r   r,   Úarg)r   r    r   r   r   Ú_print_AppliedPredicate§   s    z#ReprPrinter._print_AppliedPredicatec             C   s   t |ƒS )N)Úrepr)r   r    r   r   r   Ú
_print_strª   s    zReprPrinter._print_strc             C   s2   t |ƒdkrd|  |d ¡ S d|  |d¡ S d S )Nr   z(%s,)r   z(%s)z, )r%   r   r   )r   r    r   r   r   Ú_print_tuple­   s    zReprPrinter._print_tuplec             C   s   d|j j|jf S )Nz%s('%s'))r   r   rS   )r   r    r   r   r   Ú_print_WildFunction³   s    zReprPrinter._print_WildFunctionc             C   s$   d|j j|  |j¡|  | ¡ ¡f S )Nz
%s(%s, %s))r   r   r   ÚrootZcoeffs)r   r    r   r   r   Ú_print_AlgebraicNumber¶   s    z"ReprPrinter._print_AlgebraicNumberc             C   s,   d|j j|  |j¡|  |j¡|  |j¡f S )Nz%s(%s, %s, %s))r   r   r   ÚsymbolsÚdomainr
   )r   Úringr   r   r   Ú_print_PolyRingº   s    zReprPrinter._print_PolyRingc             C   s,   d|j j|  |j¡|  |j¡|  |j¡f S )Nz%s(%s, %s, %s))r   r   r   ra   rb   r
   )r   Úfieldr   r   r   Ú_print_FracField¾   s    zReprPrinter._print_FracFieldc             C   s>   t | ¡ ƒ}|j|jjdd d|jj|  |j¡|  |¡f S )NT)ÚkeyÚreversez
%s(%s, %s))ÚlistrB   Úsortrc   r
   r   r   r   )r   ZpolyrB   r   r   r   Ú_print_PolyElementÂ   s    zReprPrinter._print_PolyElementc             C   sp   t |j ¡ ƒ}|j|jjdd t |j ¡ ƒ}|j|jjdd |  |¡}|  |¡}d|jj	|  |j¡||f S )NT)rg   rh   z%s(%s, %s, %s))
ri   ÚnumerrB   rj   re   r
   Údenomr   r   r   )r   ZfracZnumer_termsZdenom_termsrl   rm   r   r   r   Ú_print_FracElementÇ   s    

zReprPrinter._print_FracElementc             C   s    |j j}|  |j¡}d||f S )Nz%s(%s))r   r   r   re   )r   rb   Úclsre   r   r   r   Ú_print_FractionFieldÐ   s    z ReprPrinter._print_FractionFieldc             C   sZ   |j j}|  |j¡}d t| j|jƒ¡}t|jƒ}||j	krFd| }nd}d||||f S )Nz, z, order=Ú z%s(%s, %s%s))
r   r   r   rb   r   r&   Úgensr   r
   Zdefault_order)r   rc   ro   Údomrr   r
   Zorderstrr   r   r   Ú_print_PolynomialRingBaseÕ   s    


z%ReprPrinter._print_PolynomialRingBasec             C   sP   |j j}|  |j¡}|  |j¡}|jd k	r<d|  |j¡ }nd}d||||f S )Nz, ring=rq   z%s(%s, %s%s))r   r   r   Úreprs   rc   )r   r5   ro   ru   rs   Zringstrr   r   r   Ú
_print_DMPà   s    
zReprPrinter._print_DMPc             C   s   dt |jƒ S )NzFiniteExtension(%s))r   Úmodulus)r   Úextr   r   r   Ú_print_MonogenicFiniteExtensionê   s    z+ReprPrinter._print_MonogenicFiniteExtensionc             C   s$   |   |j¡}|   |j¡}d||f S )NzExtElem(%s, %s))r   ru   rx   )r   rL   ru   rx   r   r   r   Ú_print_ExtensionElementï   s    z#ReprPrinter._print_ExtensionElement)N)N)3r   r   Ú__qualname__ZprintmethodZ_default_settingsr   r#   r(   r*   r.   r0   r1   r2   r3   r4   r6   r7   r;   Z_print_SparseMatrixZ_print_MutableSparseMatrixZ_print_ImmutableSparseMatrixZ_print_MatrixZ_print_DenseMatrixZ_print_MutableDenseMatrixZ_print_ImmutableMatrixZ_print_ImmutableDenseMatrixr=   r>   r@   rC   rE   rF   rI   rK   rN   rW   rX   rZ   r\   r]   r^   r`   rd   rf   rk   rn   rp   rt   rv   ry   rz   r   r   r   r   r	      sR   
 
	
r	   c             K   s   t |ƒ | ¡S )zreturn expr in repr form)r	   r   )r    Zsettingsr   r   r   Úsreprõ   s    r|   )Ú__doc__Z
__future__r   r   Zsympy.core.functionr   Zprinterr   Zmpmath.libmpZlibmprJ   r   Zsympy.core.compatibilityr   r	   r|   r   r   r   r   Ú<module>   s    e