B
    edo                 @   s   d Z ddlmZmZ ddlmZmZmZm	Z	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 dd	lmZ d
dlmZmZ d
dlmZmZ ddlmZmZ  G dd deZ!ee!dd Z"G dd de!Z#ee#dd Z$dS )zI
A Printer for generating readable representation of most SymPy classes.
    )AnyDict)SRationalPowBasicMulNumber)_keep_coeff)
Relational)default_sort_key)SympifyError)	FiniteSet)sift   )
precedence
PRECEDENCE)Printerprint_function)prec_to_dpsto_strc               @   s  e Zd ZdZddddddddZe ZdddZdd
dZdd Z	d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d"d# Zd$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 Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2d`da Z3dbdc Z4ddde Z5dfdg Z6dhdi Z7djdk Z8dldm Z9dndo Z:dpdq Z;drds Z<dtdu Z=dvdw Z>dxdy Z?dzd{ Z@d|d} ZAd~d ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdddZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd ZcddÄ Zdddń ZeddǄ ZfddɄ Zgdd˄ Zhdd̈́ Ziddτ ZjejZkejZlddф Zmddӄ ZnddՄ Zoddׄ Zpddل Zqddۄ Zrdd݄ Zsdd߄ Ztdd Zudd Zvdd Zwdd Zxdd Zydd Zzdd Z{dd Z|dd Z}dd Z~dd Zdd Zdd Zdd Zdd Zdd 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 ZdS (  
StrPrinterZ	_sympystrNautoFT)order	full_precsympy_integersabbrevperm_cyclicminmaxc             C   s8   t ||k s|s*t ||kr*d| | S | |S d S )Nz(%s))r   _print)selfitemlevelstrict r%   _/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/printing/str.pyparenthesize#   s    zStrPrinter.parenthesizer   c                s   |  fdd|D S )Nc                s   g | ]} | qS r%   )r'   ).0r"   )r#   r!   r%   r&   
<listcomp>*   s    z(StrPrinter.stringify.<locals>.<listcomp>)join)r!   argssepr#   r%   )r#   r!   r&   	stringify)   s    zStrPrinter.stringifyc             C   s,   t |tr|S t |tr t|S t|S d S )N)
isinstancestrr   repr)r!   exprr%   r%   r&   emptyPrinter,   s
    

zStrPrinter.emptyPrinterc       	      C   s   | j ||d}t|}g }xd|D ]\}| |}|drJd}|dd  }nd}t||k rn||d| g q |||g q W |d}|dkrd}|d| S )	N)r   -r   +z(%s)r     )Z_as_ordered_termsr   r    
startswithextendpopr*   )	r!   r1   r   termsPRECltermtsignr%   r%   r&   
_print_Add4   s     



zStrPrinter._print_Addc             C   s   dS )NTruer%   )r!   r1   r%   r%   r&   _print_BooleanTrueI   s    zStrPrinter._print_BooleanTruec             C   s   dS )NFalser%   )r!   r1   r%   r%   r&   _print_BooleanFalseL   s    zStrPrinter._print_BooleanFalsec             C   s   d|  |jd td  S )Nz~%sr   Not)r'   r+   r   )r!   r1   r%   r%   r&   
_print_NotO   s    zStrPrinter._print_Notc             C   s\   t |j}x>t|D ]2\}}t|tr|jjtjkr|	d|
| qW | |dtd S )Nr   z & Z
BitwiseAnd)listr+   	enumerater.   r   	canonicalrhsr   NegativeInfinityinsertr9   r-   r   )r!   r1   r+   jir%   r%   r&   
_print_AndR   s    

zStrPrinter._print_Andc             C   s   |  |jdtd S )Nz | Z	BitwiseOr)r-   r+   r   )r!   r1   r%   r%   r&   	_print_OrZ   s    zStrPrinter._print_Orc             C   s   |  |jdtd S )Nz ^ Z
BitwiseXor)r-   r+   r   )r!   r1   r%   r%   r&   
_print_Xor]   s    zStrPrinter._print_Xorc             C   s   d|  |j| |jdf S )Nz%s(%s)z, )r    functionr-   	arguments)r!   r1   r%   r%   r&   _print_AppliedPredicate`   s    z"StrPrinter._print_AppliedPredicatec                s*    fdd|j D }|jjdd|  S )Nc                s   g | ]}  |qS r%   )r    )r(   o)r!   r%   r&   r)   e   s    z+StrPrinter._print_Basic.<locals>.<listcomp>z(%s)z, )r+   	__class____name__r*   )r!   r1   r<   r%   )r!   r&   _print_Basicd   s    zStrPrinter._print_Basicc             C   s(   |j jdkr| |j d  | |j S )N)r   r   )r   r   )blocksshaper    )r!   Br%   r%   r&   _print_BlockMatrixh   s    zStrPrinter._print_BlockMatrixc             C   s   dS )NZCatalanr%   )r!   r1   r%   r%   r&   _print_Catalanm   s    zStrPrinter._print_Catalanc             C   s   dS )NZzoor%   )r!   r1   r%   r%   r&   _print_ComplexInfinityp   s    z!StrPrinter._print_ComplexInfinityc                sL   t  fdd|j|jfD }|jtjkr2d| S | |jf7 }d| S )Nc                s   g | ]}  |qS r%   )r    )r(   rN   )r!   r%   r&   r)   t   s    z2StrPrinter._print_ConditionSet.<locals>.<listcomp>zConditionSet(%s, %s)zConditionSet(%s, %s, %s))tuplesym	conditionZbase_setr   UniversalSetr    )r!   sr+   r%   )r!   r&   _print_ConditionSets   s
    zStrPrinter._print_ConditionSetc                s8   |j }dd |jD }ddt fdd|g|  S )Nc             S   s$   g | ]}|d  d kr|d n|qS )r   r   r%   )r(   rN   r%   r%   r&   r)   |   s    z0StrPrinter._print_Derivative.<locals>.<listcomp>zDerivative(%s)z, c                s
     | S )N)r    )arg)r!   r%   r&   <lambda>}       z.StrPrinter._print_Derivative.<locals>.<lambda>)r1   Zvariable_countr*   map)r!   r1   ZdexprZdvarsr%   )r!   r&   _print_Derivativez   s    zStrPrinter._print_Derivativec             C   sV   t | td}g }x2|D ]*}d| || || f }|| qW dd| S )N)keyz%s: %sz{%s}z, )sortedkeysr   r    appendr*   )r!   drl   itemsrj   r"   r%   r%   r&   _print_dict   s    
zStrPrinter._print_dictc             C   s
   |  |S )N)rp   )r!   r1   r%   r%   r&   _print_Dict   s    zStrPrinter._print_Dictc             C   sZ   t |drd| |  S t |drFd| |j d | |j S d| |j S d S )N
as_booleanzDomain: setz in z
Domain on )hasattrr    rr   symbolsrs   )r!   rn   r%   r%   r&   _print_RandomDomain   s    

zStrPrinter._print_RandomDomainc             C   s
   d|j  S )N_)name)r!   r1   r%   r%   r&   _print_Dummy   s    zStrPrinter._print_Dummyc             C   s   dS )NZ
EulerGammar%   )r!   r1   r%   r%   r&   _print_EulerGamma   s    zStrPrinter._print_EulerGammac             C   s   dS )NEr%   )r!   r1   r%   r%   r&   _print_Exp1   s    zStrPrinter._print_Exp1c             C   s   d|  |j|  |jf S )Nz(%s, %s))r    r1   Zcond)r!   r1   r%   r%   r&   _print_ExprCondPair   s    zStrPrinter._print_ExprCondPairc             C   s   |j jd| |jd  S )Nz(%s)z, )funcrW   r-   r+   )r!   r1   r%   r%   r&   _print_Function   s    zStrPrinter._print_Functionc             C   s   dS )NZGoldenRatior%   )r!   r1   r%   r%   r&   _print_GoldenRatio   s    zStrPrinter._print_GoldenRatioc             C   s   |j jd| |jd  S )Nz(%s)z, )r~   rW   r-   Zpargs)r!   r1   r%   r%   r&   _print_Heaviside   s    zStrPrinter._print_Heavisidec             C   s   dS )NZTribonacciConstantr%   )r!   r1   r%   r%   r&   _print_TribonacciConstant   s    z$StrPrinter._print_TribonacciConstantc             C   s   dS )NIr%   )r!   r1   r%   r%   r&   _print_ImaginaryUnit   s    zStrPrinter._print_ImaginaryUnitc             C   s   dS )NZoor%   )r!   r1   r%   r%   r&   _print_Infinity   s    zStrPrinter._print_Infinityc                s:   fdd d  fdd|jD }d|j|f S )Nc                s>   t | dkr | d S  | d ft| dd   S d S )Nr   r   )lenr    r_   )xab)r!   r%   r&   
_xab_tostr   s    z.StrPrinter._print_Integral.<locals>._xab_tostrz, c                s   g | ]} |qS r%   r%   )r(   r<   )r   r%   r&   r)      s    z.StrPrinter._print_Integral.<locals>.<listcomp>zIntegral(%s, %s))r*   limitsr    rR   )r!   r1   Lr%   )r   r!   r&   _print_Integral   s    zStrPrinter._print_Integralc             C   s   d}|j \}}}}|jr$|jr$d}nJ|jr4|s4d}n:|jrD|sDd}n*|sR|sRd}n|r`|r`d}n|rjd}nd}|jf |||dS )NzInterval{m}({a}, {b})r5   z.openz.Lopenz.Ropen)abm)r+   is_infiniteformat)r!   rN   Zfinr   r   r<   rr   r%   r%   r&   _print_Interval   s     

zStrPrinter._print_Intervalc             C   s   d|  |j|  |jf S )NzAccumBounds(%s, %s))r    r   r   )r!   rN   r%   r%   r&   _print_AccumulationBounds   s    z$StrPrinter._print_AccumulationBoundsc             C   s   d|  |jtd  S )Nz%s**(-1)r   )r'   re   r   )r!   r   r%   r%   r&   _print_Inverse   s    zStrPrinter._print_Inversec             C   sB   |j }|j}t|dkr*|d jr*|d }d| || |f S )Nr   r   zLambda(%s, %s))r1   	signaturer   Z	is_symbolr    )r!   objr1   sigr%   r%   r&   _print_Lambda   s
    zStrPrinter._print_Lambdac                s2   t |jtd}|jjdd fdd|D   S )N)rj   z(%s)z, c             3   s   | ]}  |V  qd S )N)r    )r(   re   )r!   r%   r&   	<genexpr>   s    z.StrPrinter._print_LatticeOp.<locals>.<genexpr>)rk   r+   r   r~   rW   r*   )r!   r1   r+   r%   )r!   r&   _print_LatticeOp   s    zStrPrinter._print_LatticeOpc             C   sT   |j \}}}}t|dkr4dtt| j|||f S dtt| j||||f S d S )Nr4   zLimit(%s, %s, %s)zLimit(%s, %s, %s, dir='%s'))r+   r/   r_   rh   r    )r!   r1   ezZz0dirr%   r%   r&   _print_Limit   s
    
zStrPrinter._print_Limitc             C   s   d|  |d S )Nz[%s]z, )r-   )r!   r1   r%   r%   r&   _print_list   s    zStrPrinter._print_listc             C   s
   |  |S )N)r   )r!   r1   r%   r%   r&   _print_List   s    zStrPrinter._print_Listc             C   s
   | | S )N)Z_format_str)r!   r1   r%   r%   r&   _print_MatrixBase   s    zStrPrinter._print_MatrixBasec             C   s2   | j |jtd ddd| |j| |jf  S )NAtomT)r$   z[%s, %s])r'   parentr   r    rN   rM   )r!   r1   r%   r%   r&   _print_MatrixElement   s    zStrPrinter._print_MatrixElementc                sN    fdd} j |jtd ddd ||j|jj d ||j|jj d S )	Nc                sZ   t | } | d dkr| d= | d dkr.d| d< | d |krBd| d< dt fdd| S )N   r   r   r5   :c                s
     | S )N)r    )re   )r!   r%   r&   rf     rg   zAStrPrinter._print_MatrixSlice.<locals>.strslice.<locals>.<lambda>)rG   r*   rh   )xdim)r!   r%   r&   strslice   s    z/StrPrinter._print_MatrixSlice.<locals>.strslicer   T)r$   [z, ])r'   r   r   ZrowslicerowsZcolslicecols)r!   r1   r   r%   )r!   r&   _print_MatrixSlice   s    zStrPrinter._print_MatrixSlicec             C   s   |j S )N)rx   )r!   r1   r%   r%   r&   _print_DeferredVector  s    z StrPrinter._print_DeferredVectorc                s  t | |j}|d tjks8tdd |dd  D rt|dd dd\}}xlt|D ]`\}}|jjrr|j }n$t	|jj}|d  |d< t
|}|d rt|j|d	d
n|j||< qXW g }	|r|d  rt|dg}	|	 fdd|D  }
|
sdg}
t|dkr2|d  r2t|dg}	ng }	|	 fdd|D  }d|
}d|}t|dkr|d||f S |rd||f S |S | \}}|dk rt| |}d}nd}g }g }g }jdkr| }n
t
|}dd }x|D ]}|jrt|trt|j d dk r|jtjk	rF||| n>t|jd jdkrxt|jt
tfrx|| ||j nX|jr|tjk	r|jdkr|t |j |j!dkr|t |j! n
|| qW |ptjg} fdd|D } fdd|D }x:|D ]2}|j|kr d||"|j  ||"|j< q W |sj|d| S t|dkr|d| d |d  S |d| dd|  S d S )Nr   c             s   s2   | ]*}t |tp(|jo(td d |jD V  qdS )c             s   s   | ]}|j V  qd S )N)Z
is_Integer)r(   Zair%   r%   r&   r     s    z2StrPrinter._print_Mul.<locals>.<genexpr>.<genexpr>N)r.   r	   Zis_Powallr+   )r(   r   r%   r%   r&   r     s   z(StrPrinter._print_Mul.<locals>.<genexpr>r   c             S   s    t | tot| j d dk S )Nr   )r.   r   boolexpas_coeff_Mul)r   r%   r%   r&   rf     s    z'StrPrinter._print_Mul.<locals>.<lambda>T)binaryF)evaluatec                s   g | ]}j | d dqS )F)r$   )r'   )r(   r   )precr!   r%   r&   r)   '  s   z)StrPrinter._print_Mul.<locals>.<listcomp>1c                s   g | ]}j | d dqS )F)r$   )r'   )r(   r   )r   r!   r%   r&   r)   1  s   *z%s/(%s)z%s/%sr3   r5   )oldnonec             S   sv   |   \}}tt|}|d tjkr6|dd  }n|d  |d< t|}t| trh| j	||ddS | j	|ddS )Nr   r   F)r   )
Zas_base_exprG   r   	make_argsr   NegativeOne
_from_argsr.   r   r~   )rN   r   r   Zeargsr%   r%   r&   apowO  s    

z#StrPrinter._print_Mul.<locals>.apowc                s   g | ]}j | d dqS )F)r$   )r'   )r(   r   )r   r!   r%   r&   r)   p  s    c                s   g | ]}j | d dqS )F)r$   )r'   )r(   r   )r   r!   r%   r&   r)   q  s    z(%s)/z/(%s))#r   r+   r   Oneanyr   rH   r   Z	is_NumberrG   r   r   r   baseZcould_extract_minus_signr/   r9   r   r*   r   r
   r   Zas_ordered_factorsr   is_commutativer.   r   r   rm   is_RationalInfinitypr   qindex)r!   r1   r+   rn   nrN   Zdir   dargspreZnfactorsZdfactorscr?   r   r   Z	pow_parenr   r"   Za_strZb_strr%   )r   r!   r&   
_print_Mul	  s    


&









&zStrPrinter._print_Mulc                s|      \}}d}|jr\| \}}|jr@|jr@t| | d}n|jr\|jr\t| | d}|d fdd jD  S )Nr5   r3   r   c                s   g | ]} |t qS r%   )r'   r   )r(   re   )r1   r!   r%   r&   r)     s    z,StrPrinter._print_MatMul.<locals>.<listcomp>)Zas_coeff_mmulZ	is_numberZas_real_imagis_zeroZis_negativer
   r*   r+   )r!   r1   r   r   r?   reZimr%   )r1   r!   r&   _print_MatMul  s    zStrPrinter._print_MatMulc             C   s   d |j| |jS )Nz{}.({}))r   rR   r    r1   )r!   r1   r%   r%   r&   _print_ElementwiseApplyFunction  s    z*StrPrinter._print_ElementwiseApplyFunctionc             C   s   dS )Nnanr%   )r!   r1   r%   r%   r&   
_print_NaN  s    zStrPrinter._print_NaNc             C   s   dS )Nz-oor%   )r!   r1   r%   r%   r&   _print_NegativeInfinity  s    z"StrPrinter._print_NegativeInfinityc             C   sn   |j rtdd |jD rVt|j dkr8d| |j S d| |jf|j  dd S nd| |jdd S d S )Nc             s   s   | ]}|t jkV  qd S )N)r   ZZero)r(   r   r%   r%   r&   r     s    z*StrPrinter._print_Order.<locals>.<genexpr>r   zO(%s)z, r   )	variablesr   pointr   r    r1   r-   r+   )r!   r1   r%   r%   r&   _print_Order  s
    zStrPrinter._print_Orderc             C   s   |  S )N)__str__)r!   r1   r%   r%   r&   _print_Ordinal  s    zStrPrinter._print_Ordinalc             C   s   |  S )N)r   )r!   r1   r%   r%   r&   _print_Cycle  s    zStrPrinter._print_Cyclec             C   sb  ddl m}m} ddlm} |j}|d k	rD|d| ddddd	 n| jd
d}|r|js`dS |||jd 	 t
dd  }|d}|dksd||d  kr||d  |d |  }|dd}|S | }|s|jdk rd| |j S d| |j S | |jd |d d  d| |j  }| |j }	}
t
|t
|
k rV|}	d|	 S d S )Nr   )PermutationCycle)sympy_deprecation_warningzw
                Setting Permutation.print_cyclic is deprecated. Instead use
                init_printing(perm_cyclic=z).
                z1.6z#deprecated-permutation-print_cyclic   )Zdeprecated_since_versionZactive_deprecations_target
stacklevelr   Tz()r   r   (,r5      zPermutation(%s)zPermutation([], size=%s)z	, size=%s)Z sympy.combinatorics.permutationsr   r   Zsympy.utilities.exceptionsr   Zprint_cyclic	_settingsgetsize__repr__r   rfindreplaceZsupportr    Z
array_form)r!   r1   r   r   r   r   rc   lastZtrimZusefullr%   r%   r&   _print_Permutation  s:    

"

,zStrPrinter._print_Permutationc             C   sJ   |j \}}}t|jdkr*|d }|d }d| || || |f S )Nr   r   zSubs(%s, %s, %s))r+   r   r   r    )r!   r   r1   r   newr%   r%   r&   _print_Subs  s    zStrPrinter._print_Subsc             C   s   |  S )N)r    )r!   r1   r%   r%   r&   _print_TensorIndex  s    zStrPrinter._print_TensorIndexc             C   s   |  S )N)r    )r!   r1   r%   r%   r&   _print_TensorHead  s    zStrPrinter._print_TensorHeadc             C   s   |  S )N)r    )r!   r1   r%   r%   r&   _print_Tensor  s    zStrPrinter._print_Tensorc                s*      \}}|d fdd|D  S )Nr   c                s   g | ]} |t qS r%   )r'   r   )r(   re   )r1   r!   r%   r&   r)     s    z-StrPrinter._print_TensMul.<locals>.<listcomp>)Z!_get_args_for_traditional_printerr*   )r!   r1   r?   r+   r%   )r1   r!   r&   _print_TensMul  s    zStrPrinter._print_TensMulc             C   s   |  S )N)r    )r!   r1   r%   r%   r&   _print_TensAdd  s    zStrPrinter._print_TensAddc             C   s   |  |jS )N)r    rx   )r!   r1   r%   r%   r&   _print_ArraySymbol  s    zStrPrinter._print_ArraySymbolc                s2   d  |jtd dd fdd|jD f S )Nz%s[%s]ZFuncTz, c                s   g | ]}  |qS r%   )r    )r(   rN   )r!   r%   r&   r)     s    z2StrPrinter._print_ArrayElement.<locals>.<listcomp>)r'   rx   r   r*   indices)r!   r1   r%   )r!   r&   _print_ArrayElement  s    zStrPrinter._print_ArrayElementc                s"    fdd|j D }dd| S )Nc                s   g | ]}d   | qS )z    %s)r    )r(   r   )r!   r%   r&   r)     s    z6StrPrinter._print_PermutationGroup.<locals>.<listcomp>zPermutationGroup([
%s])z,
)r+   r*   )r!   r1   r   r%   )r!   r&   _print_PermutationGroup  s    z"StrPrinter._print_PermutationGroupc             C   s   dS )Npir%   )r!   r1   r%   r%   r&   	_print_Pi  s    zStrPrinter._print_Pic                s4   dd t fdd|j |j |jf S )Nz+Polynomial ring in %s over %s with %s orderz, c                s
     | S )N)r    )rs)r!   r%   r&   rf     rg   z,StrPrinter._print_PolyRing.<locals>.<lambda>)r*   rh   ru   r    domainr   )r!   ringr%   )r!   r&   _print_PolyRing  s    zStrPrinter._print_PolyRingc                s4   dd t fdd|j |j |jf S )Nz3Rational function field in %s over %s with %s orderz, c                s
     | S )N)r    )fs)r!   r%   r&   rf     rg   z-StrPrinter._print_FracField.<locals>.<lambda>)r*   rh   ru   r    r   r   )r!   fieldr%   )r!   r&   _print_FracField  s    zStrPrinter._print_FracFieldc             C   s   |  S )N)r   )r!   elmr%   r%   r&   _print_FreeGroupElement  s    z"StrPrinter._print_FreeGroupElementc             C   s   d|j |jf S )Nz(%s + %s*I))r   y)r!   polyr%   r%   r&   _print_GaussianElement
  s    z!StrPrinter._print_GaussianElementc             C   s   | | tddS )Nz%s**%sr   )r/   r   )r!   r  r%   r%   r&   _print_PolyElement  s    zStrPrinter._print_PolyElementc             C   sR   |j dkr| |jS | j|jtd dd}| j|j td dd}|d | S d S )Nr   r   T)r$   r   r   )denomr    numerr'   r   )r!   fracr  r  r%   r%   r&   _print_FracElement  s
    
zStrPrinter._print_FracElementc                sb  t d d  g  fdd|jD  }}x&| D ]\}}g }xJt|D ]>\}}|dkrL|dkrt|||  qL||| d|   qLW d|}|jr|rd| d	 }	n
|}	nB|r|tj	kr|
d
|g q4|tjkr|
d|g q4|}	|s|	}
n|	d | }
|
dr@|
d|
dd  g q4|
d
|
g q4W |d dkr|d}|dkrd|d  |d< |jjd }ddlm} y|d|  7 }W n& |k
r   |d|  7 }Y nX |d	7 }xht|D ]\\}}t|dkr|d d dkr|t|d d  d	kr|dt|d  ||< qW |d|d|f S )Nr   r   c                s   g | ]} | qS r%   )r'   )r(   rc   )	ATOM_PRECr!   r%   r&   r)     s    z*StrPrinter._print_Poly.<locals>.<listcomp>r   z**%dr   r   )r4   r3   )r3   r4   z(%s, %s)PolynomialErrorz, modulus=%sz, domain='%s'r   r6   z, )r   gensr:   rH   rm   r*   Zis_Addr    r   r   r8   r   r7   r9   rV   rW   Zsympy.polys.polyerrorsr  Zget_modulusZ
get_domainr   )r!   r1   r:   r  ZmonomZcoeffZs_monomrN   r   Zs_coeffZs_termmodifierr   r  r   r"   r%   )r  r!   r&   _print_Poly  sV    





:zStrPrinter._print_Polyc             C   s   dS )Nrb   r%   )r!   r   r%   r%   r&   _print_UniversalSetZ  s    zStrPrinter._print_UniversalSetc             C   s*   |j r| |  S | | S d S )N)Z
is_aliasedr    Zas_polyZas_expr)r!   r1   r%   r%   r&   _print_AlgebraicNumber]  s    z!StrPrinter._print_AlgebraicNumberc                s  t |}|jtjkr(|s(d |j S |jr|j tjkrb|sbdtt fddtj	|jf S |jtj	 krd tj	 j
|j|ddf S  j
|j|dd} jdkr|jjr|jjd	kr|d
rd j
|j|dd|d	d f S d j
|j|dd|f S )a$  Printing helper function for ``Pow``

        Parameters
        ==========

        rational : bool, optional
            If ``True``, it will not attempt printing ``sqrt(x)`` or
            ``x**S.Half`` as ``sqrt``, and will use ``x**(1/2)``
            instead.

            See examples for additional details

        Examples
        ========

        >>> from sympy import sqrt, StrPrinter
        >>> from sympy.abc import x

        How ``rational`` keyword works with ``sqrt``:

        >>> printer = StrPrinter()
        >>> printer._print_Pow(sqrt(x), rational=True)
        'x**(1/2)'
        >>> printer._print_Pow(sqrt(x), rational=False)
        'sqrt(x)'
        >>> printer._print_Pow(1/sqrt(x), rational=True)
        'x**(-1/2)'
        >>> printer._print_Pow(1/sqrt(x), rational=False)
        '1/sqrt(x)'

        Notes
        =====

        ``sqrt(x)`` is canonicalized as ``Pow(x, S.Half)`` in SymPy,
        so there is no need of defining a separate printer for ``sqrt``.
        Instead, it should be handled here as well.
        zsqrt(%s)z%s/sqrt(%s)c                s
     | S )N)r    )re   )r!   r%   r&   rf     rg   z'StrPrinter._print_Pow.<locals>.<lambda>z%s/%sF)r$   Z
_sympyreprr   z	(Rationalz%s**%sr   )r   r   r   ZHalfr    r   r   r_   rh   r   r'   printmethodr   r   r7   )r!   r1   Zrationalr;   r   r%   )r!   r&   
_print_Powc  s    &"
"zStrPrinter._print_Powc             C   s   |  |jd S )Nr   )r    r+   )r!   r1   r%   r%   r&   _print_UnevaluatedExpr  s    z!StrPrinter._print_UnevaluatedExprc             C   s0   t |}d| j|j|dd| j|j|ddf S )Nz%s**%sF)r$   )r   r'   r   r   )r!   r1   r;   r%   r%   r&   _print_MatPow  s    zStrPrinter._print_MatPowc             C   s    | j ddrd| S t|jS )Nr   FzS(%s))r   r   r/   r   )r!   r1   r%   r%   r&   _print_Integer  s    zStrPrinter._print_Integerc             C   s   dS )NZIntegersr%   )r!   r1   r%   r%   r&   _print_Integers  s    zStrPrinter._print_Integersc             C   s   dS )NZNaturalsr%   )r!   r1   r%   r%   r&   _print_Naturals  s    zStrPrinter._print_Naturalsc             C   s   dS )NZ	Naturals0r%   )r!   r1   r%   r%   r&   _print_Naturals0  s    zStrPrinter._print_Naturals0c             C   s   dS )NZ	Rationalsr%   )r!   r1   r%   r%   r&   _print_Rationals  s    zStrPrinter._print_Rationalsc             C   s   dS )NZRealsr%   )r!   r1   r%   r%   r&   _print_Reals  s    zStrPrinter._print_Realsc             C   s   dS )NZ	Complexesr%   )r!   r1   r%   r%   r&   _print_Complexes  s    zStrPrinter._print_Complexesc             C   s   dS )NZEmptySetr%   )r!   r1   r%   r%   r&   _print_EmptySet  s    zStrPrinter._print_EmptySetc             C   s   dS )NZEmptySequencer%   )r!   r1   r%   r%   r&   _print_EmptySequence  s    zStrPrinter._print_EmptySequencec             C   s   t |S )N)r/   )r!   r1   r%   r%   r&   
_print_int  s    zStrPrinter._print_intc             C   s   t |S )N)r/   )r!   r1   r%   r%   r&   
_print_mpz  s    zStrPrinter._print_mpzc             C   sF   |j dkrt|jS | jddr2d|j|j f S d|j|j f S d S )Nr   r   FzS(%s)/%sz%s/%s)r   r/   r   r   r   )r!   r1   r%   r%   r&   _print_Rational  s
    

zStrPrinter._print_Rationalc             C   s(   |j dkrt|jS d|j|j f S d S )Nr   z%d/%d)r   r/   r   )r!   r1   r%   r%   r&   _print_PythonRational  s    

z StrPrinter._print_PythonRationalc             C   s(   |j dkrt|jS d|j|j f S d S )Nr   z%s/%s)denominatorr/   	numerator)r!   r1   r%   r%   r&   _print_Fraction  s    

zStrPrinter._print_Fractionc             C   s(   |j dkrt|jS d|j|j f S d S )Nr   z%s/%s)r$  r/   r%  )r!   r1   r%   r%   r&   
_print_mpq  s    

zStrPrinter._print_mpqc             C   s   |j }|dk rd}n
t|j }| jd dkr2d}n,| jd dkrFd}n| jd dkr^| jdk}d| jkrr| jd nd }d	| jkr| jd	 nd }t|j||||d
}|drd|dd   }n|drd|dd   }|dr|dd  }|S )Nr   r   r   TFr   r   r   r   )Zstrip_zerosZ	min_fixedZ	max_fixedz-.0z-0.   z.0z0.r   r4   )Z_precr   r   Z_print_levelmlib_to_strZ_mpf_r7   )r!   r1   r   Zdpsstriplowhighrvr%   r%   r&   _print_Float  s(    




zStrPrinter._print_Floatc          	   C   s~   ddddddddd	}|j |krDd
||j  | |j| |jf S d| |jt|| j|j ph|j | |jt|f S )NEqZNeZ
AssignmentZAddAugmentedAssignmentZSubAugmentedAssignmentZMulAugmentedAssignmentZDivAugmentedAssignmentZModAugmentedAssignment)z==z!=z:=z+=z-=z*=z/=z%=z
%s(%s, %s)z%s %s %s)Zrel_opr    lhsrJ   r'   r   _relationalsr   )r!   r1   charmapr%   r%   r&   _print_Relational  s    
zStrPrinter._print_Relationalc             C   s   d| j |jdd|jf S )NzCRootOf(%s, %d)lex)r   )r@   r1   r   )r!   r1   r%   r%   r&   _print_ComplexRootOf  s    zStrPrinter._print_ComplexRootOfc             C   s>   | j |jddg}|jtjk	r0|| |j dd| S )Nr4  )r   zRootSum(%s)z, )r@   r1   Zfunr   ZIdentityFunctionrm   r    r*   )r!   r1   r+   r%   r%   r&   _print_RootSum  s    zStrPrinter._print_RootSumc                s    j j} fdd jD }dd| }fdd jD }d j }d j }|g| ||g }d|d|f S )	Nc                s   g | ]}j | jd qS ))r   )r@   r   )r(   re   )basisr!   r%   r&   r)   !  s    z3StrPrinter._print_GroebnerBasis.<locals>.<listcomp>z[%s]z, c                s   g | ]}  |qS r%   )r    )r(   gen)r!   r%   r&   r)   $  s    zdomain='%s'z
order='%s'z%s(%s))rV   rW   exprsr*   r  r    r   r   )r!   r7  clsr9  r  r   r   r+   r%   )r7  r!   r&   _print_GroebnerBasis  s    zStrPrinter._print_GroebnerBasisc                s4   t |td}d fdd|D }|s,dS d| S )N)rj   z, c             3   s   | ]}  |V  qd S )N)r    )r(   r"   )r!   r%   r&   r   /  s    z(StrPrinter._print_set.<locals>.<genexpr>zset()z{%s})rk   r   r*   )r!   rc   ro   r+   r%   )r!   r&   
_print_set,  s
    zStrPrinter._print_setc                sJ   t |td}d fdd|D }tdd |D r@d|S d|S )N)rj   z, c             3   s   | ]}  |V  qd S )N)r    )r(   r"   )r!   r%   r&   r   7  s    z.StrPrinter._print_FiniteSet.<locals>.<genexpr>c             s   s   | ]}| tV  qd S )N)hasr   )r(   r"   r%   r%   r&   r   8  s    zFiniteSet({})z{{{}}})rk   r   r*   r   r   )r!   rc   ro   r+   r%   )r!   r&   _print_FiniteSet4  s
    
zStrPrinter._print_FiniteSetc                s.   t |td}d fdd|D }d|S )N)rj   z, c             3   s   | ]}  |V  qd S )N)r    )r(   re   )r!   r%   r&   r   ?  s    z.StrPrinter._print_Partition.<locals>.<genexpr>zPartition({}))rk   r   r*   r   )r!   rc   ro   r+   r%   )r!   r&   _print_Partition<  s    zStrPrinter._print_Partitionc             C   s   |sdS d|  | S )Nzfrozenset()zfrozenset(%s))r<  )r!   rc   r%   r%   r&   _print_frozensetB  s    zStrPrinter._print_frozensetc                s:   fdd d  fdd|jD }d|j|f S )Nc                s>   t | dkr | d S  | d ft| dd   S d S )Nr   r   )r   r    r_   )r   )r!   r%   r&   r   H  s    z)StrPrinter._print_Sum.<locals>._xab_tostrz, c                s   g | ]} |qS r%   r%   )r(   r<   )r   r%   r&   r)   M  s    z)StrPrinter._print_Sum.<locals>.<listcomp>zSum(%s, %s))r*   r   r    rR   )r!   r1   r   r%   )r   r!   r&   
_print_SumG  s    zStrPrinter._print_Sumc             C   s   |j S )N)rx   )r!   r1   r%   r%   r&   _print_SymbolP  s    zStrPrinter._print_Symbolc             C   s   dS )Nr   r%   )r!   r1   r%   r%   r&   _print_IdentityU  s    zStrPrinter._print_Identityc             C   s   dS )N0r%   )r!   r1   r%   r%   r&   _print_ZeroMatrixX  s    zStrPrinter._print_ZeroMatrixc             C   s   dS )Nr   r%   )r!   r1   r%   r%   r&   _print_OneMatrix[  s    zStrPrinter._print_OneMatrixc             C   s
   d|j  S )NzQ.%s)rx   )r!   r1   r%   r%   r&   _print_Predicate^  s    zStrPrinter._print_Predicatec             C   s   t |S )N)r/   )r!   r1   r%   r%   r&   
_print_stra  s    zStrPrinter._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!   r1   r%   r%   r&   _print_tupled  s    zStrPrinter._print_tuplec             C   s
   |  |S )N)rI  )r!   r1   r%   r%   r&   _print_Tuplej  s    zStrPrinter._print_Tuplec             C   s   d|  |jtd  S )Nz%s.Tr   )r'   re   r   )r!   Tr%   r%   r&   _print_Transposem  s    zStrPrinter._print_Transposec             C   s   d|  |j|  |jf S )NzUniform(%s, %s))r    r   r   )r!   r1   r%   r%   r&   _print_Uniformp  s    zStrPrinter._print_Uniformc             C   s"   | j ddrd|j S d|j S )Nr   Fz%s)r   r   r   rx   )r!   r1   r%   r%   r&   _print_Quantitys  s    
zStrPrinter._print_Quantityc                sD    fdd|j D }|d gdd t|dd  dD  }d|S )Nc                s    g | ]} j |td  ddqS )r   T)r$   )r'   r   )r(   rN   )r!   r%   r&   r)   y  s    z0StrPrinter._print_Quaternion.<locals>.<listcomp>r   c             S   s   g | ]\}}|d  | qS )r   r%   )r(   rN   rM   r%   r%   r&   r)   z  s    r   Zijkz + )r+   zipr*   )r!   r1   rc   r   r%   )r!   r&   _print_Quaternionx  s    &zStrPrinter._print_Quaternionc             C   s   t |S )N)r/   )r!   r1   r%   r%   r&   _print_Dimension}  s    zStrPrinter._print_Dimensionc             C   s
   |j d S )Nrw   )rx   )r!   r1   r%   r%   r&   _print_Wild  s    zStrPrinter._print_Wildc             C   s
   |j d S )Nrw   )rx   )r!   r1   r%   r%   r&   _print_WildFunction  s    zStrPrinter._print_WildFunctionc             C   s   |j S )N)rx   )r!   r1   r%   r%   r&   _print_WildDot  s    zStrPrinter._print_WildDotc             C   s   |j S )N)rx   )r!   r1   r%   r%   r&   _print_WildPlus  s    zStrPrinter._print_WildPlusc             C   s   |j S )N)rx   )r!   r1   r%   r%   r&   _print_WildStar  s    zStrPrinter._print_WildStarc             C   s   | j ddrdS dS )Nr   FzS(0)rD  )r   r   )r!   r1   r%   r%   r&   _print_Zero  s    zStrPrinter._print_Zeroc             C   sr   y |j d k	r| |j |S W n tk
r4   Y nX |jj}| |j}| |j}| |j }d||||f S )Nz%s(%s, %s, %s))r   r    Zto_sympyr   rV   rW   repdom)r!   r   r:  rX  rY  r   r%   r%   r&   
_print_DMP  s    
zStrPrinter._print_DMPc             C   s
   |  |S )N)rZ  )r!   r1   r%   r%   r&   
_print_DMF  s    zStrPrinter._print_DMFc             C   s
   d|j  S )NzObject("%s"))rx   )r!   r   r%   r%   r&   _print_Object  s    zStrPrinter._print_Objectc             C   s
   d|j  S )NzIdentityMorphism(%s))r   )r!   morphismr%   r%   r&   _print_IdentityMorphism  s    z"StrPrinter._print_IdentityMorphismc             C   s   d|j |j|jf S )NzNamedMorphism(%s, %s, "%s"))r   Zcodomainrx   )r!   r]  r%   r%   r&   _print_NamedMorphism  s    zStrPrinter._print_NamedMorphismc             C   s
   d|j  S )NzCategory("%s"))rx   )r!   categoryr%   r%   r&   _print_Category  s    zStrPrinter._print_Categoryc             C   s   |j j S )N)rx   )r!   Zmanifoldr%   r%   r&   _print_Manifold  s    zStrPrinter._print_Manifoldc             C   s   |j j S )N)rx   )r!   patchr%   r%   r&   _print_Patch  s    zStrPrinter._print_Patchc             C   s   |j j S )N)rx   )r!   Zcoordsr%   r%   r&   _print_CoordSystem  s    zStrPrinter._print_CoordSystemc             C   s   |j j|j jS )N)
_coord_sysru   _indexrx   )r!   r   r%   r%   r&   _print_BaseScalarField  s    z!StrPrinter._print_BaseScalarFieldc             C   s   d|j j|j j S )Nze_%s)rf  ru   rg  rx   )r!   r   r%   r%   r&   _print_BaseVectorField  s    z!StrPrinter._print_BaseVectorFieldc             C   s6   |j }t|dr$d|jj|j j S d| | S d S )Nrf  zd%szd(%s))Z_form_fieldrt   rf  ru   rg  rx   r    )r!   diffr   r%   r%   r&   _print_Differential  s    
zStrPrinter._print_Differentialc             C   s   dd|  |jd f S )Nz%s(%s)ZTrr   )r    r+   )r!   r1   r%   r%   r&   	_print_Tr  s    zStrPrinter._print_Trc             C   s   |  |jS )N)r    rx   )r!   rc   r%   r%   r&   
_print_Str  s    zStrPrinter._print_Strc             C   s*   |j }d| || |j| |jf S )Nz
%s(%s, %s))rR   r    r0  rJ   )r!   r1   relr%   r%   r&   _print_AppliedBinaryRelation  s    

z'StrPrinter._print_AppliedBinaryRelation)F)r   )N)F)rW   
__module____qualname__r  Z_default_settingsdictr1  r'   r-   r2   r@   rB   rD   rF   rO   rP   rQ   rT   rX   r\   r]   r^   rd   ri   rp   rq   rv   ry   rz   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r"  r#  r&  r'  r.  r3  r5  r6  r;  r<  r>  r?  r@  rA  rB  Z_print_MatrixSymbolZ_print_RandomSymbolrC  rE  rF  rG  rH  rI  rJ  rL  rM  rN  rP  rQ  rR  rS  rT  rU  rV  rW  rZ  r[  r\  r^  r_  ra  rb  rd  re  rh  ri  rk  rl  rm  ro  r%   r%   r%   r&   r      s   
		v	)B=	r   c             K   s   t |}|| }|S )ab  Returns the expression as a string.

    For large expressions where speed is a concern, use the setting
    order='none'. If abbrev=True setting is used then units are printed in
    abbreviated form.

    Examples
    ========

    >>> from sympy import symbols, Eq, sstr
    >>> a, b = symbols('a b')
    >>> sstr(Eq(a + b, 0))
    'Eq(a + b, 0)'
    )r   doprint)r1   settingsr   rc   r%   r%   r&   sstr  s    
ru  c               @   s    e Zd ZdZdd Zdd ZdS )StrReprPrinterz(internal) -- see sstrreprc             C   s   t |S )N)r0   )r!   rc   r%   r%   r&   rH    s    zStrReprPrinter._print_strc             C   s   d|j j| |jf S )Nz%s(%s))rV   rW   r    rx   )r!   rc   r%   r%   r&   rm    s    zStrReprPrinter._print_StrN)rW   rp  rq  __doc__rH  rm  r%   r%   r%   r&   rv    s   rv  c             K   s   t |}|| }|S )zreturn expr in mixed str/repr form

       i.e. strings are returned in repr form with quotes, and everything else
       is returned in str form.

       This function could be useful for hooking into sys.displayhook
    )rv  rs  )r1   rt  r   rc   r%   r%   r&   sstrrepr  s    

rx  N)%rw  typingr   r   ZtDictZ
sympy.corer   r   r   r   r   r	   Zsympy.core.mulr
   Zsympy.core.relationalr   Zsympy.core.sortingr   Zsympy.core.sympifyr   Zsympy.sets.setsr   Zsympy.utilities.iterablesr   r   r   printerr   r   Zmpmath.libmpr   r   r)  r   ru  rv  rx  r%   r%   r%   r&   <module>   s,           I