o
    *ήc                     @   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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Add)_keep_coeff)
Relational)default_sort_key)SympifyError)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i 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[ Z/d\d] Z0d^d_ Z1d`da Z2dbdc Z3ddde Z4dfdg Z5dhdi Z6djdk Z7dldm Z8dndo Z9dpdq Z:drds Z;dtdu Z<dvdw Z=dxdy Z>dzd{ Z?d|d} Z@d~d ZAdd ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdddZMdd 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τ ZieiZjeiZkddф Zlddӄ 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 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 (  
StrPrinter	_sympystrNautoFT)order	full_precsympy_integersabbrevperm_cyclicminmaxc                 C   s4   t ||k s|st ||krd| | S | |S )N(%s))r   _print)selfitemlevelstrict r'   9/tmp/pip-target-vg8gfxp4/lib/python/sympy/printing/str.pyparenthesize!   s   
zStrPrinter.parenthesizer   c                    s   |  fdd|D S )Nc                       g | ]} | qS r'   r)   .0r$   r%   r#   r'   r(   
<listcomp>(       z(StrPrinter.stringify.<locals>.<listcomp>)join)r#   argssepr%   r'   r.   r(   	stringify'      zStrPrinter.stringifyc                 C   s(   t |tr|S t |trt|S t|S N)
isinstancestrr   reprr#   exprr'   r'   r(   emptyPrinter*   s
   

zStrPrinter.emptyPrinterc           	      C   s   | j ||d}t|}g }|D ]3}| |}|dr$d}|dd  }nd}t||k s1t|tr;||d| g q|||g q|d}|dkrNd}|d| S )	Nr   -r   +r!   r     )	_as_ordered_termsr   r"   
startswithr7   r
   extendpopr1   )	r#   r;   r   termsPRECltermtsignr'   r'   r(   
_print_Add2   s    


zStrPrinter._print_Addc                 C      dS )NTruer'   r:   r'   r'   r(   _print_BooleanTrueG      zStrPrinter._print_BooleanTruec                 C   rM   )NFalser'   r:   r'   r'   r(   _print_BooleanFalseJ   rP   zStrPrinter._print_BooleanFalsec                 C   s   d|  |jd td  S )Nz~%sr   Not)r)   r2   r   r:   r'   r'   r(   
_print_NotM   r5   zStrPrinter._print_Notc                 C   sX   t |j}t|D ]\}}t|tr"|jjtju r"|	d|
| q	| |dtd S )Nr   z & 
BitwiseAnd)listr2   	enumerater7   r   	canonicalrhsr   NegativeInfinityinsertrE   r4   r   )r#   r;   r2   jir'   r'   r(   
_print_AndP   s   

zStrPrinter._print_Andc                 C      |  |jdtd S )Nz | 	BitwiseOrr4   r2   r   r:   r'   r'   r(   	_print_OrX      zStrPrinter._print_Orc                 C   r_   )Nz ^ 
BitwiseXorra   r:   r'   r'   r(   
_print_Xor[   rc   zStrPrinter._print_Xorc                 C   s   d|  |j| |jdf S )N%s(%s), )r"   functionr4   	argumentsr:   r'   r'   r(   _print_AppliedPredicate^   s   z"StrPrinter._print_AppliedPredicatec                    s*    fdd|j D }|jjdd|  S )Nc                       g | ]}  |qS r'   r"   )r-   or#   r'   r(   r/   c       z+StrPrinter._print_Basic.<locals>.<listcomp>r!   rg   )r2   	__class____name__r1   )r#   r;   rH   r'   rn   r(   _print_Basicb   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_BlockMatrixf   s   zStrPrinter._print_BlockMatrixc                 C   rM   )NCatalanr'   r:   r'   r'   r(   _print_Catalank   rP   zStrPrinter._print_Catalanc                 C   rM   )Nzoor'   r:   r'   r'   r(   _print_ComplexInfinityn   rP   z!StrPrinter._print_ComplexInfinityc                    sL   t  fdd|j|jfD }|jtju rd| S | |jf7 }d| S )Nc                    rk   r'   rl   r-   r]   rn   r'   r(   r/   r   ro   z2StrPrinter._print_ConditionSet.<locals>.<listcomp>zConditionSet(%s, %s)zConditionSet(%s, %s, %s))tuplesym	conditionbase_setr   UniversalSetr"   )r#   sr2   r'   rn   r(   _print_ConditionSetq   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{   r'   r'   r(   r/   z   s   $ z0StrPrinter._print_Derivative.<locals>.<listcomp>zDerivative(%s)rg   c                    
     | S r6   rl   argrn   r'   r(   <lambda>{      
 z.StrPrinter._print_Derivative.<locals>.<lambda>)r;   variable_countr1   map)r#   r;   dexprdvarsr'   rn   r(   _print_Derivativex   s   "zStrPrinter._print_Derivativec                 C   sR   t | td}g }|D ]}d| || || f }|| qdd| S )Nkeyz%s: %s{%s}rg   )sortedkeysr   r"   appendr1   )r#   dr   itemsr   r$   r'   r'   r(   _print_dict}   s   zStrPrinter._print_dictc                 C   
   |  |S r6   )r   r:   r'   r'   r(   _print_Dict      
zStrPrinter._print_Dictc                 C   sV   t |drd| |  S t |dr#d| |j d | |j S d| |j S )N
as_booleanzDomain: setz in z
Domain on )hasattrr"   r   symbolsr   )r#   r   r'   r'   r(   _print_RandomDomain   s   


zStrPrinter._print_RandomDomainc                 C   s
   d|j  S N_namer:   r'   r'   r(   _print_Dummy   r   zStrPrinter._print_Dummyc                 C   rM   )N
EulerGammar'   r:   r'   r'   r(   _print_EulerGamma   rP   zStrPrinter._print_EulerGammac                 C   rM   )NEr'   r:   r'   r'   r(   _print_Exp1   rP   zStrPrinter._print_Exp1c                 C      d|  |j|  |jf S )Nz(%s, %s))r"   r;   condr:   r'   r'   r(   _print_ExprCondPair      zStrPrinter._print_ExprCondPairc                 C      |j jd| |jd  S Nr!   rg   )funcrq   r4   r2   r:   r'   r'   r(   _print_Function   r5   zStrPrinter._print_Functionc                 C   rM   )NGoldenRatior'   r:   r'   r'   r(   _print_GoldenRatio   rP   zStrPrinter._print_GoldenRatioc                 C   r   r   )r   rq   r4   pargsr:   r'   r'   r(   _print_Heaviside   s   zStrPrinter._print_Heavisidec                 C   rM   )NTribonacciConstantr'   r:   r'   r'   r(   _print_TribonacciConstant   rP   z$StrPrinter._print_TribonacciConstantc                 C   rM   NIr'   r:   r'   r'   r(   _print_ImaginaryUnit   rP   zStrPrinter._print_ImaginaryUnitc                 C   rM   )Noor'   r:   r'   r'   r(   _print_Infinity   rP   zStrPrinter._print_Infinityc                    :   fdd d  fdd|jD }d|j|f S )Nc                    :   t | dkr | d S  | d ft| dd   S Nr   r   lenr"   r|   xabrn   r'   r(   
_xab_tostr       z.StrPrinter._print_Integral.<locals>._xab_tostrrg   c                       g | ]} |qS r'   r'   r-   rH   r   r'   r(   r/          z.StrPrinter._print_Integral.<locals>.<listcomp>zIntegral(%s, %s)r1   limitsr"   rh   r#   r;   Lr'   r   r#   r(   _print_Integral      zStrPrinter._print_Integralc                 C   s   d}|j \}}}}|jr|jrd}n%|jr|sd}n|jr"|s"d}n|s)|s)d}n|r0|r0d}n|r5d}nd}|jdi |||dS )NzInterval{m}({a}, {b})r@   z.openz.Lopenz.Ropen)abmr'   )r2   is_infiniteformat)r#   r]   finr   r   rH   rr   r'   r'   r(   _print_Interval   s    

zStrPrinter._print_Intervalc                 C   r   )NzAccumBounds(%s, %s))r"   r   r    )r#   r]   r'   r'   r(   _print_AccumulationBounds   s   
z$StrPrinter._print_AccumulationBoundsc                 C      d|  |jtd  S )Nz%s**(-1)r   r)   r   r   )r#   r   r'   r'   r(   _print_Inverse      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))r;   	signaturer   	is_symbolr"   )r#   objr;   sigr'   r'   r(   _print_Lambda   s
   zStrPrinter._print_Lambdac                    s2   t |jtd}|jjdd fdd|D   S )Nr   r!   rg   c                 3       | ]}  |V  qd S r6   rl   r-   r   rn   r'   r(   	<genexpr>       z.StrPrinter._print_LatticeOp.<locals>.<genexpr>)r   r2   r   r   rq   r1   r#   r;   r2   r'   rn   r(   _print_LatticeOp   s   $zStrPrinter._print_LatticeOpc                 C   sP   |j \}}}}t|dkrdtt| j|||f S dtt| j||||f S )Nr?   zLimit(%s, %s, %s)zLimit(%s, %s, %s, dir='%s'))r2   r8   r|   r   r"   )r#   r;   ezz0dirr'   r'   r(   _print_Limit   s   

zStrPrinter._print_Limitc                 C   s   d|  |d S )N[%s]rg   )r4   r:   r'   r'   r(   _print_list      zStrPrinter._print_listc                 C   r   r6   )r   r:   r'   r'   r(   _print_List   r   zStrPrinter._print_Listc                 C   s
   | | S r6   )_format_strr:   r'   r'   r(   _print_MatrixBase   r   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"   r]   r\   r:   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r!d| d< dt fdd| S )N   r   r   r@   :c                    r   r6   rl   r   rn   r'   r(   r      r   zAStrPrinter._print_MatrixSlice.<locals>.strslice.<locals>.<lambda>)rV   r1   r   )xdimrn   r'   r(   strslice   s   z/StrPrinter._print_MatrixSlice.<locals>.strslicer   Tr   [rg   ])r)   r   r   rowslicerowscolslicecols)r#   r;   r   r'   rn   r(   _print_MatrixSlice   s   	zStrPrinter._print_MatrixSlicec                 C      |j S r6   r   r:   r'   r'   r(   _print_DeferredVector     z StrPrinter._print_DeferredVectorc                    s  t | |j}|d tju stdd |dd  D rt|dd dd\}}t|D ]0\}}|jjr7|j }nt	|jj}|d  |d< t
|}|d rUt|j|d	d
n|j||< q*g }	|rm|d  rmt|dg}	|	 fdd|D  }
|
s~dg}
t|dkr|d  rt|dg}	ng }	|	 fdd|D  }d|
}d|}t|dkrd||f S |rd||f S |S | \}}|dk rt| |}d}nd}g }g }g }jdvr| }nt
|}dd }|D ]s}|jr8t|tr8t|j d dk r8|jtjur||| qt|jd jdkr1t|jt
tfr1|| ||j q|jr_|tjur_|jdkrP|t |j |j!dkr^|t |j! q|| q|pktjg} fdd|D } fdd|D }|D ]}|j|v rd||"|j  ||"|j< q|s|d| S t|dkr|d| d |d  S |d| dd|  S )Nr   c                 s   s4    | ]}t |tp|jotd d |jD V  qdS )c                 s   s    | ]}|j V  qd S r6   )
is_Integer)r-   air'   r'   r(   r     s    z2StrPrinter._print_Mul.<locals>.<genexpr>.<genexpr>N)r7   r	   is_Powallr2   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   )r7   r   boolexpas_coeff_Mul)r   r'   r'   r(   r     s    z'StrPrinter._print_Mul.<locals>.<lambda>T)binaryFevaluatec                       g | ]
}j | d dqS Fr   r+   r  precr#   r'   r(   r/   %      z)StrPrinter._print_Mul.<locals>.<listcomp>1c                    r  r  r+   r  r  r'   r(   r/   /  r  *z%s/(%s)%s/%sr>   r@   )oldnonec                 S   sv   |   \}}tt|}|d tju r|dd  }n|d  |d< t|}t| tr4| j	||ddS | j	|ddS )Nr   r   Fr  )
as_base_exprV   r   	make_argsr   NegativeOne
_from_argsr7   r   r   )r]   r   r   eargsr'   r'   r(   apowM  s   

z#StrPrinter._print_Mul.<locals>.apowc                    r  r  r+   r-   r   r  r'   r(   r/   n      c                    r  r  r+   r   r  r'   r(   r/   o  r!  r!   /z/(%s))#r   r2   r   Oneanyr   rW   r  	is_NumberrV   r   r  r   basecould_extract_minus_signr8   rE   r   r1   r  r   r   as_ordered_factorsr  is_commutativer7   r
  r  r   is_RationalInfinitypr   qindex)r#   r;   r2   r   nr]   dir   dargsprenfactorsdfactorscrK   r   r   	pow_parenr  r$   a_strb_strr'   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 )Nr@   r>   r  c                       g | ]
} |t qS r'   r)   r   r   r;   r#   r'   r(   r/     r!  z,StrPrinter._print_MatMul.<locals>.<listcomp>)as_coeff_mmul	is_numberas_real_imagis_zerois_negativer   r1   r2   )r#   r;   r5  r   rK   reimr'   r<  r(   _print_MatMul}  s   zStrPrinter._print_MatMulc                 C   s   d |j| |jS )Nz{}.({}))r   rh   r"   r;   r:   r'   r'   r(   _print_ElementwiseApplyFunction  s   
z*StrPrinter._print_ElementwiseApplyFunctionc                 C   rM   )Nnanr'   r:   r'   r'   r(   
_print_NaN  rP   zStrPrinter._print_NaNc                 C   rM   )Nz-oor'   r:   r'   r'   r(   _print_NegativeInfinity  rP   z"StrPrinter._print_NegativeInfinityc                 C   sh   |j rtdd |jD r*t|j dkrd| |j S d| |jf|j  dd S d| |jdd S )Nc                 s   s    | ]}|t ju V  qd S r6   )r   Zero)r-   r,  r'   r'   r(   r     r   z*StrPrinter._print_Order.<locals>.<genexpr>r   zO(%s)rg   r   )	variablesr  pointr   r"   r;   r4   r2   r:   r'   r'   r(   _print_Order  s
   zStrPrinter._print_Orderc                 C      |  S r6   __str__r:   r'   r'   r(   _print_Ordinal     zStrPrinter._print_Ordinalc                 C   rM  r6   rN  r:   r'   r'   r(   _print_Cycle  rQ  zStrPrinter._print_Cyclec                 C   sZ  ddl m}m} ddlm} |j}|d ur"|d| ddddd	 n| jd
d}|rf|js0dS |||jd 	 t
dd  }|d}|dks^d||d  vr^||d  |d |  }|dd}|S | }|s|jdk ryd| |j S d| |j S | |jd |d d  d| |j  }| |j }	}
t
|t
|
k r|}	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   )deprecated_since_versionactive_deprecations_target
stacklevelr   Tz()r   rT  (,r@      zPermutation(%s)zPermutation([], size=%s)z	, size=%s) sympy.combinatorics.permutationsrS  rT  sympy.utilities.exceptionsrU  print_cyclic	_settingsgetsize__repr__r   rfindreplacesupportr"   
array_form)r#   r;   rS  rT  rU  r   r   lasttrim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))r2   r   rK  r"   )r#   r   r;   r  newr'   r'   r(   _print_Subs  s   zStrPrinter._print_Subsc                 C   rM  r6   rl   r:   r'   r'   r(   _print_TensorIndex  rQ  zStrPrinter._print_TensorIndexc                 C   rM  r6   rl   r:   r'   r'   r(   _print_TensorHead  rQ  zStrPrinter._print_TensorHeadc                 C   rM  r6   rl   r:   r'   r'   r(   _print_Tensor  rQ  zStrPrinter._print_Tensorc                    s*      \}}|d fdd|D  S )Nr  c                    r:  r'   r;  r   r<  r'   r(   r/     r!  z-StrPrinter._print_TensMul.<locals>.<listcomp>)!_get_args_for_traditional_printerr1   )r#   r;   rK   r2   r'   r<  r(   _print_TensMul  s   zStrPrinter._print_TensMulc                 C   rM  r6   rl   r:   r'   r'   r(   _print_TensAdd  rQ  zStrPrinter._print_TensAddc                 C      |  |jS r6   r"   r   r:   r'   r'   r(   _print_ArraySymbol     zStrPrinter._print_ArraySymbolc                    s2   d  |jtd dd fdd|jD f S )Nz%s[%s]FuncTrg   c                    rk   r'   rl   r{   rn   r'   r(   r/     ro   z2StrPrinter._print_ArrayElement.<locals>.<listcomp>)r)   r   r   r1   indicesr:   r'   rn   r(   _print_ArrayElement  s   *zStrPrinter._print_ArrayElementc                    s"    fdd|j D }dd| S )Nc                    s   g | ]	}d   | qS )z    %srl   r  rn   r'   r(   r/     s    z6StrPrinter._print_PermutationGroup.<locals>.<listcomp>zPermutationGroup([
%s])z,
)r2   r1   )r#   r;   r,  r'   rn   r(   _print_PermutationGroup  s   z"StrPrinter._print_PermutationGroupc                 C   rM   )Npir'   r:   r'   r'   r(   	_print_Pi  rP   zStrPrinter._print_Pic                    4   dd t fdd|j |j |jf S )Nz+Polynomial ring in %s over %s with %s orderrg   c                    r   r6   rl   )rsrn   r'   r(   r     r   z,StrPrinter._print_PolyRing.<locals>.<lambda>r1   r   r   r"   domainr   )r#   ringr'   rn   r(   _print_PolyRing  
   zStrPrinter._print_PolyRingc                    r  )Nz3Rational function field in %s over %s with %s orderrg   c                    r   r6   rl   )fsrn   r'   r(   r     r   z-StrPrinter._print_FracField.<locals>.<lambda>r  r#   fieldr'   rn   r(   _print_FracField   r  zStrPrinter._print_FracFieldc                 C   rM  r6   rN  )r#   elmr'   r'   r(   _print_FreeGroupElement  rQ  z"StrPrinter._print_FreeGroupElementc                 C   s   d|j |jf S )Nz(%s + %s*I))r   yr#   polyr'   r'   r(   _print_GaussianElement  r   z!StrPrinter._print_GaussianElementc                 C   s   | | tddS )N%s**%sr  )r8   r   r  r'   r'   r(   _print_PolyElement  r   zStrPrinter._print_PolyElementc                 C   sN   |j dkr| |jS | j|jtd dd}| j|j td dd}|d | S )Nr   r   Tr   r   r"  )denomr"   numerr)   r   )r#   fracr  r  r'   r'   r(   _print_FracElement  s
   
zStrPrinter._print_FracElementc                    sD  t d d  g  fdd|jD }}| D ]\}}g }t|D ]\}}|dkrA|dkr6|||  q"||| d|   q"d|}|jr\|rVd| d	 }	n'|}	n!|rx|tj	u rk|
d
|g q|tju rx|
d|g q|}	|s|	}
n|	d | }
|
dr|
d|
dd  g q|
d
|
g q|d dv r|d}|dkrd|d  |d< |jjd }ddlm} z
|d|  7 }W n |y   |d|  7 }Y nw |d	7 }t|D ]-\}}t|dkr|d d dkr|t|d d  d	kr|dt|d  ||< q|d|d|f S )Nr   r   c                    r*   r'   r+   )r-   r   	ATOM_PRECr#   r'   r(   r/     r0   z*StrPrinter._print_Poly.<locals>.<listcomp>r   z**%dr  rZ  )r?   r>   )r>   r?   z(%s, %s)PolynomialErrorz, modulus=%sz, domain='%s'r   rA   rg   )r   gensrF   rW   r   r1   is_Addr"   r   r#  rD   r  rC   rE   rp   rq   sympy.polys.polyerrorsr  get_modulus
get_domainr   )r#   r;   rF   r  monomcoeffs_monomr]   r   s_coeffs_termmodifierr   r  r.  r$   r'   r  r(   _print_Poly  s\   





:zStrPrinter._print_Polyc                 C   rM   )Nr   r'   )r#   r,  r'   r'   r(   _print_UniversalSetX  rP   zStrPrinter._print_UniversalSetc                 C   s&   |j r| |  S | | S r6   )
is_aliasedr"   as_polyas_exprr:   r'   r'   r(   _print_AlgebraicNumber[  s   z!StrPrinter._print_AlgebraicNumberc                    s  t |}|jtju r|sd |j S |jrI|j tju r1|s1dtt fddtj	|jf S |jtj	 u rId tj	 j
|j|ddf S  j
|j|dd} jdkrw|jjrw|jjd	krw|d
rwd 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                    r   r6   rl   r   rn   r'   r(   r     r   z'StrPrinter._print_Pow.<locals>.<lambda>r  Fr   
_sympyreprr   z	(Rationalr  r]  )r   r  r   Halfr"   r&  r)  r|   r   r#  r)   printmethodr*  r-  rC   )r#   r;   rationalrG   r   r'   rn   r(   
_print_Powa  s   &"
"zStrPrinter._print_Powc                 C   s   |  |jd S r	  r"   r2   r:   r'   r'   r(   _print_UnevaluatedExpr  r   z!StrPrinter._print_UnevaluatedExprc                 C   s0   t |}d| j|j|dd| j|j|ddf S )Nr  Fr   )r   r)   r&  r  )r#   r;   rG   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))ra  rb  r8   r,  r:   r'   r'   r(   _print_Integer  s   
zStrPrinter._print_Integerc                 C   rM   )NIntegersr'   r:   r'   r'   r(   _print_Integers  rP   zStrPrinter._print_Integersc                 C   rM   )NNaturalsr'   r:   r'   r'   r(   _print_Naturals  rP   zStrPrinter._print_Naturalsc                 C   rM   )N	Naturals0r'   r:   r'   r'   r(   _print_Naturals0  rP   zStrPrinter._print_Naturals0c                 C   rM   )N	Rationalsr'   r:   r'   r'   r(   _print_Rationals  rP   zStrPrinter._print_Rationalsc                 C   rM   )NRealsr'   r:   r'   r'   r(   _print_Reals  rP   zStrPrinter._print_Realsc                 C   rM   )N	Complexesr'   r:   r'   r'   r(   _print_Complexes  rP   zStrPrinter._print_Complexesc                 C   rM   )NEmptySetr'   r:   r'   r'   r(   _print_EmptySet  rP   zStrPrinter._print_EmptySetc                 C   rM   )NEmptySequencer'   r:   r'   r'   r(   _print_EmptySequence  rP   zStrPrinter._print_EmptySequencec                 C      t |S r6   r8   r:   r'   r'   r(   
_print_int  rQ  zStrPrinter._print_intc                 C   r  r6   r  r:   r'   r'   r(   
_print_mpz  rQ  zStrPrinter._print_mpzc                 C   sB   |j dkr
t|jS | jddrd|j|j f S d|j|j f S )Nr   r   FzS(%s)/%sr  )r-  r8   r,  ra  rb  r:   r'   r'   r(   _print_Rational  s
   

zStrPrinter._print_Rationalc                 C   $   |j dkr
t|jS d|j|j f S )Nr   z%d/%d)r-  r8   r,  r:   r'   r'   r(   _print_PythonRational     

z StrPrinter._print_PythonRationalc                 C   r  Nr   r  denominatorr8   	numeratorr:   r'   r'   r(   _print_Fraction  r  zStrPrinter._print_Fractionc                 C   r  r  r  r:   r'   r'   r(   
_print_mpq  r  zStrPrinter._print_mpqc                 C   s   |j }|dk r
d}nt|j }| jd du rd}n| jd du r#d}n| jd dkr/| jdk}d| jv r9| jd nd }d	| jv rE| jd	 nd }t|j||||d
}|dr_d|dd   }n|drld|dd   }|drw|dd  }|S )Nr\  r   r   TFr   r   r   r    )strip_zeros	min_fixed	max_fixedz-.0z-0.   z.0z0.r   r?   )_precr   ra  _print_levelmlib_to_str_mpf_rC   )r#   r;   r  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 |v r"d
||j  | |j| |jf S d| |jt|| j|j p4|j | |jt|f S )NEqNe
AssignmentAddAugmentedAssignmentSubAugmentedAssignmentMulAugmentedAssignmentDivAugmentedAssignmentModAugmentedAssignment)z==z!=z:=z+=z-=z*=z/=z%=
%s(%s, %s)z%s %s %s)rel_opr"   lhsrY   r)   r   _relationalsrb  )r#   r;   charmapr'   r'   r(   _print_Relational  s"   

zStrPrinter._print_Relationalc                 C   s   d| j |jdd|jf S )NzCRootOf(%s, %d)lexr=   )rL   r;   r.  r:   r'   r'   r(   _print_ComplexRootOf  s   zStrPrinter._print_ComplexRootOfc                 C   s>   | j |jddg}|jtjur|| |j dd| S )Nr  r=   zRootSum(%s)rg   )rL   r;   funr   IdentityFunctionr   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=   )rL   r   r   basisr#   r'   r(   r/     r!  z3StrPrinter._print_GroebnerBasis.<locals>.<listcomp>r   rg   c                    rk   r'   rl   )r-   genrn   r'   r(   r/   "  ro   zdomain='%s'z
order='%s'rf   )rp   rq   exprsr1   r  r"   r  r   )r#   r  clsr  r  r  r   r2   r'   r  r(   _print_GroebnerBasis  s   zStrPrinter._print_GroebnerBasisc                    s4   t |td}d fdd|D }|sdS d| S )Nr   rg   c                 3   r   r6   rl   r,   rn   r'   r(   r   -  r   z(StrPrinter._print_set.<locals>.<genexpr>zset()r   )r   r   r1   r#   r   r   r2   r'   rn   r(   
_print_set*  s
   zStrPrinter._print_setc                    sZ   ddl m  t|td}dfdd|D }t fdd|D r(d|S d	|S )
Nr   	FiniteSetr   rg   c                 3   r   r6   rl   r,   rn   r'   r(   r   6  r   z.StrPrinter._print_FiniteSet.<locals>.<genexpr>c                 3   s    | ]}|  V  qd S r6   )hasr,   r  r'   r(   r   7  r   zFiniteSet({})z{{{}}})sympy.sets.setsr  r   r   r1   r$  r   r  r'   )r  r#   r(   _print_FiniteSet2  s   

zStrPrinter._print_FiniteSetc                    s.   t |td}d fdd|D }d|S )Nr   rg   c                 3   r   r6   rl   r   rn   r'   r(   r   >  r   z.StrPrinter._print_Partition.<locals>.<genexpr>zPartition({}))r   r   r1   r   r  r'   rn   r(   _print_Partition;  s   
zStrPrinter._print_Partitionc                 C   s   |sdS d|  | S )Nzfrozenset()zfrozenset(%s))r  r#   r   r'   r'   r(   _print_frozensetA  s   zStrPrinter._print_frozensetc                    r   )Nc                    r   r   r   r   rn   r'   r(   r   G  r   z)StrPrinter._print_Sum.<locals>._xab_tostrrg   c                    r   r'   r'   r   r   r'   r(   r/   L  r   z)StrPrinter._print_Sum.<locals>.<listcomp>zSum(%s, %s)r   r   r'   r   r(   
_print_SumF  r   zStrPrinter._print_Sumc                 C   r  r6   r   r:   r'   r'   r(   _print_SymbolO  r  zStrPrinter._print_Symbolc                 C   rM   r   r'   r:   r'   r'   r(   _print_IdentityT  rP   zStrPrinter._print_Identityc                 C   rM   )N0r'   r:   r'   r'   r(   _print_ZeroMatrixW  rP   zStrPrinter._print_ZeroMatrixc                 C   rM   )Nr  r'   r:   r'   r'   r(   _print_OneMatrixZ  rP   zStrPrinter._print_OneMatrixc                 C   
   d|j  S )NzQ.%sr   r:   r'   r'   r(   _print_Predicate]  r   zStrPrinter._print_Predicatec                 C   r  r6   r  r:   r'   r'   r(   
_print_str`  rQ  zStrPrinter._print_strc                 C   s.   t |dkrd| |d  S d| |d S )Nr   z(%s,)r   r!   rg   )r   r"   r4   r:   r'   r'   r(   _print_tuplec  s   zStrPrinter._print_tuplec                 C   r   r6   )r  r:   r'   r'   r(   _print_Tuplei  r   zStrPrinter._print_Tuplec                 C   r   )Nz%s.Tr   r   )r#   Tr'   r'   r(   _print_Transposel  r   zStrPrinter._print_Transposec                 C   r   )NzUniform(%s, %s))r"   r   r   r:   r'   r'   r(   _print_Uniformo  r   zStrPrinter._print_Uniformc                 C   s"   | j ddrd|j S d|j S )Nr   Fz%s)ra  rb  r   r   r:   r'   r'   r(   _print_Quantityr  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   Tr   )r)   r   r{   rn   r'   r(   r/   x  s     z0StrPrinter._print_Quaternion.<locals>.<listcomp>r   c                 S   s   g | ]
\}}|d  | qS )r  r'   )r-   r]   r\   r'   r'   r(   r/   y  r!  r   ijkz + )r2   zipr1   )r#   r;   r   r   r'   rn   r(   _print_Quaternionw  s   &
zStrPrinter._print_Quaternionc                 C   r  r6   r  r:   r'   r'   r(   _print_Dimension|  rQ  zStrPrinter._print_Dimensionc                 C   
   |j d S r   r   r:   r'   r'   r(   _print_Wild  r   zStrPrinter._print_Wildc                 C   r  r   r   r:   r'   r'   r(   _print_WildFunction  r   zStrPrinter._print_WildFunctionc                 C   r  r6   r   r:   r'   r'   r(   _print_WildDot  r  zStrPrinter._print_WildDotc                 C   r  r6   r   r:   r'   r'   r(   _print_WildPlus  r  zStrPrinter._print_WildPlusc                 C   r  r6   r   r:   r'   r'   r(   _print_WildStar  r  zStrPrinter._print_WildStarc                 C   s   | j ddr	dS dS )Nr   FzS(0)r  )ra  rb  r:   r'   r'   r(   _print_Zero  s   zStrPrinter._print_Zeroc                 C   sr   z|j d ur| |j |W S W n	 ty   Y nw |jj}| |j}| |j}| |j }d||||f S )Nz%s(%s, %s, %s))r  r"   to_sympyr   rp   rq   repdom)r#   r,  r  r  r   r  r'   r'   r(   
_print_DMP  s   
zStrPrinter._print_DMPc                 C   r   r6   )r!  r:   r'   r'   r(   
_print_DMF  r   zStrPrinter._print_DMFc                 C   r
  )NzObject("%s")r   )r#   r   r'   r'   r(   _print_Object  r   zStrPrinter._print_Objectc                 C   r
  )NzIdentityMorphism(%s))r  r#   morphismr'   r'   r(   _print_IdentityMorphism  r   z"StrPrinter._print_IdentityMorphismc                 C   s   d|j |j|jf S )NzNamedMorphism(%s, %s, "%s"))r  codomainr   r$  r'   r'   r(   _print_NamedMorphism  s   zStrPrinter._print_NamedMorphismc                 C   r
  )NzCategory("%s")r   )r#   categoryr'   r'   r(   _print_Category  r   zStrPrinter._print_Categoryc                 C      |j j S r6   r   )r#   manifoldr'   r'   r(   _print_Manifold  rQ  zStrPrinter._print_Manifoldc                 C   r+  r6   r   )r#   patchr'   r'   r(   _print_Patch  rQ  zStrPrinter._print_Patchc                 C   r+  r6   r   )r#   coordsr'   r'   r(   _print_CoordSystem  rQ  zStrPrinter._print_CoordSystemc                 C   s   |j j|j jS r6   
_coord_sysr   _indexr   r  r'   r'   r(   _print_BaseScalarField  r   z!StrPrinter._print_BaseScalarFieldc                 C   s   d|j j|j j S )Nze_%sr2  r  r'   r'   r(   _print_BaseVectorField  rc   z!StrPrinter._print_BaseVectorFieldc                 C   s2   |j }t|drd|jj|j j S d| | S )Nr3  zd%szd(%s))_form_fieldr   r3  r   r4  r   r"   )r#   diffr  r'   r'   r(   _print_Differential  s   
zStrPrinter._print_Differentialc                 C   s   dd|  |jd f S )Nrf   Trr   r  r:   r'   r'   r(   	_print_Tr     zStrPrinter._print_Trc                 C   rv  r6   rw  r  r'   r'   r(   
_print_Str  ry  zStrPrinter._print_Strc                 C   s*   |j }d| || |j| |jf S )Nr  )rh   r"   r  rY   )r#   r;   relr'   r'   r(   _print_AppliedBinaryRelation  s
   


z'StrPrinter._print_AppliedBinaryRelation)F)r   r6   )rq   
__module____qualname__r  _default_settingsr  r)   r4   r<   rL   rO   rR   rT   r^   rb   re   rj   rr   rv   rx   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  r9  rD  rE  rG  rH  rL  rP  rR  rm  ro  rp  rq  rr  rt  ru  rx  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  _print_MatrixSymbol_print_RandomSymbolr  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/  r1  r5  r6  r9  r;  r=  r?  r'   r'   r'   r(   r      s$   

		v	)B=		r   c                 K      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r;   settingsr,  r   r'   r'   r(   sstr  s   
rI  c                   @   s    e Zd ZdZdd Zdd ZdS )StrReprPrinterz(internal) -- see sstrreprc                 C   r  r6   )r9   r  r'   r'   r(   r    rQ  zStrReprPrinter._print_strc                 C   s   d|j j| |jf S )Nrf   )rp   rq   r"   r   r  r'   r'   r(   r=    r<  zStrReprPrinter._print_StrN)rq   r@  rA  __doc__r  r=  r'   r'   r'   r(   rJ    s    rJ  c                 K   rE  )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
    )rJ  rF  rG  r'   r'   r(   sstrrepr  s   

rL  N)$rK  typingr   r   tDict
sympy.corer   r   r   r   r   r	   r
   sympy.core.mulr   sympy.core.relationalr   sympy.core.sortingr   sympy.core.sympifyr   sympy.utilities.iterablesr   r   r   printerr   r   mpmath.libmpr   r   r  r   rI  rJ  rL  r'   r'   r'   r(   <module>   s0    $       J
