o
    8Va                     @   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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)_coeff_isneg)	FiniteSet   )Printerprint_function)
precedence
PRECEDENCE)prec_to_dpsto_str)default_sort_keysiftc                   @   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 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 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 ZdS (  
StrPrinterZ	_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%   4/usr/lib/python3/dist-packages/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 ].}| |}|dr$d}|dd  }nd}t||k r6||d| g q|||g q|d}|dkrId}|d| S )	Nr   -r   +r   r     )Z_as_ordered_termsr   r    
startswithextendpopr/   )	r!   r9   r   termsPREClZtermtsignr%   r%   r&   
_print_Add2   s    


zStrPrinter._print_Addc                 C      dS )NTruer%   r8   r%   r%   r&   _print_BooleanTrueG      zStrPrinter._print_BooleanTruec                 C   rI   )NFalser%   r8   r%   r%   r&   _print_BooleanFalseJ   rL   zStrPrinter._print_BooleanFalsec                 C   s   d|  |jd td  S )Nz~%sr   ZNot)r'   r0   r   r8   r%   r%   r&   
_print_NotM   r3   zStrPrinter._print_Notc                 C      |  |jdtd S )Nz & Z
BitwiseAndr2   r0   r   r8   r%   r%   r&   
_print_AndP      zStrPrinter._print_Andc                 C   rP   )Nz | Z	BitwiseOrrQ   r8   r%   r%   r&   	_print_OrS   rS   zStrPrinter._print_Orc                 C   rP   )Nz ^ Z
BitwiseXorrQ   r8   r%   r%   r&   
_print_XorV   rS   zStrPrinter._print_Xorc                 C   s   d|  |j| |jdf S )N%s(%s), )r    functionr2   Z	argumentsr8   r%   r%   r&   _print_AppliedPredicateY   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-   ^       z+StrPrinter._print_Basic.<locals>.<listcomp>r   rW   )r0   	__class____name__r/   )r!   r9   rE   r%   r]   r&   _print_Basic]   s   zStrPrinter._print_Basicc                 C   s(   |j jdkr| |j d  | |j S )N)r   r   )r   r   )Zblocksshaper    )r!   Br%   r%   r&   _print_BlockMatrixa   s   zStrPrinter._print_BlockMatrixc                 C   rI   )NZCatalanr%   r8   r%   r%   r&   _print_Catalanf   rL   zStrPrinter._print_Catalanc                 C   rI   )NZzoor%   r8   r%   r%   r&   _print_ComplexInfinityi   rL   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                    rZ   r%   r[   r+   ir]   r%   r&   r-   m   r^   z2StrPrinter._print_ConditionSet.<locals>.<listcomp>zConditionSet(%s, %s)zConditionSet(%s, %s, %s))tupleZsymZ	conditionZbase_setr   UniversalSetr    )r!   sr0   r%   r]   r&   _print_ConditionSetl   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%   rg   r%   r%   r&   r-   u   s   $ z0StrPrinter._print_Derivative.<locals>.<listcomp>zDerivative(%s)rW   c                    
     | S r4   r[   argr]   r%   r&   <lambda>v      
 z.StrPrinter._print_Derivative.<locals>.<lambda>)r9   Zvariable_countr/   map)r!   r9   ZdexprZdvarsr%   r]   r&   _print_Derivatives   s   "zStrPrinter._print_Derivativec                 C   sR   t | td}g }|D ]}d| || || f }|| qdd| S )Nkeyz%s: %s{%s}rW   )sortedkeysr   r    appendr/   )r!   drx   itemsru   r"   r%   r%   r&   _print_dictx   s   zStrPrinter._print_dictc                 C   
   |  |S r4   )r|   r8   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!   rz   r%   r%   r&   _print_RandomDomain   s   


zStrPrinter._print_RandomDomainc                 C   s
   d|j  S N_namer8   r%   r%   r&   _print_Dummy   r   zStrPrinter._print_Dummyc                 C   rI   )NZ
EulerGammar%   r8   r%   r%   r&   _print_EulerGamma   rL   zStrPrinter._print_EulerGammac                 C   rI   )NEr%   r8   r%   r%   r&   _print_Exp1   rL   zStrPrinter._print_Exp1c                 C      d|  |j|  |jf S )Nz(%s, %s))r    r9   Zcondr8   r%   r%   r&   _print_ExprCondPair      zStrPrinter._print_ExprCondPairc                 C      |j jd| |jd  S Nr   rW   )funcr`   r2   r0   r8   r%   r%   r&   _print_Function   r3   zStrPrinter._print_Functionc                 C   rI   )NZGoldenRatior%   r8   r%   r%   r&   _print_GoldenRatio   rL   zStrPrinter._print_GoldenRatioc                 C   r   r   )r   r`   r2   Zpargsr8   r%   r%   r&   _print_Heaviside   s   zStrPrinter._print_Heavisidec                 C   rI   )NZTribonacciConstantr%   r8   r%   r%   r&   _print_TribonacciConstant   rL   z$StrPrinter._print_TribonacciConstantc                 C   rI   NIr%   r8   r%   r%   r&   _print_ImaginaryUnit   rL   zStrPrinter._print_ImaginaryUnitc                 C   rI   )NZoor%   r8   r%   r%   r&   _print_Infinity   rL   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    ri   Zxabr]   r%   r&   
_xab_tostr       z.StrPrinter._print_Integral.<locals>._xab_tostrrW   c                       g | ]} |qS r%   r%   r+   rE   r   r%   r&   r-          z.StrPrinter._print_Integral.<locals>.<listcomp>zIntegral(%s, %s)r/   Zlimitsr    rX   r!   r9   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%   )r0   Zis_infiniteformat)r!   rh   Zfinr   r   rE   rr   r%   r%   r&   _print_Interval   s    

zStrPrinter._print_Intervalc                 C   r   )NzAccumBounds(%s, %s))r    r   r   )r!   rh   r%   r%   r&   _print_AccumulationBounds   s   
z$StrPrinter._print_AccumulationBoundsc                 C      d|  |jtd  S )Nz%s**(-1)r   r'   ro   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))r9   Z	signaturer   Z	is_symbolr    )r!   objr9   Zsigr%   r%   r&   _print_Lambda   s
   zStrPrinter._print_Lambdac                    s2   t |jtd}|jjdd fdd|D   S )Nrt   r   rW   c                 3       | ]}  |V  qd S r4   r[   r+   ro   r]   r%   r&   	<genexpr>       z.StrPrinter._print_LatticeOp.<locals>.<genexpr>)rw   r0   r   r   r`   r/   r!   r9   r0   r%   r]   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'))r0   r6   ri   rr   r    )r!   r9   ezZz0dirr%   r%   r&   _print_Limit   s   

zStrPrinter._print_Limitc                 C   s   d|  |d S )N[%s]rW   )r2   r8   r%   r%   r&   _print_list      zStrPrinter._print_listc                 C   s
   | | S r4   )Z_format_strr8   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    rh   jr8   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                    rm   r4   r[   rn   r]   r%   r&   rp      rq   zAStrPrinter._print_MatrixSlice.<locals>.strslice.<locals>.<lambda>)listr/   rr   )xZdimr]   r%   r&   strslice   s   z/StrPrinter._print_MatrixSlice.<locals>.strslicer   Tr   [rW   ])r'   r   r   ZrowsliceZrowsZcolsliceZcols)r!   r9   r   r%   r]   r&   _print_MatrixSlice   s   	zStrPrinter._print_MatrixSlicec                 C      |j S r4   r   r8   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*t|d rjt|dg}	ng }	|	 fdd|D  }
t|dkrt|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r2t|tr2t|j d dk r2|jtjur||| qt|jd jdkr+t|jt
tfr+|| ||j q|jrY|tjurY|jdkrJ|t |j |j!dkrX|t |j! q|| q|pet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 r4   )Z
is_Integer)r+   Zair%   r%   r&   r   
  s    z2StrPrinter._print_Mul.<locals>.<genexpr>.<genexpr>N)r5   r	   Zis_Powallr0   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   )r5   r   boolexpas_coeff_Mul)r   r%   r%   r&   rp     s    z'StrPrinter._print_Mul.<locals>.<lambda>T)binaryFZevaluatec                       g | ]
}j | d dqS Fr   r)   r   precr!   r%   r&   r-         z)StrPrinter._print_Mul.<locals>.<listcomp>c                    r   r   r)   r   r   r%   r&   r-   %  r   *z%s/(%s)%s/%sr<   r>   )oldZ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   )
Zas_base_expr   r   	make_argsr   NegativeOne
_from_argsr5   r   r   )rh   r   r   Zeargsr%   r%   r&   apowC  s   

z#StrPrinter._print_Mul.<locals>.apowc                    r   r   r)   r+   r   r   r%   r&   r-   d      c                    r   r   r)   r   r   r%   r&   r-   e  r   r   /z/(%s))#r   r0   r   Oneanyr   	enumerater   Z	is_Numberr   r   r   r   baser   r6   rB   r   r/   r   r
   r   Zas_ordered_factorsr   is_commutativer5   r   r   ry   is_RationalZInfinitypr   qindex)r!   r9   r0   rz   nrh   Zdir   ZdargsZpreZnfactorsZdfactorscrG   r   r   Z	pow_parenr   r"   Za_strZ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   r9   r!   r%   r&   r-     r   z,StrPrinter._print_MatMul.<locals>.<listcomp>)Zas_coeff_mmulZ	is_numberZas_real_imagZis_zeroZis_negativer
   r/   r0   )r!   r9   r   r   rG   reZimr%   r   r&   _print_MatMuls  s   zStrPrinter._print_MatMulc                 C   s   d |j| |jS )Nz{}.({}))r   rX   r    r9   r8   r%   r%   r&   _print_ElementwiseApplyFunction  s   
z*StrPrinter._print_ElementwiseApplyFunctionc                 C   rI   )Nnanr%   r8   r%   r%   r&   
_print_NaN  rL   zStrPrinter._print_NaNc                 C   rI   )Nz-oor%   r8   r%   r%   r&   _print_NegativeInfinity  rL   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 r4   )r   ZZero)r+   r   r%   r%   r&   r     r   z*StrPrinter._print_Order.<locals>.<genexpr>r   zO(%s)rW   r   )Z	variablesr   pointr   r    r9   r2   r0   r8   r%   r%   r&   _print_Order  s
   zStrPrinter._print_Orderc                 C      |  S r4   __str__r8   r%   r%   r&   _print_Ordinal     zStrPrinter._print_Ordinalc                 C   r  r4   r  r8   r%   r%   r&   _print_Cycle  r
  zStrPrinter._print_Cyclec                 C   sb  ddl m}m} ddlm} |j}|d ur&|d|d|ddd  n| j	d	d
}|rj|j
s4dS |||j
d  tdd  }|d}|dksbd||d  vrb||d  |d |  }|dd}|S | }|s|j
dk r}d| |j S d| |j
 S | |jd |d d  d| |j
  }| |j }	}
t|t|
k r|}	d|	 S )Nr   )PermutationCycle)SymPyDeprecationWarningzPermutation.print_cyclic = {}zinit_printing(perm_cyclic={})ia;  z1.6)ZfeatureZ
useinsteadZissueZdeprecated_since_versionr   Tz()r   r  (,r>      zPermutation(%s)zPermutation([], size=%s)z	, size=%s)Z sympy.combinatorics.permutationsr  r  Zsympy.utilities.exceptionsr  Zprint_cyclicr   warn	_settingsgetsize__repr__r   rfindreplaceZsupportr    Z
array_form)r!   r9   r  r  r  r   rk   ZlastZtrimZuseZ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))r0   r   r  r    )r!   r   r9   r   newr%   r%   r&   _print_Subs  s   zStrPrinter._print_Subsc                 C   r  r4   r[   r8   r%   r%   r&   _print_TensorIndex  r
  zStrPrinter._print_TensorIndexc                 C   r  r4   r[   r8   r%   r%   r&   _print_TensorHead  r
  zStrPrinter._print_TensorHeadc                 C   r  r4   r[   r8   r%   r%   r&   _print_Tensor  r
  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>)Z!_get_args_for_traditional_printerr/   )r!   r9   rG   r0   r%   r   r&   _print_TensMul  s   zStrPrinter._print_TensMulc                 C   r  r4   r[   r8   r%   r%   r&   _print_TensAdd  r
  zStrPrinter._print_TensAddc                 C      |  |jS r4   r    r   r8   r%   r%   r&   _print_ArraySymbol     zStrPrinter._print_ArraySymbolc                    s$   d|j d fdd|jD f S )Nz%s[%s]rW   c                    rZ   r%   r[   rg   r]   r%   r&   r-     r^   z2StrPrinter._print_ArrayElement.<locals>.<listcomp>)r   r/   indicesr8   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    %sr[   r   r]   r%   r&   r-     s    z6StrPrinter._print_PermutationGroup.<locals>.<listcomp>zPermutationGroup([
%s])z,
)r0   r/   )r!   r9   r   r%   r]   r&   _print_PermutationGroup  s   z"StrPrinter._print_PermutationGroupc                 C   rI   )NZpir%   r8   r%   r%   r&   	_print_Pi  rL   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 orderrW   c                    rm   r4   r[   )Zrsr]   r%   r&   rp     rq   z,StrPrinter._print_PolyRing.<locals>.<lambda>r/   rr   r   r    domainr   )r!   ringr%   r]   r&   _print_PolyRing  
   zStrPrinter._print_PolyRingc                    r*  )Nz3Rational function field in %s over %s with %s orderrW   c                    rm   r4   r[   )Zfsr]   r%   r&   rp     rq   z-StrPrinter._print_FracField.<locals>.<lambda>r+  r!   fieldr%   r]   r&   _print_FracField  r/  zStrPrinter._print_FracFieldc                 C   r  r4   r  )r!   Zelmr%   r%   r&   _print_FreeGroupElement  r
  z"StrPrinter._print_FreeGroupElementc                 C   s   d|j |jf S )Nz(%s + %s*I))r   yr!   Zpolyr%   r%   r&   _print_GaussianElement  r   z!StrPrinter._print_GaussianElementc                 C   s   | | tddS )N%s**%sr   )r6   r   r5  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!   Zfracr:  r9  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+   rk   Z	ATOM_PRECr!   r%   r&   r-   
  r.   z*StrPrinter._print_Poly.<locals>.<listcomp>r   z**%dr   r  )r=   r<   )r<   r=   z(%s, %s)PolynomialErrorz, modulus=%sz, domain='%s'r   r?   rW   )r   gensrC   r   ry   r/   Zis_Addr    r   r   rA   r   r@   rB   r_   r`   Zsympy.polys.polyerrorsr>  Zget_modulusZ
get_domainr   )r!   r9   rC   r?  ZmonomZcoeffZs_monomrh   r   Zs_coeffZs_termmodifierr   r>  r   r"   r%   r<  r&   _print_Poly  s\   





:zStrPrinter._print_Polyc                 C   rI   )Nrj   r%   )r!   r   r%   r%   r&   _print_UniversalSetJ  rL   zStrPrinter._print_UniversalSetc                 C   s&   |j r| |  S | | S r4   )Z
is_aliasedr    Zas_polyZas_exprr8   r%   r%   r&   _print_AlgebraicNumberM  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 )aX  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.functions import sqrt
        >>> from sympy.printing.str import 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                    rm   r4   r[   rn   r]   r%   r&   rp     rq   z'StrPrinter._print_Pow.<locals>.<lambda>r   Fr   Z
_sympyreprr   z	(Rationalr7  r  )r   r   r   ZHalfr    r   r   ri   rr   r   r'   printmethodr   r   r@   )r!   r9   ZrationalrD   r   r%   r]   r&   
_print_PowS  s   '"
"zStrPrinter._print_Powc                 C   s   |  |jd S r   r    r0   r8   r%   r%   r&   _print_UnevaluatedExpr  r   z!StrPrinter._print_UnevaluatedExprc                 C   s0   t |}d| j|j|dd| j|j|ddf S )Nr7  Fr   )r   r'   r   r   )r!   r9   rD   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  r6   r   r8   r%   r%   r&   _print_Integer  s   
zStrPrinter._print_Integerc                 C   rI   )NZIntegersr%   r8   r%   r%   r&   _print_Integers  rL   zStrPrinter._print_Integersc                 C   rI   )NZNaturalsr%   r8   r%   r%   r&   _print_Naturals  rL   zStrPrinter._print_Naturalsc                 C   rI   )NZ	Naturals0r%   r8   r%   r%   r&   _print_Naturals0  rL   zStrPrinter._print_Naturals0c                 C   rI   )NZ	Rationalsr%   r8   r%   r%   r&   _print_Rationals  rL   zStrPrinter._print_Rationalsc                 C   rI   )NZRealsr%   r8   r%   r%   r&   _print_Reals  rL   zStrPrinter._print_Realsc                 C   rI   )NZ	Complexesr%   r8   r%   r%   r&   _print_Complexes  rL   zStrPrinter._print_Complexesc                 C   rI   )NZEmptySetr%   r8   r%   r%   r&   _print_EmptySet  rL   zStrPrinter._print_EmptySetc                 C   rI   )NZEmptySequencer%   r8   r%   r%   r&   _print_EmptySequence  rL   zStrPrinter._print_EmptySequencec                 C      t |S r4   r6   r8   r%   r%   r&   
_print_int  r
  zStrPrinter._print_intc                 C   rR  r4   rS  r8   r%   r%   r&   
_print_mpz  r
  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   r6   r   r  r  r8   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   r6   r   r8   r%   r%   r&   _print_PythonRational     

z StrPrinter._print_PythonRationalc                 C   rW  Nr   r   denominatorr6   	numeratorr8   r%   r%   r&   _print_Fraction  rY  zStrPrinter._print_Fractionc                 C   rW  rZ  r[  r8   r%   r%   r&   
_print_mpq  rY  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   )Zstrip_zerosZ	min_fixedZ	max_fixedz-.0z-0.   z.0z0.r   r=   )Z_precr   r  Z_print_levelmlib_to_strZ_mpf_r@   )r!   r9   r   ZdpsstripZlowZhighZ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 )NZEqZNeZ
AssignmentZAddAugmentedAssignmentZSubAugmentedAssignmentZMulAugmentedAssignmentZDivAugmentedAssignmentZModAugmentedAssignment)z==z!=z:=z+=z-=z*=z/=z%=
%s(%s, %s)z%s %s %s)Zrel_opr    lhsrhsr'   r   _relationalsr  )r!   r9   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;   )rH   r9   r   r8   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 )Nrj  r;   zRootSum(%s)rW   )rH   r9   Zfunr   ZIdentityFunctionry   r    r/   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;   )rH   r   r   basisr!   r%   r&   r-     r   z3StrPrinter._print_GroebnerBasis.<locals>.<listcomp>r   rW   c                    rZ   r%   r[   )r+   genr]   r%   r&   r-     r^   zdomain='%s'z
order='%s'rV   )r_   r`   exprsr/   r?  r    r,  r   )r!   rn  clsrp  r?  r,  r   r0   r%   rm  r&   _print_GroebnerBasis  s   zStrPrinter._print_GroebnerBasisc                    s4   t |td}d fdd|D }|sdS d| S )Nrt   rW   c                 3   r   r4   r[   r*   r]   r%   r&   r      r   z(StrPrinter._print_set.<locals>.<genexpr>zset()rv   )rw   r   r/   r!   rk   r{   r0   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 )Nrt   rW   c                 3   r   r4   r[   r*   r]   r%   r&   r   (  r   z.StrPrinter._print_FiniteSet.<locals>.<genexpr>c                 s   s    | ]}| tV  qd S r4   )Zhasr   r*   r%   r%   r&   r   )  r   zFiniteSet({})z{{{}}})rw   r   r/   r   r   rs  r%   r]   r&   _print_FiniteSet%  s
   

zStrPrinter._print_FiniteSetc                    s.   t |td}d fdd|D }d|S )Nrt   rW   c                 3   r   r4   r[   r   r]   r%   r&   r   0  r   z.StrPrinter._print_Partition.<locals>.<genexpr>zPartition({}))rw   r   r/   r   rs  r%   r]   r&   _print_Partition-  s   
zStrPrinter._print_Partitionc                 C   s   |sdS d|  | S )Nzfrozenset()zfrozenset(%s))rt  r!   rk   r%   r%   r&   _print_frozenset3  s   zStrPrinter._print_frozensetc                    r   )Nc                    r   r   r   r   r]   r%   r&   r   9  r   z)StrPrinter._print_Sum.<locals>._xab_tostrrW   c                    r   r%   r%   r   r   r%   r&   r-   >  r   z)StrPrinter._print_Sum.<locals>.<listcomp>zSum(%s, %s)r   r   r%   r   r&   
_print_Sum8  r   zStrPrinter._print_Sumc                 C   r   r4   r   r8   r%   r%   r&   _print_SymbolA  r   zStrPrinter._print_Symbolc                 C   rI   r   r%   r8   r%   r%   r&   _print_IdentityF  rL   zStrPrinter._print_Identityc                 C   rI   )N0r%   r8   r%   r%   r&   _print_ZeroMatrixI  rL   zStrPrinter._print_ZeroMatrixc                 C   rI   )N1r%   r8   r%   r%   r&   _print_OneMatrixL  rL   zStrPrinter._print_OneMatrixc                 C   
   d|j  S )NzQ.%sr   r8   r%   r%   r&   _print_PredicateO  r   zStrPrinter._print_Predicatec                 C   rR  r4   rS  r8   r%   r%   r&   
_print_strR  r
  zStrPrinter._print_strc                 C   s.   t |dkrd| |d  S d| |d S )Nr   z(%s,)r   r   rW   )r   r    r2   r8   r%   r%   r&   _print_tupleU  s   zStrPrinter._print_tuplec                 C   r}   r4   )r  r8   r%   r%   r&   _print_Tuple[  r   zStrPrinter._print_Tuplec                 C   r   )Nz%s.Tr   r   )r!   Tr%   r%   r&   _print_Transpose^  r   zStrPrinter._print_Transposec                 C   r   )NzUniform(%s, %s))r    r   r   r8   r%   r%   r&   _print_Uniforma  r   zStrPrinter._print_Uniformc                 C   s"   | j ddrd|j S d|j S )Nr   Fz%s)r  r  r   r   r8   r%   r%   r&   _print_Quantityd  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   rg   r]   r%   r&   r-   j  s     z0StrPrinter._print_Quaternion.<locals>.<listcomp>r   c                 S   s   g | ]
\}}|d  | qS )r   r%   )r+   rh   r   r%   r%   r&   r-   k  r   r   Zijkz + )r0   zipr/   )r!   r9   rk   r   r%   r]   r&   _print_Quaternioni  s   &
zStrPrinter._print_Quaternionc                 C   rR  r4   rS  r8   r%   r%   r&   _print_Dimensionn  r
  zStrPrinter._print_Dimensionc                 C   
   |j d S r   r   r8   r%   r%   r&   _print_Wildq  r   zStrPrinter._print_Wildc                 C   r  r   r   r8   r%   r%   r&   _print_WildFunctiont  r   zStrPrinter._print_WildFunctionc                 C   r   r4   r   r8   r%   r%   r&   _print_WildDotw  r   zStrPrinter._print_WildDotc                 C   r   r4   r   r8   r%   r%   r&   _print_WildPlusz  r   zStrPrinter._print_WildPlusc                 C   r   r4   r   r8   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|  )r  r  r8   r%   r%   r&   _print_Zero  s   zStrPrinter._print_Zeroc                 C   s~   ddl m} z|jd ur| |j|W S W n	 |y    Y nw |jj}| |j}| |j}| |j}d||||f S )Nr   )SympifyErrorz%s(%s, %s, %s))	Zsympy.core.sympifyr  r-  r    Zto_sympyr_   r`   repdom)r!   r   r  rq  r  r  r-  r%   r%   r&   
_print_DMP  s   
zStrPrinter._print_DMPc                 C   r}   r4   )r  r8   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!   Zmorphismr%   r%   r&   _print_IdentityMorphism  r   z"StrPrinter._print_IdentityMorphismc                 C   s   d|j |j|jf S )NzNamedMorphism(%s, %s, "%s"))r,  Z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 r4   r   )r!   Zmanifoldr%   r%   r&   _print_Manifold  r
  zStrPrinter._print_Manifoldc                 C   r  r4   r   )r!   Zpatchr%   r%   r&   _print_Patch  r
  zStrPrinter._print_Patchc                 C   r  r4   r   )r!   Zcoordsr%   r%   r&   _print_CoordSystem  r
  zStrPrinter._print_CoordSystemc                 C   s   |j j|j jS r4   
_coord_sysr   _indexr   r0  r%   r%   r&   _print_BaseScalarField  r   z!StrPrinter._print_BaseScalarFieldc                 C   s   d|j j|j j S )Nze_%sr  r0  r%   r%   r&   _print_BaseVectorField  rS   z!StrPrinter._print_BaseVectorFieldc                 C   s2   |j }t|drd|jj|j j S d| | S )Nr  zd%szd(%s))Z_form_fieldr   r  r   r  r   r    )r!   Zdiffr1  r%   r%   r&   _print_Differential  s   
zStrPrinter._print_Differentialc                 C   s   dd|  |jd f S )NrV   ZTrr   rF  r8   r%   r%   r&   	_print_Tr     zStrPrinter._print_Trc                 C   r"  r4   r#  rw  r%   r%   r&   
_print_Str  r%  zStrPrinter._print_Strc                 C   s*   |j }d| || |j| |jf S )Nrd  )rX   r    re  rf  )r!   r9   Zrelr%   r%   r&   _print_AppliedBinaryRelation  s
   


z'StrPrinter._print_AppliedBinaryRelation)F)r   r4   )r`   
__module____qualname__rD  Z_default_settingsdictrg  r'   r2   r:   rH   rK   rN   rO   rR   rT   rU   rY   ra   rd   re   rf   rl   rs   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.  r2  r3  r6  r8  r;  rA  rB  rC  rE  rG  rH  rI  rJ  rK  rL  rM  rN  rO  rP  rQ  rT  rU  rV  rX  r^  r_  rc  ri  rk  rl  rr  rt  ru  rv  rx  ry  rz  Z_print_MatrixSymbolZ_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  r  r  r  r  r  r  r  r%   r%   r%   r&   r      s"   

		t	&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r9   Zsettingsr   rk   r%   r%   r&   sstr  s   
r  c                   @   s    e Zd ZdZdd Zdd ZdS )StrReprPrinterz(internal) -- see sstrreprc                 C   rR  r4   )r7   rw  r%   r%   r&   r    r
  zStrReprPrinter._print_strc                 C   s   d|j j| |jf S )NrV   )r_   r`   r    r   rw  r%   r%   r&   r    r  zStrReprPrinter._print_StrN)r`   r  r  __doc__r  r  r%   r%   r%   r&   r    s    r  c                 K   r  )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
    )r  r  r  r%   r%   r&   sstrrepr  s   

r  N)"r  typingr   r   Z
sympy.corer   r   r   r   r   r	   Zsympy.core.mulr
   Zsympy.core.functionr   Zsympy.sets.setsr   Zprinterr   r   Zsympy.printing.precedencer   r   Zmpmath.libmpr   r   ra  Zsympy.utilitiesr   r   r   r  r  r  r%   r%   r%   r&   <module>   s.            =
