o
    8Va                  	   @   s2  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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' ddl%m(Z( ddl)m*Z* ddl+m,Z, ddl-Z-g dZ.i 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/d0d1d2d3d4i d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGd(dHdIdJd@dKdLdMdNdOdPdQdRdSdTdUdVdWZ/h dXZ0i dYdZd[ d\d]d[ d^d_d[ d`dad[ dbdcd[ ddded[ dfdgd[ dhdid[ djdkd[ dldmd[ dndod[ dpdqd[ drdsd[ dtdud[ dvdwd[ dxdyd[ dzd{d[ d|d[ d}d[ d~d[ dd[ dd[ dd[ dd[ dZ1e2eZ3e-4de-4dfZ5dd Z6G dd deZ7dd Z8ee7dd Z9dd Z:dddZ;dS )zC
A Printer which converts an expression into its LaTeX equivalent.
    )AnyDictN)AddFloatModMulNumberSSymbol)greeks)Tuple)_coeff_isnegAppliedUndef
Derivative)AssocOp)SympifyError)true)precedence_traditional)Printerprint_function)split_super_subrequires_partial)
precedence
PRECEDENCE)prec_to_dps)default_sort_key)has_variety)ZarcsinZarccosZarctanZsinZcosZtanZsinhZcoshZtanhZsqrtZlnlogZsecZcscZcotZcothreZimfracrootargZAlphaAZBetaBZGamma\GammaZDeltaz\DeltaZEpsilonEZZetaZZEtaHZThetaz\ThetaZIotaIZKappaKZLambdaz\LambdaZMuMZNuNZXiz\XiZomicronoZOmicronOZPiz\PiZRhoPZSigmaz\SigmaZTauTZUpsilonz\UpsilonZPhiz\PhiChiXZPsiz\PsiZOmegaz\Omegalamdaz\lambdaZLamdaZkhiz\chiZKhiZ
varepsilonz\varepsilonZvarkappaz	\varkappaZvarphiz\varphiZvarpiz\varpiZvarrhoz\varrhoz	\varsigmaz	\vartheta)ZvarsigmaZvartheta>
   wpellethmhobethhbaralephgimeldalethhslashZmathringc                 C      d|  d S )Nz
\mathring{} sr?   r?   6/usr/lib/python3/dist-packages/sympy/printing/latex.py<lambda>V       rC   Zddddotc                 C   r=   )Nz\ddddot{r>   r?   r@   r?   r?   rB   rC   W   rD   Zdddotc                 C   r=   )Nz\dddot{r>   r?   r@   r?   r?   rB   rC   X   rD   Zddotc                 C   r=   )Nz\ddot{r>   r?   r@   r?   r?   rB   rC   Y   rD   dotc                 C   r=   )Nz\dot{r>   r?   r@   r?   r?   rB   rC   Z   rD   checkc                 C   r=   )Nz\check{r>   r?   r@   r?   r?   rB   rC   [   rD   Zbrevec                 C   r=   )Nz\breve{r>   r?   r@   r?   r?   rB   rC   \   rD   Zacutec                 C   r=   )Nz\acute{r>   r?   r@   r?   r?   rB   rC   ]   rD   Zgravec                 C   r=   )Nz\grave{r>   r?   r@   r?   r?   rB   rC   ^   rD   tildec                 C   r=   )Nz\tilde{r>   r?   r@   r?   r?   rB   rC   _   rD   Zhatc                 C   r=   )Nz\hat{r>   r?   r@   r?   r?   rB   rC   `   rD   Zbarc                 C   r=   )Nz\bar{r>   r?   r@   r?   r?   rB   rC   a   rD   vecc                 C   r=   )Nz\vec{r>   r?   r@   r?   r?   rB   rC   b   rD   primec                 C   r=   N{z}'r?   r@   r?   r?   rB   rC   c   rD   Zprmc                 C   r=   rJ   r?   r@   r?   r?   rB   rC   d   rD   boldc                 C   r=   Nz\boldsymbol{r>   r?   r@   r?   r?   rB   rC   f   rD   bmc                 C   r=   rM   r?   r@   r?   r?   rB   rC   g   rD   c                 C   r=   )Nz	\mathcal{r>   r?   r@   r?   r?   rB   rC   h   rD   c                 C   r=   )Nz	\mathscr{r>   r?   r@   r?   r?   rB   rC   i   rD   c                 C   r=   )Nz
\mathfrak{r>   r?   r@   r?   r?   rB   rC   j   rD   c                 C   r=   )Nz\left\|{z	}\right\|r?   r@   r?   r?   rB   rC   l   rD   c                 C   r=   )Nz\left\langle{z}\right\rangler?   r@   r?   r?   rB   rC   m   rD   c                 C   r=   Nz\left|{z}\right|r?   r@   r?   r?   rB   rC   n   rD   c                 C   r=   rO   r?   r@   r?   r?   rB   rC   o   rD   )ZcalZscrZfrakZnormZ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 can't 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)rA   cr?   r?   rB   latex_escapez   s   rV   c                       s:  e Zd ZdZi 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i dddddddddddddZdTddZdd Zd d! ZdUd"d#Zd$d% Z	d&d' Z
d(d) Zd*d+ ZdUd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 ZeZeZd:d; ZdTd<d=Zd>d? Zd@dA ZdBdC ZdDdE ZdFdG ZdHdI ZdJdK ZdLdM ZdNdO Z dPdQ Z!dRdS Z"dTdU Z#dVdW Z$dXdY Z%dZd[ Z&d\d] Z'd^d_ Z(d`da Z)dbdc Z*ddde Z+dfdg Z,dhdi Z-djdk Z.dldm Z/dTdndoZ0dpdq Z1drds Z2e3dtdu Z4dvdw Z5dxdy Z6dzd{ Z7dTd|d}Z8e8 Z9Z:dTd~dZ;dTddZ<dTddZ=dTddZ>e>Z?dTddZ@dTddZAdd ZBdd ZCdd ZDdd ZEdd ZFdTddZGdTddZHdTddZIdTddZJdTddZKdTddZLdTddZMdTddZNdTddZOdTddZPdTddZQdVddZRdTddZSdTddZTdTddZUdTddZVeVZWdTddZXdTddZYdTddZZdTddZ[dTddZ\dTddZ]dTddZ^dTddĄZ_dTddƄZ`dTddȄZaddʄ Zbdd̄ ZcdTdd΄ZddTddЄZedTdd҄ZfdTddԄZgdTddքZhdTdd؄ZidTddڄZjdTdd܄ZkdTddބZldTddZmdWddZndWddZodTddZpdTddZqdTddZrdTddZsdTddZtdTddZudTddZvdTddZwdTddZxdTddZydTddZzdTddZ{dTddZ|dTd dZ}dTddZ~dTddZdTddZdTdd	ZdTd
dZdTddZdTddZdTddZdXddZdTddZdTddZdTddZdTddZdd Zdd ZdYd d!ZeZdYd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 ZdTd8d9Zd:d; Zd<d= Zd>d? Zd@dA ZdBdC ZdDdE ZdFdG ZdHdI ZdJdK ZdLdM Zi fdNdOZdPdQ ZdRdS ZdTdU ZdVdW ZdXdY ZdZd[ Zd\d] Zd^d_ Zd`da ZdTdbdcZddde Zdfdg Zdhdi Zdjdk Zdldm Zdndo Zdpdq ZdTdrdsZdTdtduZdTdvdwZdTdxdyZdTdzd{Zd|d} Zd~d Zdd ZeZdd ZdTddZdTddZdTddZÐdTddZĐdTddZŐdT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dTdԐdՄZdTd֐dׄZdZ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dT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d0d1 Zd2d3 Zd4d5 Zd6d7 ZdTd8d9ZdTd:d;ZdTd<d=Z dTd>d?Z!dTd@dAZ"dTdBdCZ#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z* fdRdSZ+  Z,S ([  LatexPrinterZ_latex	full_precFfold_frac_powersfold_func_bracketsfold_short_fracNinv_trig_styleabbreviateditexln_notationlong_frac_ratio	mat_delim[mat_strmodeplain
mul_symbolordersymbol_namesroot_notationTmat_symbol_styleimaginary_unitiperiod)gothic_re_imdecimal_separatorperm_cyclicparenthesize_superminmaxc                 C   sf  t | | d| jv rg d}| jd |vrtd| jd d u r-| jd dkr-d| jd< ddd	d
d}z|| jd  | jd< W n tyQ   | jd | jd< Y nw z|| jd pZd | jd< W n" ty   | jd  dv rx|d | jd< n| jd | jd< Y nw ddd| _dddddddd}z|| jd  | jd< W d S  ty   | jd | jd< Y d S w )Nrd   )inlinere   Zequationz	equation*zB'mode' must be one of 'inline', 'plain', 'equation' or 'equation*'r[   rt   T z \,.\, z \cdot  \times )NZldotrE   timesrf   mul_symbol_latexrE   mul_symbol_latex_numbers) ru   rQ   z\,z\:\;z\quad)])(rb   rl   z
\mathrm{i}z\text{i}jz
\mathrm{j}z\text{j})Nrl   ZriZtir   ZrjZtjrk   Zimaginary_unit_latex)r   __init__	_settings
ValueErrorKeyErrorstrip_delim_dict)selfsettingsZvalid_modesZmul_symbol_tableZimaginary_unit_tabler?   r?   rB   r      sd   


		zLatexPrinter.__init__c                 C   
   d |S )Nz\left({}\right)formatr   rA   r?   r?   rB   _add_parens      
zLatexPrinter._add_parensc                 C   r   )Nz\left( {}\right)r   r   r?   r?   rB   _add_parens_lspace   r   zLatexPrinter._add_parens_lspacec                 C   sN   t |}|r|r| | |S ||k s|s"||kr"| | |S | |S N)r   r   _print)r   itemlevelis_negstrictZprec_valr?   r?   rB   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.
        rS   rq   z{{{}}})r   r   r   r   r?   r?   rB   rq      s
   


zLatexPrinter.parenthesize_superc                 C   s^   t | |}| jd dkr|S | jd dkrd| S | jd r#d| S | jd }d|||f S )Nrd   re   rt   z$%s$r^   z$$%s$$z\begin{%s}%s\end{%s})r   doprintr   )r   exprtexZenv_strr?   r?   rB   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   r?   r?   rB   _needs_brackets
  s   zLatexPrinter._needs_bracketsc                 C   sJ   |  |sdS |jr| |sdS |jr| |sdS |js!|jr#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.
        FT)r   is_Mul_mul_is_cleanZis_Pow_pow_is_cleanis_Addis_Functionr   r?   r?   rB   _needs_function_brackets  s   
z%LatexPrinter._needs_function_bracketsc                    s   ddl m}m}m}  jr|st rdS nt td k r dS  jr%dS  j	r*dS t
 fddtfD r8dS |sJt
 fdd|||fD rJd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   )IntegralProductSumTr   c                       g | ]}  |qS r?   has.0xr   r?   rB   
<listcomp>@      z4LatexPrinter._needs_mul_brackets.<locals>.<listcomp>c                    r   r?   r   r   r   r?   rB   r   C  r   F)sympyr   r   r   r   r   r   r   is_RelationalZis_Piecewiseanyr   )r   r   firstlastr   r   r   r?   r   rB   _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rd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                    r   r?   r   r   r   r?   rB   r   P  r   z4LatexPrinter._needs_add_brackets.<locals>.<listcomp>F)r   r   r   r   r   r?   r   rB   _needs_add_bracketsH  s   z LatexPrinter._needs_add_bracketsc                 C   s   |j D ]}|jr dS qdS )NFT)argsr   )r   r   r!   r?   r?   rB   r   V  s
   
zLatexPrinter._mul_is_cleanc                 C   s   |  |j S r   )r   baser   r?   r?   rB   r   \     zLatexPrinter._pow_is_cleanc                 C   s   |d ur
d||f S |S )N\left(%s\right)^{%s}r?   r   r   expr?   r?   rB   _do_exponent_  s   zLatexPrinter._do_exponentc                    s0    fdd|j D } |jjdd|  S )Nc                    r   r?   r   )r   r,   r   r?   rB   r   f  r   z-LatexPrinter._print_Basic.<locals>.<listcomp>\left(%s\right), )r   _deal_with_super_sub	__class____name__join)r   r   Zlsr?   r   rB   _print_Basice  s   zLatexPrinter._print_Basicc                 C      d| S N	\text{%s}r?   r   er?   r?   rB   _print_boolj     zLatexPrinter._print_boolc                 C   r   r   r?   r   r?   r?   rB   _print_NoneTypep  r   zLatexPrinter._print_NoneTypec                 C   sv   | j ||d}d}t|D ]+\}}|dkrnt|r"|d7 }| }n|d7 }| |}| |r4d| }||7 }q|S )N)rg   rz   r    -  + r   )Z_as_ordered_terms	enumerater   r   r   )r   r   rg   termsr   rl   termterm_texr?   r?   rB   
_print_Adds  s   


zLatexPrinter._print_Addc                 C   s   ddl m} |jdkrdS ||}|j}|j}|jd |d kr(||d gg }d}|D ]}|t|dd7 }q,|d	d
}|dd}|S )Nr   Permutation\left( \right)   rz   ,r{   rb   z\left( r}   \right)) sympy.combinatorics.permutationsr   sizeZcyclic_form
array_formstrrT   )r   r   r   Z	expr_permZsizr   rl   r?   r?   rB   _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  n jd	d
}|r2 	|S |j
dkr9dS  fdd|jD } fddtt|D }d|}d|}d||f}	d|	 S )Nr   r   )SymPyDeprecationWarningzPermutation.print_cyclic = {}zinit_printing(perm_cyclic={})ia;  z1.6)ZfeatureZ
useinsteadZissueZdeprecated_since_versionrp   Tr   c                    r   r?   r   r   r!   r   r?   rB   r     r   z3LatexPrinter._print_Permutation.<locals>.<listcomp>c                    r   r?   r   r   r   r?   rB   r     r    & z \\ z \begin{pmatrix} %s \end{pmatrix})r   r   Zsympy.utilities.exceptionsr   Zprint_cyclicr   warnr   getr   r   r   rangelenr   )
r   r   r   r   rp   lowerupperZrow1Zrow2matr?   r   rB   _print_Permutation  s.   



zLatexPrinter._print_Permutationc                 C   s"   |j \}}d| || |f S )Nz\sigma_{%s}(%s))r   r   )r   r   Zpermvarr?   r?   rB   _print_AppliedPermutation  s   
z&LatexPrinter._print_AppliedPermutationc           
      C   s   t |j}| jd rdnd}d| jv r| jd nd }d| jv r$| jd nd }tj|j||||d}| jd }d|v ra|d\}}	|	d	 d
krM|	dd  }	| jd dkrZ|dd}d|||	f S |dkrgdS |dkrmdS | jd dkrz|dd}|S )NrX   FTrr   rs   )Zstrip_zerosZ	min_fixedZ	max_fixedry   r   r   +r   ro   comma.z{,}z%s%s10^{%s}z+infz\inftyz-infz- \infty)r   Z_precr   mlibZto_strZ_mpf_splitrT   )
r   r   Zdpsr   ZlowZhighZstr_real	separatorZmantr   r?   r?   rB   _print_Float  s(   

zLatexPrinter._print_Floatc                 C   0   |j }|j}d| |td | |td f S )Nz%s \times %sr   Z_expr1Z_expr2r   r   r   r   Zvec1Zvec2r?   r?   rB   _print_Cross  
   zLatexPrinter._print_Crossc                 C      |j }d| |td  S )Nz\nabla\times %sr   Z_exprr   r   r   r   rH   r?   r?   rB   _print_Curl     zLatexPrinter._print_Curlc                 C   r   )Nz\nabla\cdot %sr   r   r   r?   r?   rB   _print_Divergence  r   zLatexPrinter._print_Divergencec                 C   r   )Nz%s \cdot %sr   r   r   r?   r?   rB   
_print_Dot  r   zLatexPrinter._print_Dotc                 C   r   )Nz	\nabla %sr   r   r   r   funcr?   r?   rB   _print_Gradient  r   zLatexPrinter._print_Gradientc                 C   r   )Nz\triangle %sr   r   r  r?   r?   rB   _print_Laplacian  r   zLatexPrinter._print_Laplacianc                    s  ddl m  ddlm ddlm} jd jd  fdd}fd	d
t|trM|j	}|d t
ju sItdd |dd  D rM|S d}t|rb| }d}|jra|d7 }d}nd}||dd\}}|t
ju r dddd|j	vr|||7 }n||}	||}
t|
 }jd }jd r|dkrd|
vrj|ddr|d|	|
f 7 }n|d|	|
f 7 }n|d urLt|	 || krLj|ddr|d|
|	f 7 }nx|jrBt
j}t
j}|j	D ]2}j|ddst|||  || ks|j|j  u rdu rn n||9 }q||9 }qj|ddr3|d|||
||f 7 }n!|d|||
||f 7 }n|d |
|	f 7 }n|d!|	|
f 7 }|r[|d"7 }|S )#Nr   )Pow)Quantity)fractionrx   ry   c                    sP   | j s
t| S jdvr|  }nt| j}t| fddd}|S )N)oldZnonec                    s    t | pt |  ot | jS r   )
isinstancer   r   )r  r  r?   rB   rC     s   
 
z:LatexPrinter._print_Mul.<locals>.convert.<locals>.<lambda>key)r   r   r   rg   as_ordered_factorslistr   sorted)r   r   )r  r  convert_argsr   r?   rB   convert  s   


z(LatexPrinter._print_Mul.<locals>.convertc                    s   d }}t | D ]<\}}|}j||dk|t| d kdr%d| }td |r8td |r8| 7 }n|r>|7 }||7 }|}q|S )Nrz   r   r   )r   r   r   )r   r   r   r   _between_two_numbers_psearchmatch)r   Z_texZlast_term_texrl   r   r   )	numbersepr   r   r?   rB   r  	  s    

z-LatexPrinter._print_Mul.<locals>.convert_argsc                 s   s    | ]}t |tV  qd S r   )r
  r   r   r?   r?   rB   	<genexpr>%      z*LatexPrinter._print_Mul.<locals>.<genexpr>r   F- r~   Trz   )exactr   )Zevaluater`   r[      rS   )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.core.powerr  Zsympy.physics.unitsr  Zsympy.simplifyr  r   r
  r   r   r	   Oner   r   r   r   r   r   r   is_commutative)r   r   r  r  r   Zinclude_parensr   numerdenomZsnumerZsdenomZldenomZratioabr   r?   )r  r  r  r  r   r   rB   
_print_Mul  s   


(





zLatexPrinter._print_Mulc                 C   s  |j jrFt|j jdkrF|j jdkrF| jd rF| |j}|j j}|dkr*d| }n| jd r6d||f }nd||f }|j jrDd| S |S | jd	 r|j jr|j jdkr| 	|jt
d
 }|j j|j j}}|jjrp| |}|jjr| j|jd||f dS d|||f S |j jr|j jr|jjr|jdkrd|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 )Nr   ri   r  z	\sqrt{%s}r^   z\root{%d}{%s}z\sqrt[%d]{%s}z\frac{1}{%s}rY   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   rP   pqr   r   r   Zis_negativer   r   	is_Symbolrq   r   r  r#  _helper_print_standard_power)r   r   r   Zexpqr   r&  r'  r?   r?   rB   
_print_Powe  sR   $







zLatexPrinter._print_Powc                 C   sv   |  |j}| |jtd }|jjr| |}nt|jtr5|	dr5t
d|r5|dr5|dd }|||f S )Nr  \left(z\\left\(\\d?d?dotr      i)r   r   r   r   r   r(  rq   r
  r   
startswithr   r  endswith)r   r   templater   r   r?   r?   rB   r)    s   
z)LatexPrinter._helper_print_standard_powerc                 C   s   |  |jd S Nr   r   r   r   r?   r?   rB   _print_UnevaluatedExpr     z#LatexPrinter._print_UnevaluatedExprc                       t |jdkrd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 }|S ||j7 }|S )Nr   z\sum_{%s=%s}^{%s} c                    r   r?   r   r   rl   r   r?   rB   r     r   z+LatexPrinter._print_Sum.<locals>.<listcomp>r   c                    ,   dt  fdd| d | d | d fD  S )N%s \leq %s \leq %sc                    r   r?   r   r   rA   r   r?   rB   r     r   zALatexPrinter._print_Sum.<locals>._format_ineq.<locals>.<listcomp>r   r   r  tuplelr   r?   rB   _format_ineq     &z-LatexPrinter._print_Sum.<locals>._format_ineqz\sum_{\substack{%s}} \\c                       g | ]} |qS r?   r?   r   r<  r=  r?   rB   r         r   	r   limitsr:  r   r   r
  functionr   r   r   r   r   r?   r=  r   rB   
_print_Sum     zLatexPrinter._print_Sumc                    r4  )Nr   z\prod_{%s=%s}^{%s} c                    r   r?   r   r5  r   r?   rB   r     r   z/LatexPrinter._print_Product.<locals>.<listcomp>r   c                    r6  )Nr7  c                    r   r?   r   r8  r   r?   rB   r     r   zELatexPrinter._print_Product.<locals>._format_ineq.<locals>.<listcomp>r   r   r  r9  r;  r   r?   rB   r=    r>  z1LatexPrinter._print_Product.<locals>._format_ineqz\prod_{\substack{%s}} r?  c                    r@  r?   r?   rA  rB  r?   rB   r     rC  r   rD  rG  r?   rH  rB   _print_Product  rJ  zLatexPrinter._print_Productc                 C   s  ddl m} g }||jkr|jjS t||r|  }nd|fg}|D ]G\}}t|j }|j	dd d |D ]1\}}	|	dkrJ|
d|j  q9|	dkrW|
d	|j  q9d
| |	 d }
|
d|
 |j  q9q$d|}|d dkr|dd  }|S |dd  }|S )Nr   )Vectorc                 S   s   | d   S r0  )__str__r  r?   r?   rB   rC     rD   z4LatexPrinter._print_BasisDependent.<locals>.<lambda>r  r   r   r   r   r~   r|   rz   -   )Zsympy.vectorrL  ZzeroZ_latex_formr
  Zseparateitemsr  
componentssortappendr   r   )r   r   rL  Zo1rP  systemZvectZ
inneritemskvZarg_strZoutstrr?   r?   rB   _print_BasisDependent  s0   



	z"LatexPrinter._print_BasisDependentc                 C   s4   |  |j}d| d ddt| j |j  }|S )NrK   r>   _{%s}r   )r   r   r   mapindices)r   r   Ztex_baser   r?   r?   rB   _print_Indexed  s
   zLatexPrinter._print_Indexedc                 C      |  |jS r   )r   Zlabelr   r?   r?   rB   _print_IndexedBase     zLatexPrinter._print_IndexedBasec              	   C   s   t |jrd}nd}d}d}t|jD ]*\}}||7 }|dkr+|d|| |f 7 }q|d|| | || |f 7 }q|dkrId||f }n
d	|| ||f }td
d |jD rmd|| j|jt	d dddf S d|| j|jt	d dddf S )Nz\partialdrz   r   r   %s %sz
%s %s^{%s}r  z\frac{%s^{%s}}{%s}c                 s       | ]}t |V  qd S r   r   r5  r?   r?   rB   r        z1LatexPrinter._print_Derivative.<locals>.<genexpr>r   Tr   r   F)
r   r   reversedZvariable_countr   rq   r   r   r   r   )r   r   Zdiff_symbolr   dimr   numr?   r?   rB   _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       | ]}  |V  qd S r   r   r   r   r   r?   rB   r    r  z+LatexPrinter._print_Subs.<locals>.<genexpr>c                 3   ri  r   r   rj  r   r?   rB   r    r  z\\ c                 s   s$    | ]}|d  d |d  V  qdS )r   =r   Nr?   rj  r?   r?   rB   r        
z#\left. %s \right|_{\substack{ %s }})r   r   r   zip)	r   subsr   r	  newZ
latex_exprZ	latex_oldZ	latex_newZ
latex_subsr?   r   rB   _print_Subs  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 }nZt|jD ]T}|d }|d7 }t |dkr| jd dkrS jd sS|d7 }t |dkrk|d |d  |d f 7 }t |dkr||d |d  7 }|dd |  q3d| j|jt	d t
dd |jD ddd|f S )Nrz      c                 s   s    | ]	}t |d kV  qdS )r   Nr   )r   limr?   r?   rB   r  %  s    z/LatexPrinter._print_Integral.<locals>.<genexpr>z\irl   r   ntc                    s   g | ]}d   |d  qS )\, d%sr   r   r   symbolr   r?   rB   r   )  s    z0LatexPrinter._print_Integral.<locals>.<listcomp>r   z\intrd   rt   r^   z\limitsrO  z
_{%s}^{%s}r  ^{%s}ru  z%s %s%sr   c                 s   ra  r   rb  r5  r?   r?   rB   r  @  rc  Trd  )r   rE  allre  r   r   insertr   rF  r   r   r   r   )r   r   r   symbolsrs  rw  r?   r   rB   _print_Integral!  s8   
"
zLatexPrinter._print_Integralc                 C   s   |j \}}}}d| | }t|dks|tjtjfv r&|d| | 7 }n|d| || |f 7 }t|trBd|| |f S d|| |f S )Nz\lim_{%s \to z+-z%s}z%s^%s}%s\left(%s\right)r`  )r   r   r   r	   InfinityNegativeInfinityr
  r   )r   r   r   zZz0dirr   r?   r?   rB   _print_LimitD  s   
zLatexPrinter._print_Limitc                 C   sH   |  |}|tv rd| }|S t|dks|dr|}|S 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   rQ   z\operatorname{%s})r   accepted_latex_functionsr   r-  )r   r  namer?   r?   rB   _hprint_FunctionR  s   
zLatexPrinter._hprint_Functionc                    sx  |j j}t d| rt|tst d| ||S  fdd|jD } jd }d} jd o>t|dko> 	|jd  }g d	}||v rk|d
krLn|dkrYd|dd  }n|dkrk|dd }d}|durkd}|r{|t
v rvd| }	nd| }	n|dur |}
 |
}
d|
|f }	n |}	|r|t
v r|	d7 }	n	|	d7 }	n|	d7 }	|r|dur|	d| 7 }	|	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 r?   )r   r   r   r   r?   rB   r   t      z0LatexPrinter._print_Function.<locals>.<listcomp>r\   FrZ   r   r   )ZasinZacosZatanZacscZasecZacotZasinhZacoshZatanhZacschZasechZacothr]   ZfullZarcNpowerTz\%s^{-1}z\operatorname{%s}^{-1}r%  z {%s}%s{\left(%s \right)}rx  r   )r  r   hasattrr
  r   getattrr   r   r   r   r  r  rq   r   )r   r   r   r  r   r\   Zinv_trig_power_caseZcan_fold_bracketsZinv_trig_tabler  Zfunc_texr?   r   rB   _print_Functionc  sR   









zLatexPrinter._print_Functionc                 C      |  t|S r   )r  r   r   r?   r?   rB   _print_UndefinedFunction  r   z%LatexPrinter._print_UndefinedFunctionc                 C      d|  |j|  |jf S )Nz{%s}_{\circ}\left({%s}\right))r   rF  r   r   r?   r?   rB   _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)r0   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_functionsr0   )r   r  r  r  r  r  r0   r?   r?   rB   _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   clsr?   r?   rB   _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   r:  )r   r   r{  r   r?   r?   rB   _print_Lambda  s   
zLatexPrinter._print_Lambdac                 C      dS )Nz\left( x \mapsto x \right)r?   r   r?   r?   rB   _print_IdentityFunction     z$LatexPrinter._print_IdentityFunctionc                    sT   t |jtd} fdd|D }dt|j d|f }|d ur(d||f S |S )Nr  c                       g | ]	}d   | qS )r  r   rv  r   r?   rB   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   r?   r   rB   _hprint_variadic_function  s   z&LatexPrinter._hprint_variadic_functionc                 C   ,   d|  |jd  }|d urd||f S |S )Nz\left\lfloor{%s}\right\rfloorr   r%  r1  r   r   r   r   r?   r?   rB   _print_floor     zLatexPrinter._print_floorc                 C   r  )Nz\left\lceil{%s}\right\rceilr   r%  r1  r  r?   r?   rB   _print_ceiling  r  zLatexPrinter._print_ceilingc                 C   sL   | j d sd| |jd  }n
d| |jd  }|d ur$d||f S |S )Nr_   z\log{\left(%s \right)}r   z\ln{\left(%s \right)}r%  )r   r   r   r  r?   r?   rB   
_print_log  s   
zLatexPrinter._print_logc                 C   r  )Nz\left|{%s}\right|r   r%  r1  r  r?   r?   rB   
_print_Abs  r  zLatexPrinter._print_Absc                 C   N   | j d rd| |jd td  }nd| |jd td }| ||S )Nrn   z\Re{%s}r   Atomz\operatorname{{re}}{{{}}}r   r   r   r   r   r   r  r?   r?   rB   	_print_re     
zLatexPrinter._print_rec                 C   r  )Nrn   z\Im{%s}r   r  z\operatorname{{im}}{{{}}}r  r  r?   r?   rB   	_print_im  r  zLatexPrinter._print_imc                 C   s   ddl m}m} t|jd |r| |jd dS t|jd |r*| |jd dS |jd jr:d| |jd  S d| |jd  S )Nr   )
EquivalentImpliesz\not\Leftrightarrowz\not\Rightarrowz\neg \left(%s\right)z\neg %s)	r   r  r  r
  r   _print_Equivalent_print_Implies
is_Booleanr   )r   r   r  r  r?   r?   rB   
_print_Not  s   zLatexPrinter._print_Notc                 C   s   |d }|j r|jsd| | }nd| | }|dd  D ]}|j r3|js3|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   r?   r?   rB   _print_LogOp&  s   zLatexPrinter._print_LogOpc                 C      t |jtd}| |dS )Nr  z\wedger  r   r   r  r   r   r   r?   r?   rB   
_print_And5     zLatexPrinter._print_Andc                 C   r  )Nr  z\veer  r  r?   r?   rB   	_print_Or9  r  zLatexPrinter._print_Orc                 C   r  )Nr  z\veebarr  r  r?   r?   rB   
_print_Xor=  r  zLatexPrinter._print_Xorc                 C   s   |  |j|pdS )Nz\Rightarrow)r  r   )r   r   altcharr?   r?   rB   r  A     zLatexPrinter._print_Impliesc                 C   s   t |jtd}| ||pdS )Nr  z\Leftrightarrowr  )r   r   r  r   r?   r?   rB   r  D  s   zLatexPrinter._print_Equivalentc                 C   r  )Nz\overline{%s}r   r%  r1  r  r?   r?   rB   _print_conjugateH  r  zLatexPrinter._print_conjugatec                 C   s:   d}d|  |jd  }|d urd|||f S d||f S )Nz\operatorname{polar\_lift}r  r   	%s^{%s}%s%s%sr1  )r   r   r   r  r!   r?   r?   rB   _print_polar_liftP  s
   zLatexPrinter._print_polar_liftc                 C   s    d|  |jd  }| ||S )Nze^{%s}r   )r   r   r   r  r?   r?   rB   _print_ExpBaseY  s   zLatexPrinter._print_ExpBasec                 C   r  )Nr   r?   r   r?   r?   rB   _print_Exp1_  r  zLatexPrinter._print_Exp1c                 C   0   d|  |jd  }|d urd||f S d| S )Nr   r   zK^{%s}%szK%sr1  r  r?   r?   rB   _print_elliptic_kb  s   zLatexPrinter._print_elliptic_kc                 C   @   d|  |jd |  |jd f }|d urd||f S d| S )N\left(%s\middle| %s\right)r   r   zF^{%s}%szF%sr1  r  r?   r?   rB   _print_elliptic_fi  s   zLatexPrinter._print_elliptic_fc                 C   sd   t |jdkrd| |jd | |jd f }n
d| |jd  }|d ur.d||f S d| S )Nr  r  r   r   r   zE^{%s}%szE%sr   r   r   r  r?   r?   rB   _print_elliptic_eq  s   zLatexPrinter._print_elliptic_ec                 C   s   t |jdkr!d| |jd | |jd | |jd f }nd| |jd | |jd f }|d ur=d||f S d| S )	NrO  z\left(%s; %s\middle| %s\right)r   r   r  r  z
\Pi^{%s}%sz\Pi%sr  r  r?   r?   rB   _print_elliptic_pi|  s   zLatexPrinter._print_elliptic_pic                 C   r  )N\left(%s, %s\right)r   r   z\operatorname{B}^{%s}%sz\operatorname{B}%sr1  r  r?   r?   rB   _print_beta     zLatexPrinter._print_betar#   c                    sb    fdd|j D }d|d |d f }|d ur%d||d |d ||f S d	||d |d |f S )
Nc                    r   r?   r   r   r   r?   rB   r     r   z/LatexPrinter._print_betainc.<locals>.<listcomp>r  r   r   z#\operatorname{%s}_{(%s, %s)}^{%s}%sr  rO  z\operatorname{%s}_{(%s, %s)}%s)r   )r   r   r   operatorZlargsr   r?   r   rB   _print_betainc  s
   zLatexPrinter._print_betaincc                 C   s   | j ||ddS )Nr(   )r  )r  r   r?   r?   rB   _print_betainc_regularized  r3  z'LatexPrinter._print_betainc_regularizedc                 C   r  )Nr  r   r   z\Gamma^{%s}%sz\Gamma%sr1  r  r?   r?   rB   _print_uppergamma  r  zLatexPrinter._print_uppergammac                 C   r  )Nr  r   r   z\gamma^{%s}%s\gamma%sr1  r  r?   r?   rB   _print_lowergamma  r  zLatexPrinter._print_lowergammac                 C   sF   d|  |jd  }|d urd|  |j||f S d|  |j|f S Nr   r   r  r  )r   r   r  r  r?   r?   rB   _hprint_one_arg_func  s   z!LatexPrinter._hprint_one_arg_funcc                 C   r  )Nr   r   z\operatorname{Chi}^{%s}%sz\operatorname{Chi}%sr1  r  r?   r?   rB   
_print_Chi     zLatexPrinter._print_Chic                 C   sF   d|  |jd  }|  |jd }|d urd|||f S d||f S )Nr   r   r   z\operatorname{E}_{%s}^{%s}%sz\operatorname{E}_{%s}%sr1  )r   r   r   r   Znur?   r?   rB   _print_expint  s
   zLatexPrinter._print_expintc                 C   r  )Nr   r   zS^{%s}%szS%sr1  r  r?   r?   rB   _print_fresnels  r  zLatexPrinter._print_fresnelsc                 C   r  )Nr   r   zC^{%s}%szC%sr1  r  r?   r?   rB   _print_fresnelc  r  zLatexPrinter._print_fresnelcc                 C   2   d|  |jd td  }|d urd||f S |S )Nz!%sr   Funcr   r   r   r   r  r?   r?   rB   _print_subfactorial     z LatexPrinter._print_subfactorialc                 C   r  )Nz%s!r   r  r%  r  r  r?   r?   rB   _print_factorial  r  zLatexPrinter._print_factorialc                 C   r  )Nz%s!!r   r  r%  r  r  r?   r?   rB   _print_factorial2  r  zLatexPrinter._print_factorial2c                 C   s<   d|  |jd |  |jd f }|d urd||f S |S )Nz{\binom{%s}{%s}}r   r   r%  r1  r  r?   r?   rB   _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   nrU  r   r   r?   r?   rB   _print_RisingFactorial     
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  rU  subr   r?   r?   rB   _print_FallingFactorial  r  z$LatexPrinter._print_FallingFactorialc                 C   sf   d| }d}|d ur| ddkrd||f }nd}d|| |j| |jf }|r1| ||}|S )Nr  FrS   r   r%  T%s_{%s}\left(%s\right))findr   rg   argumentr   )r   r   r   symr   Zneed_expr?   r?   rB   _hprint_BesselBase  s   
zLatexPrinter._hprint_BesselBasec                 C   sF   |sdS d}|d d D ]}|d|  | 7 }q||  |d 7 }|S )Nrz   r   z%s, r   )r   rH   rA   rl   r?   r?   rB   _hprint_vec  s   zLatexPrinter._hprint_vecc                 C      |  ||dS )NJr  r   r?   r?   rB   _print_besselj&  r   zLatexPrinter._print_besseljc                 C   r  )Nr(   r  r   r?   r?   rB   _print_besseli)  r   zLatexPrinter._print_besselic                 C   r  )Nr)   r  r   r?   r?   rB   _print_besselk,  r   zLatexPrinter._print_besselkc                 C   r  )NYr  r   r?   r?   rB   _print_bessely/  r   zLatexPrinter._print_besselyc                 C   r  )Nyr  r   r?   r?   rB   	_print_yn2  r   zLatexPrinter._print_ync                 C   r  )Nr   r  r   r?   r?   rB   	_print_jn5  r   zLatexPrinter._print_jnc                 C   r  )NzH^{(1)}r  r   r?   r?   rB   _print_hankel18  r   zLatexPrinter._print_hankel1c                 C   r  )NzH^{(2)}r  r   r?   r?   rB   _print_hankel2;  r   zLatexPrinter._print_hankel2c                 C   r  )Nzh^{(1)}r  r   r?   r?   rB   
_print_hn1>  r   zLatexPrinter._print_hn1c                 C   r  )Nzh^{(2)}r  r   r?   r?   rB   
_print_hn2A  r   zLatexPrinter._print_hn2rz   c                 C   6   d|  |jd  }|d urd|||f S d||f S r  r1  r   r   r   Znotationr   r?   r?   rB   _hprint_airyD     zLatexPrinter._hprint_airyc                 C   r  )Nr   r   z{%s^\prime}^{%s}%sz%s^\prime%sr1  r   r?   r?   rB   _hprint_airy_primeL  r  zLatexPrinter._hprint_airy_primec                 C   r  NZAir  r   r?   r?   rB   _print_airyaiT  r   zLatexPrinter._print_airyaic                 C   r  NZBir  r   r?   r?   rB   _print_airybiW  r   zLatexPrinter._print_airybic                 C   r  r  r  r   r?   r?   rB   _print_airyaiprimeZ  r   zLatexPrinter._print_airyaiprimec                 C   r  r  r	  r   r?   r?   rB   _print_airybiprime]  r   zLatexPrinter._print_airybiprimec                 C   sZ   d|  t|j|  t|j| |j| |j|  |jf }|d ur+d||f }|S )NzN{{}_{%s}F_{%s}\left(\begin{matrix} %s \\ %s \end{matrix}\middle| {%s} \right)}	{%s}^{%s})r   r   apbqr  r  r  r?   r?   rB   _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rCd||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  rN   Zanr  ZaotherZbotherr  r  r?   r?   rB   _print_meijergk  s   
zLatexPrinter._print_meijergc                 C   r  )Nr   r   z\eta^{%s}%sz\eta%sr1  r  r?   r?   rB   _print_dirichlet_etax  s   z!LatexPrinter._print_dirichlet_etac                 C   sV   t |jdkrdtt| j|j }n
d| |jd  }|d ur'd||f S d| S )Nr  r  r   r   z\zeta^{%s}%sz\zeta%sr   r   r:  rY  r   r  r?   r?   rB   _print_zeta~     zLatexPrinter._print_zetac                 C   sV   t |jdkrdtt| j|j }n
d| |jd  }|d ur'd||f S d| S )Nr  z_{%s}\left(%s\right)rX  r   z\gamma%s^{%s}r  r  r  r?   r?   rB   _print_stieltjes  r  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)r:  rY  r   r   r  r?   r?   rB   _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rY  r   r   )r   r   r   rA   r  r   r?   r?   rB   _print_polylog  s
   zLatexPrinter._print_polylogc                 C   B   t | j|j\}}}}d||||f }|d urd| d|  }|S )Nz*P_{%s}^{\left(%s,%s\right)}\left(%s\right)r+  \right)^{%s}r  )r   r   r   r  r!  r"  r   r   r?   r?   rB   _print_jacobi  
   zLatexPrinter._print_jacobic                 C   >   t | j|j\}}}d|||f }|d urd| d|  }|S )Nz'C_{%s}^{\left(%s\right)}\left(%s\right)r+  r  r  r   r   r   r  r!  r   r   r?   r?   rB   _print_gegenbauer  
   zLatexPrinter._print_gegenbauerc                 C   :   t | j|j\}}d||f }|d urd| d|  }|S )NzT_{%s}\left(%s\right)r+  r  r  r   r   r   r  r   r   r?   r?   rB   _print_chebyshevt  
   zLatexPrinter._print_chebyshevtc                 C   r!  )NzU_{%s}\left(%s\right)r+  r  r  r"  r?   r?   rB   _print_chebyshevu  r$  zLatexPrinter._print_chebyshevuc                 C   r!  )NzP_{%s}\left(%s\right)r+  r  r  r"  r?   r?   rB   _print_legendre  r$  zLatexPrinter._print_legendrec                 C   r  )Nz'P_{%s}^{\left(%s\right)}\left(%s\right)r+  r  r  r  r?   r?   rB   _print_assoc_legendre  r   z"LatexPrinter._print_assoc_legendrec                 C   r!  )NzH_{%s}\left(%s\right)r+  r  r  r"  r?   r?   rB   _print_hermite  r$  zLatexPrinter._print_hermitec                 C   r!  )NzL_{%s}\left(%s\right)r+  r  r  r"  r?   r?   rB   _print_laguerre  r$  zLatexPrinter._print_laguerrec                 C   r  )Nz'L_{%s}^{\left(%s\right)}\left(%s\right)r+  r  r  r  r?   r?   rB   _print_assoc_laguerre  r   z"LatexPrinter._print_assoc_laguerrec                 C   r  )NzY_{%s}^{%s}\left(%s,%s\right)r+  r  r  r   r   r   r  mZthetaZphir   r?   r?   rB   
_print_Ynm  r  zLatexPrinter._print_Ynmc                 C   r  )NzZ_{%s}^{%s}\left(%s,%s\right)r+  r  r  r+  r?   r?   rB   
_print_Znm  r  zLatexPrinter._print_Znmc           	      C   sB   t | j|\}}}|rdnd}|sdnd| }d||||||f S )Nz	^{\prime}rz   rx  z%s%s\left(%s, %s, %s\right)%s)rY  r   )	r   	characterr   rI   r   r!  r'  r  supr?   r?   rB   Z__print_mathieu_functions  s   z&LatexPrinter.__print_mathieu_functionsc                 C      | j d|j|dS )NCr$  &_LatexPrinter__print_mathieu_functionsr   r   r?   r?   rB   _print_mathieuc  r  zLatexPrinter._print_mathieucc                 C   r1  )Nr	   r$  r3  r   r?   r?   rB   _print_mathieus  r  zLatexPrinter._print_mathieusc                 C      | j d|jd|dS )Nr2  TrI   r   r3  r   r?   r?   rB   _print_mathieucprime     z!LatexPrinter._print_mathieucprimec                 C   r7  )Nr	   Tr8  r3  r   r?   r?   rB   _print_mathieusprime  r:  z!LatexPrinter._print_mathieusprimec                 C   s^   |j dkr)d}|j}|jdk rd}| }| jd r!d|||j f S d|||j f S | |jS )Nr   rz   r   r  r[   z	%s%d / %dz%s\frac{%d}{%d})r'  r&  r   r   )r   r   signr&  r?   r?   rB   _print_Rational  s   


zLatexPrinter._print_Rationalc                 C   s   |  |j}|jrtdd |jD st|jdkr\|d7 }t|jdkr.||  |j7 }n|jr;||  |jd 7 }|d7 }t|jdkrR||  |j7 }d| S ||  |jd 7 }d| S )Nc                 s   s    | ]}|t jkV  qd S r   )r	   ZZero)r   r&  r?   r?   rB   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   rA   r?   r?   rB   _print_Order	  s   zLatexPrinter._print_Orderc                 C   s,   || j d v r| j d | S | j|j|dS )Nrh   style)r   r   r  )r   r   rC  r?   r?   rB   _print_Symbol  s   zLatexPrinter._print_Symbolc                 C   s   d|v r|g g }}}nt |\}}}t|}dd |D }dd |D }|dkr/d|}|r:|dd| 7 }|rE|d	d| 7 }|S )
NrK   c                 S      g | ]}t |qS r?   	translater   r0  r?   r?   rB   r   (  rC  z5LatexPrinter._deal_with_super_sub.<locals>.<listcomp>c                 S   rE  r?   rF  r   r  r?   r?   rB   r   )  rC  rL   \mathbf{{{}}}rx  ru   rX  )r   rG  r   r   )r   stringrC  r  supersrn  r?   r?   rB   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><rk  z\geqz\leqz\neq)z==rM  rN  z>=z<=z!=z%s %s %s)r   r   lhsZrel_oprhs)r   r   gtltcharmapr?   r?   rB   _print_Relational7  s   
	zLatexPrinter._print_Relationalc                    s    fdd|j d d D }|j d jtkr%|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   rU   r   r?   rB   r   L  s    z1LatexPrinter._print_Piecewise.<locals>.<listcomp>r   z%s & \text{otherwise}rU  z\begin{cases} %s \end{cases}z \\)r   Zcondr   rS  r   r   r   )r   r   Zecpairsr   r?   r   rB   _print_PiecewiseK  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r2d}n|jdkd	u r<d
}nd}d}|d|}|dkrW|ddd|j  d } jd rp jd } j| }d| | d | }|d| S )Nr   c                    r   r?   r   r5  r   r?   rB   r   \  r   z2LatexPrinter._print_MatrixBase.<locals>.<listcomp>rc   rd   rt   smallmatrix
   Tmatrixarray \begin{%MATSTR%}%s\end{%MATSTR%}%MATSTR%r  rK   rU   z}%sra   \left\rightr?  )r   rowsrS  r   r   colsrT   r   )r   r   lineslinerc   out_str
left_delimright_delimr?   r   rB   _print_MatrixBaseX  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   rl   r   r   r?   r?   rB   _print_MatrixElements  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r!d | d< d fdd| D S )Nr  r   r   :c                 3   s&    | ]}|d ur  |ndV  qd S Nrz   r   )r   Zxir   r?   rB   r       $ zFLatexPrinter._print_MatrixSlice.<locals>.latexslice.<locals>.<genexpr>)r  r   )r   rf  r   r?   rB   
latexslicex  s   z3LatexPrinter._print_MatrixSlice.<locals>.latexslicer  Trg  \left[r   \right])r   rh  r   Zrowslicer_  Zcolslicer`  )r   r   rm  r?   r   rB   _print_MatrixSlicew  s   	zLatexPrinter._print_MatrixSlicec                 C   r\  r   )r   Zblocksr   r?   r?   rB   _print_BlockMatrix  r^  zLatexPrinter._print_BlockMatrixc                 C   s@   |j }ddlm} t||sd| | S d| |t|d S )Nr   MatrixSymbolz\left(%s\right)^{T}z%s^{T}T)r!   sympy.matricesrs  r
  r   r   r   r   r   r   rs  r?   r?   rB   _print_Transpose  s
   
zLatexPrinter._print_Transposec                 C   s   |j }d| | S )Nz!\operatorname{tr}\left(%s \right))r!   r   r   r   r   r?   r?   rB   _print_Trace  s   zLatexPrinter._print_Tracec                 C   s8   |j }ddlm} t||sd| | S d| | S )Nr   rr  z\left(%s\right)^{\dagger}z%s^{\dagger})r!   rt  rs  r
  r   ru  r?   r?   rB   _print_Adjoint  s
   
zLatexPrinter._print_Adjointc                    s   ddl m}m}  fdd} j}t|d |r(|d  t|dd   }nt|}t |rSt rS|d dkrB|dd  }n|d  |d< ddt	|| S dt	||S )	Nr   )MatMulr   c                        | t dS NFr   r   r  r   r   r?   rB   rC         z,LatexPrinter._print_MatMul.<locals>.<lambda>r   r   r  ru   )
r   rz  r   r   r
  r  r  r   r   rY  )r   r   rz  r   parensr   r?   r~  rB   _print_MatMul  s   zLatexPrinter._print_MatMulc                 C   sf   |d urd| j |jd td dd| |jd |f S d| j |jd td dd| |jd f S )Nz\left(%s\bmod{%s}\right)^{%s}r   r   Trg  r   z%s\bmod{%s})r   r   r   r   r   r?   r?   rB   
_print_Mod  s   zLatexPrinter._print_Modc                    .   |j }td | j dt fdd|S )Nr  z \circ c                        | ddS NTrg  r?   r!   r  precr?   rB   rC         z5LatexPrinter._print_HadamardProduct.<locals>.<lambda>r   r   r   r   rY  r   r   r   r?   r  rB   _print_HadamardProduct     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   r)  )r   r   r/  r?   r?   rB   _print_HadamardPower  s   z!LatexPrinter._print_HadamardPowerc                    r  )Nr  	 \otimes c                    r  r  r?   r  r  r?   rB   rC     r  z6LatexPrinter._print_KroneckerProduct.<locals>.<lambda>r  r  r?   r  rB   _print_KroneckerProduct  r  z$LatexPrinter._print_KroneckerProductc                 C   sT   |j |j}}ddlm} t||sd| || |f S d| || |f S )Nr   rr  r   r%  )r   r   rt  rs  r
  r   )r   r   r   r   rs  r?   r?   rB   _print_MatPow  s   

zLatexPrinter._print_MatPowc                 C   s   | j || jd dS )Nrj   rB  )rD  r   r   r?   r?   rB   _print_MatrixSymbol  s   
z LatexPrinter._print_MatrixSymbolc                 C      | j d dkr	dS dS )Nrj   re   z
\mathbb{0}z
\mathbf{0}r   )r   r&   r?   r?   rB   _print_ZeroMatrix     zLatexPrinter._print_ZeroMatrixc                 C   r  )Nrj   re   z
\mathbb{1}z
\mathbf{1}r  )r   r-   r?   r?   rB   _print_OneMatrix  r  zLatexPrinter._print_OneMatrixc                 C   r  )Nrj   re   z
\mathbb{I}z
\mathbf{I}r  )r   r(   r?   r?   rB   _print_Identity  r  zLatexPrinter._print_Identityc                 C   s   |  |jd }d| S )Nr   zP_{%s}r1  )r   r.   Zperm_strr?   r?   rB   _print_PermutationMatrix  s   z%LatexPrinter._print_PermutationMatrixc              
   C   s  |  dkr| |d S | jd }|d u r2| jd dkr d}n|  dks-|jd dkr0d	}nd
}d}|d|}| jd rS| jd }| j| }d| | d | }|  dkr]|d S g gdd t|  D  }dd |jD }tj| D ]n}|d 	| ||  d}	t|  d ddD ]S}
t
||
d  |j|
 k r nC|	r||
 	d||
d   n(||
 	|d||
d    t
||
d  dkrd||
 d  d ||
 d< |	 }	g ||
d < qqx|d d }|  d dkr|| }|S )Nr   r?   rc   rd   rt   rW  r   rX  rY  rZ  r[  r\  ra   r]  r^  rz   c                 S   s   g | ]}g qS r?   r?   r5  r?   r?   rB   r     s    z1LatexPrinter._print_NDimArray.<locals>.<listcomp>c                 S   s   g | ]}t t|qS r?   )r  r   r5  r?   r?   rB   r   	      Tr   r   r?  rn  ro  r  )rankr   r   shaperT   r   r   	itertoolsproductrS  r   r   )r   r   rc   Z	block_strrd  re  Z	level_strZshape_rangesZouter_iZevenZback_outer_irc  r?   r?   rB   _print_NDimArray  sb   





zLatexPrinter._print_NDimArrayc           	      C   s   |  |}d }d }|D ]M}|j}||v s|r||kr|d7 }||kr6|d ur*|d7 }|jr2|d7 }n|d7 }||  |jd 7 }||v rT|d7 }||  || 7 }d}nd}|}q|d ura|d7 }|S )	Nr   r>   z{}^{z{}_{r   rk  TF)r   is_upr   )	r   r  rZ  	index_maprc  Zlast_valenceZprev_mapindexZnew_valencer?   r?   rB   _printer_tensor_indices#  s2   

z$LatexPrinter._printer_tensor_indicesc                 C   s$   |j d j d }| }| ||S r0  )r   get_indicesr  )r   r   r  rZ  r?   r?   rB   _print_Tensor?  s   zLatexPrinter._print_Tensorc                 C   s0   |j jd jd }|j  }|j}| |||S r0  )r   r   r  r  r  )r   r   r  rZ  r  r?   r?   rB   _print_TensorElementD  s   
z!LatexPrinter._print_TensorElementc                    s*      \}}|d fdd|D  S )Nrz   c                    s   g | ]
} |t qS r?   )r   r   r   r~  r?   rB   r   N      z/LatexPrinter._print_TensMul.<locals>.<listcomp>)Z!_get_args_for_traditional_printerr   )r   r   r<  r   r?   r~  rB   _print_TensMulJ  s   zLatexPrinter._print_TensMulc                 C   sL   g }|j }|D ]}|| |t| q|  d|}|dd}|S )Nr   z+ -r  )r   rS  r   r   rR  r   rT   )r   r   r!  r   r   rA   r?   r?   rB   _print_TensAddQ  s   
zLatexPrinter._print_TensAddc                 C   s"   d|j rdnd| |jd f S )Nz{}%s{%s}rS   _r   )r  r   r   r   r?   r?   rB   _print_TensorIndex[  s   zLatexPrinter._print_TensorIndexc                    sp   t |jdkrd |jd  |jtd df S dt |jd fdd	|jD  |jtd df S )
Nr   z"\frac{\partial}{\partial {%s}}{%s}r   r   Fz\frac{\partial^{%s}}{%s}{%s}ru   c                    r  )z\partial {%s}r   r5  r   r?   rB   r   j  r  z9LatexPrinter._print_PartialDerivative.<locals>.<listcomp>)r   r?  r   r   r   r   r   r   r?   r   rB   _print_PartialDerivativea  s   z%LatexPrinter._print_PartialDerivativec                 C   r\  r   )r   r  r   r?   r?   rB   _print_ArraySymboln  r^  zLatexPrinter._print_ArraySymbolc                    s$   d|j d fdd|jD f S )Nz{{%s}_{%s}}r   c                    s   g | ]}  | qS r?   r   r5  r   r?   rB   r   r  r  z4LatexPrinter._print_ArrayElement.<locals>.<listcomp>)r  r   rZ  r   r?   r   rB   _print_ArrayElementq  s   $z LatexPrinter._print_ArrayElementc                 C   r  )Nz
\mathbb{U}r?   r   r?   r?   rB   _print_UniversalSett  r  z LatexPrinter._print_UniversalSetc                 C   s4   |d u rd|  |jd  S d|  |jd |f S )Nz$\operatorname{frac}{\left(%s\right)}r   z)\operatorname{frac}{\left(%s\right)}^{%s}r1  r   r?   r?   rB   _print_fracw  s
   zLatexPrinter._print_fracc                    sv    j d dkr
d}n j d dkrd}ntdt|dkr*  |d | S  |d	  fd
d|D S )Nro   r   ;rm   r   Unknown Decimal Separatorr   r   z \  c                    r   r?   r   r5  r   r?   rB   r     r   z-LatexPrinter._print_tuple.<locals>.<listcomp>)r   r   r   r   r   r   )r   r   sepr?   r   rB   _print_tuple~  s   zLatexPrinter._print_tuplec                        fdd|j D }d|S )Nc                    r   r?   r   r   r!  r   r?   rB   r     r   z5LatexPrinter._print_TensorProduct.<locals>.<listcomp>r  r   r   r   r   elementsr?   r   rB   _print_TensorProduct     
z!LatexPrinter._print_TensorProductc                    r  )Nc                    r   r?   r   r  r   r?   rB   r     r   z4LatexPrinter._print_WedgeProduct.<locals>.<listcomp>z \wedge r  r  r?   r   rB   _print_WedgeProduct  r  z LatexPrinter._print_WedgeProductc                 C   
   |  |S r   )r  r   r?   r?   rB   _print_Tuple  r   zLatexPrinter._print_Tuplec                    s\    j d dkrdd fdd|D  S  j d dkr*dd fd	d|D  S td
)Nro   r   z\left[ %s\right]z; \  c                    r   r?   r   r5  r   r?   rB   r     r   z,LatexPrinter._print_list.<locals>.<listcomp>rm   , \  c                    r   r?   r   r5  r   r?   rB   r     r   r  )r   r   r   r   r?   r   rB   _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   rS  r   r   )r   r_  r  rP  r  valr?   r?   rB   _print_dict  s    zLatexPrinter._print_dictc                 C   r  r   )r  r   r?   r?   rB   _print_Dict  r   zLatexPrinter._print_Dictc                 C   sj   t |jdks|jd dkrd| |jd  }nd| |jd | |jd f }|r3d||f }|S )Nr   r   z\delta\left(%s\right)z+\delta^{\left( %s \right)}\left( %s \right)r   r  r  r?   r?   rB   _print_DiracDelta  s   zLatexPrinter._print_DiracDeltac                 C   sP   |  |jd |jd  }|  |jd }d||f }|d ur&d|||f }|S )Nr   r   r  z${\left\langle %s \right\rangle}^{%s}z-{\left({\langle %s \rangle}^{%s}\right)}^{%s}r1  )r   r   r   shiftr  r   r?   r?   rB   _print_SingularityFunction  s   z'LatexPrinter._print_SingularityFunctionc                    s6   d  fdd|jD }d| }|rd||f }|S )Nr   c                 3   ri  r   r   r   r   r?   rB   r    r  z0LatexPrinter._print_Heaviside.<locals>.<genexpr>z\theta\left(%s\right)r   )r   pargs)r   r   r   r  r   r?   r   rB   _print_Heaviside  s
   zLatexPrinter._print_Heavisidec                 C   sj   |  |jd }|  |jd }|jd jr#|jd jr#d||f }nd||f }|d ur3d||f }|S )Nr   r   z\delta_{%s %s}z\delta_{%s, %s}r   )r   r   r   )r   r   r   rl   r   r   r?   r?   rB   _print_KroneckerDelta  s   z"LatexPrinter._print_KroneckerDeltac                 C   sT   t | j|j}tdd |jD rdd| }ndd| }|r(d||f }|S )Nc                 s   s    | ]}|j V  qd S r   )r   r   r?   r?   rB   r    s    z1LatexPrinter._print_LeviCivita.<locals>.<genexpr>z\varepsilon_{%s}ru   r   r   )rY  r   r   ry  r   )r   r   r   rZ  r   r?   r?   rB   _print_LeviCivita  s   zLatexPrinter._print_LeviCivitac                 C   sj   t |drd| |  S t |dr#d| |j d | |j S t |dr0d| |j S | d S )N
as_booleanz\text{Domain: }setz\text{ in }r{  z\text{Domain on })r  r   r  r{  r  )r   r_  r?   r?   rB   _print_RandomDomain  s   




z LatexPrinter._print_RandomDomainc                 C   s   t |jtd}| |S )Nr  )r  r   r   
_print_setr   rA   rP  r?   r?   rB   _print_FiniteSet  s   
zLatexPrinter._print_FiniteSetc                 C   sd   t |td}| jd dkrdt| j|}d| S | jd dkr.dt| j|}d| S td)	Nr  ro   r   r>  rm   r   r  \left\{%s\right\})r  r   r   r   rY  r   r   r  r?   r?   rB   r    s   zLatexPrinter._print_setc                    s   t   |tr|S |jjr)|jjr)|jjr! ddd f}nF ddd f}n>|jjr: |d |j |d f}n-|jjrLt	|}t
|t
| f}nt|dkrct	|}t
|t
| |d f}nt|}dd fdd|D  d	 S )
Nr   r   r   rq  z\left\{r   c                 3   &    | ]}| ur |nd V  qdS z\ldotsNr   r   eldotsr   r?   rB   r    rl  z,LatexPrinter._print_Range.<locals>.<genexpr>z\right\})objectr   r
   r   startZis_infinitestopstepZis_positiveiternextr   r:  r   )r   rA   printsetitr?   r  rB   _print_Range  s*   

zLatexPrinter._print_Rangec                 C   s   t |jdkr2|d urd|| |jd || |jd f S d|| |jd | |jd f S d|| |jd f }|d urHd||f }|S )Nr  z%s_{%s}^{%s}\left(%s\right)r   r   r  z%s_{%s}r%  r  )r   r   Zletterr   r   r?   r?   rB   Z__print_number_polynomial  s   z&LatexPrinter.__print_number_polynomialc                 C      |  |d|S Nr#   &_LatexPrinter__print_number_polynomialr   r?   r?   rB   _print_bernoulli"  r   zLatexPrinter._print_bernoullic                    s   t |jdkr=d |jd  |jd f }dd fdd|jd	 D  }|d ur7d
|||f }|S || }|S  |d|S )NrO  z
B_{%s, %s}r   r   r   r   c                 3   ri  r   r   r  r   r?   rB   r  )  s    z+LatexPrinter._print_bell.<locals>.<genexpr>r  r  r#   )r   r   r   r   r  )r   r   r   Ztex1Ztex2r   r?   r   rB   _print_bell%  s   
zLatexPrinter._print_bellc                 C   r  NFr  r   r?   r?   rB   _print_fibonacci3  r   zLatexPrinter._print_fibonaccic                 C   ,   d|  |jd  }|d urd||f }|S )NzL_{%s}r   r%  r1  r  r?   r?   rB   _print_lucas6     zLatexPrinter._print_lucasc                 C   r  )Nr/   r  r   r?   r?   rB   _print_tribonacci<  r   zLatexPrinter._print_tribonaccic                    s   t   t|jjdkst|jjdkr-d|j|jd |j|jf S |jtj	u rP|j} |
|d |
|d |
|d |
|f}n|jtju s[|jdkrg|d d }|  nt|}dd fd	d
|D  d S )Nr   z\left\{%s\right\}_{%s=%s}^{%s}rO  r  r   rq  rn  r   c                 3   r  r  r   r  r  r?   rB   r  S  rl  z1LatexPrinter._print_SeqFormula.<locals>.<genexpr>ro  )r  r   r  Zfree_symbolsr  r   Zformular?  r	   r  coeffr~  lengthrS  r:  r   )r   rA   r  r  r?   r  rB   _print_SeqFormula?  s,    


zLatexPrinter._print_SeqFormulac                 C   s\   |j |jkrd| |j  S |jrd}nd}|jrd}nd}d|| |j | |j|f S )Nr  r~   rb   r|   r}   z\left%s%s, %s\right%s)r  endr   Z	left_openZ
right_open)r   rl   leftrightr?   r?   rB   _print_IntervalZ  s   zLatexPrinter._print_Intervalc                 C   r  )Nz \left\langle %s, %s\right\rangle)r   rr   rs   r   rl   r?   r?   rB   _print_AccumulationBoundsl  s   z&LatexPrinter._print_AccumulationBoundsc                    (   t |  fdd|jD }d|S )Nc                       g | ]} | qS r?   r   r5  r  r   r?   rB   r   r  r  z-LatexPrinter._print_Union.<locals>.<listcomp>z \cup r   r   r   r   uargs_strr?   r  rB   _print_Unionp     
zLatexPrinter._print_Unionc                    r  )Nc                    r  r?   r  r5  r  r?   rB   r   w  r  z2LatexPrinter._print_Complement.<locals>.<listcomp>z \setminus r  r  r?   r  rB   _print_Complementu  r  zLatexPrinter._print_Complementc                    r  )Nc                    r  r?   r  r5  r  r?   rB   r   |  r  z4LatexPrinter._print_Intersection.<locals>.<listcomp>z \cap r  r  r?   r  rB   _print_Intersectionz  r  z LatexPrinter._print_Intersectionc                    r  )Nc                    r  r?   r  r5  r  r?   rB   r     r  z;LatexPrinter._print_SymmetricDifference.<locals>.<listcomp>z \triangle r  r  r?   r  rB   _print_SymmetricDifference  r  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}rv   c                 3   s    | ]	} | V  qd S r   r  )r   r  r  r?   rB   r    s    
z1LatexPrinter._print_ProductSet.<locals>.<genexpr>)r   r   setsr   r   r   r   r&  r?   r  rB   _print_ProductSet  s    zLatexPrinter._print_ProductSetc                 C   r  )Nz	\emptysetr?   r   r?   r?   rB   _print_EmptySet  r  zLatexPrinter._print_EmptySetc                 C   r  )Nz
\mathbb{N}r?   r   r  r?   r?   rB   _print_Naturals  r  zLatexPrinter._print_Naturalsc                 C   r  )Nz\mathbb{N}_0r?   r	  r?   r?   rB   _print_Naturals0  r  zLatexPrinter._print_Naturals0c                 C   r  Nz
\mathbb{Z}r?   r  r?   r?   rB   _print_Integers  r  zLatexPrinter._print_Integersc                 C   r  Nz
\mathbb{Q}r?   r  r?   r?   rB   _print_Rationals  r  zLatexPrinter._print_Rationalsc                 C   r  Nz
\mathbb{R}r?   r  r?   r?   rB   _print_Reals  r  zLatexPrinter._print_Realsc                 C   r  Nz
\mathbb{C}r?   r  r?   r?   rB   _print_Complexes  r  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   r?   rB   r    s   & z/LatexPrinter._print_ImageSet.<locals>.<genexpr>z , c                 s   s    | ]}d | V  qdS )	%s \in %sNr?   )r   Zxyr?   r?   rB   r    rc  z!\left\{%s\; \middle|\; %s\right\})r2   r   Z	signaturerm  Z	base_setsr   r   )r   rA   r   ZsigZxysZxinysr?   r   rB   _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                    r   r?   r   r   r   r   r?   rB   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   Z	conditionr   rA   Z
vars_printr?   r   rB   _print_ConditionSet  s   

z LatexPrinter._print_ConditionSetc                    s8   d  fdd|jD }d |j| |jf S )Nr   c                    r   r?   r   r  r   r?   rB   r     r   z5LatexPrinter._print_ComplexRegion.<locals>.<listcomp>z)\left\{%s\; \middle|\; %s \in %s \right\})r   r?  r   r   r  r  r?   r   rB   _print_ComplexRegion  s   

z!LatexPrinter._print_ComplexRegionc                    s   dt  fdd|jD  S )Nr  c                 3   ri  r   r   r  r   r?   rB   r    r  z/LatexPrinter._print_Contains.<locals>.<genexpr>)r:  r   r   r?   r   rB   _print_Contains     zLatexPrinter._print_Containsc                 C   s   |  | d S )Nz	 + \ldots)r   truncater   r?   r?   rB   _print_FourierSeries  r  z!LatexPrinter._print_FourierSeriesc                 C   r\  r   )r   Zinfiniter   r?   r?   rB   _print_FormalPowerSeries  r^  z%LatexPrinter._print_FormalPowerSeriesc                 C   s
   d|j  S )Nz\mathbb{F}_{%s})modr   r?   r?   rB   _print_FiniteField  r   zLatexPrinter._print_FiniteFieldc                 C   r  r  r?   r   r?   r?   rB   _print_IntegerRing  r  zLatexPrinter._print_IntegerRingc                 C   r  r  r?   r   r?   r?   rB   _print_RationalField  r  z!LatexPrinter._print_RationalFieldc                 C   r  r  r?   r   r?   r?   rB   _print_RealField  r  zLatexPrinter._print_RealFieldc                 C   r  r  r?   r   r?   r?   rB   _print_ComplexField  r  z LatexPrinter._print_ComplexFieldc                 C   ,   |  |j}dt| j |j}d||f S )Nr   z%s\left[%s\right]r   domainr   rY  r{  r   r   r'  r{  r?   r?   rB   _print_PolynomialRing     z"LatexPrinter._print_PolynomialRingc                 C   r%  )Nr   r}  r&  r(  r?   r?   rB   _print_FractionField  r*  z!LatexPrinter._print_FractionFieldc                 C   s<   |  |j}dt| j |j}d}|jsd}d|||f S )Nr   rz   zS_<^{-1}z%s%s\left[%s\right])r   r'  r   rY  r{  Zis_Poly)r   r   r'  r{  invr?   r?   rB   _print_PolynomialRingBase  s   z&LatexPrinter._print_PolynomialRingBasec                 C   s  |j j}g }| D ]\}}d}t|D ]$\}}|dkr8|dkr+|| |j| 7 }q|| t|j| |7 }q|jrL|rFd| | }	n'| |}	n!|rh|tj	u r[|
d|g q
|tju rh|
d|g q
| |}	|sr|	}
n|	d | }
|
dr|
d|
dd  g q
|
d|
g q
|d dv r|d}|dkrd|d  |d< d|}tt| j|j}d	| |  }d
|g| |g }|tv rd||f }|S d||f }|S )Nrz   r   r   r   r   rN  ru   )rN  r   z	domain=%sr   z\%s {\left(%s \right)}z$\operatorname{%s}{\left( %s \right)})r   r   r   r   r   genspowr   r	   r  extendr   r-  popr   r  rY  Z
get_domainr  )r   polyr  r   Zmonomr  Zs_monomrl   r   Zs_coeffZs_termmodifierr   r.  r'  r   r   r?   r?   rB   _print_Poly  sR   





zLatexPrinter._print_Polyc                 C   sJ   |j j}|dkr
d}| |j}|j}|tv rd|||f S d|||f 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  r?   r?   rB   _print_ComplexRootOf	  s   z!LatexPrinter._print_ComplexRootOfc                 C   s`   |j j}| |jg}|jtjur|| |j |tv r'd|d	|f S d|d	|f S )Nz\%s {\left(%s\right)}r   z#\operatorname{%s} {\left(%s\right)})
r   r   r   r   Zfunr	   ZIdentityFunctionrS  r  r   )r   r   r  r   r?   r?   rB   _print_RootSum)	  s   zLatexPrinter._print_RootSumc                 C   s   | j d }|| td|S )Nrx   z	{%s}^{%d})r   r   r   )r   r2  rf   r?   r?   rB   _print_PolyElement6	  s   
zLatexPrinter._print_PolyElementc                 C   s:   |j dkr| |jS | |j}| |j }d||f S )Nr   r  )r   r   r  )r   r   r  r   r?   r?   rB   _print_FracElement:	  s
   
zLatexPrinter._print_FracElementc                 C   sf   t |jdkr|jd d fn|j\}}d| | }|d ur$d||f }|d ur1d|| |f }|S )Nr   r   zE_{%s}r%  r}  r  )r   r   r   r,  r   r   r?   r?   rB   _print_eulerB	  s   &zLatexPrinter._print_eulerc                 C   r  )NzC_{%s}r   r%  r1  r  r?   r?   rB   _print_catalanK	  r  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}rz   r   r   r  )r   r   r   )r   r   rA   Zinverser?   r?   rB   _print_UnifiedTransformQ	  s   >z$LatexPrinter._print_UnifiedTransformc                 C      |  |dS )Nr*   r;  r   r?   r?   rB   _print_MellinTransformT	  r^  z#LatexPrinter._print_MellinTransformc                 C      |  |ddS )Nr*   Tr=  r   r?   r?   rB   _print_InverseMellinTransformW	  r   z*LatexPrinter._print_InverseMellinTransformc                 C   r<  )NLr=  r   r?   r?   rB   _print_LaplaceTransformZ	  r^  z$LatexPrinter._print_LaplaceTransformc                 C   r?  )NrA  Tr=  r   r?   r?   rB   _print_InverseLaplaceTransform]	  r   z+LatexPrinter._print_InverseLaplaceTransformc                 C   r<  r  r=  r   r?   r?   rB   _print_FourierTransform`	  r^  z$LatexPrinter._print_FourierTransformc                 C   r?  )Nr  Tr=  r   r?   r?   rB   _print_InverseFourierTransformc	  r   z+LatexPrinter._print_InverseFourierTransformc                 C   r<  )NSINr=  r   r?   r?   rB   _print_SineTransformf	  r^  z!LatexPrinter._print_SineTransformc                 C   r?  )NrF  Tr=  r   r?   r?   rB   _print_InverseSineTransformi	  r   z(LatexPrinter._print_InverseSineTransformc                 C   r<  )NCOSr=  r   r?   r?   rB   _print_CosineTransforml	  r^  z#LatexPrinter._print_CosineTransformc                 C   r?  )NrI  Tr=  r   r?   r?   rB   _print_InverseCosineTransformo	  r   z*LatexPrinter._print_InverseCosineTransformc                 C   sD   z|j d ur| |j |W S W n	 ty   Y nw | t|S r   )ringr   Zto_sympyr   reprr  r?   r?   rB   
_print_DMPr	  s   
zLatexPrinter._print_DMPc                 C   r  r   )rN  r  r?   r?   rB   
_print_DMF{	  r   zLatexPrinter._print_DMFc                 C   s   |  t|jS r   r   r
   r  )r   r  r?   r?   rB   _print_Object~	  r3  zLatexPrinter._print_Objectc                 C   sf   |  |jd }|d urd|f nd}t|jdkr"d||f }|S |  |jd }d|||}|S )Nr   rx  rz   r   zW%s\left(%s\right)zW{0}_{{{1}}}\left({2}\right))r   r   r   r   )r   r   r   Zarg0resultZarg1r?   r?   rB   _print_LambertW	  s   zLatexPrinter._print_LambertWc                 C   s$   |  |j}|  |j}d||f S )Nz%s\rightarrow %s)r   r'  codomain)r   morphismr'  rT  r?   r?   rB   _print_Morphism	  s   zLatexPrinter._print_Morphismc                 C   s&   |  |j|  |j}}d||f S )Nr  )r   rg  den)r   r   rg  rW  r?   r?   rB   _print_TransferFunction	  s   z$LatexPrinter._print_TransferFunctionc                    s(   t  j} fdd}dt||S )Nc                    r{  r|  r}  r  r~  r?   rB   rC   	  r  z,LatexPrinter._print_Series.<locals>.<lambda>ru   r  r   r   rY  r   r   r   r  r?   r~  rB   _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 r|  )r
  r   r   r   r  r\  r   r   r?   rB   rC   	  s
   
z0LatexPrinter._print_MIMOSeries.<locals>.<lambda>z\cdot)Zsympy.physics.control.ltir\  r  r   r   rY  rZ  r?   r]  rB   _print_MIMOSeries	  s   zLatexPrinter._print_MIMOSeriesc                    &   t |j} fdd}dt||S )Nc                    
     | S r   r   r  r   r?   rB   rC   	     
 z.LatexPrinter._print_Parallel.<locals>.<lambda>r   rY  r   r   r   r  r?   r   rB   _print_Parallel	     
zLatexPrinter._print_Parallelc                    r_  )Nc                    r`  r   r   r  r   r?   rB   rC   	  ra  z2LatexPrinter._print_MIMOParallel.<locals>.<lambda>r   rY  rb  r?   r   rB   _print_MIMOParallel	  rd  z LatexPrinter._print_MIMOParallelc                 C   s  ddl m}m} |j|dd|j}}t||rt|jn|g}t|j|r,t|jjn|jg}|}t||rGt|j|rG|g ||R  }	n[t||rit|j|ri|j|kr\|| }	nF||g ||jR  f}	n9t||rt|j|r||kr}|| }	n%||g|R  }	n||kr|| }	n|j|kr|| }	n	|g ||R  }	| 	|}
| 	|}| 	|	}|j
dkrdnd}d|
|||f S )Nr   )TransferFunctionSeriesr   r   r   rN  z\frac{%s}{%s %s %s})sympy.physics.controlrf  rg  sys1r   r
  r  r   sys2r   r<  )r   r   rf  rg  rg  ZtfZnum_arg_listZden_arg_listZ
den_term_1Z
den_term_2r  Zdenom_1Zdenom_2_signr?   r?   rB   _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   rN  z)\left(I_{\tau} %s %s\right)^{-1} \cdot %s)rh  rm  r   rj  ri  r<  )r   r   rm  Zinv_matri  rk  r?   r?   rB   _print_MIMOFeedback	  s
   z LatexPrinter._print_MIMOFeedbackc                 C   s   |  |j}d| S )Nz%s_\tau)r   Z	_expr_matrw  r?   r?   rB   _print_TransferFunctionMatrix	  s   z*LatexPrinter._print_TransferFunctionMatrixc                 C   s&   |  t|j}| |}d||f S )Nz%s:%s)r   r
   r  rV  )r   rU  Zpretty_namepretty_morphismr?   r?   rB   _print_NamedMorphism	  s   
z!LatexPrinter._print_NamedMorphismc                 C   s"   ddl m} | ||j|jdS )Nr   )NamedMorphismid)Zsympy.categoriesrr  rq  r'  rT  )r   rU  rr  r?   r?   rB   _print_IdentityMorphism	  s   
z$LatexPrinter._print_IdentityMorphismc                    s<    fdd|j D }|  d|d } |}|| S )Nc                    s   g | ]
}  t|jqS r?   rP  )r   Z	componentr   r?   rB   r   	  s    z9LatexPrinter._print_CompositeMorphism.<locals>.<listcomp>z\circ rj  )rQ  reverser   rV  )r   rU  Zcomponent_names_listZcomponent_namesrp  r?   r   rB   _print_CompositeMorphism	  s   

z%LatexPrinter._print_CompositeMorphismc                 C   s   d | t|jS NrJ  )r   r   r
   r  )r   rU  r?   r?   rB   _print_Category	  s   zLatexPrinter._print_Categoryc                 C   s<   |j s	| tjS | |j }|jr|d| |j 7 }|S )Nz\Longrightarrow %s)Zpremisesr   r	   ZEmptySetZconclusions)r   Zdiagramlatex_resultr?   r?   rB   _print_Diagram	  s   
zLatexPrinter._print_Diagramc                 C   s   dd|j   }t|jD ]8}t|j D ]!}|||f r%|t|||f 7 }|d7 }||j d kr4|d7 }q||jd kr@|d7 }|d7 }q|d7 }|S )	Nz\begin{array}{%s}
rU   ru   r   & r?  
z\end{array}
)widthr   Zheightlatex)r   Zgridry  rl   r   r?   r?   rB   _print_DiagramGrid	  s   
zLatexPrinter._print_DiagramGridc                 C      d | |j| |jS )Nz{{{}}}^{{{}}})r   r   rL  r  r   r*   r?   r?   rB   _print_FreeModule
  r  zLatexPrinter._print_FreeModulec                    s   d d fdd|D S )Nz\left[ {} \right]r   c                 3   "    | ]}d   | d V  qdS rK   r>   Nr   r   r   r?   rB   r  
      
z8LatexPrinter._print_FreeModuleElement.<locals>.<genexpr>)r   r   r   r,  r?   r   rB   _print_FreeModuleElement
  s   
z%LatexPrinter._print_FreeModuleElementc                    s    d d fdd|jD S )N\left\langle {} \right\rangler   c                 3   r  r  r   r   r   r?   rB   r  
  r  z0LatexPrinter._print_SubModule.<locals>.<genexpr>)r   r   r.  r  r?   r   rB   _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   r?   rB   r  
  rl  z=LatexPrinter._print_ModuleImplementedIdeal.<locals>.<genexpr>)r   r   _moduler.  r  r?   r   rB   _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   Trg  )r   r   r5  r   r?   rB   r   #
  s    z2LatexPrinter._print_Quaternion.<locals>.<listcomp>r   c                 S   s   g | ]
\}}|d  | qS )ru   r?   )r   rl   r   r?   r?   rB   r   %
  r  r   Zijkr   )r   rm  r   )r   r   rA   r!  r?   r   rB   _print_Quaternion 
  s
   
&
zLatexPrinter._print_Quaternionc                 C   r  Nz\frac{{{}}}{{{}}})r   r   rL  
base_ideal)r   Rr?   r?   rB   _print_QuotientRing(
     
z LatexPrinter._print_QuotientRingc                 C      d | |j| |jjS Nz{{{}}} + {{{}}})r   r   datarL  r  )r   r   r?   r?   rB   _print_QuotientRingElement-
     z'LatexPrinter._print_QuotientRingElementc                 C   r  r  )r   r   r  modulekilled_moduler  r?   r?   rB   _print_QuotientModuleElement1
  r  z)LatexPrinter._print_QuotientModuleElementc                 C   r  r  )r   r   r   r  r  r?   r?   rB   _print_QuotientModule5
  r  z"LatexPrinter._print_QuotientModulec                 C   s(   d | | | |j| |jS )Nz{{{}}} : {{{}}} \to {{{}}})r   r   Z_sympy_matrixr'  rT  )r   hr?   r?   rB   _print_MatrixHomomorphism:
  s   z&LatexPrinter._print_MatrixHomomorphismc                 C   s   |j j }d|v r|g g }}}nt|\}}}t|}dd |D }dd |D }d| }|r9|dd| 7 }|rD|dd| 7 }|S )	NrK   c                 S   rE  r?   rF  rH  r?   r?   rB   r   F
  rC  z0LatexPrinter._print_Manifold.<locals>.<listcomp>c                 S   rE  r?   rF  rI  r?   r?   rB   r   G
  rC  r   rx  ru   rX  )r  r   rG  r   )r   manifoldrK  r  rL  rn  r?   r?   rB   _print_Manifold>
  s   zLatexPrinter._print_Manifoldc                 C   r  )Nz\text{%s}_{%s})r   r  r  )r   patchr?   r?   rB   _print_PatchQ
  r  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coordsysr?   r?   rB   _print_CoordSystemT
  s    zLatexPrinter._print_CoordSystemc                 C   s   d|  |j S )Nz\mathbb{\nabla}_{%s})r   Z_wrt)r   Zcvdr?   r?   rB   _print_CovarDerivativeOpY
  r3  z%LatexPrinter._print_CovarDerivativeOpc                 C   $   |j j|j j}d| t|S rw  
_coord_sysr{  _indexr  r   r   r
   r   fieldrK  r?   r?   rB   _print_BaseScalarField\
     z#LatexPrinter._print_BaseScalarFieldc                 C   r  )Nz\partial_{{{}}}r  r  r?   r?   rB   _print_BaseVectorField`
  r  z#LatexPrinter._print_BaseVectorFieldc                 C   sH   |j }t|dr|jj|j j}d| t|S | |}d|S )Nr  z\operatorname{{d}}{}z!\operatorname{{d}}\left({}\right))	Z_form_fieldr  r  r{  r  r  r   r   r
   )r   Zdiffr  rK  r?   r?   rB   _print_Differentiald
  s   


z LatexPrinter._print_Differentialc                 C   s   |  |jd }d|S )Nr   z"\operatorname{{tr}}\left({}\right))r   r   r   )r   r&  contentsr?   r?   rB   	_print_Trm
  s   
zLatexPrinter._print_Trc                 C   4   |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)r1  r   r?   r?   rB   _print_totientr
  
   zLatexPrinter._print_totientc                 C   r  )Nz(\left(\lambda\left(%s\right)\right)^{%s}r   z\lambda\left(%s\right)r1  r   r?   r?   rB   _print_reduced_totientx
  r  z#LatexPrinter._print_reduced_totientc                 C   d   t |jdkrdtt| j|jd |jd f }n
d| |jd  }|d ur.d||f S d| S )Nr  _%s\left(%s\right)r   r   r   z\sigma^{%s}%sz\sigma%sr  r  r?   r?   rB   _print_divisor_sigma~
     

z!LatexPrinter._print_divisor_sigmac                 C   r  )Nr  r  r   r   r   z\sigma^*^{%s}%sz
\sigma^*%sr  r  r?   r?   rB   _print_udivisor_sigma
  r  z"LatexPrinter._print_udivisor_sigmac                 C   r  )Nz$\left(\nu\left(%s\right)\right)^{%s}r   z\nu\left(%s\right)r1  r   r?   r?   rB   _print_primenu
  r  zLatexPrinter._print_primenuc                 C   r  )Nz'\left(\Omega\left(%s\right)\right)^{%s}r   z\Omega\left(%s\right)r1  r   r?   r?   rB   _print_primeomega
  r  zLatexPrinter._print_primeomegac                 C   s
   t |jS r   )r   r  r   r?   r?   rB   
_print_Str
  r   zLatexPrinter._print_Strc                 C   r  r   )r   r   r   r?   r?   rB   _print_float
  r   zLatexPrinter._print_floatc                 C      t |S r   r   r   r?   r?   rB   
_print_int
  r   zLatexPrinter._print_intc                 C   r  r   r  r   r?   r?   rB   
_print_mpz
  r   zLatexPrinter._print_mpzc                 C   r  r   r  r   r?   r?   rB   
_print_mpq
  r   zLatexPrinter._print_mpqc                 C   r  r   r  r   r?   r?   rB   _print_Predicate
  r   zLatexPrinter._print_Predicatec                    s:   |j }|j} |}d fdd|D }d||f S )Nr   c                    r   r?   r   r  r   r?   rB   r   
  r   z8LatexPrinter._print_AppliedPredicate.<locals>.<listcomp>z%s(%s))rF  Z	argumentsr   r   )r   r   Zpredr   Z
pred_latexZ
args_latexr?   r   rB   _print_AppliedPredicate
  s
   
z$LatexPrinter._print_AppliedPredicatec                    s   t  |}dt| S )Nz\mathtt{\text{%s}})superemptyPrinterrV   r@  r   r?   rB   r  
  s   zLatexPrinter.emptyPrinterr   )FFr  rk  )FN)re   )F(-  r   
__module____qualname__ZprintmethodZ_default_settingsr   r   r   r   rq   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*  r)  r2  rI  rK  rW  r[  r]  rh  rp  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.  r4  r5  r6  r9  r;  r=  rA  rD  Z_print_RandomSymbolr   rT  rV  rf  ri  rp  rq  rv  rx  ry  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-  r4  r5  r6  r7  r8  r9  r:  r;  r>  r@  rB  rC  rD  rE  rG  rH  rJ  rK  rN  rO  rQ  rS  rV  rX  r[  r^  rc  re  rl  rn  ro  rq  rt  rv  rx  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __classcell__r?   r?   r  rB   rW      s   	
8

s2!
#L

										
	4



9		
$	

rW   c                 C   s   t | }|r	|S |  tv rd|   S | tv rd|  S tt dd ddD ]"}|  |rJt	| t	|krJt| t
| dt	|    S q(| 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}}}'"
    rQ   c                 S   s   t | S r   rr  )rU  r?   r?   rB   rC   
  s    ztranslate.<locals>.<lambda>T)r  ru  N)tex_greek_dictionaryr   r   greek_letters_setother_symbolsr  modifier_dictr  r.  r   rG  )rA   r   r  r?   r?   rB   rG  
  s   
"rG  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.

    )rW   r   r   r   r?   r?   rB   r~  
  s    Mr~  c                 K   s   t t| fi | dS )z`Prints LaTeX representation of the given expression. Takes the same
    settings as ``latex()``.N)printr~  r  r?   r?   rB   print_latex  s   r  r   align*Fc              
   K   s~  t di |}|dkrd}d}d}	d}
d}n%|dkr%d}d}d}	d	}
d}n|d
kr4d}d}d}	d}
d}ntd|d}|rAd}| }t|}d}t|D ]i}|| }d}d}d}||krh|rdd}nd}d}||kr}||d k r{||	 d d }nd}| d dkrd| }d}|dkr|dkrd}|d|| |||||7 }n|d|||||7 }|d7 }qO||
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= &rz   z
\end{align*}FzUnknown environment: {}z\dotsr   r   z& & r{  r?  r|  r   r   rN  z{:s} {:s}{:s} {:s} {:s}z{:s}{:s} {:s} {:s}Nr?   )rW   r   r   Zas_ordered_termsr   r   r  r   )rO  rP  Zterms_per_lineZenvironmentZuse_dotsr   r<  rR  Z
first_termZnonumberZend_termZdoubleetr  r   Zn_termsZ
term_countrl   r   Z
term_startZterm_endr<  r?   r?   rB   multiline_latex  sn   C


r  )r   r  F)<__doc__typingr   r   r  Z
sympy.corer   r   r   r   r   r	   r
   Zsympy.core.alphabetsr   Zsympy.core.containersr   Zsympy.core.functionr   r   r   Zsympy.core.operationsr   Zsympy.core.sympifyr   Zsympy.logic.boolalgr   Zsympy.printing.precedencer   Zsympy.printing.printerr   r   Zsympy.printing.conventionsr   r   r   r   Zmpmath.libmpZlibmpr   r   Zsympy.core.compatibilityr   Zsympy.utilities.iterablesr   r   r  r  r  r  	frozensetr  compiler  rV   rW   rG  r~  r  r  r?   r?   r?   rB   <module>   sP   $	
 !"#'	
                    I
 O