a
    <b                 %   @   sZ  d Z ddlmZmZ ddl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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( ddl)m*Z*m+Z, ddl-m.Z. ddl/Z/g dZ0ddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-dd.d*d/d0d1d2d3d4d5d6$Z1h d7Z2d8d9 d:d9 d;d9 d<d9 d=d9 d>d9 d?d9 d@d9 dAd9 dBd9 dCd9 dDd9 dEd9 dFd9 dGd9 dHd9 dId9 dJd9 dKd9 dLd9 dMd9 dNd9 dOd9 dPd9 dQZ3e4eZ5e/6dRe/6dSfZ7dTdU Z8G dVdW dWe"Z9dXdY Z:e#e9dZd[ Z;d\d] Z<dcdadbZ=dS )dzC
A Printer which converts an expression into its LaTeX equivalent.
    )AnyDictN)AddFloatModMulNumberSSymbol)greeks)Tuple)AppliedUndef
Derivative)AssocOp)Pow)default_sort_key)SympifyError)true)precedence_traditional)Printerprint_function)split_super_subrequires_partial)
precedence
PRECEDENCE)prec_to_dpsto_str)has_variety)ZarcsinZarccosZarctansincostansinhcoshtanhsqrtlnlogsecZcscZcotZcothreZimfracrootargAB\Gammaz\DeltaEZHz\ThetaIKz\LambdaMNz\XioOz\PiPz\SigmaTz\Upsilonz\PhiXz\Psiz\Omegaz\lambdaz\chiz\varepsilonz	\varkappaz\varphiz\varpiz\varrhoz	\varsigmaz	\vartheta)$AlphaBetaGammaDeltaEpsilonZetaEtaThetaIotaKappaLambdaMuNuXiomicronOmicronPiRhoSigmaTauUpsilonPhiChiPsiOmegalamdaZLamdaZkhiZKhiZ
varepsilonZvarkappaZvarphiZvarpiZvarrhoZvarsigmaZvartheta>
   ZwpZgimelethZdalethZhbarZellZbethZhslashZalephZmhoc                 C   s   d|  d S )Nz
\mathring{} srW   rW   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/printing/latex.py<lambda>V       r[   c                 C   s   d|  d S )Nz\ddddot{rV   rW   rX   rW   rW   rZ   r[   W   r\   c                 C   s   d|  d S )Nz\dddot{rV   rW   rX   rW   rW   rZ   r[   X   r\   c                 C   s   d|  d S )Nz\ddot{rV   rW   rX   rW   rW   rZ   r[   Y   r\   c                 C   s   d|  d S )Nz\dot{rV   rW   rX   rW   rW   rZ   r[   Z   r\   c                 C   s   d|  d S )Nz\check{rV   rW   rX   rW   rW   rZ   r[   [   r\   c                 C   s   d|  d S )Nz\breve{rV   rW   rX   rW   rW   rZ   r[   \   r\   c                 C   s   d|  d S )Nz\acute{rV   rW   rX   rW   rW   rZ   r[   ]   r\   c                 C   s   d|  d S )Nz\grave{rV   rW   rX   rW   rW   rZ   r[   ^   r\   c                 C   s   d|  d S )Nz\tilde{rV   rW   rX   rW   rW   rZ   r[   _   r\   c                 C   s   d|  d S )Nz\hat{rV   rW   rX   rW   rW   rZ   r[   `   r\   c                 C   s   d|  d S )Nz\bar{rV   rW   rX   rW   rW   rZ   r[   a   r\   c                 C   s   d|  d S )Nz\vec{rV   rW   rX   rW   rW   rZ   r[   b   r\   c                 C   s   d|  d S N{z}'rW   rX   rW   rW   rZ   r[   c   r\   c                 C   s   d|  d S r]   rW   rX   rW   rW   rZ   r[   d   r\   c                 C   s   d|  d S Nz\boldsymbol{rV   rW   rX   rW   rW   rZ   r[   f   r\   c                 C   s   d|  d S r_   rW   rX   rW   rW   rZ   r[   g   r\   c                 C   s   d|  d S )Nz	\mathcal{rV   rW   rX   rW   rW   rZ   r[   h   r\   c                 C   s   d|  d S )Nz	\mathscr{rV   rW   rX   rW   rW   rZ   r[   i   r\   c                 C   s   d|  d S )Nz
\mathfrak{rV   rW   rX   rW   rW   rZ   r[   j   r\   c                 C   s   d|  d S )Nz\left\|{z	}\right\|rW   rX   rW   rW   rZ   r[   l   r\   c                 C   s   d|  d S )Nz\left\langle{z}\right\ranglerW   rX   rW   rW   rZ   r[   m   r\   c                 C   s   d|  d S Nz\left|{z}\right|rW   rX   rW   rW   rZ   r[   n   r\   c                 C   s   d|  d S r`   rW   rX   rW   rW   rZ   r[   o   r\   )ZmathringZddddotZdddotZddotdotcheckZbreveacuteZgravetildeZhatbarvecprimeZprmboldbmcalZscrZfrakZnormavgabsZmagz[0-9][} ]*$z[0-9]c                 C   sB   |  dd} dD ]}|  |d| } q|  dd} |  dd} | S )z
    Escape a string such that latex interprets it as plaintext.

    We cannot use verbatim easily with mathjax, so escaping is easier.
    Rules from https://tex.stackexchange.com/a/34586/41112.
    \z\textbackslashz&%$#_{}~z\textasciitilde^z\textasciicircum)replace)rY   crW   rW   rZ   latex_escapez   s    rr   c                       sr  e Zd ZdZdddddddddddddi ddddd	ddddd
ZdYddZdd Zdd ZdZddZdd Z	dd Z
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eZeZd)d* Zd\d+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Z d?d@ Z!dAdB Z"dCdD Z#dEdF Z$dGdH Z%dIdJ Z&dKdL Z'dMdN Z(dOdP Z)dQdR Z*dSdT Z+dUdV Z,dWdX Z-dYdZ Z.d[d\ Z/d]d^ Z0d_d` Z1d]dadbZ2dcdd Z3dedf Z4e5dgdh Z6didj Z7dkdl Z8dmdn Z9d^dodpZ:e: Z;Z<d_dqdrZ=d`dsdtZ>dadudvZ?dbdwdxZ@e@ZAdcdydzZBddd{d|ZCd}d~ ZDdd ZEdd ZFdd ZGdd ZHdeddZIdfddZJdgddZKdhddZLdiddZMdjddZNdkddZOdlddZPdmddZQdnddZRdoddZSdpddZTdqddZUdrddZVdsddZWdtddZXeXZYduddZZdvddZ[dwddZ\dxddZ]dyddZ^dzddZ_d{ddZ`d|ddZad}ddZbd~ddZcdd Zddd ZedddZfdddÄZgdddńZhdddǄZidddɄZjddd˄Zkddd̈́ZldddτZmdddфZndddӄZodddքZpddd؄ZqdddڄZrddd܄ZsdddބZtdddZudddZvdddZwdddZxdddZydddZzdddZ{dddZ|dddZ}dddZ~dddZdddZdddZdddZdddZdddZddd ZdddZdddZdddZdddZdd	d
ZdddZdddZdd Zdd ZdddZeZ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+d,Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ Zi fdAdBZdCdD ZdEdF ZdGdH ZdIdJ ZdKdL ZdMdN ZdOdP ZdQdR ZdSdT ZddUdVZdWdX ZdYdZ Zd[d\ Zd]d^ Zd_d` Zdadb Zdcdd ZddedfZddgdhZddidjZddkdlZddmdnZdodp Zdqdr Zdsdt ZeZdudv ZddwdxZÐddydzZĐdd{d|ZŐdd}d~ZƐdddZǐdddZȐdd ZeZeZeZ̐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ݐ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dɐdʄ Zdːd̄ Zd͐d΄ ZddϐdЄZd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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e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!d-d. Z"d/d0 Z#d1d2 Z$d3d4 Z%d5d6 Z&d7d8 Z'd9d: Z(d;d< Z)dd=d>Z*dd?d@Z+ddAdBZ,ddCdDZ-ddEdFZ.ddGdHZ/dIdJ Z0dKdL Z1dMdN Z2dOdP Z3dQdR Z4dSdT Z5dUdV Z6 fdWdXZ7  Z8S (  LatexPrinterZ_latexFNabbreviated[plainTiperiod)	full_precfold_frac_powersfold_func_bracketsfold_short_fracinv_trig_styleitexln_notationlong_frac_ratio	mat_delimmat_strmode
mul_symbolordersymbol_namesroot_notationmat_symbol_styleimaginary_unitgothic_re_imdecimal_separatorperm_cyclicparenthesize_superminmaxc                 C   sj  t | | d| jv r4g d}| jd |vr4td| jd d u rZ| jd dkrZd| jd< ddd	d
d}z|| jd  | jd< W n" ty   | jd | jd< Y n0 z|| jd pd | jd< W nF ty   | jd  dv r|d | jd< n| jd | jd< Y n0 ddd| _dddddddd}z|| jd  | jd< W n$ tyd   | jd | jd< Y n0 d S )Nr   )inlinerv   Zequationz	equation*zB'mode' must be one of 'inline', 'plain', 'equation' or 'equation*'r|   r   T z \,.\, z \cdot  \times )NZldotra   timesr   mul_symbol_latexra   mul_symbol_latex_numbers) r   rm   z\,z\:\;z\quad)])(ru   rw   z
\mathrm{i}z\text{i}jz
\mathrm{j}z\text{j})Nrw   riZtir   ZrjZtjr   Zimaginary_unit_latex)r   __init__	_settings
ValueErrorKeyErrorstrip_delim_dict)selfsettingsZvalid_modesZmul_symbol_tableZimaginary_unit_tablerW   rW   rZ   r      s\    


	zLatexPrinter.__init__c                 C   s
   d |S )Nz\left({}\right)formatr   rY   rW   rW   rZ   _add_parens   s    zLatexPrinter._add_parensc                 C   s
   d |S )Nz\left( {}\right)r   r   rW   rW   rZ   _add_parens_lspace   s    zLatexPrinter._add_parens_lspacec                 C   sR   t |}|r |r | | |S ||k s4|sD||krD| | |S | |S d S N)r   r   _print)r   itemlevelis_negstrictZprec_valrW   rW   rZ   parenthesize   s    zLatexPrinter.parenthesizec                 C   s*   d|v r&| j d r| |S d|S |S )z
        Protect superscripts in s

        If the parenthesize_super option is set, protect with parentheses, else
        wrap in braces.
        ro   r   z{{{}}})r   r   r   r   rW   rW   rZ   r      s
    


zLatexPrinter.parenthesize_superc                 C   sb   t | |}| jd dkr|S | jd dkr4d| S | jd rFd| S | jd }d|||f S d S )Nr   rv   r   z$%s$r~   z$$%s$$z\begin{%s}%s\end{%s})r   doprintr   )r   exprtexZenv_strrW   rW   rZ   r      s    

zLatexPrinter.doprintc                 C   s(   |j r|jp$|jo$|tjuo$|jdu  S )z
        Returns True if the expression needs to be wrapped in brackets when
        printed, False otherwise. For example: a + b => True; a => False;
        10 => False; -10 => True.
        F)Z
is_IntegerZis_nonnegativeis_Atomr	   NegativeOneis_Rationalr   r   rW   rW   rZ   _needs_brackets
  s    zLatexPrinter._needs_bracketsc                 C   sN   |  |sdS |jr"| |s"dS |jr6| |s6dS |jsB|jrFdS dS dS )a  
        Returns True if the expression needs to be wrapped in brackets when
        passed as an argument to a function, False otherwise. This is a more
        liberal version of _needs_brackets, in that many expressions which need
        to be wrapped in brackets when added/subtracted/raised to a power do
        not need them when passed to a function. Such an example is a*b.
        FTN)r   is_Mul_mul_is_cleanZis_Pow_pow_is_cleanis_Addis_Functionr   rW   rW   rZ   _needs_function_brackets  s    
z%LatexPrinter._needs_function_bracketsc                    s   ddl m} ddlm} ddlm}  jr<|sZ  rZdS nt t	d k rPdS  j
rZdS  jrddS t fddtfD rdS |st fd	d|||fD rdS d
S )a  
        Returns True if the expression needs to be wrapped in brackets when
        printed as part of a Mul, False otherwise. This is True for Add,
        but also for some container objects that would not need brackets
        when appearing last in a Mul, e.g. an Integral. ``last=True``
        specifies that this expr is the last to appear in a Mul.
        ``first=True`` specifies that this expr is the first to appear in
        a Mul.
        r   )Product)Sum)IntegralTr   c                 3   s   | ]}  |V  qd S r   Zhas.0xr   rW   rZ   	<genexpr>B  r\   z3LatexPrinter._needs_mul_brackets.<locals>.<genexpr>c                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r   E  r\   F)Zsympy.concrete.productsr   Zsympy.concrete.summationsr   Zsympy.integrals.integralsr   r   could_extract_minus_signr   r   is_RelationalZis_Piecewiseanyr   )r   r   firstlastr   r   r   rW   r   rZ   _needs_mul_brackets+  s&    
z LatexPrinter._needs_mul_bracketsc                    s4    j r
dS t fddtfD r&dS  jr0dS dS )z
        Returns True if the expression needs to be wrapped in brackets when
        printed as part of an Add, False otherwise.  This is False for most
        things.
        Tc                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r   R  r\   z3LatexPrinter._needs_add_brackets.<locals>.<genexpr>F)r   r   r   r   r   rW   r   rZ   _needs_add_bracketsJ  s    z LatexPrinter._needs_add_bracketsc                 C   s   |j D ]}|jr dS qdS )NFT)argsr   )r   r   r+   rW   rW   rZ   r   X  s    
zLatexPrinter._mul_is_cleanc                 C   s   |  |j S r   )r   baser   rW   rW   rZ   r   ^  s    zLatexPrinter._pow_is_cleanc                 C   s   |d urd||f S |S d S )N\left(%s\right)^{%s}rW   r   r   exprW   rW   rZ   _do_exponenta  s    zLatexPrinter._do_exponentc                    sL     |jj}|jr> fdd|jD }d}||d|S d|S d S )Nc                    s   g | ]}  |qS rW   r   )r   r6   r   rW   rZ   
<listcomp>j  r\   z-LatexPrinter._print_Basic.<locals>.<listcomp>z"\operatorname{{{}}}\left({}\right), z\text{{{}}})_deal_with_super_sub	__class____name__r   r   join)r   r   nameZlsrY   rW   r   rZ   _print_Basicg  s    zLatexPrinter._print_Basicc                 C   s   d| S N	\text{%s}rW   r   erW   rW   rZ   _print_boolp  s    zLatexPrinter._print_boolc                 C   s   d| S r   rW   r   rW   rW   rZ   _print_NoneTypev  s    zLatexPrinter._print_NoneTypec                 C   sv   | j ||d}d}t|D ]V\}}|dkr,n | rD|d7 }| }n|d7 }| |}| |rhd| }||7 }q|S )N)r   r   r    -  + \left(%s\right))Z_as_ordered_terms	enumerater   r   r   )r   r   r   termsr   rw   termterm_texrW   rW   rZ   
_print_Addy  s    


zLatexPrinter._print_Addc                 C   s   ddl m} |jdkrdS ||}|j}|j}|jd |d krP||d gg }d}|D ]}|t|dd7 }qX|d	d
}|dd}|S )Nr   Permutation\left( \right)   r   ,r   ru   z\left( r   \right)) sympy.combinatorics.permutationsr   sizeZcyclic_form
array_formstrrp   )r   r   r   Z	expr_permZsizr   rw   rW   rW   rZ   _print_Cycle  s    
zLatexPrinter._print_Cyclec           
         s   ddl m} ddlm} |j}|d ur@|d| ddddd	 n jd
d}|r\ |S |jdkrjdS  fdd|j	D } fddt
t|D }d|}d|}d||f}	d|	 S )Nr   r   )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   Tr   c                    s   g | ]}  |qS rW   r   r   r+   r   rW   rZ   r     r\   z3LatexPrinter._print_Permutation.<locals>.<listcomp>c                    s   g | ]}  |qS rW   r   r   r   rW   rZ   r     r\    & z \\ z \begin{pmatrix} %s \end{pmatrix})r   r   Zsympy.utilities.exceptionsr   Zprint_cyclicr   getr   r   r   rangelenr   )
r   r   r   r   r   lowerupperZrow1Zrow2matrW   r   rZ   _print_Permutation  s.    




zLatexPrinter._print_Permutationc                 C   s"   |j \}}d| || |f S )Nz\sigma_{%s}(%s))r   r   )r   r   permvarrW   rW   rZ   _print_AppliedPermutation  s    
z&LatexPrinter._print_AppliedPermutationc           
      C   s   t |j}| jd rdnd}d| jv r0| jd nd }d| jv rH| jd nd }t|j||||d}| jd }d|v r|d\}}	|	d	 d
kr|	dd  }	| jd dkr|dd}d|||	f S |dkrdS |dkrdS | jd dkr|dd}|S d S )Nry   FTr   r   )Zstrip_zerosZ	min_fixedZ	max_fixedr   r   r   +r   r   comma.z{,}z%s%s10^{%s}z+infz\inftyz-infz- \infty)r   Z_precr   mlib_to_strZ_mpf_splitrp   )
r   r   Zdpsr   lowhighZstr_real	separatorZmantr   rW   rW   rZ   _print_Float  s(    

zLatexPrinter._print_Floatc                 C   s0   |j }|j}d| |td | |td f S )Nz%s \times %sr   Z_expr1Z_expr2r   r   r   r   Zvec1Zvec2rW   rW   rZ   _print_Cross  s
    zLatexPrinter._print_Crossc                 C   s   |j }d| |td  S )Nz\nabla\times %sr   Z_exprr   r   r   r   rf   rW   rW   rZ   _print_Curl  s    zLatexPrinter._print_Curlc                 C   s   |j }d| |td  S )Nz\nabla\cdot %sr   r  r  rW   rW   rZ   _print_Divergence  s    zLatexPrinter._print_Divergencec                 C   s0   |j }|j}d| |td | |td f S )Nz%s \cdot %sr   r  r  rW   rW   rZ   
_print_Dot  s
    zLatexPrinter._print_Dotc                 C   s   |j }d| |td  S )Nz	\nabla %sr   r  r   r   funcrW   rW   rZ   _print_Gradient  s    zLatexPrinter._print_Gradientc                 C   s   |j }d| |td  S )Nz\triangle %sr   r  r  rW   rW   rZ   _print_Laplacian  s    zLatexPrinter._print_Laplacianc                    s  ddl m  ddlm} jd jd  fdd}fdd	t|tr|j}|d tj	u st
d
d |dd  D r|S d}| r| }d}|jr|d7 }d}nd}||dd\}}|tj	u rtdddd|jvr|||7 }n||}	||}
t|
 }jd }jd rt|dkrtd|
vrtj|ddr`|d|	|
f 7 }n|d|	|
f 7 }n6|d urt|	 || krj|ddr|d|
|	f 7 }n|jrtj	}tj	}|jD ]f}j|dds$t|||  || ks$|j|j  u r du r.n n
||9 }n||9 }q҈j|ddrh|d|||
||f 7 }n|d|||
||f 7 }n|d|
|	f 7 }n|d |	|
f 7 }|r|d!7 }|S )"Nr   Quantity)fractionr   r   c                    sR   | j st| S jdvr(|  }n
t| j}t| fddd}|S d S )N)oldnonec                    s    t |  pt | tot | j S r   )
isinstancer   r   r   r  rW   rZ   r[     s   

z:LatexPrinter._print_Mul.<locals>.convert.<locals>.<lambda>key)r   r   r   r   as_ordered_factorslistr   sorted)r   r   )r  convert_argsr   rW   rZ   convert  s    


z(LatexPrinter._print_Mul.<locals>.convertc                    s   d }}t | D ]|\}}|}j||dk|t| d kdrJd| }td |rttd t|rt| 7 }n|r|7 }||7 }|}q|S )Nr   r   r   )r   r   r   )r   r   r   r   _between_two_numbers_psearchmatchr   )r   Z_texZlast_term_texrw   r   r   )	numbersepr   r  rW   rZ   r)    s     

z-LatexPrinter._print_Mul.<locals>.convert_argsc                 s   s   | ]}t |tV  qd S r   )r"  r   r   rW   rW   rZ   r   -  r\   z*LatexPrinter._print_Mul.<locals>.<genexpr>r   F- r   Tr   )exactr   )evaluater   r|      ro   )r   z\left(%s\right) / %sz%s / %sz\frac{1}{%s}%s\left(%s\right)z\frac{%s}{%s}%s\left(%s\right)z\frac{%s}{%s}%s%sz\frac{1}{%s}%s%s\frac{%s}{%s}r   )Zsympy.physics.unitsr  Zsympy.simplifyr  r   r"  r   r   r	   Oner   r   r   r   r   r  r   r   is_commutative)r   r   r  r*  r   Zinclude_parensr   numerdenomZsnumerZsdenomZldenomZratioabr   rW   )r  r)  r.  r   r  rZ   
_print_Mul  s~    


(




zLatexPrinter._print_Mulc                 C   s*   |j r| |  S | | S d S r   )Z
is_aliasedr   Zas_polyZas_exprr   rW   rW   rZ   _print_AlgebraicNumberm  s    z#LatexPrinter._print_AlgebraicNumberc                 C   s  |j jrt|j jdkr|j jdkr| jd r| |j}|j j}|dkrTd| }n$| jd rld||f }nd||f }|j jrd| S |S nd| jd	 r|j jr|j jdkr| 	|jt
d
 }|j j|j j }}|jjr| |}|jjr| j|jd||f dS d|||f S |j jr|j jr|jjr|jdkrTd|j|j f S |jjr|jj|jj t|jjkr|j dkrd|jj|jjf S d|jj|jjt|j f S | |S |jjr| j|j| |j dS d}| ||S d S )Nr   r   r2  z	\sqrt{%s}r~   z\root{%d}{%s}z\sqrt[%d]{%s}z\frac{1}{%s}rz   r   z%s/%sr   z
%s^{%s/%s}%s^{%s}r   z\frac{1}{\frac{%s}{%s}}z\frac{1}{(\frac{%s}{%s})^{%s}})r   r   rl   pqr   r   r   Zis_negativer   r   	is_Symbolr   r   r5  r:  _helper_print_standard_power)r   r   r   Zexpqr   r>  r?  rW   rW   rZ   
_print_Pows  sR    $







zLatexPrinter._print_Powc                 C   sv   |  |j}| |jtd }|jjr2| |}n8t|jtrj|	drjt
d|rj|drj|dd }|||f S )Nr   \left(z\\left\(\\d?d?dotr      i)r   r   r   r   r   r@  r   r"  r   
startswithr(   r-  endswith)r   r   templater   r   rW   rW   rZ   rA    s    
z)LatexPrinter._helper_print_standard_powerc                 C   s   |  |jd S Nr   r   r   r   rW   rW   rZ   _print_UnevaluatedExpr  s    z#LatexPrinter._print_UnevaluatedExprc                    s   t |jdkr0dtfdd|jd D  }n,fdd dtd	 fd
d|jD  }t|jtr~|d|j 7 }n||j7 }|S )Nr   z\sum_{%s=%s}^{%s} c                    s   g | ]}  |qS rW   r   r   rw   r   rW   rZ   r     r\   z+LatexPrinter._print_Sum.<locals>.<listcomp>r   c                    s,   dt  fdd| d | d | d fD  S )N%s \leq %s \leq %sc                    s   g | ]}  |qS rW   r   r   rY   r   rW   rZ   r     r\   zALatexPrinter._print_Sum.<locals>._format_ineq.<locals>.<listcomp>r   r   r2  tuplelr   rW   rZ   _format_ineq  s    &z-LatexPrinter._print_Sum.<locals>._format_ineqz\sum_{\substack{%s}} \\c                    s   g | ]} |qS rW   rW   r   rQ  rR  rW   rZ   r     r\   r   	r   limitsrO  r   r   r"  functionr   r   r   r   r   rW   rR  r   rZ   
_print_Sum  s    zLatexPrinter._print_Sumc                    s   t |jdkr0dtfdd|jd D  }n,fdd dtd	 fd
d|jD  }t|jtr~|d|j 7 }n||j7 }|S )Nr   z\prod_{%s=%s}^{%s} c                    s   g | ]}  |qS rW   r   rK  r   rW   rZ   r     r\   z/LatexPrinter._print_Product.<locals>.<listcomp>r   c                    s,   dt  fdd| d | d | d fD  S )NrL  c                    s   g | ]}  |qS rW   r   rM  r   rW   rZ   r     r\   zELatexPrinter._print_Product.<locals>._format_ineq.<locals>.<listcomp>r   r   r2  rN  rP  r   rW   rZ   rR    s    &z1LatexPrinter._print_Product.<locals>._format_ineqz\prod_{\substack{%s}} rS  c                    s   g | ]} |qS rW   rW   rT  rU  rW   rZ   r     r\   r   rV  rY  rW   rZ  rZ   _print_Product  s    zLatexPrinter._print_Productc                 C   s  ddl m} g }||jkr"|jjS t||r:|  }n
d|fg}|D ]\}}t|j }|j	dd d |D ]b\}}	|	dkr|
d|j  qr|	dkr|
d	|j  qrd
| |	 d }
|
d|
 |j  qrqHd|}|d dkr|dd  }n|dd  }|S )Nr   )Vectorc                 S   s   | d   S rH  )__str__r#  rW   rW   rZ   r[     r\   z4LatexPrinter._print_BasisDependent.<locals>.<lambda>r$  r   r   r   r   r   r   r   -   )Zsympy.vectorr]  ZzeroZ_latex_formr"  Zseparateitemsr'  
componentssortappendr   r   )r   r   r]  Zo1ra  systemZvectZ
inneritemskvZarg_strZoutstrrW   rW   rZ   _print_BasisDependent  s,    



z"LatexPrinter._print_BasisDependentc                 C   s4   |  |j}d| d ddt| j |j  }|S )Nr^   rV   _{%s}r   )r   r   r   mapindices)r   r   Ztex_baser   rW   rW   rZ   _print_Indexed  s
    zLatexPrinter._print_Indexedc                 C   s   |  |jS r   )r   labelr   rW   rW   rZ   _print_IndexedBase  s    zLatexPrinter._print_IndexedBasec                 C   sf   |  |j}|jd urb|  |j}|jd ur:|  |j}n|  tj}dj||d}dj||dS |S )Nz${lower}\mathrel{{..}}\nobreak{upper})r  r  z{{{label}}}_{{{interval}}})rm  interval)r   rm  r  r  r	   Zeror   )r   r   rm  r  r  ro  rW   rW   rZ   
_print_Idx  s    

zLatexPrinter._print_Idxc              	   C   s   t |jrd}nd}d}d}t|jD ]T\}}||7 }|dkrV|d|| |f 7 }q&|d|| | || |f 7 }q&|dkrd||f }nd	|| ||f }td
d |jD rd|| j|jt	d dddf S d|| j|jt	d dddf S )Nz\partialdr   r   r   %s %sz
%s %s^{%s}r3  z\frac{%s^{%s}}{%s}c                 s   s   | ]}|  V  qd S r   r   rK  rW   rW   rZ   r   )  r\   z1LatexPrinter._print_Derivative.<locals>.<genexpr>r   Tr   r   F)
r   r   reversedZvariable_countr   r   r   r   r   r   )r   r   Zdiff_symbolr   dimr   numrW   rW   rZ   _print_Derivative  s6    


zLatexPrinter._print_Derivativec           	         s`   |j \}}} |} fdd|D } fdd|D }ddd t||D }d||f S )Nc                 3   s   | ]}  |V  qd S r   r   r   r   r   rW   rZ   r   7  r\   z+LatexPrinter._print_Subs.<locals>.<genexpr>c                 3   s   | ]}  |V  qd S r   r   rz  r   rW   rZ   r   8  r\   z\\ c                 s   s"   | ]}|d  d |d  V  qdS )r   =r   NrW   rz  rW   rW   rZ   r   9  s   z#\left. %s \right|_{\substack{ %s }})r   r   r   zip)	r   subsr   r   newZ
latex_exprZ	latex_oldZ	latex_newZ
latex_subsrW   r   rZ   _print_Subs4  s    

zLatexPrinter._print_Subsc                    sH  dg  }}t |jdkr\tdd |jD r\ddt |jd   d } fd	d
|jD }nt|jD ]}|d }|d7 }t |dkr jd dkr jd s|d7 }t |dkr|d |d  |d f 7 }t |dkr|d |d  7 }|dd |  qfd| j|jt	d t
dd |jD ddd|f S )Nr      c                 s   s   | ]}t |d kV  qdS )r   N)r   )r   limrW   rW   rZ   r   B  r\   z/LatexPrinter._print_Integral.<locals>.<genexpr>z\irw   r   ntc                    s   g | ]}d   |d  qS )\, d%sr   r   r   symbolr   rW   rZ   r   F  s   z0LatexPrinter._print_Integral.<locals>.<listcomp>r   z\intr   r   r~   z\limitsr`  z
_{%s}^{%s}r2  ^{%s}r  z%s %s%sr   c                 s   s   | ]}|  V  qd S r   rt  rK  rW   rW   rZ   r   ]  r\   Tru  )r   rW  allrv  r   r   insertr   rX  r   r   r   r   )r   r   r   symbolsr  r  rW   r   rZ   _print_Integral>  s8    
"
zLatexPrinter._print_Integralc                 C   s   |j \}}}}d| | }t|dks8|tjtjfv rL|d| | 7 }n|d| || |f 7 }t|trd|| |f S d|| |f S d S )Nz\lim_{%s \to z+-z%s}z%s^%s}%s\left(%s\right)rs  )r   r   r   r	   InfinityNegativeInfinityr"  r   )r   r   r   zZz0dirr   rW   rW   rZ   _print_Limita  s    
zLatexPrinter._print_Limitc                 C   sD   |  |}|tv rd| }n$t|dks2|dr8|}nd| }|S )aJ  
        Logic to decide how to render a function to latex
          - if it is a recognized latex name, use the appropriate latex command
          - if it is a single letter, just use that letter
          - if it is a longer name, then put \operatorname{} around it and be
            mindful of undercores in the name
        z\%sr   rm   z\operatorname{%s})r   accepted_latex_functionsr   rE  )r   r  r   rW   rW   rZ   _hprint_Functiono  s    

zLatexPrinter._hprint_Functionc                    s  |j j}t d| r4t|ts4t d| ||S  fdd|jD } jd }d} jd o|t|dko| 	|jd  }g d	}||v r|d
krnN|dkr|d dkrdnd|dd  }n$|dkr|dd }d}|durd}|r
|t
v r d| }	nd| }	n6|dur6 |}
 |
}
d|
|f }	n
 |}	|rd|t
v rZ|	d7 }	n|	d7 }	n|	d7 }	|r|dur|	d| 7 }	|	d| S dS )a#  
        Render functions to LaTeX, handling functions that LaTeX knows about
        e.g., sin, cos, ... by using the proper LaTeX command (\sin, \cos, ...).
        For single-letter function names, render them as regular LaTeX math
        symbols. For multi-letter function names that LaTeX does not know
        about, (e.g., Li, sech) use \operatorname{} so that the function name
        is rendered in Roman font and LaTeX handles spacing properly.

        expr is the expression involving the function
        exp is an exponent
        Z_print_c                    s   g | ]}t  |qS rW   )r   r   r   r   rW   rZ   r     r\   z0LatexPrinter._print_Function.<locals>.<listcomp>r}   Fr{   r   r   )asinacosatanZacscZasecZacotasinhacoshatanhZacschZasechZacothrt   fullr   harZarcNpowerTz\%s^{-1}z\operatorname{%s}^{-1}r=  z {%s}%s{\left(%s \right)}r  r   )r  r   hasattrr"  r   getattrr   r   r   r   r  r  r   r   )r   r   r   r  r   r}   Zinv_trig_power_caseZcan_fold_bracketsZinv_trig_tabler   Zfunc_texrW   r   rZ   _print_Function  sR    


"









zLatexPrinter._print_Functionc                 C   s   |  t|S r   )r  r   r   rW   rW   rZ   _print_UndefinedFunction  s    z%LatexPrinter._print_UndefinedFunctionc                 C   s   d|  |j|  |jf S )Nz{%s}_{\circ}\left({%s}\right))r   rX  r   r   rW   rW   rZ   _print_ElementwiseApplyFunction  s    

z,LatexPrinter._print_ElementwiseApplyFunctionc                 C   s\   ddl m} ddlm}m} ddlm} ddlm} ddl	m
} |d|d|d	|d
|d|diS )Nr   )KroneckerDelta)gamma
lowergamma)beta)
DiracDelta)rQ   z\deltar.   z\gammaz\operatorname{B}z\operatorname{Chi})Z(sympy.functions.special.tensor_functionsr  Z'sympy.functions.special.gamma_functionsr  r  Z&sympy.functions.special.beta_functionsr  Z'sympy.functions.special.delta_functionsr  Z'sympy.functions.special.error_functionsrQ   )r   r  r  r  r  r  rQ   rW   rW   rZ   _special_function_classes  s    z&LatexPrinter._special_function_classesc                 C   s>   | j D ](}t||r|j|jkr| j |   S q| t|S r   )r  
issubclassr   r  r   )r   r   clsrW   rW   rZ   _print_FunctionClass  s    
z!LatexPrinter._print_FunctionClassc                 C   sJ   |j \}}t|dkr&| |d }n| t|}d|| |f }|S )Nr   r   z\left( %s \mapsto %s \right))r   r   r   rO  )r   r   r  r   rW   rW   rZ   _print_Lambda  s    
zLatexPrinter._print_Lambdac                 C   s   dS )Nz\left( x \mapsto x \right)rW   r   rW   rW   rZ   _print_IdentityFunction  s    z$LatexPrinter._print_IdentityFunctionc                    sX   t |jtd} fdd|D }dt|j d|f }|d urPd||f S |S d S )Nr$  c                    s   g | ]}d   | qS )r  r   r  r   rW   rZ   r     r\   z:LatexPrinter._hprint_variadic_function.<locals>.<listcomp>z\%s\left(%s\right)r   r=  )r(  r   r   r   r  r  r   )r   r   r   r   Ztexargsr   rW   r   rZ   _hprint_variadic_function  s    z&LatexPrinter._hprint_variadic_functionc                 C   s0   d|  |jd  }|d ur(d||f S |S d S )Nz\left\lfloor{%s}\right\rfloorr   r=  rI  r   r   r   r   rW   rW   rZ   _print_floor  s    zLatexPrinter._print_floorc                 C   s0   d|  |jd  }|d ur(d||f S |S d S )Nz\left\lceil{%s}\right\rceilr   r=  rI  r  rW   rW   rZ   _print_ceiling  s    zLatexPrinter._print_ceilingc                 C   sP   | j d s d| |jd  }nd| |jd  }|d urHd||f S |S d S )Nr   z\log{\left(%s \right)}r   z\ln{\left(%s \right)}r=  )r   r   r   r  rW   rW   rZ   
_print_log  s    
zLatexPrinter._print_logc                 C   s0   d|  |jd  }|d ur(d||f S |S d S )Nz\left|{%s}\right|r   r=  rI  r  rW   rW   rZ   
_print_Abs  s    zLatexPrinter._print_Absc                 C   sN   | j d r&d| |jd td  }nd| |jd td }| ||S )Nr   z\Re{%s}r   Atomz\operatorname{{re}}{{{}}}r   r   r   r   r   r   r  rW   rW   rZ   	_print_re(  s    
zLatexPrinter._print_rec                 C   sN   | j d r&d| |jd td  }nd| |jd td }| ||S )Nr   z\Im{%s}r   r  z\operatorname{{im}}{{{}}}r  r  rW   rW   rZ   	_print_im0  s    
zLatexPrinter._print_imc                 C   s   ddl m}m} t|jd |r2| |jd dS t|jd |rT| |jd dS |jd jrtd| |jd  S d| |jd  S d S )Nr   )
EquivalentImpliesz\not\Leftrightarrowz\not\Rightarrowz\neg \left(%s\right)z\neg %s)	sympy.logic.boolalgr  r  r"  r   _print_Equivalent_print_Implies
is_Booleanr   )r   r   r  r  rW   rW   rZ   
_print_Not8  s    zLatexPrinter._print_Notc                 C   s   |d }|j r$|js$d| | }nd| | }|dd  D ]>}|j rf|jsf|d|| |f 7 }q>|d|| |f 7 }q>|S )Nr   r   r  r   z %s \left(%s\right)z %s %s)r  Zis_Notr   )r   r   charr+   r   rW   rW   rZ   _print_LogOpC  s    zLatexPrinter._print_LogOpc                 C   s   t |jtd}| |dS )Nr$  z\wedger(  r   r   r  r   r   r   rW   rW   rZ   
_print_AndR  s    zLatexPrinter._print_Andc                 C   s   t |jtd}| |dS )Nr$  z\veer  r  rW   rW   rZ   	_print_OrV  s    zLatexPrinter._print_Orc                 C   s   t |jtd}| |dS )Nr$  z\veebarr  r  rW   rW   rZ   
_print_XorZ  s    zLatexPrinter._print_Xorc                 C   s   |  |j|pdS )Nz\Rightarrow)r  r   )r   r   altcharrW   rW   rZ   r  ^  s    zLatexPrinter._print_Impliesc                 C   s   t |jtd}| ||pdS )Nr$  z\Leftrightarrowr  )r   r   r  r   rW   rW   rZ   r  a  s    zLatexPrinter._print_Equivalentc                 C   s0   d|  |jd  }|d ur(d||f S |S d S )Nz\overline{%s}r   r=  rI  r  rW   rW   rZ   _print_conjugatee  s    zLatexPrinter._print_conjugatec                 C   s>   d}d|  |jd  }|d ur.d|||f S d||f S d S )Nz\operatorname{polar\_lift}r  r   	%s^{%s}%s%s%srI  )r   r   r   r  r+   rW   rW   rZ   _print_polar_liftm  s
    zLatexPrinter._print_polar_liftc                 C   s    d|  |jd  }| ||S )Nze^{%s}r   )r   r   r   r  rW   rW   rZ   _print_ExpBasev  s    zLatexPrinter._print_ExpBasec                 C   s   dS )Nr   rW   r   rW   rW   rZ   _print_Exp1|  s    zLatexPrinter._print_Exp1c                 C   s4   d|  |jd  }|d ur(d||f S d| S d S )Nr   r   zK^{%s}%szK%srI  r  rW   rW   rZ   _print_elliptic_k  s    zLatexPrinter._print_elliptic_kc                 C   sD   d|  |jd |  |jd f }|d ur8d||f S d| S d S )N\left(%s\middle| %s\right)r   r   zF^{%s}%szF%srI  r  rW   rW   rZ   _print_elliptic_f  s    zLatexPrinter._print_elliptic_fc                 C   sh   t |jdkr4d| |jd | |jd f }nd| |jd  }|d ur\d||f S d| S d S )Nr2  r  r   r   r   zE^{%s}%szE%sr   r   r   r  rW   rW   rZ   _print_elliptic_e  s    zLatexPrinter._print_elliptic_ec                 C   s   t |jdkrBd| |jd | |jd | |jd f }n$d| |jd | |jd f }|d urzd||f S d| S d S )	Nr`  z\left(%s; %s\middle| %s\right)r   r   r2  r  z
\Pi^{%s}%sz\Pi%sr  r  rW   rW   rZ   _print_elliptic_pi  s    zLatexPrinter._print_elliptic_pic                 C   sD   d|  |jd |  |jd f }|d ur8d||f S d| S d S )N\left(%s, %s\right)r   r   z\operatorname{B}^{%s}%sz\operatorname{B}%srI  r  rW   rW   rZ   _print_beta  s    zLatexPrinter._print_betar-   c                    sf    fdd|j D }d|d |d f }|d urJd||d |d ||f S d	||d |d |f S d S )
Nc                    s   g | ]}  |qS rW   r   r   r   rW   rZ   r     r\   z/LatexPrinter._print_betainc.<locals>.<listcomp>r  r   r   z#\operatorname{%s}_{(%s, %s)}^{%s}%sr2  r`  z\operatorname{%s}_{(%s, %s)}%s)r   )r   r   r   operatorlargsr   rW   r   rZ   _print_betainc  s
    zLatexPrinter._print_betaincc                 C   s   | j ||ddS )Nr2   )r  )r  r   rW   rW   rZ   _print_betainc_regularized  s    z'LatexPrinter._print_betainc_regularizedc                 C   sD   d|  |jd |  |jd f }|d ur8d||f S d| S d S )Nr  r   r   z\Gamma^{%s}%sz\Gamma%srI  r  rW   rW   rZ   _print_uppergamma  s    zLatexPrinter._print_uppergammac                 C   sD   d|  |jd |  |jd f }|d ur8d||f S d| S d S )Nr  r   r   z\gamma^{%s}%s\gamma%srI  r  rW   rW   rZ   _print_lowergamma  s    zLatexPrinter._print_lowergammac                 C   sJ   d|  |jd  }|d ur2d|  |j||f S d|  |j|f S d S Nr   r   r  r  )r   r   r  r  rW   rW   rZ   _hprint_one_arg_func  s    z!LatexPrinter._hprint_one_arg_funcc                 C   s4   d|  |jd  }|d ur(d||f S d| S d S )Nr   r   z\operatorname{Chi}^{%s}%sz\operatorname{Chi}%srI  r  rW   rW   rZ   
_print_Chi  s    zLatexPrinter._print_Chic                 C   sJ   d|  |jd  }|  |jd }|d ur:d|||f S d||f S d S )Nr   r   r   z\operatorname{E}_{%s}^{%s}%sz\operatorname{E}_{%s}%srI  )r   r   r   r   nurW   rW   rZ   _print_expint  s
    zLatexPrinter._print_expintc                 C   s4   d|  |jd  }|d ur(d||f S d| S d S )Nr   r   zS^{%s}%szS%srI  r  rW   rW   rZ   _print_fresnels  s    zLatexPrinter._print_fresnelsc                 C   s4   d|  |jd  }|d ur(d||f S d| S d S )Nr   r   zC^{%s}%szC%srI  r  rW   rW   rZ   _print_fresnelc  s    zLatexPrinter._print_fresnelcc                 C   s6   d|  |jd td  }|d ur.d||f S |S d S )Nz!%sr   Funcr   r   r   r   r  rW   rW   rZ   _print_subfactorial  s    z LatexPrinter._print_subfactorialc                 C   s6   d|  |jd td  }|d ur.d||f S |S d S )Nz%s!r   r  r=  r  r  rW   rW   rZ   _print_factorial   s    zLatexPrinter._print_factorialc                 C   s6   d|  |jd td  }|d ur.d||f S |S d S )Nz%s!!r   r  r=  r  r  rW   rW   rZ   _print_factorial2  s    zLatexPrinter._print_factorial2c                 C   s@   d|  |jd |  |jd f }|d ur8d||f S |S d S )Nz{\binom{%s}{%s}}r   r   r=  rI  r  rW   rW   rZ   _print_binomial  s    zLatexPrinter._print_binomialc                 C   s<   |j \}}d| |td  }d|| |f }| ||S )Nr  r  z{%s}^{\left(%s\right)}r   r   r   r   r   )r   r   r   nrf  r   r   rW   rW   rZ   _print_RisingFactorial  s    
z#LatexPrinter._print_RisingFactorialc                 C   s<   |j \}}d| |td  }d| ||f }| ||S )Nr  r  z{\left(%s\right)}_{%s}r  )r   r   r   r  rf  subr   rW   rW   rZ   _print_FallingFactorial!  s    
z$LatexPrinter._print_FallingFactorialc                 C   sf   d| }d}|d ur4| ddkr0d||f }nd}d|| |j| |jf }|rb| ||}|S )Nr  Fro   r   r=  T%s_{%s}\left(%s\right))findr   r   argumentr   )r   r   r   symr   Zneed_exprW   rW   rZ   _hprint_BesselBase)  s    
zLatexPrinter._hprint_BesselBasec                 C   sF   |sdS d}|d d D ]}|d|  | 7 }q||  |d 7 }|S )Nr   r   z%s, r   )r   rf   rY   rw   rW   rW   rZ   _hprint_vec:  s    zLatexPrinter._hprint_vecc                 C   s   |  ||dS )NJr  r   rW   rW   rZ   _print_besseljC  s    zLatexPrinter._print_besseljc                 C   s   |  ||dS )Nr2   r  r   rW   rW   rZ   _print_besseliF  s    zLatexPrinter._print_besselic                 C   s   |  ||dS )Nr3   r  r   rW   rW   rZ   _print_besselkI  s    zLatexPrinter._print_besselkc                 C   s   |  ||dS )NYr  r   rW   rW   rZ   _print_besselyL  s    zLatexPrinter._print_besselyc                 C   s   |  ||dS )Nyr  r   rW   rW   rZ   	_print_ynO  s    zLatexPrinter._print_ync                 C   s   |  ||dS )Nr   r  r   rW   rW   rZ   	_print_jnR  s    zLatexPrinter._print_jnc                 C   s   |  ||dS )NzH^{(1)}r  r   rW   rW   rZ   _print_hankel1U  s    zLatexPrinter._print_hankel1c                 C   s   |  ||dS )NzH^{(2)}r  r   rW   rW   rZ   _print_hankel2X  s    zLatexPrinter._print_hankel2c                 C   s   |  ||dS )Nzh^{(1)}r  r   rW   rW   rZ   
_print_hn1[  s    zLatexPrinter._print_hn1c                 C   s   |  ||dS )Nzh^{(2)}r  r   rW   rW   rZ   
_print_hn2^  s    zLatexPrinter._print_hn2r   c                 C   s:   d|  |jd  }|d ur*d|||f S d||f S d S r  rI  r   r   r   notationr   rW   rW   rZ   _hprint_airya  s    zLatexPrinter._hprint_airyc                 C   s:   d|  |jd  }|d ur*d|||f S d||f S d S )Nr   r   z{%s^\prime}^{%s}%sz%s^\prime%srI  r  rW   rW   rZ   _hprint_airy_primei  s    zLatexPrinter._hprint_airy_primec                 C   s   |  ||dS NZAir  r   rW   rW   rZ   _print_airyaiq  s    zLatexPrinter._print_airyaic                 C   s   |  ||dS NZBir  r   rW   rW   rZ   _print_airybit  s    zLatexPrinter._print_airybic                 C   s   |  ||dS r  r  r   rW   rW   rZ   _print_airyaiprimew  s    zLatexPrinter._print_airyaiprimec                 C   s   |  ||dS r
  r  r   rW   rW   rZ   _print_airybiprimez  s    zLatexPrinter._print_airybiprimec                 C   sZ   d|  t|j|  t|j| |j| |j|  |jf }|d urVd||f }|S )NzN{{}_{%s}F_{%s}\left(\begin{matrix} %s \\ %s \end{matrix}\middle| {%s} \right)}	{%s}^{%s})r   r   apbqr  r  r  rW   rW   rZ   _print_hyper}  s    
zLatexPrinter._print_hyperc                 C   s   d|  t|j|  t|j|  t|j|  t|j| |j| |j| |j| |j|  |j	f	 }|d urd||f }|S )Nz^{G_{%s, %s}^{%s, %s}\left(\begin{matrix} %s & %s \\%s & %s \end{matrix} \middle| {%s} \right)}r  )
r   r   r  r  ri   anr  ZaotherZbotherr  r  rW   rW   rZ   _print_meijerg  s    
zLatexPrinter._print_meijergc                 C   s0   d|  |jd  }|d ur(d||f S d| S )Nr   r   z\eta^{%s}%sz\eta%srI  r  rW   rW   rZ   _print_dirichlet_eta  s    z!LatexPrinter._print_dirichlet_etac                 C   sV   t |jdkr&dtt| j|j }nd| |jd  }|d urNd||f S d| S )Nr2  r  r   r   z\zeta^{%s}%sz\zeta%sr   r   rO  rj  r   r  rW   rW   rZ   _print_zeta  s    zLatexPrinter._print_zetac                 C   sV   t |jdkr&dtt| j|j }nd| |jd  }|d urNd||f S d| S )Nr2  z_{%s}\left(%s\right)ri  r   z\gamma%s^{%s}r  r  r  rW   rW   rZ   _print_stieltjes  s    zLatexPrinter._print_stieltjesc                 C   s2   dt t| j|j }|d u r&d| S d||f S )Nz\left(%s, %s, %s\right)z\Phi%sz\Phi^{%s}%s)rO  rj  r   r   r  rW   rW   rZ   _print_lerchphi  s    zLatexPrinter._print_lerchphic                 C   s<   t | j|j\}}d| }|d u r.d||f S d|||f S )Nr   z\operatorname{Li}_{%s}%sz\operatorname{Li}_{%s}^{%s}%srj  r   r   )r   r   r   rY   r  r   rW   rW   rZ   _print_polylog  s
    zLatexPrinter._print_polylogc                 C   sB   t | j|j\}}}}d||||f }|d ur>d| d|  }|S )Nz*P_{%s}^{\left(%s,%s\right)}\left(%s\right)rC  \right)^{%s}r  )r   r   r   r  r8  r9  r   r   rW   rW   rZ   _print_jacobi  s
    zLatexPrinter._print_jacobic                 C   s>   t | j|j\}}}d|||f }|d ur:d| d|  }|S )Nz'C_{%s}^{\left(%s\right)}\left(%s\right)rC  r  r  r   r   r   r  r8  r   r   rW   rW   rZ   _print_gegenbauer  s
    zLatexPrinter._print_gegenbauerc                 C   s:   t | j|j\}}d||f }|d ur6d| d|  }|S )NzT_{%s}\left(%s\right)rC  r  r  r   r   r   r  r   r   rW   rW   rZ   _print_chebyshevt  s
    zLatexPrinter._print_chebyshevtc                 C   s:   t | j|j\}}d||f }|d ur6d| d|  }|S )NzU_{%s}\left(%s\right)rC  r  r  r   rW   rW   rZ   _print_chebyshevu  s
    zLatexPrinter._print_chebyshevuc                 C   s:   t | j|j\}}d||f }|d ur6d| d|  }|S )NzP_{%s}\left(%s\right)rC  r  r  r   rW   rW   rZ   _print_legendre  s
    zLatexPrinter._print_legendrec                 C   s>   t | j|j\}}}d|||f }|d ur:d| d|  }|S )Nz'P_{%s}^{\left(%s\right)}\left(%s\right)rC  r  r  r  rW   rW   rZ   _print_assoc_legendre  s
    z"LatexPrinter._print_assoc_legendrec                 C   s:   t | j|j\}}d||f }|d ur6d| d|  }|S )NzH_{%s}\left(%s\right)rC  r  r  r   rW   rW   rZ   _print_hermite  s
    zLatexPrinter._print_hermitec                 C   s:   t | j|j\}}d||f }|d ur6d| d|  }|S )NzL_{%s}\left(%s\right)rC  r  r  r   rW   rW   rZ   _print_laguerre  s
    zLatexPrinter._print_laguerrec                 C   s>   t | j|j\}}}d|||f }|d ur:d| d|  }|S )Nz'L_{%s}^{\left(%s\right)}\left(%s\right)rC  r  r  r  rW   rW   rZ   _print_assoc_laguerre  s
    z"LatexPrinter._print_assoc_laguerrec                 C   sB   t | j|j\}}}}d||||f }|d ur>d| d|  }|S )NzY_{%s}^{%s}\left(%s,%s\right)rC  r  r  r   r   r   r  mthetaphir   rW   rW   rZ   
_print_Ynm  s
    zLatexPrinter._print_Ynmc                 C   sB   t | j|j\}}}}d||||f }|d ur>d| d|  }|S )NzZ_{%s}^{%s}\left(%s,%s\right)rC  r  r  r(  rW   rW   rZ   
_print_Znm   s
    zLatexPrinter._print_Znmc           	      C   sB   t | j|\}}}|rdnd}|s&dnd| }d||||||f S )Nz	^{\prime}r   r  z%s%s\left(%s, %s, %s\right)%s)rj  r   )	r   	characterr   rg   r   r8  r?  r  suprW   rW   rZ   Z__print_mathieu_functions  s    z&LatexPrinter.__print_mathieu_functionsc                 C   s   | j d|j|dS )NCr<  &_LatexPrinter__print_mathieu_functionsr   r   rW   rW   rZ   _print_mathieuc  s    zLatexPrinter._print_mathieucc                 C   s   | j d|j|dS )Nr	   r<  r1  r   rW   rW   rZ   _print_mathieus  s    zLatexPrinter._print_mathieusc                 C   s   | j d|jd|dS )Nr0  Trg   r   r1  r   rW   rW   rZ   _print_mathieucprime  s    z!LatexPrinter._print_mathieucprimec                 C   s   | j d|jd|dS )Nr	   Tr5  r1  r   rW   rW   rZ   _print_mathieusprime  s    z!LatexPrinter._print_mathieusprimec                 C   sb   |j dkrRd}|j}|jdk r(d}| }| jd rBd|||j f S d|||j f S | |jS d S )Nr   r   r   r/  r|   z	%s%d / %dz%s\frac{%d}{%d})r?  r>  r   r   )r   r   signr>  rW   rW   rZ   _print_Rational  s    


zLatexPrinter._print_Rationalc                 C   s   |  |j}|jr&tdd |jD s4t|jdkr|d7 }t|jdkr\||  |j7 }n|jrv||  |jd 7 }|d7 }t|jdkr||  |j7 }n||  |jd 7 }d| S )Nc                 s   s   | ]}|t jkV  qd S r   )r	   rp  )r   r>  rW   rW   rZ   r   (  r\   z,LatexPrinter._print_Order.<locals>.<genexpr>r   ; r   z\rightarrow zO\left(%s\right))r   r   Zpointr   r   	variablesr   r   rY   rW   rW   rZ   _print_Order&  s    zLatexPrinter._print_Orderc                 C   s,   || j d v r| j d | S | j|j|dS )Nr   style)r   r   r   )r   r   r?  rW   rW   rZ   _print_Symbol6  s    zLatexPrinter._print_Symbolc                 C   s   d|v r|g g   }}}n2t |\}}}t|}dd |D }dd |D }|dkr^d|}|rt|dd| 7 }|r|d	d| 7 }|S )
Nr^   c                 S   s   g | ]}t |qS rW   	translater   r/  rW   rW   rZ   r   E  r\   z5LatexPrinter._deal_with_super_sub.<locals>.<listcomp>c                 S   s   g | ]}t |qS rW   rA  r   r  rW   rW   rZ   r   F  r\   rh   \mathbf{{{}}}r  r   ri  )r   rB  r   r   )r   stringr?  r   supersr}  rW   rW   rZ   r   >  s    
z!LatexPrinter._deal_with_super_subc                 C   sR   | j d rd}d}nd}d}d||ddd	d
}d| |j||j | |jf S )Nr~   z\gtz\lt><r{  z\geqz\leqz\neq)z==rH  rI  z>=z<=z!=z%s %s %s)r   r   lhsZrel_oprhs)r   r   gtltcharmaprW   rW   rZ   _print_RelationalT  s    
	zLatexPrinter._print_Relationalc                    s    fdd|j d d D }|j d jtkrJ|d |j d j  n.|d |j d j |j d jf  d}|d| S )Nc                    s(   g | ] \}}d   |  |f qS )%s & \text{for}\: %sr   )r   r   rq   r   rW   rZ   r   i  s   z1LatexPrinter._print_Piecewise.<locals>.<listcomp>r   z%s & \text{otherwise}rP  z\begin{cases} %s \end{cases}z \\)r   Zcondr   rd  r   r   r   )r   r   Zecpairsr   rW   r   rZ   _print_Piecewiseh  s    
zLatexPrinter._print_Piecewisec              
      s   g }t |jD ].}|d fdd||d d f D  q jd }|d u r| jd dkrdd}n|jdkd	u rxd
}nd}d}|d|}|dkr|ddd|j  d } jd r jd } j| }d| | d | }|d| S )Nr   c                    s   g | ]}  |qS rW   r   rK  r   rW   rZ   r   y  r\   z2LatexPrinter._print_MatrixBase.<locals>.<listcomp>r   r   r   smallmatrix
   Tmatrixarray \begin{%MATSTR%}%s\end{%MATSTR%}%MATSTR%r  r^   rq   z}%sr   \left\rightrS  )r   rowsrd  r   r   colsrp   r   )r   r   linesliner   out_str
left_delimright_delimrW   r   rZ   _print_MatrixBaseu  s.    ,




zLatexPrinter._print_MatrixBasec                 C   s2   | j |jtd ddd| |j| |jf  S )Nr  Tr   z	_{%s, %s})r   parentr   r   rw   r   r   rW   rW   rZ   _print_MatrixElement  s    z!LatexPrinter._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 fdd| D S )Nr2  r   r   :c                 3   s$   | ]}|d ur  |ndV  qd S )Nr   r   )r   xir   rW   rZ   r     r\   zFLatexPrinter._print_MatrixSlice.<locals>.latexslice.<locals>.<genexpr>)r'  r   )r   rw  r   rW   rZ   
latexslice  s    z3LatexPrinter._print_MatrixSlice.<locals>.latexslicer  Trb  \left[r   \right])r   rc  r   ZrowslicerZ  Zcolslicer[  )r   r   rg  rW   r   rZ   _print_MatrixSlice  s    	zLatexPrinter._print_MatrixSlicec                 C   s   |  |jS r   )r   blocksr   rW   rW   rZ   _print_BlockMatrix  s    zLatexPrinter._print_BlockMatrixc                 C   s^   |j }ddlm} t||s0|jr0d| | S | |t|d}d|v rRd| S d| S d S )Nr   MatrixSymbolz\left(%s\right)^{T}Tro   z%s^{T}r+   sympy.matricesrn  r"  Zis_MatrixExprr   r   r   r   r   r  rn  rY   rW   rW   rZ   _print_Transpose  s    zLatexPrinter._print_Transposec                 C   s   |j }d| | S )Nz!\operatorname{tr}\left(%s \right))r+   r   r   r   r  rW   rW   rZ   _print_Trace  s    zLatexPrinter._print_Tracec                 C   s^   |j }ddlm} t||s0|jr0d| | S | |t|d}d|v rRd| S d| S d S )Nr   rm  z\left(%s\right)^{\dagger}Tro   z%s^{\dagger}ro  rq  rW   rW   rZ   _print_Adjoint  s    zLatexPrinter._print_Adjointc                    s   ddl m}  fdd} j}t|d trL|d  t|dd   }nt|}t |r  r|d dkr|dd  }n|d  |d< ddt	|| S dt	||S d S )	Nr   )MatMulc                    s    | t dS NFr   r   r#  r   r   rW   rZ   r[     s   z,LatexPrinter._print_MatMul.<locals>.<lambda>r   r   r/  r   )
Z!sympy.matrices.expressions.matmulrv  r   r"  r   r&  r'  r   r   rj  )r   r   rv  parensr   rW   ry  rZ   _print_MatMul  s    zLatexPrinter._print_MatMulc                 C   sz   |d urBd| j |jd td dd| j |jd td dd|f S d| j |jd td dd| j |jd td ddf S )Nz\left(%s \bmod %s\right)^{%s}r   r   Trb  r   z%s \bmod %sr  r   rW   rW   rZ   
_print_Mod  s(    zLatexPrinter._print_Modc                    s.   |j }td | j dt fdd|S )Nr   z \circ c                    s    | ddS NTrb  rW   r+   rz  precrW   rZ   r[     r\   z5LatexPrinter._print_HadamardProduct.<locals>.<lambda>r   r   r   r   rj  r   r   r   rW   r  rZ   _print_HadamardProduct  s    z#LatexPrinter._print_HadamardProductc                 C   s(   t |jtd k rd}nd}| ||S )Nr   z%s^{\circ \left({%s}\right)}z%s^{\circ {%s}})r   r   r   rA  )r   r   rG  rW   rW   rZ   _print_HadamardPower  s    z!LatexPrinter._print_HadamardPowerc                    s.   |j }td | j dt fdd|S )Nr   	 \otimes c                    s    | ddS r}  rW   r~  r  rW   rZ   r[     r\   z6LatexPrinter._print_KroneckerProduct.<locals>.<lambda>r  r  rW   r  rZ   _print_KroneckerProduct  s    z$LatexPrinter._print_KroneckerProductc                 C   sv   |j |j }}ddlm} t||s<d| || |f S | |}d|v r`d|| |f S d|| |f S d S )Nr   rm  r   ro   r=  )r   r   rp  rn  r"  r   )r   r   r   r   rn  Zbase_strrW   rW   rZ   _print_MatPow  s    


zLatexPrinter._print_MatPowc                 C   s   | j || jd dS )Nr   r>  )r@  r   r   rW   rW   rZ   _print_MatrixSymbol	  s    
z LatexPrinter._print_MatrixSymbolc                 C   s   | j d dkrdS dS )Nr   rv   0z
\mathbf{0}r   )r   r0   rW   rW   rZ   _print_ZeroMatrix  s    zLatexPrinter._print_ZeroMatrixc                 C   s   | j d dkrdS dS )Nr   rv   1z
\mathbf{1}r  )r   r7   rW   rW   rZ   _print_OneMatrix  s    zLatexPrinter._print_OneMatrixc                 C   s   | j d dkrdS dS )Nr   rv   z
\mathbb{I}z
\mathbf{I}r  )r   r2   rW   rW   rZ   _print_Identity  s    zLatexPrinter._print_Identityc                 C   s   |  |jd }d| S )Nr   zP_{%s}rI  )r   r8   Zperm_strrW   rW   rZ   _print_PermutationMatrix  s    z%LatexPrinter._print_PermutationMatrixc              
   C   s   |  dkr| |d S | jd }|d u rd| jd dkr@d}n$|  dksZ|jd dkr`d	}nd
}d}|d|}| jd r| jd }| j| }d| | d | }|  dkr|d S g gdd t|  D  }dd |jD }tj| D ]}|d 	| ||  d}	t|  d ddD ]}
t
||
d  |j|
 k rH q|	rl||
 	d||
d   nR||
 	|d||
d    t
||
d  dkrd||
 d  d ||
 d< |	 }	g ||
d < q$q|d d }|  d dkr|| }|S )Nr   rW   r   r   r   rR  r   rS  rT  rU  rV  rW  r   rX  rY  r   c                 S   s   g | ]}g qS rW   rW   rK  rW   rW   rZ   r   6  r\   z1LatexPrinter._print_NDimArray.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rW   )r'  r   rK  rW   rW   rZ   r   7  r\   Tr   r   rS  rh  ri  r2  )rankr   r   shaperp   r   r   	itertoolsproductrd  r   r   )r   r   r   Z	block_strr_  r`  Z	level_strZshape_rangesZouter_iZevenZback_outer_ir^  rW   rW   rZ   _print_NDimArray  s`    





zLatexPrinter._print_NDimArrayc           	      C   s   |  |}d }d }|D ]}|j}||v s,|r<||kr<|d7 }||krl|d urT|d7 }|jrd|d7 }n|d7 }||  |jd 7 }||v r|d7 }||  || 7 }d}nd}|}q|d ur|d7 }|S )	Nr   rV   z{}^{z{}_{r   r{  TF)r   is_upr   )	r   r   rk  	index_mapr^  Zlast_valenceZprev_mapindexZnew_valencerW   rW   rZ   _printer_tensor_indicesQ  s2    

z$LatexPrinter._printer_tensor_indicesc                 C   s$   |j d j d }| }| ||S rH  )r   get_indicesr  )r   r   r   rk  rW   rW   rZ   _print_Tensorm  s    zLatexPrinter._print_Tensorc                 C   s0   |j jd jd }|j  }|j}| |||S rH  )r   r   r  r  r  )r   r   r   rk  r  rW   rW   rZ   _print_TensorElementr  s    
z!LatexPrinter._print_TensorElementc                    s*      \}}|d fdd|D  S )Nr   c                    s   g | ]} |t qS rW   )r   r   r   ry  rW   rZ   r   |  r\   z/LatexPrinter._print_TensMul.<locals>.<listcomp>)Z!_get_args_for_traditional_printerr   )r   r   r8  r   rW   ry  rZ   _print_TensMulx  s    zLatexPrinter._print_TensMulc                 C   sL   g }|j }|D ]}|| |t| q|  d|}|dd}|S )Nr   z+ -r/  )r   rd  r   r   rc  r   rp   )r   r   r8  r   r   rY   rW   rW   rZ   _print_TensAdd  s    
zLatexPrinter._print_TensAddc                 C   s"   d|j rdnd| |jd f S )Nz{}%s{%s}ro   _r   )r  r   r   r   rW   rW   rZ   _print_TensorIndex  s    zLatexPrinter._print_TensorIndexc                    st   t |jdkr6d |jd  |jtd df S dt |jd fdd	|jD  |jtd df S d S )
Nr   z"\frac{\partial}{\partial {%s}}{%s}r   r   Fz\frac{\partial^{%s}}{%s}{%s}r   c                    s   g | ]}d   | qS )z\partial {%s}r   rK  r   rW   rZ   r     r\   z9LatexPrinter._print_PartialDerivative.<locals>.<listcomp>)r   r;  r   r   r   r   r   r   rW   r   rZ   _print_PartialDerivative  s    z%LatexPrinter._print_PartialDerivativec                 C   s   |  |jS r   )r   r   r   rW   rW   rZ   _print_ArraySymbol  s    zLatexPrinter._print_ArraySymbolc                    s2   d  |jtd dd fdd|jD f S )Nz{{%s}_{%s}}r  Tr   c                    s   g | ]}  | qS rW   r   rK  r   rW   rZ   r     r\   z4LatexPrinter._print_ArrayElement.<locals>.<listcomp>)r   r   r   r   rk  r   rW   r   rZ   _print_ArrayElement  s    z LatexPrinter._print_ArrayElementc                 C   s   dS )Nz
\mathbb{U}rW   r   rW   rW   rZ   _print_UniversalSet  s    z LatexPrinter._print_UniversalSetc                 C   s8   |d u rd|  |jd  S d|  |jd |f S d S )Nz$\operatorname{frac}{\left(%s\right)}r   z)\operatorname{frac}{\left(%s\right)}^{%s}rI  r   rW   rW   rZ   _print_frac  s
    zLatexPrinter._print_fracc                    sz    j d dkrd}n j d dkr(d}ntdt|dkrT  |d | S  |d	  fd
d|D S d S )Nr   r	  ;rx   r   Unknown Decimal Separatorr   r   z \  c                    s   g | ]}  |qS rW   r   rK  r   rW   rZ   r     r\   z-LatexPrinter._print_tuple.<locals>.<listcomp>)r   r   r   r   r   r   )r   r   seprW   r   rZ   _print_tuple  s    zLatexPrinter._print_tuplec                    s    fdd|j D }d|S )Nc                    s   g | ]}  |qS rW   r   r   r8  r   rW   rZ   r     r\   z5LatexPrinter._print_TensorProduct.<locals>.<listcomp>r  r   r   r   r   elementsrW   r   rZ   _print_TensorProduct  s    z!LatexPrinter._print_TensorProductc                    s    fdd|j D }d|S )Nc                    s   g | ]}  |qS rW   r   r  r   rW   rZ   r     r\   z4LatexPrinter._print_WedgeProduct.<locals>.<listcomp>z \wedge r  r  rW   r   rZ   _print_WedgeProduct  s    z LatexPrinter._print_WedgeProductc                 C   s
   |  |S r   )r  r   rW   rW   rZ   _print_Tuple  s    zLatexPrinter._print_Tuplec                    s`    j d dkr*dd fdd|D  S  j d dkrTdd fd	d|D  S td
d S )Nr   r	  z\left[ %s\right]z; \  c                    s   g | ]}  |qS rW   r   rK  r   rW   rZ   r     r\   z,LatexPrinter._print_list.<locals>.<listcomp>rx   , \  c                    s   g | ]}  |qS rW   r   rK  r   rW   rZ   r     r\   r  )r   r   r   r   rW   r   rZ   _print_list  s    zLatexPrinter._print_listc                 C   sR   t | td}g }|D ]*}|| }|d| || |f  qdd| S )Nr$  z%s : %sz\left\{ %s\right\}r  )r(  keysr   rd  r   r   )r   rr  r  ra  r%  valrW   rW   rZ   _print_dict  s     zLatexPrinter._print_dictc                 C   s
   |  |S r   )r  r   rW   rW   rZ   _print_Dict  s    zLatexPrinter._print_Dictc                 C   sj   t |jdks|jd dkr2d| |jd  }n$d| |jd | |jd f }|rfd||f }|S )Nr   r   z\delta\left(%s\right)z+\delta^{\left( %s \right)}\left( %s \right)r   r  r  rW   rW   rZ   _print_DiracDelta  s    zLatexPrinter._print_DiracDeltac                 C   sP   |  |jd |jd  }|  |jd }d||f }|d urLd|||f }|S )Nr   r   r2  z${\left\langle %s \right\rangle}^{%s}z-{\left({\langle %s \rangle}^{%s}\right)}^{%s}rI  )r   r   r   shiftr  r   rW   rW   rZ   _print_SingularityFunction  s    z'LatexPrinter._print_SingularityFunctionc                    s6   d  fdd|jD }d| }|r2d||f }|S )Nr   c                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r     r\   z0LatexPrinter._print_Heaviside.<locals>.<genexpr>z\theta\left(%s\right)r   )r   pargs)r   r   r   r  r   rW   r   rZ   _print_Heaviside  s
    zLatexPrinter._print_Heavisidec                 C   sj   |  |jd }|  |jd }|jd jrF|jd jrFd||f }nd||f }|d urfd||f }|S )Nr   r   z\delta_{%s %s}z\delta_{%s, %s}r   )r   r   r   )r   r   r   rw   r   r   rW   rW   rZ   _print_KroneckerDelta  s    z"LatexPrinter._print_KroneckerDeltac                 C   sT   t | j|j}tdd |jD r2dd| }ndd| }|rPd||f }|S )Nc                 s   s   | ]}|j V  qd S r   )r   r   rW   rW   rZ   r     r\   z1LatexPrinter._print_LeviCivita.<locals>.<genexpr>z\varepsilon_{%s}r   r   r   )rj  r   r   r  r   )r   r   r   rk  r   rW   rW   rZ   _print_LeviCivita  s    zLatexPrinter._print_LeviCivitac                 C   sn   t |drd| |  S t |drFd| |j d | |j S t |dr`d| |j S | d S d S )N
as_booleanz\text{Domain: }setz \in r  z\text{Domain on })r  r   r  r  r  )r   rr  rW   rW   rZ   _print_RandomDomain  s    



z LatexPrinter._print_RandomDomainc                 C   s   t |jtd}| |S )Nr$  )r(  r   r   
_print_setr   rY   ra  rW   rW   rZ   _print_FiniteSet  s    zLatexPrinter._print_FiniteSetc                 C   s`   t |td}| jd dkr.dt| j|}n*| jd dkrPdt| j|}ntdd| S )	Nr$  r   r	  r:  rx   r   r  \left\{%s\right\})r(  r   r   r   rj  r   r   r  rW   rW   rZ   r    s    zLatexPrinter._print_setc                    s  fdd}t   jjrLjjrLjjr< ddd f}q ddd f}njjrn d j d f}n|jjrt}t|t| f}nXjd urj	dk dkrt
}qjrt}t|t| d f}q| S n| S dd	 fd
d|D  d S )Nc                     s    j d dkrJ j d dkr. j d } qdfdd j D } nL j d dkr|dfdd j d d D } ndfdd j D } d	|  d
S )Nr   r2  r   r   c                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r   1  r\   zKLatexPrinter._print_Range.<locals>._print_symbolic_range.<locals>.<genexpr>c                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r   4  r\   c                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r   6  r\   z\text{Range}\left(r   )r   r   r   )cont)rY   r   rW   rZ   _print_symbolic_range+  s    $z8LatexPrinter._print_Range.<locals>._print_symbolic_ranger   r   r   r  Tz\left\{r   c                 3   s$   | ]}| ur |nd V  qdS z\ldotsNr   r   eldotsr   rW   rZ   r   Q  r\   z,LatexPrinter._print_Range.<locals>.<genexpr>z\right\})objectstartis_infinitestopstepZis_positiveiternextZis_emptyr   rO  is_iterabler   )r   rY   r  printsetitrW   )r  rY   r   rZ   _print_Range*  s0    

zLatexPrinter._print_Rangec                 C   s   t |jdkrd|d ur>d|| |jd || |jd f S d|| |jd | |jd f S d|| |jd f }|d urd||f }|S )Nr2  z%s_{%s}^{%s}\left(%s\right)r   r   r  z%s_{%s}r=  r  )r   r   letterr   r   rW   rW   rZ   Z__print_number_polynomialT  s    z&LatexPrinter.__print_number_polynomialc                 C   s   |  |d|S )Nr-   &_LatexPrinter__print_number_polynomialr   rW   rW   rZ   _print_bernoullib  s    zLatexPrinter._print_bernoullic                    s   t |jdkrxd |jd  |jd f }dd fdd|jd	 D  }|d urld
|||f }n|| }|S  |d|S )Nr`  z
B_{%s, %s}r   r   r   r   c                 3   s   | ]}  |V  qd S r   r   r  r   rW   rZ   r   i  s   z+LatexPrinter._print_bell.<locals>.<genexpr>r2  r  r-   )r   r   r   r   r  )r   r   r   Ztex1Ztex2r   rW   r   rZ   _print_belle  s    
zLatexPrinter._print_bellc                 C   s   |  |d|S NFr  r   rW   rW   rZ   _print_fibonaccis  s    zLatexPrinter._print_fibonaccic                 C   s,   d|  |jd  }|d ur(d||f }|S )NzL_{%s}r   r=  rI  r  rW   rW   rZ   _print_lucasv  s    zLatexPrinter._print_lucasc                 C   s   |  |d|S )Nr9   r  r   rW   rW   rZ   _print_tribonacci|  s    zLatexPrinter._print_tribonaccic                    s   t   t|jjdks&t|jjdkrZd|j|jd |j|jf S |jtj	u r|j} |
|d |
|d |
|d |
|f}n6|jtju s|jdkr|d d }|  nt|}dd fd	d
|D  d S )Nr   z\left\{%s\right\}_{%s=%s}^{%s}r`  r2  r   r  rh  r   c                 3   s$   | ]}| ur |nd V  qdS r  r   r  r  rW   rZ   r     r\   z1LatexPrinter._print_SeqFormula.<locals>.<genexpr>ri  )r  r   r  Zfree_symbolsr  r   formular;  r	   r  coeffr  lengthrd  rO  r   )r   rY   r  r  rW   r  rZ   _print_SeqFormula  s,     


zLatexPrinter._print_SeqFormulac                 C   s`   |j |jkrd| |j  S |jr(d}nd}|jr8d}nd}d|| |j | |j|f S d S )Nr  r   ru   r   r   z\left%s%s, %s\right%s)r  endr   Z	left_openZ
right_open)r   rw   leftrightrW   rW   rZ   _print_Interval  s    zLatexPrinter._print_Intervalc                 C   s   d|  |j|  |jf S )Nz \left\langle %s, %s\right\rangle)r   r   r   r   rw   rW   rW   rZ   _print_AccumulationBounds  s    z&LatexPrinter._print_AccumulationBoundsc                    s(   t |  fdd|jD }d|S )Nc                    s   g | ]} | qS rW   r   rK  r  r   rW   rZ   r     r\   z-LatexPrinter._print_Union.<locals>.<listcomp>z \cup r   r   r   r   uargs_strrW   r  rZ   _print_Union  s    zLatexPrinter._print_Unionc                    s(   t |  fdd|jD }d|S )Nc                    s   g | ]} | qS rW   r  rK  r  rW   rZ   r     r\   z2LatexPrinter._print_Complement.<locals>.<listcomp>z \setminus r  r  rW   r  rZ   _print_Complement  s    zLatexPrinter._print_Complementc                    s(   t |  fdd|jD }d|S )Nc                    s   g | ]} | qS rW   r  rK  r  rW   rZ   r     r\   z4LatexPrinter._print_Intersection.<locals>.<listcomp>z \cap r  r  rW   r  rZ   _print_Intersection  s    z LatexPrinter._print_Intersectionc                    s(   t |  fdd|jD }d|S )Nc                    s   g | ]} | qS rW   r  rK  r  rW   rZ   r     r\   z;LatexPrinter._print_SymmetricDifference.<locals>.<listcomp>z \triangle r  r  rW   r  rZ   _print_SymmetricDifference  s    z'LatexPrinter._print_SymmetricDifferencec                    s\   t | t|jdkr@t|js@|jd  dt|j  S d fdd|jD S )Nr   r   z^{%d}r   c                 3   s   | ]} | V  qd S r   r  )r   r  r  rW   rZ   r     s   z1LatexPrinter._print_ProductSet.<locals>.<genexpr>)r   r   setsr   r   r   r   r>  rW   r  rZ   _print_ProductSet  s     zLatexPrinter._print_ProductSetc                 C   s   dS )Nz	\emptysetrW   r   rW   rW   rZ   _print_EmptySet  s    zLatexPrinter._print_EmptySetc                 C   s   dS )Nz
\mathbb{N}rW   r   r  rW   rW   rZ   _print_Naturals  s    zLatexPrinter._print_Naturalsc                 C   s   dS )Nz\mathbb{N}_0rW   r  rW   rW   rZ   _print_Naturals0  s    zLatexPrinter._print_Naturals0c                 C   s   dS Nz
\mathbb{Z}rW   r  rW   rW   rZ   _print_Integers  s    zLatexPrinter._print_Integersc                 C   s   dS Nz
\mathbb{Q}rW   r  rW   rW   rZ   _print_Rationals  s    zLatexPrinter._print_Rationalsc                 C   s   dS Nz
\mathbb{R}rW   r  rW   rW   rZ   _print_Reals  s    zLatexPrinter._print_Realsc                 C   s   dS Nz
\mathbb{C}rW   r  rW   rW   rZ   _print_Complexes  s    zLatexPrinter._print_Complexesc                    sP   |j j}|j j} fddt||jD }ddd |D }d ||f S )Nc                 3   s&   | ]\}}  |  |fV  qd S r   r   )r   r   r  r   rW   rZ   r     r\   z/LatexPrinter._print_ImageSet.<locals>.<genexpr>r   c                 s   s   | ]}d | V  qdS )	%s \in %sNrW   )r   ZxyrW   rW   rZ   r     r\   z!\left\{%s\; \middle|\; %s\right\})rT   r   	signaturer|  Z	base_setsr   r   )r   rY   r   sigZxysZxinysrW   r   rZ   _print_ImageSet  s
    zLatexPrinter._print_ImageSetc                    s^   d  fddt|jD }|jtju r>d| |jf S d|| |j |jf S )Nr   c                    s   g | ]}  |qS rW   r   r   r  r   rW   rZ   r     r\   z4LatexPrinter._print_ConditionSet.<locals>.<listcomp>z"\left\{%s\; \middle|\; %s \right\}z3\left\{%s\; \middle|\; %s \in %s \wedge %s \right\})r   r   r  Zbase_setr	   ZUniversalSetr   	conditionr   rY   Z
vars_printrW   r   rZ   _print_ConditionSet  s    

z LatexPrinter._print_ConditionSetc                 C   s   |  |jd }d|S )Nr   z\mathcal{{P}}\left({}\right)r   r   r   )r   r   Z	arg_printrW   rW   rZ   _print_PowerSet  s    zLatexPrinter._print_PowerSetc                    s8   d  fdd|jD }d |j| |jf S )Nr   c                    s   g | ]}  |qS rW   r   r  r   rW   rZ   r     r\   z5LatexPrinter._print_ComplexRegion.<locals>.<listcomp>z)\left\{%s\; \middle|\; %s \in %s \right\})r   r;  r   r   r  r	  rW   r   rZ   _print_ComplexRegion  s    

z!LatexPrinter._print_ComplexRegionc                    s   dt  fdd|jD  S )Nr  c                 3   s   | ]}  |V  qd S r   r   r  r   rW   rZ   r     r\   z/LatexPrinter._print_Contains.<locals>.<genexpr>)rO  r   r   rW   r   rZ   _print_Contains  s    zLatexPrinter._print_Containsc                 C   s:   |j jtju r(|jjtju r(| |jS | | d S )Nz	 + \ldots)	r  r  r	   rp  Zbnr   Za0r   truncater   rW   rW   rZ   _print_FourierSeries	  s    z!LatexPrinter._print_FourierSeriesc                 C   s   |  |jS r   )r   Zinfiniter   rW   rW   rZ   _print_FormalPowerSeries	  s    z%LatexPrinter._print_FormalPowerSeriesc                 C   s
   d|j  S )Nz\mathbb{F}_{%s})modr   rW   rW   rZ   _print_FiniteField		  s    zLatexPrinter._print_FiniteFieldc                 C   s   dS r  rW   r   rW   rW   rZ   _print_IntegerRing	  s    zLatexPrinter._print_IntegerRingc                 C   s   dS r  rW   r   rW   rW   rZ   _print_RationalField	  s    z!LatexPrinter._print_RationalFieldc                 C   s   dS r  rW   r   rW   rW   rZ   _print_RealField	  s    zLatexPrinter._print_RealFieldc                 C   s   dS r  rW   r   rW   rW   rZ   _print_ComplexField	  s    z LatexPrinter._print_ComplexFieldc                 C   s,   |  |j}dt| j |j}d||f S )Nr   z%s\left[%s\right]r   domainr   rj  r  r   r   r  r  rW   rW   rZ   _print_PolynomialRing	  s    z"LatexPrinter._print_PolynomialRingc                 C   s,   |  |j}dt| j |j}d||f S )Nr   r  r  r  rW   rW   rZ   _print_FractionField	  s    z!LatexPrinter._print_FractionFieldc                 C   s<   |  |j}dt| j |j}d}|js.d}d|||f S )Nr   r   zS_<^{-1}z%s%s\left[%s\right])r   r  r   rj  r  Zis_Poly)r   r   r  r  invrW   rW   rZ   _print_PolynomialRingBase"	  s    z&LatexPrinter._print_PolynomialRingBasec                 C   s  |j j}g }| D ]\}}d}t|D ]H\}}|dkr*|dkrX|| |j| 7 }q*|| t|j| |7 }q*|jr|rd| | }	q| |}	nB|r|tj	u r|
d|g q|tju r|
d|g q| |}	|s|	}
n|	d | }
|
dr|
d|
dd  g q|
d|
g q|d dv rX|d}|dkrXd|d  |d< d|}tt| j|j}d	| |  }d
|g| |g }|tv rd||f }nd||f }|S )Nr   r   r   r   r  r_  r   )r_  r  z	domain=%sr   z\%s {\left(%s \right)}z$\operatorname{%s}{\left( %s \right)})r   r   r   r   r   genspowr   r	   r4  extendr   rE  popr   r'  rj  Z
get_domainr  )r   polyr  r   Zmonomr  Zs_monomrw   r   Zs_coeffZs_termmodifierr   r  r  r   r   rW   rW   rZ   _print_Poly*	  sN    






zLatexPrinter._print_Polyc                 C   sN   |j j}|dkrd}| |j}|j}|tv r<d|||f S d|||f S d S )NZComplexRootOfZCRootOfz\%s {\left(%s, %d\right)}z'\operatorname{%s} {\left(%s, %d\right)})r   r   r   r   r  r  )r   r*   r  r   r  rW   rW   rZ   _print_ComplexRootOfc	  s    z!LatexPrinter._print_ComplexRootOfc                 C   sd   |j j}| |jg}|jtjur4|| |j |tv rNd|d	|f S d|d	|f S d S )Nz\%s {\left(%s\right)}r   z#\operatorname{%s} {\left(%s\right)})
r   r   r   r   Zfunr	   ZIdentityFunctionrd  r  r   )r   r   r  r   rW   rW   rZ   _print_RootSumo	  s    zLatexPrinter._print_RootSumc                 C   s   dS )N\omegarW   r   rW   rW   rZ   _print_OrdinalOmega|	  s    z LatexPrinter._print_OrdinalOmegac                 C   sL   |j \}}|dkr2|dkr&d||S d|S n|dkrDd|S dS d S )Nr   z{} \omega^{{{}}}z	{} \omegaz\omega^{{{}}}r(  )r   r   )r   r   r   mulrW   rW   rZ   _print_OmegaPower	  s    

zLatexPrinter._print_OmegaPowerc                    s   d  fdd|jD S )Nr   c                    s   g | ]}  |qS rW   r   r   r   rW   rZ   r   	  r\   z/LatexPrinter._print_Ordinal.<locals>.<listcomp>)r   r   r   rW   r   rZ   _print_Ordinal	  s    zLatexPrinter._print_Ordinalc                 C   s   | j d }|| td|S )Nr   z	{%s}^{%d})r   r   r   )r   r#  r   rW   rW   rZ   _print_PolyElement	  s    
zLatexPrinter._print_PolyElementc                 C   s>   |j dkr| |jS | |j}| |j }d||f S d S )Nr   r3  )r7  r   r6  )r   r)   r6  r7  rW   rW   rZ   _print_FracElement	  s
    
zLatexPrinter._print_FracElementc                 C   sf   t |jdkr|jd d fn|j\}}d| | }|d urHd||f }|d urbd|| |f }|S )Nr   r   zE_{%s}r=  r  r  )r   r   r   r)  r   r   rW   rW   rZ   _print_euler	  s    &zLatexPrinter._print_eulerc                 C   s,   d|  |jd  }|d ur(d||f }|S )NzC_{%s}r   r=  rI  r  rW   rW   rZ   _print_catalan	  s    zLatexPrinter._print_catalanc              
   C   s>   d ||rdnd| |jd | |jd | |jd S )Nz5\mathcal{{{}}}{}_{{{}}}\left[{}\right]\left({}\right)z^{-1}r   r   r   r2  r   r   r   )r   r   rY   ZinverserW   rW   rZ   _print_UnifiedTransform	  s    z$LatexPrinter._print_UnifiedTransformc                 C   s   |  |dS )Nr4   r2  r   rW   rW   rZ   _print_MellinTransform	  s    z#LatexPrinter._print_MellinTransformc                 C   s   |  |ddS )Nr4   Tr3  r   rW   rW   rZ   _print_InverseMellinTransform	  s    z*LatexPrinter._print_InverseMellinTransformc                 C   s   |  |dS )NLr3  r   rW   rW   rZ   _print_LaplaceTransform	  s    z$LatexPrinter._print_LaplaceTransformc                 C   s   |  |ddS )Nr6  Tr3  r   rW   rW   rZ   _print_InverseLaplaceTransform	  s    z+LatexPrinter._print_InverseLaplaceTransformc                 C   s   |  |dS r  r3  r   rW   rW   rZ   _print_FourierTransform	  s    z$LatexPrinter._print_FourierTransformc                 C   s   |  |ddS )Nr  Tr3  r   rW   rW   rZ   _print_InverseFourierTransform	  s    z+LatexPrinter._print_InverseFourierTransformc                 C   s   |  |dS )NSINr3  r   rW   rW   rZ   _print_SineTransform	  s    z!LatexPrinter._print_SineTransformc                 C   s   |  |ddS )Nr;  Tr3  r   rW   rW   rZ   _print_InverseSineTransform	  s    z(LatexPrinter._print_InverseSineTransformc                 C   s   |  |dS )NCOSr3  r   rW   rW   rZ   _print_CosineTransform	  s    z#LatexPrinter._print_CosineTransformc                 C   s   |  |ddS )Nr>  Tr3  r   rW   rW   rZ   _print_InverseCosineTransform	  s    z*LatexPrinter._print_InverseCosineTransformc                 C   sD   z"|j d ur | |j |W S W n ty4   Y n0 | t|S r   )ringr   Zto_sympyr   reprr  rW   rW   rZ   
_print_DMP	  s    
zLatexPrinter._print_DMPc                 C   s
   |  |S r   )rC  r  rW   rW   rZ   
_print_DMF	  s    zLatexPrinter._print_DMFc                 C   s   |  t|jS r   r   r
   r   )r   r  rW   rW   rZ   _print_Object	  s    zLatexPrinter._print_Objectc                 C   sd   |  |jd }|d ur"d|f nd}t|jdkrBd||f }n|  |jd }d|||}|S )Nr   r  r   r   zW%s\left(%s\right)zW{0}_{{{1}}}\left({2}\right))r   r   r   r   )r   r   r   Zarg0resultZarg1rW   rW   rZ   _print_LambertW	  s    zLatexPrinter._print_LambertWc                 C   s   d | |jd S )Nz!\operatorname{{E}}\left[{}\right]r   r1  r   rW   rW   rZ   _print_Expectation	  s    zLatexPrinter._print_Expectationc                 C   s   d | |jd S )Nz#\operatorname{{Var}}\left({}\right)r   r1  r   rW   rW   rZ   _print_Variance	  s    zLatexPrinter._print_Variancec                    s    d d fdd|jD S )Nz#\operatorname{{Cov}}\left({}\right)r   c                 3   s   | ]}  |V  qd S r   r   r   r   rW   rZ   r   	  r\   z1LatexPrinter._print_Covariance.<locals>.<genexpr>)r   r   r   r   rW   r   rZ   _print_Covariance	  s    zLatexPrinter._print_Covariancec                 C   s   d | |jd S )Nz!\operatorname{{P}}\left({}\right)r   r1  r   rW   rW   rZ   _print_Probability	  s    zLatexPrinter._print_Probabilityc                 C   s$   |  |j}|  |j}d||f S )Nz%s\rightarrow %s)r   r  codomain)r   morphismr  rM  rW   rW   rZ   _print_Morphism	  s    zLatexPrinter._print_Morphismc                 C   s&   |  |j|  |j }}d||f S )Nr3  )r   rx  den)r   r   rx  rP  rW   rW   rZ   _print_TransferFunction	  s    z$LatexPrinter._print_TransferFunctionc                    s(   t  j} fdd}dt||S )Nc                    s    | t dS rw  rx  r#  ry  rW   rZ   r[   	  s   z,LatexPrinter._print_Series.<locals>.<lambda>r   )r'  r   r   rj  r   r   r   rz  rW   ry  rZ   _print_Series	  s    
zLatexPrinter._print_Seriesc                    s@   ddl m  tjd d d } fdd}dt||S )Nr   )MIMOParallelr   c                    s&   t |  r| tdS | S rw  )r"  r   r   r   r#  rT  r   r   rW   rZ   r[   
  s
    z0LatexPrinter._print_MIMOSeries.<locals>.<lambda>z\cdot)Zsympy.physics.control.ltirT  r'  r   r   rj  rR  rW   rU  rZ   _print_MIMOSeries	  s    zLatexPrinter._print_MIMOSeriesc                 C   s   d t| j|jS Nr   r   rj  r   r   r   rW   rW   rZ   _print_Parallel
  s    zLatexPrinter._print_Parallelc                 C   s   d t| j|jS rW  rX  r   rW   rW   rZ   _print_MIMOParallel	
  s    z LatexPrinter._print_MIMOParallelc                 C   s  ddl m}m} |j|dd|j }}t||r:t|jn|g}t|j|rXt|jjn|jg}|}t||rt|j|r|g ||R  }	nt||rt|j|r|j|kr|| }	n||g ||jR  f}	n|t||rt|j|r||kr || }	n||g|R  }	n<||kr&|| }	n(|j|kr<|| }	n|g ||R  }	| 	|}
| 	|}| 	|	}|j
dkr|dnd}d|
|||f S )Nr   )TransferFunctionSeriesr   r   r  r_  z\frac{%s}{%s %s %s})sympy.physics.controlr[  r\  sys1r  r"  r'  r   sys2r   r8  )r   r   r[  r\  rx  tfZnum_arg_listZden_arg_listZ
den_term_1Z
den_term_2r6  Zdenom_1Zdenom_2_signrW   rW   rZ   _print_Feedback
  s8    










zLatexPrinter._print_Feedbackc                 C   sL   ddl m} | ||j|j}| |j}|jdkr:dnd}d|||f S )Nr   )
MIMOSeriesr   r  r_  z)\left(I_{\tau} %s %s\right)^{-1} \cdot %s)r]  rc  r   r_  r^  r8  )r   r   rc  Zinv_matr^  ra  rW   rW   rZ   _print_MIMOFeedback0
  s
    z LatexPrinter._print_MIMOFeedbackc                 C   s   |  |j}d| S )Nz%s_\tau)r   Z	_expr_matrs  rW   rW   rZ   _print_TransferFunctionMatrix7
  s    z*LatexPrinter._print_TransferFunctionMatrixc                 C   s   d |jj|jS )Nz\text{{{}}}_{{{}}})r   r   r   r  r   rW   rW   rZ   
_print_DFT;
  s    zLatexPrinter._print_DFTc                 C   s&   |  t|j}| |}d||f S )Nz%s:%s)r   r
   r   rO  )r   rN  Zpretty_namepretty_morphismrW   rW   rZ   _print_NamedMorphism?
  s    
z!LatexPrinter._print_NamedMorphismc                 C   s"   ddl m} | ||j|jdS )Nr   )NamedMorphismid)Zsympy.categoriesri  rh  r  rM  )r   rN  ri  rW   rW   rZ   _print_IdentityMorphismD
  s    
z$LatexPrinter._print_IdentityMorphismc                    s<    fdd|j D }|  d|d } |}|| S )Nc                    s   g | ]}  t|jqS rW   rE  )r   	componentr   rW   rZ   r   L
  s   z9LatexPrinter._print_CompositeMorphism.<locals>.<listcomp>z\circ re  )rb  reverser   rO  )r   rN  Zcomponent_names_listZcomponent_namesrg  rW   r   rZ   _print_CompositeMorphismI
  s    

z%LatexPrinter._print_CompositeMorphismc                 C   s   d | t|jS NrE  )r   r   r
   r   )r   rN  rW   rW   rZ   _print_CategoryT
  s    zLatexPrinter._print_Categoryc                 C   s<   |j s| tjS | |j }|jr8|d| |j 7 }|S )Nz\Longrightarrow %s)Zpremisesr   r	   ZEmptySetZconclusions)r   Zdiagramlatex_resultrW   rW   rZ   _print_DiagramW
  s    
zLatexPrinter._print_Diagramc                 C   s   dd|j   }t|jD ]p}t|j D ]B}|||f rJ|t|||f 7 }|d7 }||j d kr&|d7 }q&||jd kr|d7 }|d7 }q|d7 }|S )	Nz\begin{array}{%s}
rq   r   r   & rS  
z\end{array}
)widthr   heightlatex)r   Zgridrq  rw   r   rW   rW   rZ   _print_DiagramGridc
  s    

zLatexPrinter._print_DiagramGridc                 C   s   d | |j| |jS )Nz{{{}}}^{{{}}})r   r   rA  r  r   r4   rW   rW   rZ   _print_FreeModuleu
  s    zLatexPrinter._print_FreeModulec                    s   d d fdd|D S )Nz\left[ {} \right]r   c                 3   s    | ]}d   | d V  qdS r^   rV   Nr   r   r   rW   rZ   r   z
  s   z8LatexPrinter._print_FreeModuleElement.<locals>.<genexpr>)r   r   r   r)  rW   r   rZ   _print_FreeModuleElementx
  s    z%LatexPrinter._print_FreeModuleElementc                    s    d d fdd|jD S )N\left\langle {} \right\rangler   c                 3   s    | ]}d   | d V  qdS r{  r   r   r   rW   rZ   r   ~
  s   z0LatexPrinter._print_SubModule.<locals>.<genexpr>)r   r   r  r|  rW   r   rZ   _print_SubModule}
  s    zLatexPrinter._print_SubModulec                    s"   d d fdd|jjD S )Nr~  r   c                 3   s"   | ]\}d   | d V  qdS r{  r   r   r   rW   rZ   r   
  s   z=LatexPrinter._print_ModuleImplementedIdeal.<locals>.<genexpr>)r   r   _moduler  r|  rW   r   rZ   _print_ModuleImplementedIdeal
  s    z*LatexPrinter._print_ModuleImplementedIdealc                    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   Trb  )r   r   rK  r   rW   rZ   r   
  s   z2LatexPrinter._print_Quaternion.<locals>.<listcomp>r   c                 S   s   g | ]\}}|d  | qS )r   rW   )r   rw   r   rW   rW   rZ   r   
  r\   r   Zijkr   )r   r|  r   )r   r   rY   r8  rW   r   rZ   _print_Quaternion
  s
    
&zLatexPrinter._print_Quaternionc                 C   s   d | |j| |jS Nz\frac{{{}}}{{{}}})r   r   rA  
base_ideal)r   RrW   rW   rZ   _print_QuotientRing
  s    
z LatexPrinter._print_QuotientRingc                 C   s   d | |j| |jjS Nz{{{}}} + {{{}}})r   r   datarA  r  )r   r   rW   rW   rZ   _print_QuotientRingElement
  s    z'LatexPrinter._print_QuotientRingElementc                 C   s   d | |j| |jjS r  )r   r   r  modulekilled_moduler|  rW   rW   rZ   _print_QuotientModuleElement
  s    z)LatexPrinter._print_QuotientModuleElementc                 C   s   d | |j| |jS r  )r   r   r   r  ry  rW   rW   rZ   _print_QuotientModule
  s    
z"LatexPrinter._print_QuotientModulec                 C   s(   d | | | |j| |jS )Nz{{{}}} : {{{}}} \to {{{}}})r   r   Z_sympy_matrixr  rM  )r   r  rW   rW   rZ   _print_MatrixHomomorphism
  s    z&LatexPrinter._print_MatrixHomomorphismc                 C   s   |j j }d|v r"|g g   }}}n2t|\}}}t|}dd |D }dd |D }d| }|rr|dd| 7 }|r|dd| 7 }|S )	Nr^   c                 S   s   g | ]}t |qS rW   rA  rC  rW   rW   rZ   r   
  r\   z0LatexPrinter._print_Manifold.<locals>.<listcomp>c                 S   s   g | ]}t |qS rW   rA  rD  rW   rW   rZ   r   
  r\   r   r  r   ri  )r   r   rB  r   )r   manifoldrF  r   rG  r}  rW   rW   rZ   _print_Manifold
  s    zLatexPrinter._print_Manifoldc                 C   s   d|  |j|  |jf S )Nz\text{%s}_{%s})r   r   r  )r   patchrW   rW   rZ   _print_Patch
  s    zLatexPrinter._print_Patchc                 C   s(   d|  |j|  |jj|  |jf S )Nz\text{%s}^{\text{%s}}_{%s})r   r   r  r  )r   ZcoordsysrW   rW   rZ   _print_CoordSystem
  s     zLatexPrinter._print_CoordSystemc                 C   s   d|  |j S )Nz\mathbb{\nabla}_{%s})r   Z_wrt)r   ZcvdrW   rW   rZ   _print_CovarDerivativeOp
  s    z%LatexPrinter._print_CovarDerivativeOpc                 C   s$   |j j|j j}d| t|S ro  
_coord_sysr  _indexr   r   r   r
   r   fieldrF  rW   rW   rZ   _print_BaseScalarField
  s    z#LatexPrinter._print_BaseScalarFieldc                 C   s$   |j j|j j}d| t|S )Nz\partial_{{{}}}r  r  rW   rW   rZ   _print_BaseVectorField
  s    z#LatexPrinter._print_BaseVectorFieldc                 C   sL   |j }t|dr4|jj|j j}d| t|S | |}d|S d S )Nr  z\operatorname{{d}}{}z!\operatorname{{d}}\left({}\right))	Z_form_fieldr  r  r  r  r   r   r   r
   )r   diffr  rF  rW   rW   rZ   _print_Differential
  s    

z LatexPrinter._print_Differentialc                 C   s   |  |jd }d|S )Nr   z"\operatorname{{tr}}\left({}\right)r  )r   r>  contentsrW   rW   rZ   	_print_Tr
  s    zLatexPrinter._print_Trc                 C   s4   |d ur d|  |jd |f S d|  |jd  S )Nz%\left(\phi\left(%s\right)\right)^{%s}r   z\phi\left(%s\right)rI  r   rW   rW   rZ   _print_totient
  s
    zLatexPrinter._print_totientc                 C   s4   |d ur d|  |jd |f S d|  |jd  S )Nz(\left(\lambda\left(%s\right)\right)^{%s}r   z\lambda\left(%s\right)rI  r   rW   rW   rZ   _print_reduced_totient
  s
    z#LatexPrinter._print_reduced_totientc                 C   sd   t |jdkr4dtt| j|jd |jd f }nd| |jd  }|d ur\d||f S d| S )Nr2  _%s\left(%s\right)r   r   r   z\sigma^{%s}%sz\sigma%sr  r  rW   rW   rZ   _print_divisor_sigma
  s    

z!LatexPrinter._print_divisor_sigmac                 C   sd   t |jdkr4dtt| j|jd |jd f }nd| |jd  }|d ur\d||f S d| S )Nr2  r  r   r   r   z\sigma^*^{%s}%sz
\sigma^*%sr  r  rW   rW   rZ   _print_udivisor_sigma
  s    

z"LatexPrinter._print_udivisor_sigmac                 C   s4   |d ur d|  |jd |f S d|  |jd  S )Nz$\left(\nu\left(%s\right)\right)^{%s}r   z\nu\left(%s\right)rI  r   rW   rW   rZ   _print_primenu
  s
    zLatexPrinter._print_primenuc                 C   s4   |d ur d|  |jd |f S d|  |jd  S )Nz'\left(\Omega\left(%s\right)\right)^{%s}r   z\Omega\left(%s\right)rI  r   rW   rW   rZ   _print_primeomega
  s
    zLatexPrinter._print_primeomegac                 C   s
   t |jS r   )r   r   r   rW   rW   rZ   
_print_Str  s    zLatexPrinter._print_Strc                 C   s   |  t|S r   )r   r   r   rW   rW   rZ   _print_float  s    zLatexPrinter._print_floatc                 C   s   t |S r   r   r   rW   rW   rZ   
_print_int	  s    zLatexPrinter._print_intc                 C   s   t |S r   r  r   rW   rW   rZ   
_print_mpz  s    zLatexPrinter._print_mpzc                 C   s   t |S r   r  r   rW   rW   rZ   
_print_mpq  s    zLatexPrinter._print_mpqc                 C   s   d tt|jS )Nz"\operatorname{{Q}}_{{\text{{{}}}}})r   rr   r   r   r   rW   rW   rZ   _print_Predicate  s    zLatexPrinter._print_Predicatec                    s:   |j }|j} |}d fdd|D }d||f S )Nr   c                    s   g | ]}  |qS rW   r   r  r   rW   rZ   r     r\   z8LatexPrinter._print_AppliedPredicate.<locals>.<listcomp>z%s(%s))rX  	argumentsr   r   )r   r   predr   Z
pred_latexZ
args_latexrW   r   rZ   _print_AppliedPredicate  s
    
z$LatexPrinter._print_AppliedPredicatec                    s   t  |}dt| S )Nz\mathtt{\text{%s}})superemptyPrinterrr   r<  r   rW   rZ   r    s    zLatexPrinter.emptyPrinter)N)FF)FF)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)Nr-   )N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)Nr   )Nr   )N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)FN)N)N)N)N)rv   )rv   )N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)F)N)N)N)N)N)N)N(9  r   
__module____qualname__ZprintmethodZ_default_settingsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z_print_BooleanTrueZ_print_BooleanFalser   r   r   r  r  r  r  r  r  r  r  r  r:  r;  rB  rA  rJ  r[  r\  rh  rl  rn  rq  ry  r  r  r  r  r  r  r  propertyr  r  r  r  r  Z
_print_MinZ
_print_Maxr  r  r  r  Z_print_Determinantr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  Z_print_gammar  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  r4  r6  r7  r9  r=  r@  Z_print_RandomSymbolr   rO  rQ  ra  rd  rj  rl  rr  rt  ru  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  Z_print_frozensetr  r  r  r  r  r  r  r  Z_print_SeqPerZ_print_SeqAddZ_print_SeqMulr  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/  r0  r2  r4  r5  r7  r8  r9  r:  r<  r=  r?  r@  rC  rD  rF  rH  rI  rJ  rK  rL  rO  rQ  rS  rV  rY  rZ  rb  rd  re  rf  Z_print_IDFTrh  rk  rn  rp  rr  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  r  r  r  __classcell__rW   rW   r  rZ   rs      s  8

	!r2!
#L

										4



*9		
$	

rs   c                 C   s   t | }|r|S |  tv r*d|   S | tv r:d|  S tt tddD ]D}|  	|rLt| t|krLt| t
| dt|    S qL| S dS )a  
    Check for a modifier ending the string.  If present, convert the
    modifier to latex and translate the rest recursively.

    Given a description of a Greek letter or other special character,
    return the appropriate latex.

    Let everything else pass as given.

    >>> from sympy.printing.latex import translate
    >>> translate('alphahatdotprime')
    "{\\dot{\\hat{\\alpha}}}'"
    rm   T)r%  rm  N)tex_greek_dictionaryr   r  greek_letters_setother_symbolsr(  modifier_dictr  r   rF  rB  )rY   r   r%  rW   rW   rZ   rB  #  s    
$rB  c                 K   s   t || S )a#  Convert the given expression to LaTeX string representation.

    Parameters
    ==========
    full_prec: boolean, optional
        If set to True, a floating point number is printed with full precision.
    fold_frac_powers : boolean, optional
        Emit ``^{p/q}`` instead of ``^{\frac{p}{q}}`` for fractional powers.
    fold_func_brackets : boolean, optional
        Fold function brackets where applicable.
    fold_short_frac : boolean, optional
        Emit ``p / q`` instead of ``\frac{p}{q}`` when the denominator is
        simple enough (at most two terms and no powers). The default value is
        ``True`` for inline mode, ``False`` otherwise.
    inv_trig_style : string, optional
        How inverse trig functions should be displayed. Can be one of
        ``abbreviated``, ``full``, or ``power``. Defaults to ``abbreviated``.
    itex : boolean, optional
        Specifies if itex-specific syntax is used, including emitting
        ``$$...$$``.
    ln_notation : boolean, optional
        If set to ``True``, ``\ln`` is used instead of default ``\log``.
    long_frac_ratio : float or None, optional
        The allowed ratio of the width of the numerator to the width of the
        denominator before the printer breaks off long fractions. If ``None``
        (the default value), long fractions are not broken up.
    mat_delim : string, optional
        The delimiter to wrap around matrices. Can be one of ``[``, ``(``, or
        the empty string. Defaults to ``[``.
    mat_str : string, optional
        Which matrix environment string to emit. ``smallmatrix``, ``matrix``,
        ``array``, etc. Defaults to ``smallmatrix`` for inline mode, ``matrix``
        for matrices of no more than 10 columns, and ``array`` otherwise.
    mode: string, optional
        Specifies how the generated code will be delimited. ``mode`` can be one
        of ``plain``, ``inline``, ``equation`` or ``equation*``.  If ``mode``
        is set to ``plain``, then the resulting code will not be delimited at
        all (this is the default). If ``mode`` is set to ``inline`` then inline
        LaTeX ``$...$`` will be used. If ``mode`` is set to ``equation`` or
        ``equation*``, the resulting code will be enclosed in the ``equation``
        or ``equation*`` environment (remember to import ``amsmath`` for
        ``equation*``), unless the ``itex`` option is set. In the latter case,
        the ``$$...$$`` syntax is used.
    mul_symbol : string or None, optional
        The symbol to use for multiplication. Can be one of ``None``, ``ldot``,
        ``dot``, or ``times``.
    order: string, optional
        Any of the supported monomial orderings (currently ``lex``, ``grlex``,
        or ``grevlex``), ``old``, and ``none``. This parameter does nothing for
        Mul objects. Setting order to ``old`` uses the compatibility ordering
        for Add defined in Printer. For very large expressions, set the
        ``order`` keyword to ``none`` if speed is a concern.
    symbol_names : dictionary of strings mapped to symbols, optional
        Dictionary of symbols and the custom strings they should be emitted as.
    root_notation : boolean, optional
        If set to ``False``, exponents of the form 1/n are printed in fractonal
        form. Default is ``True``, to print exponent in root form.
    mat_symbol_style : string, optional
        Can be either ``plain`` (default) or ``bold``. If set to ``bold``,
        a MatrixSymbol A will be printed as ``\mathbf{A}``, otherwise as ``A``.
    imaginary_unit : string, optional
        String to use for the imaginary unit. Defined options are "i" (default)
        and "j". Adding "r" or "t" in front gives ``\mathrm`` or ``\text``, so
        "ri" leads to ``\mathrm{i}`` which gives `\mathrm{i}`.
    gothic_re_im : boolean, optional
        If set to ``True``, `\Re` and `\Im` is used for ``re`` and ``im``, respectively.
        The default is ``False`` leading to `\operatorname{re}` and `\operatorname{im}`.
    decimal_separator : string, optional
        Specifies what separator to use to separate the whole and fractional parts of a
        floating point number as in `2.5` for the default, ``period`` or `2{,}5`
        when ``comma`` is specified. Lists, sets, and tuple are printed with semicolon
        separating the elements when ``comma`` is chosen. For example, [1; 2; 3] when
        ``comma`` is chosen and [1,2,3] for when ``period`` is chosen.
    parenthesize_super : boolean, optional
        If set to ``False``, superscripted expressions will not be parenthesized when
        powered. Default is ``True``, which parenthesizes the expression when powered.
    min: Integer or None, optional
        Sets the lower bound for the exponent to print floating point numbers in
        fixed-point format.
    max: Integer or None, optional
        Sets the upper bound for the exponent to print floating point numbers in
        fixed-point format.

    Notes
    =====

    Not using a print statement for printing, results in double backslashes for
    latex commands since that's the way Python escapes backslashes in strings.

    >>> from sympy import latex, Rational
    >>> from sympy.abc import tau
    >>> latex((2*tau)**Rational(7,2))
    '8 \\sqrt{2} \\tau^{\\frac{7}{2}}'
    >>> print(latex((2*tau)**Rational(7,2)))
    8 \sqrt{2} \tau^{\frac{7}{2}}

    Examples
    ========

    >>> from sympy import latex, pi, sin, asin, Integral, Matrix, Rational, log
    >>> from sympy.abc import x, y, mu, r, tau

    Basic usage:

    >>> print(latex((2*tau)**Rational(7,2)))
    8 \sqrt{2} \tau^{\frac{7}{2}}

    ``mode`` and ``itex`` options:

    >>> print(latex((2*mu)**Rational(7,2), mode='plain'))
    8 \sqrt{2} \mu^{\frac{7}{2}}
    >>> print(latex((2*tau)**Rational(7,2), mode='inline'))
    $8 \sqrt{2} \tau^{7 / 2}$
    >>> print(latex((2*mu)**Rational(7,2), mode='equation*'))
    \begin{equation*}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation*}
    >>> print(latex((2*mu)**Rational(7,2), mode='equation'))
    \begin{equation}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation}
    >>> print(latex((2*mu)**Rational(7,2), mode='equation', itex=True))
    $$8 \sqrt{2} \mu^{\frac{7}{2}}$$
    >>> print(latex((2*mu)**Rational(7,2), mode='plain'))
    8 \sqrt{2} \mu^{\frac{7}{2}}
    >>> print(latex((2*tau)**Rational(7,2), mode='inline'))
    $8 \sqrt{2} \tau^{7 / 2}$
    >>> print(latex((2*mu)**Rational(7,2), mode='equation*'))
    \begin{equation*}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation*}
    >>> print(latex((2*mu)**Rational(7,2), mode='equation'))
    \begin{equation}8 \sqrt{2} \mu^{\frac{7}{2}}\end{equation}
    >>> print(latex((2*mu)**Rational(7,2), mode='equation', itex=True))
    $$8 \sqrt{2} \mu^{\frac{7}{2}}$$

    Fraction options:

    >>> print(latex((2*tau)**Rational(7,2), fold_frac_powers=True))
    8 \sqrt{2} \tau^{7/2}
    >>> print(latex((2*tau)**sin(Rational(7,2))))
    \left(2 \tau\right)^{\sin{\left(\frac{7}{2} \right)}}
    >>> print(latex((2*tau)**sin(Rational(7,2)), fold_func_brackets=True))
    \left(2 \tau\right)^{\sin {\frac{7}{2}}}
    >>> print(latex(3*x**2/y))
    \frac{3 x^{2}}{y}
    >>> print(latex(3*x**2/y, fold_short_frac=True))
    3 x^{2} / y
    >>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=2))
    \frac{\int r\, dr}{2 \pi}
    >>> print(latex(Integral(r, r)/2/pi, long_frac_ratio=0))
    \frac{1}{2 \pi} \int r\, dr

    Multiplication options:

    >>> print(latex((2*tau)**sin(Rational(7,2)), mul_symbol="times"))
    \left(2 \times \tau\right)^{\sin{\left(\frac{7}{2} \right)}}

    Trig options:

    >>> print(latex(asin(Rational(7,2))))
    \operatorname{asin}{\left(\frac{7}{2} \right)}
    >>> print(latex(asin(Rational(7,2)), inv_trig_style="full"))
    \arcsin{\left(\frac{7}{2} \right)}
    >>> print(latex(asin(Rational(7,2)), inv_trig_style="power"))
    \sin^{-1}{\left(\frac{7}{2} \right)}

    Matrix options:

    >>> print(latex(Matrix(2, 1, [x, y])))
    \left[\begin{matrix}x\\y\end{matrix}\right]
    >>> print(latex(Matrix(2, 1, [x, y]), mat_str = "array"))
    \left[\begin{array}{c}x\\y\end{array}\right]
    >>> print(latex(Matrix(2, 1, [x, y]), mat_delim="("))
    \left(\begin{matrix}x\\y\end{matrix}\right)

    Custom printing of symbols:

    >>> print(latex(x**2, symbol_names={x: 'x_i'}))
    x_i^{2}

    Logarithms:

    >>> print(latex(log(10)))
    \log{\left(10 \right)}
    >>> print(latex(log(10), ln_notation=True))
    \ln{\left(10 \right)}

    ``latex()`` also supports the builtin container types :class:`list`,
    :class:`tuple`, and :class:`dict`:

    >>> print(latex([2/x, y], mode='inline'))
    $\left[ 2 / x, \  y\right]$

    Unsupported types are rendered as monospaced plaintext:

    >>> print(latex(int))
    \mathtt{\text{<class 'int'>}}
    >>> print(latex("plain % text"))
    \mathtt{\text{plain \% text}}

    See :ref:`printer_method_example` for an example of how to override
    this behavior for your own types by implementing ``_latex``.

    .. versionchanged:: 1.7.0
        Unsupported types no longer have their ``str`` representation treated as valid latex.

    )rs   r   r   r   rW   rW   rZ   rw  B  s     Mrw  c                 K   s   t t| fi | dS )z`Prints LaTeX representation of the given expression. Takes the same
    settings as ``latex()``.N)printrw  r  rW   rW   rZ   print_latex  s    r  r   align*Fc              
   K   s  t f i |}|dkr,d}d}d}	d}
d}nJ|dkrJd}d}d}	d	}
d}n,|d
krhd}d}d}	d}
d}ntd|d}|rd}| }t|}d}t|D ]}|| }d}d}d}||kr|rd}nd}d}||kr||d k r||	 d d }nd}| d dkrd| }d}|dkrT|dkr0d}|d|| |||||7 }n|d|||||7 }|d7 }q||
7 }|S )a  
    This function generates a LaTeX equation with a multiline right-hand side
    in an ``align*``, ``eqnarray`` or ``IEEEeqnarray`` environment.

    Parameters
    ==========

    lhs : Expr
        Left-hand side of equation

    rhs : Expr
        Right-hand side of equation

    terms_per_line : integer, optional
        Number of terms per line to print. Default is 1.

    environment : "string", optional
        Which LaTeX wnvironment to use for the output. Options are "align*"
        (default), "eqnarray", and "IEEEeqnarray".

    use_dots : boolean, optional
        If ``True``, ``\\dots`` is added to the end of each line. Default is ``False``.

    Examples
    ========

    >>> from sympy import multiline_latex, symbols, sin, cos, exp, log, I
    >>> x, y, alpha = symbols('x y alpha')
    >>> expr = sin(alpha*y) + exp(I*alpha) - cos(log(y))
    >>> print(multiline_latex(x, expr))
    \begin{align*}
    x = & e^{i \alpha} \\
    & + \sin{\left(\alpha y \right)} \\
    & - \cos{\left(\log{\left(y \right)} \right)}
    \end{align*}

    Using at most two terms per line:
    >>> print(multiline_latex(x, expr, 2))
    \begin{align*}
    x = & e^{i \alpha} + \sin{\left(\alpha y \right)} \\
    & - \cos{\left(\log{\left(y \right)} \right)}
    \end{align*}

    Using ``eqnarray`` and dots:
    >>> print(multiline_latex(x, expr, terms_per_line=2, environment="eqnarray", use_dots=True))
    \begin{eqnarray}
    x & = & e^{i \alpha} + \sin{\left(\alpha y \right)} \dots\nonumber\\
    & & - \cos{\left(\log{\left(y \right)} \right)}
    \end{eqnarray}

    Using ``IEEEeqnarray``:
    >>> print(multiline_latex(x, expr, environment="IEEEeqnarray"))
    \begin{IEEEeqnarray}{rCl}
    x & = & e^{i \alpha} \nonumber\\
    & & + \sin{\left(\alpha y \right)} \nonumber\\
    & & - \cos{\left(\log{\left(y \right)} \right)}
    \end{IEEEeqnarray}

    Notes
    =====

    All optional parameters from ``latex`` can also be used.

    Zeqnarrayz\begin{eqnarray}
z& = &z	\nonumberz
\end{eqnarray}TZIEEEeqnarrayz\begin{IEEEeqnarray}{rCl}
z
\end{IEEEeqnarray}r  z\begin{align*}
z= &r   z
\end{align*}FzUnknown environment: {}z\dotsr   r  z& & rs  rS  rt  r   r   r_  z{:s} {:s}{:s} {:s} {:s}z{:s}{:s} {:s} {:s})rs   r   r   Zas_ordered_termsr   r   r&  r   )rJ  rK  Zterms_per_lineenvironmentZuse_dotsr   rQ  rG  Z
first_termZnonumberZend_termZdoubleetr  r   Zn_termsZ
term_countrw   r   Z
term_startZterm_endr8  rW   rW   rZ   multiline_latex  sn    C




r  )r   r  F)>__doc__typingr   r   ZtDictr  Z
sympy.corer   r   r   r   r   r	   r
   Zsympy.core.alphabetsr   Zsympy.core.containersr   Zsympy.core.functionr   r   Zsympy.core.operationsr   Zsympy.core.powerr   Zsympy.core.sortingr   Zsympy.core.sympifyr   r  r   Zsympy.printing.precedencer   Zsympy.printing.printerr   r   Zsympy.printing.conventionsr   r   r   r   Zmpmath.libmp.libmpfr   r   r  Zsympy.utilities.iterablesr   r(   r  r  r  r  	frozensetr  compiler+  rr   rs   rB  rw  r  r  rW   rW   rW   rZ   <module>   s   $'                     /
 O