o
    Th                 	   @  s  U d Z ddlmZ ddlmZ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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#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/m0Z0m1Z2 ddl3m4Z4m5Z5 ddl6Z6erddl7m8Z8 ddl9m: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/d0d1d2d3d4d5d6d7d8i d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKd,dLdMdNdDdOdPdQdRdSdTdUdVdWdXdYdZd[Z<h d\Z=i 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_ dd_ dd_ dd_ dd_ dd_ dd_ dd_ dZ>de?d< e@eZAe6Bde6BdfZCdddZDG dd de(ZEdddZFe)eEdd ZGdd ZHdddZIdS )zC
A Printer which converts an expression into its LaTeX equivalent.
    )annotations)AnyCallableTYPE_CHECKINGN)AddFloatModMulNumberSSymbolExpr)greeks)Tuple)FunctionAppliedUndef
Derivative)AssocOp)Pow)default_sort_key)SympifyError)trueBooleanTrueBooleanFalse)precedence_traditional)Printerprint_function)split_super_subrequires_partial)
precedence
PRECEDENCE)prec_to_dpsto_str)has_varietysift)	NDimArray)BasisDependent)arcsinarccosarctansincostansinhcoshtanhsqrtlnlogseccsccotcothreimfracrootargAlphaz
\mathrm{A}Betaz
\mathrm{B}Gamma\GammaDeltaz\DeltaEpsilonz
\mathrm{E}Zetaz
\mathrm{Z}Etaz
\mathrm{H}Thetaz\ThetaIotaz
\mathrm{I}Kappaz
\mathrm{K}Lambdaz\LambdaMuz
\mathrm{M}Nuz
\mathrm{N}Xiz\XiomicronoOmicronz
\mathrm{O}Piz\PiRhoz
\mathrm{P}Sigmaz\SigmaTauz
\mathrm{T}Upsilonz\UpsilonPhiz\PhiChiz
\mathrm{X}Psiz\PsiOmegaz\Omegalamdaz\lambdaLamdakhiz\chiKhi
varepsilonz\varepsilonvarkappaz	\varkappavarphiz\varphivarpiz\varpivarrhoz\varrhoz	\varsigmaz	\vartheta)varsigmavartheta>
   wpellethmhobethhbaralephgimeldalethhslashmathringc                 C     d|  d S )Nz
\mathring{} sro   ro   U/home/air/segue/gemini/back/venv/lib/python3.10/site-packages/sympy/printing/latex.py<lambda>[       rs   ddddotc                 C  rm   )Nz\ddddot{rn   ro   rp   ro   ro   rr   rs   \   rt   dddotc                 C  rm   )Nz\dddot{rn   ro   rp   ro   ro   rr   rs   ]   rt   ddotc                 C  rm   )Nz\ddot{rn   ro   rp   ro   ro   rr   rs   ^   rt   dotc                 C  rm   )Nz\dot{rn   ro   rp   ro   ro   rr   rs   _   rt   checkc                 C  rm   )Nz\check{rn   ro   rp   ro   ro   rr   rs   `   rt   brevec                 C  rm   )Nz\breve{rn   ro   rp   ro   ro   rr   rs   a   rt   acutec                 C  rm   )Nz\acute{rn   ro   rp   ro   ro   rr   rs   b   rt   gravec                 C  rm   )Nz\grave{rn   ro   rp   ro   ro   rr   rs   c   rt   tildec                 C  rm   )Nz\tilde{rn   ro   rp   ro   ro   rr   rs   d   rt   hatc                 C  rm   )Nz\hat{rn   ro   rp   ro   ro   rr   rs   e   rt   barc                 C  rm   )Nz\bar{rn   ro   rp   ro   ro   rr   rs   f   rt   vecc                 C  rm   )Nz\vec{rn   ro   rp   ro   ro   rr   rs   g   rt   primec                 C  rm   N{z}'ro   rp   ro   ro   rr   rs   h   rt   prmc                 C  rm   r   ro   rp   ro   ro   rr   rs   i   rt   boldc                 C  rm   Nz\boldsymbol{rn   ro   rp   ro   ro   rr   rs   k   rt   bmc                 C  rm   r   ro   rp   ro   ro   rr   rs   l   rt   c                 C  rm   )Nz	\mathcal{rn   ro   rp   ro   ro   rr   rs   m   rt   c                 C  rm   )Nz	\mathscr{rn   ro   rp   ro   ro   rr   rs   n   rt   c                 C  rm   )Nz
\mathfrak{rn   ro   rp   ro   ro   rr   rs   o   rt   c                 C  rm   )Nz\left\|{z	}\right\|ro   rp   ro   ro   rr   rs   q   rt   c                 C  rm   )Nz\left\langle{z}\right\ranglero   rp   ro   ro   rr   rs   r   rt   c                 C  rm   Nz\left|{z}\right|ro   rp   ro   ro   rr   rs   s   rt   c                 C  rm   r   ro   rp   ro   ro   rr   rs   t   rt   )calscrfraknormavgabsmagzdict[str, Callable[[str], str]]modifier_dictz[0-9][} ]*$z(\d|\\frac{\d+}{\d+})rq   strreturnc                 C  sB   |  dd} dD ]
}|  |d| } q|  dd} |  dd} | S )z
    Escape a string such that latex interprets it as plaintext.

    We cannot use verbatim easily with mathjax, so escaping is easier.
    Rules from https://tex.stackexchange.com/a/34586/41112.
    \z\textbackslashz&%$#_{}~z\textasciitilde^z\textasciicircum)replace)rq   cro   ro   rr   latex_escape   s   r   c                      s  e Zd ZU 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ddd	Zded< dd d!Zdd$d%Zdd&d'Zddd(d)Z	d*d+ Z
dd,d-Zdd/d0Zdd1d2Zddd3d4Zdd5d6Zdd7d8Zdd9d:Zdd<d=Zd>d? ZddBdCZeZeZdDdE ZddFdGZdHdI ZdJdK ZdLdM ZdNdO ZdPdQ ZdRdS ZdTdU ZdVdW Z dXdY Z!dZd[ Z"dd]d^Z#d_d` Z$dadb Z%ddddeZ&ddgdhZ'didj Z(dkdl Z)dmdn Z*ddpdqZ+drds Z,dtdu Z-dvdw Z.dxdy Z/dzd{ Z0d|d} Z1d~d Z2dddZ3ddddZ4dd Z5dd Z6e7dd Z8dd Z9dd Z:dd Z;ddddZ<e< Z=Z>dddZ?dddZ@dddZAdddZBdddZCdddZDdd ZEdd ZFdd ZGdd ZHdd ZIdddZJdddZKdddZLdddZMdddZNdddZOdddZPdddZQdddZRdddZSdddZTdddZUdddĄZVdddƄZWdddȄZXddddʄZYeYZZddd̄Z[ddd΄Z\dddЄZ]ddd҄Z^dddԄZ_dddքZ`ddd؄ZadddڄZbddd܄ZcdddބZddddZedddZfdddZgdddZhdddZidddZjdddZkdddZldddZmdddZndddZodddZpddddZqddddZrdddZsddd ZtdddZudddZvdddZwdddZxdd	d
ZydddZzdddZ{dddZ|dddZ}dddZ~dddZdddZdddZdddZdddZddd Zdd!d"Zdd#d$Zdd%d&Zdd'd(Zdd)d*Zdd+d,Zdd-d.Zdd/d0Zdd1d2Zd3d4 Zd5d6 Zddd8d9ZeZdd<d=Zddd?d@ZdAdB ZdCdD ZdEdF ZdGdH ZdIdJ ZdKdL ZdMdN ZdOdP ZdQdR ZdSdT ZdUdV ZdWdX ZdYdZ Zdd[d\Zd]d^ Zd_d` Zdadb Zdcdd Zdedf Zdgdh Zdidj Zdkdl Zdmdn ZddpdqZddtduZdvdw Zdxdy Zdzd{ Zd|d} Zd~d Zdd Zdd Zdd Zdd ZdddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZdddZdddZdddZdddZdddZdd ZÐdd ZĐdd ZeZƐdd ZǐdddZȐdddZɐdddZʐdddZːdddZ̐dddZ͐dddZΐdddZϐdd ZeZeZeZӐdd ZԐdd ZՐdd Z֐ddÄ ZאdĐdń ZؐdƐdǄ ZِdȐdɄ Zڐdʐd˄ Zېd̐d̈́ Zܐdΐdτ ZݐdАdф ZސdҐdӄ ZߐdԐdՄ Zd֐dׄ Zdؐdل Zdڐdۄ Zdܐd݄ Zdސd߄ Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d Zdd Zdd ZdddZddd	Zdd
dZdd Zdd Zdd Zdd Zdd Z dd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zdd&d'Z	d(d) Z
d*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Zd@dA ZdBdC ZeZdDdE ZdFdG ZdHdI ZdJdK ZdLdM Z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'dbdc Z(ddde Z)dfdg Z*dhdi Z+djdk Z,dldm Z-dndo Z.dpdq Z/drds Z0dtdu Z1ddvdwZ2ddxdyZ3ddzd{Z4dd|d}Z5dd~dZ6dddZ7dd Z8dd Z9dd Z:dd Z;dd Z<dd Z=dd Z>dd Z?dd Z@ fddZA  ZBS (  LatexPrinter_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ddagger)	gothic_re_imdecimal_separatorperm_cyclicparenthesize_superminmaxdiff_operatoradjoint_styledisable_split_super_subzdict[str, Any]_default_settingsc                 C  sp  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}| jd }|||| jd< ddddd }| jd! }|||| jd"< d S )#Nr   )inliner   equationz	equation*zB'mode' must be one of 'inline', 'plain', 'equation' or 'equation*'r   r   T z \,.\,  \cdot  \times )Nldotrx   timesr   mul_symbol_latexrx   mul_symbol_latex_numbers) r   r   z\,z\:\;z\quad)])(r   r   z
\mathrm{i}z\text{i}jz
\mathrm{j}z\text{j})Nr   ritir   rjtjr   imaginary_unit_latexr   z
\mathrm{d}z\text{d})Nr   rdtdr   diff_operator_latex)r   __init__	_settings
ValueErrorKeyErrorstrip_delim_dictget)selfsettingsvalid_modesmul_symbol_tableimaginary_unit_table	imag_unitdiff_operator_tabler   ro   ro   rr   r      sf   


	
	
zLatexPrinter.__init__r   r   c                 C  
   d |S )Nz\left({}\right)formatr   rq   ro   ro   rr   _add_parens      
zLatexPrinter._add_parensc                 C  r   )Nz\left( {}\right)r   r   ro   ro   rr   _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strictprec_valro   ro   rr   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.
        r   r   z{{{}}})r   r   r   r   ro   ro   rr   r      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 )Nr   r   r   z$%s$r   z$$%s$$z\begin{%s}%s\end{%s})r   doprintr   )r   exprtexenv_strro   ro   rr   r   
  s   

zLatexPrinter.doprintboolc                 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)
is_Integeris_nonnegativeis_Atomr   NegativeOneis_Rationalr   r   ro   ro   rr   _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_cleanis_Pow_pow_is_cleanis_Addis_Functionr   ro   ro   rr   _needs_function_brackets!  s   
z%LatexPrinter._needs_function_bracketsc                   s   ddl m} ddlm} ddlm}  jr|s  rdS nt t	d k r(dS  j
r-dS  jr2dS t fddtfD r@dS |sRt fd	d|||fD rRdS d
S )a  
        Returns True if the expression needs to be wrapped in brackets when
        printed as part of a Mul, False otherwise. This is True for Add,
        but also for some container objects that would not need brackets
        when appearing last in a Mul, e.g. an Integral. ``last=True``
        specifies that this expr is the last to appear in a Mul.
        ``first=True`` specifies that this expr is the first to appear in
        a Mul.
        r   )Product)Sum)IntegralTr	   c                 3      | ]}  |V  qd S r   has.0xr   ro   rr   	<genexpr>O      z3LatexPrinter._needs_mul_brackets.<locals>.<genexpr>c                 3  r  r   r  r
  r  ro   rr   r  R  r  F)sympy.concrete.productsr  sympy.concrete.summationsr  sympy.integrals.integralsr  r   could_extract_minus_signr   r    is_Relationalis_Piecewiseanyr   )r   r   firstlastr  r  r  ro   r  rr   _needs_mul_brackets8  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                 3  r  r   r  r
  r  ro   rr   r  _  r  z3LatexPrinter._needs_add_brackets.<locals>.<genexpr>F)r  r  r   r  r   ro   r  rr   _needs_add_bracketsW  s   z LatexPrinter._needs_add_bracketsc                 C  s   |j D ]}|jr dS qdS )NFT)argsr  )r   r   r;   ro   ro   rr   r   e  s
   
zLatexPrinter._mul_is_cleanc                 C  s   |  |j S r   )r   baser   ro   ro   rr   r   k     zLatexPrinter._pow_is_cleanr   c                 C  s   |d ur
d||f S |S )N\left(%s\right)^{%s}ro   r   r   expro   ro   rr   _do_exponentn  s   zLatexPrinter._do_exponentc                   sH     |jj}|jr fdd|jD }d}||d|S d|S )Nc                      g | ]}  |qS ro   r   )r  rL   r   ro   rr   
<listcomp>w      z-LatexPrinter._print_Basic.<locals>.<listcomp>z"\operatorname{{{}}}\left({}\right), z\text{{{}}})_deal_with_super_sub	__class____name__r  r   join)r   r   namelsrq   ro   r$  rr   _print_Basict  s   
zLatexPrinter._print_Basice!bool | BooleanTrue | BooleanFalsec                 C     d| S N	\text{%s}ro   r   r/  ro   ro   rr   _print_bool}     zLatexPrinter._print_boolc                 C  r1  r2  ro   r4  ro   ro   rr   _print_NoneType  r6  zLatexPrinter._print_NoneTypec                 C  sv   | j ||d}d}t|D ]+\}}|dkrn| r"|d7 }| }n|d7 }| |}| |r4d| }||7 }q|S )N)r   r   r    -  + \left(%s\right))_as_ordered_terms	enumerater  r   r  )r   r   r   termsr   r   termterm_texro   ro   rr   
_print_Add  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)   r   ,r   r   z\left( r   \right)) sympy.combinatorics.permutationsrB  sizecyclic_form
array_formr   r   )r   r   rB  	expr_permsizr?  r   ro   ro   rr   _print_Cycle  s   
zLatexPrinter._print_Cyclec           
        s   ddl m} ddlm} |j}|d ur |d| ddddd	 n jd
d}|r. |S |jdkr5dS  fdd|j	D } fddt
t|D }d|}d|}d||f}	d|	 S )Nr   rA  )sympy_deprecation_warningzw
                Setting Permutation.print_cyclic is deprecated. Instead use
                init_printing(perm_cyclic=z).
                z1.6z#deprecated-permutation-print_cyclic   )deprecated_since_versionactive_deprecations_target
stacklevelr   TrC  c                   r"  ro   r#  r  r;   r$  ro   rr   r%    r&  z3LatexPrinter._print_Permutation.<locals>.<listcomp>c                   r"  ro   r#  rT  r$  ro   rr   r%    r&   & z \\ z \begin{pmatrix} %s \end{pmatrix})rH  rB  sympy.utilities.exceptionsrO  print_cyclicr   r   rN  rI  rK  rangelenr+  )
r   r   rB  rO  r   lowerupperrow1row2matro   r$  rr   _print_Permutation  s.   




zLatexPrinter._print_Permutationc                 C  s"   |j \}}d| || |f S )Nz\sigma_{%s}(%s))r  r   )r   r   permvarro   ro   rr   _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||||d}| jd }d|v r`|d\}}	|	d	 d
krL|	dd  }	| jd dkrY|dd}d|||	f S |dkrfdS |dkrldS | jd dkry|dd}|S )Nr   FTr   r   )strip_zeros	min_fixed	max_fixedr   r/  r   +rE  r   comma.z{,}z%s%s10^{%s}z+infz\inftyz-infz- \infty)r!   _precr   mlib_to_str_mpf_splitr   )
r   r   dpsr   lowhighstr_real	separatormantr   ro   ro   rr   _print_Float  s(   

zLatexPrinter._print_Floatc                 C  0   |j }|j}d| |td | |td f S )Nz%s \times %sr	   _expr1_expr2r   r    r   r   vec1vec2ro   ro   rr   _print_Cross  
   zLatexPrinter._print_Crossc                 C     |j }d| |td  S )Nz\nabla\times %sr	   _exprr   r    r   r   r   ro   ro   rr   _print_Curl     zLatexPrinter._print_Curlc                 C  r}  )Nz\nabla\cdot %sr	   r~  r  ro   ro   rr   _print_Divergence  r  zLatexPrinter._print_Divergencec                 C  rt  )Nz%s \cdot %sr	   ru  rx  ro   ro   rr   
_print_Dot  r|  zLatexPrinter._print_Dotc                 C  r}  )Nz	\nabla %sr	   r~  r   r   funcro   ro   rr   _print_Gradient   r  zLatexPrinter._print_Gradientc                 C  r}  )Nz	\Delta %sr	   r~  r  ro   ro   rr   _print_Laplacian  r  zLatexPrinter._print_Laplacianr   c                   s  ddl m} jd jd d# fdd}d#fd	d
 t|trA|j}|d tju s=tdd |dd  D rA |S d}|	 rV| }d}|j
rU|d7 }d}nd}||dd\}}|tju rvtdddd|jvrv|||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r@t|	 || kr@j|ddr|d|
|	f 7 }nx|jr6t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r'|d|||
||f 7 }n!|d|||
||f 7 }n|d |
|	f 7 }n|d!|	|
f 7 }|rO|d"7 }|S )$Nr   )fractionr   r   r   r   c                   sn   | j s
t| S jdvr|  }nt| j}t|dd dd\}}t|dd dd\}} || | S )N)oldnonec                 S  s*   t | dpt | dpt| tot | jdS )N_scale_factoris_physical_constant)hasattr
isinstancer   r  r  ro   ro   rr   rs     s    
z:LatexPrinter._print_Mul.<locals>.convert.<locals>.<lambda>T)binaryc                 S  s
   t | dS )Nr  )r  r  ro   ro   rr   rs     s   
 )r   r   r   r   as_ordered_factorslistr  r$   )r   r  unitsnonunitsprefixes)convert_argsr   ro   rr   convert  s   




z(LatexPrinter._print_Mul.<locals>.convertc                   s   d }}t | D ]M\}}|}t|dsIt|dsIj||dk|t| d kdr/d| }td |rBtd |rB| 7 }n|rH|7 }n|rO|7 }||7 }|}q|S )Nr   r  r  r   rE  )r  r  r:  )r<  r   r  r  rY  _between_two_numbers_psearchmatch)r  _texlast_term_texr   r>  r?  )	numbersepr   rq  ro   rr   r    s(   

z-LatexPrinter._print_Mul.<locals>.convert_argsc                 s  s    | ]}t |tV  qd S r   )r  r
   rT  ro   ro   rr   r  ;  r  z*LatexPrinter._print_Mul.<locals>.<genexpr>rE  F- r   Tr   )exactrD  )evaluater   r      r   )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   r   r   )sympy.simplifyr  r   r  r	   r  r   Oner  r  r  r   rY  rl  r  r   is_commutative)r   r   r  r  r  include_parensr   numerdenomsnumersdenomldenomratioabr  ro   )r  r  r   rq  rr   
_print_Mul  sr   


(


 

zLatexPrinter._print_Mulc                 C  s&   |j r| |  S | | S r   )
is_aliasedr   as_polyas_exprr   ro   ro   rr   _print_AlgebraicNumber{  s   z#LatexPrinter._print_AlgebraicNumberc                 C  s@   |  |j}|jrd| dS |  |j }d| d| dS )N\left(rG  r'  )r   pis_inertalphar  )r   r   r  r  ro   ro   rr   _print_PrimeIdeal  s
   zLatexPrinter._print_PrimeIdealr   c                 C  s  |j jr|j j}|j j}t|dkrF|dkrF| jd rF| |j}|dkr*d| }n| jd r6d||f }nd||f }|j jrDd| S |S | jd	 rx|dkrx| 	|jt
d
 }|jjra| |}|jjrq| j|jd||f dS d|||f S |j jr|jjr|jdkrd|j|j f S |jjr|jj}|jj}|| t|kr|j dkrd||f S d||t|j f S | |S |jjr| j|j| |j dS d}| ||S )NrE  r   r  z	\sqrt{%s}r   z\root{%d}{%s}z\sqrt[%d]{%s}z\frac{1}{%s}r   r   z%s/%sr   z
%s^{%s/%s}%s^{%s}rD  z\frac{1}{\frac{%s}{%s}}z\frac{1}{(\frac{%s}{%s})^{%s}})r   r   r  qr   r   r   r  is_negativer   r    	is_Symbolr   r  r  r  _helper_print_standard_power)r   r   r  r  r  r   base_pbase_qro   ro   rr   
_print_Pow  sF   





zLatexPrinter._print_Powtemplatec                 C  s   |  |j}| |jtd }|jjr| |}n%|jjr"d| }nt|jt	r>|
dr>td|r>|dr>|dd }|||f S )Nr   z{%s}r  z\\left\(\\d?d?dotrG     i)r   r   r   r  r    r  r   is_Floatr  r   
startswithr7   r  endswith)r   r   r  r   r  ro   ro   rr   r    s   

z)LatexPrinter._helper_print_standard_powerc                 C  s   |  |jd S Nr   r   r  r   ro   ro   rr   _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 )NrE  z\sum_{%s=%s}^{%s} c                   r"  ro   r#  r  r   r$  ro   rr   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"  ro   r#  r  rq   r$  ro   rr   r%    r&  zALatexPrinter._print_Sum.<locals>._format_ineq.<locals>.<listcomp>rE  r   r  tuplelr$  ro   rr   _format_ineq     &z-LatexPrinter._print_Sum.<locals>._format_ineqz\sum_{\substack{%s}} \\c                      g | ]} |qS ro   ro   r  r  r  ro   rr   r%        r:  	rY  limitsr  r   r+  r  functionr   r   r   r   r   ro   r  r   rr   
_print_Sum     zLatexPrinter._print_Sumc                   r  )NrE  z\prod_{%s=%s}^{%s} c                   r"  ro   r#  r  r$  ro   rr   r%    r&  z/LatexPrinter._print_Product.<locals>.<listcomp>r   c                   r  )Nr  c                   r"  ro   r#  r  r$  ro   rr   r%    r&  zELatexPrinter._print_Product.<locals>._format_ineq.<locals>.<listcomp>rE  r   r  r  r  r$  ro   rr   r    r  z1LatexPrinter._print_Product.<locals>._format_ineqz\prod_{\substack{%s}} r  c                   r  ro   ro   r  r  ro   rr   r%    r  r:  r  r  ro   r  rr   _print_Product  r  zLatexPrinter._print_Product'BasisDependent'c                 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 r  )__str__r  ro   ro   rr   rs     rt   z4LatexPrinter._print_BasisDependent.<locals>.<lambda>keyrE  r9  rD  r8  r  rG  r   -   )sympy.vectorr  zero_latex_formr  separateitemsr  
componentssortappendr   r+  )r   r   r  o1r  systemvect
inneritemskvarg_stroutstrro   ro   rr   _print_BasisDependent  s0   



	z"LatexPrinter._print_BasisDependentc                 C  s4   |  |j}d| d ddt| j |j  }|S )Nr   rn   _{%s}rF  )r   r  r+  mapindices)r   r   tex_baser   ro   ro   rr   _print_Indexed  s
   zLatexPrinter._print_Indexedc                 C     |  |jS r   )r   labelr   ro   ro   rr   _print_IndexedBase     zLatexPrinter._print_IndexedBasec                 C  sf   |  |j}|jd ur1|  |j}|jd ur|  |j}n|  tj}dj||d}dj||dS |S )Nz%{lower}\mathrel{{..}}\nobreak {upper})rZ  r[  z{{{label}}}_{{{interval}}})r  interval)r   r  r[  rZ  r   Zeror   )r   r   r  r[  rZ  r  ro   ro   rr   
_print_Idx  s   

zLatexPrinter._print_Idxc              	   C  s   t |jrd}n| jd }d}d}t|jD ]*\}}||7 }|dkr.|d|| |f 7 }q|d|| | || |f 7 }q|dkrLd||f }n
d	|| ||f }td
d |jD rpd|| j	|jt
d dddf S d|| j	|jt
d dddf S )Nz\partialr   r   r   rE  %s %sz
%s %s^{%s}r  z\frac{%s^{%s}}{%s}c                 s      | ]}|  V  qd S r   r  r  ro   ro   rr   r  ;      z1LatexPrinter._print_Derivative.<locals>.<genexpr>r	   Tr   r   F)r   r   r   reversedvariable_countr   r   r  r  r   r    )r   r   diff_symbolr   dimr  numro   ro   rr   _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  r  r   r#  r  r/  r$  ro   rr   r  I  r  z+LatexPrinter._print_Subs.<locals>.<genexpr>c                 3  r  r   r#  r  r$  ro   rr   r  J  r  z\\ c                 s  s$    | ]}|d  d |d  V  qdS )r   =rE  Nro   r  ro   ro   rr   r  K  s    
z#\left. %s \right|_{\substack{ %s }})r  r   r+  zip)	r   subsr   r  new
latex_expr	latex_old	latex_new
latex_subsro   r$  rr   _print_SubsF  s   

zLatexPrinter._print_Subsc              	     sX  dg }}j d  t|jdkr4tdd |jD r4ddt|jd   d	 } fd
d|jD }n\t|jD ]V}|d }|d7 }t|dkrj d dkrYj d sY|d7 }t|dkrq|d|d |d f 7 }t|dkr|d|d  7 }|dd |f  q9d|j|jt	d t
dd |jD ddd|f S )Nr   r      c                 s  s    | ]	}t |d kV  qdS )rE  N)rY  )r  limro   ro   rr   r  U  s    z/LatexPrinter._print_Integral.<locals>.<genexpr>z\ir   rE  ntc                   s"   g | ]}d   |d f qS )\, %s%sr   r#  r  symbolr  r   ro   rr   r%  Y  s    z0LatexPrinter._print_Integral.<locals>.<listcomp>r   z\intr   r   r   z\limitsr  z
_{%s}^{%s}r  ^{%s}r!  z%s %s%sr	   c                 s  r
  r   r  r  ro   ro   rr   r  p  r  Tr  )r   rY  r  allr  r   insertr   r  r    r  r  r+  )r   r   r   symbolsr  r#  ro   r$  rr   _print_IntegralP  s:   

"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/  zz0dirr   ro   ro   rr   _print_Limitt  s   
zLatexPrinter._print_Limitr  c                 C  s   |  |}|d}|d}|tv rd| }|S t|dks,|ds,|dks,|dkr0|}|S |dkrN|dkrNd|dt|| |t||d f }|S |dkrbd|d| ||d f }|S |dkrvd|d| ||d f }|S d	| }|S )
ak  
        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, excluding sub- and superscripts, just use that letter
          - if it is a longer name, then put \operatorname{} around it and be
            mindful of undercores in the name
        r   _z\%srE  r   r   z\operatorname{%s}%sNz\operatorname{%s})r(  findaccepted_latex_functionsrY  r  r   )r   r  superscriptidxsubscriptidxr,  ro   ro   rr   _hprint_Function  s:   


&

	

zLatexPrinter._hprint_Functionr   c                   s  |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 rs|d
krLn'|dkra|d dkrXdnd|dd  }n|dkrs|dd }d}|dursd}|r|t
v r~d| }	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
        _print_c                   s   g | ]	}t  |qS ro   )r   r   rT  r$  ro   rr   r%        z0LatexPrinter._print_Function.<locals>.<listcomp>r   Fr   rE  r   )asinacosatanacscasecacotasinhacoshatanhacschasechacothr   fullrD  hararcNpowerTz\%s^{-1}z\operatorname{%s}^{-1}r  z {%s}%s{\left(%s \right)}r%  rF  )r  r*  r  r  r   getattrr  r   rY  r  r3  r6  r   r+  )r   r   r   r  r  r   inv_trig_power_casecan_fold_bracketsinv_trig_tabler,  func_texro   r$  rr   _print_Function  sR   


"






zLatexPrinter._print_Functionc                 C     |  t|S r   )r6  r   r   ro   ro   rr   _print_UndefinedFunction  r  z%LatexPrinter._print_UndefinedFunctionc                 C     d|  |j|  |jf S )Nz{%s}_{\circ}\left({%s}\right))r   r  r   r   ro   ro   rr   _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)rT   z\deltar?   z\gammaz\operatorname{B}z\operatorname{Chi})(sympy.functions.special.tensor_functionsrV  'sympy.functions.special.gamma_functionsrW  rX  &sympy.functions.special.beta_functionsrY  'sympy.functions.special.delta_functionsrZ  'sympy.functions.special.error_functionsrT   )r   rV  rW  rX  rY  rZ  rT   ro   ro   rr   _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*  r6  r   )r   r   clsro   ro   rr   _print_FunctionClass  s
   
z!LatexPrinter._print_FunctionClassc                 C  sJ   |j \}}t|dkr| |d }n| t|}d|| |f }|S )NrE  r   z\left( %s \mapsto %s \right))r  rY  r   r  )r   r   r(  r   ro   ro   rr   _print_Lambda  s   
zLatexPrinter._print_Lambdac                 C     dS )Nz\left( x \mapsto x \right)ro   r   ro   ro   rr   _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 )rJ  r#  r"  r$  ro   rr   r%    r8  z:LatexPrinter._hprint_variadic_function.<locals>.<listcomp>z\%s\left(%s\right)r'  r  )sortedr  r   r   r  rZ  r+  )r   r   r   r  texargsr   ro   r$  rr   _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  r  r   r   r   r   ro   ro   rr   _print_floor&     zLatexPrinter._print_floorc                 C  rl  )Nz\left\lceil{%s}\right\rceilr   r  r  rm  ro   ro   rr   _print_ceiling.  ro  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  rm  ro   ro   rr   
_print_log6  s   
zLatexPrinter._print_logc                 C  rl  )N\left|{%s}\right|r   r  r  rm  ro   ro   rr   
_print_AbsA  ro  zLatexPrinter._print_Absc                 C  N   | j d rd| |jd td  }nd| |jd td }| ||S )Nr   z\Re{%s}r   Atomz\operatorname{{re}}{{{}}}r   r   r  r    r   r!  rm  ro   ro   rr   	_print_reI     
zLatexPrinter._print_rec                 C  rt  )Nr   z\Im{%s}r   ru  z\operatorname{{im}}{{{}}}rv  rm  ro   ro   rr   	_print_imQ  rx  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)	sympy.logic.boolalgrz  r{  r  r  _print_Equivalent_print_Implies
is_Booleanr   )r   r/  rz  r{  ro   ro   rr   
_print_NotY  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:  rJ  rE  z %s \left(%s\right)z %s %s)r  is_Notr   )r   r  charr;   r   ro   ro   rr   _print_LogOpd  s   zLatexPrinter._print_LogOpc                 C     t |jtd}| |dS )Nr  z\wedgeri  r  r   r  r   r/  r  ro   ro   rr   
_print_Ands     zLatexPrinter._print_Andc                 C  r  )Nr  z\veer  r  ro   ro   rr   	_print_Orw  r  zLatexPrinter._print_Orc                 C  r  )Nr  z\veebarr  r  ro   ro   rr   
_print_Xor{  r  zLatexPrinter._print_Xorc                 C  s   |  |j|pdS )Nz\Rightarrow)r  r  )r   r/  altcharro   ro   rr   r~       zLatexPrinter._print_Impliesc                 C  s   t |jtd}| ||pdS )Nr  z\Leftrightarrowr  )r   r/  r  r  ro   ro   rr   r}    s   zLatexPrinter._print_Equivalentc                 C  rl  )Nz\overline{%s}r   r  r  rm  ro   ro   rr   _print_conjugate  ro  zLatexPrinter._print_conjugatec                 C  s:   d}d|  |jd  }|d urd|||f S d||f S )Nz\operatorname{polar\_lift}rK  r   	%s^{%s}%s%s%sr  )r   r   r   r  r;   ro   ro   rr   _print_polar_lift  s
   zLatexPrinter._print_polar_liftc                 C  s    d|  |jd  }| ||S )Nze^{%s}r   )r   r  r!  rm  ro   ro   rr   _print_ExpBase  s   zLatexPrinter._print_ExpBasec                 C  re  )Nr/  ro   r  ro   ro   rr   _print_Exp1  rg  zLatexPrinter._print_Exp1c                 C  0   d|  |jd  }|d urd||f S d| S )Nr:  r   zK^{%s}%szK%sr  rm  ro   ro   rr   _print_elliptic_k  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   rE  zF^{%s}%szF%sr  rm  ro   ro   rr   _print_elliptic_f  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   rE  r:  zE^{%s}%szE%srY  r  r   rm  ro   ro   rr   _print_elliptic_e  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 )	Nr  z\left(%s; %s\middle| %s\right)r   rE  r  r  z
\Pi^{%s}%sz\Pi%sr  rm  ro   ro   rr   _print_elliptic_pi  s   zLatexPrinter._print_elliptic_pic                 C  sZ   |j d }t|j dkr|j d n|j d }d| d| d}|d ur)d||f S d| S )Nr   rE  r  r'  rG  z\operatorname{B}^{%s}%sz\operatorname{B}%s)r  rY  )r   r   r   r  yr   ro   ro   rr   _print_beta  s   
"zLatexPrinter._print_betaBc                   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"  ro   r#  rT  r$  ro   rr   r%    r&  z/LatexPrinter._print_betainc.<locals>.<listcomp>\left(%s, %s\right)r   rE  z#\operatorname{%s}_{(%s, %s)}^{%s}%sr  r  z\operatorname{%s}_{(%s, %s)}%s)r  )r   r   r   operatorlargsr   ro   r$  rr   _print_betainc  s
   zLatexPrinter._print_betaincc                 C  s   | j ||ddS )NI)r  )r  r  ro   ro   rr   _print_betainc_regularized  r  z'LatexPrinter._print_betainc_regularizedc                 C  r  )Nr  r   rE  z\Gamma^{%s}%sz\Gamma%sr  rm  ro   ro   rr   _print_uppergamma     zLatexPrinter._print_uppergammac                 C  r  )Nr  r   rE  z\gamma^{%s}%s\gamma%sr  rm  ro   ro   rr   _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  rm  ro   ro   rr   _hprint_one_arg_func  s   z!LatexPrinter._hprint_one_arg_funcc                 C  r  )Nr:  r   z\operatorname{Chi}^{%s}%sz\operatorname{Chi}%sr  rm  ro   ro   rr   
_print_Chi     zLatexPrinter._print_Chic                 C  sF   d|  |jd  }|  |jd }|d urd|||f S d||f S )Nr:  rE  r   z\operatorname{E}_{%s}^{%s}%sz\operatorname{E}_{%s}%sr  )r   r   r   r   nuro   ro   rr   _print_expint  s
   zLatexPrinter._print_expintc                 C  r  )Nr:  r   zS^{%s}%szS%sr  rm  ro   ro   rr   _print_fresnels  r  zLatexPrinter._print_fresnelsc                 C  r  )Nr:  r   zC^{%s}%szC%sr  rm  ro   ro   rr   _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    rm  ro   ro   rr   _print_subfactorial     z LatexPrinter._print_subfactorialc                 C  r  )Nz%s!r   r  r  r  rm  ro   ro   rr   _print_factorial#  r  zLatexPrinter._print_factorialc                 C  r  )Nz%s!!r   r  r  r  rm  ro   ro   rr   _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   rE  r  r  rm  ro   ro   rr   _print_binomial3  s   zLatexPrinter._print_binomialc                 C  s<   |j \}}d| |td  }d|| |f }| ||S )NrJ  r  z{%s}^{\left(%s\right)}r  r   r    r   r!  )r   r   r   nr  r  r   ro   ro   rr   _print_RisingFactorial<     
z#LatexPrinter._print_RisingFactorialc                 C  s<   |j \}}d| |td  }d| ||f }| ||S )NrJ  r  z{\left(%s\right)}_{%s}r  )r   r   r   r  r  subr   ro   ro   rr   _print_FallingFactorialD  r  z$LatexPrinter._print_FallingFactorialsymc                 C  sf   d| }d}|d ur| ddkrd||f }nd}d|| |j| |jf }|r1| ||}|S )NrJ  Fr   rD  r  T%s_{%s}\left(%s\right))r2  r   r   argumentr!  )r   r   r   r  r   need_expro   ro   rr   _hprint_BesselBaseL  s   
zLatexPrinter._hprint_BesselBasec                 C  sF   |sdS d}|d d D ]}|d|  | 7 }q||  |d 7 }|S )Nr   rD  z%s, r#  )r   r   rq   r   ro   ro   rr   _hprint_vec]  s   zLatexPrinter._hprint_vecc                 C     |  ||dS )NJr  r  ro   ro   rr   _print_besseljf  r  zLatexPrinter._print_besseljc                 C  r  )Nr  r  r  ro   ro   rr   _print_besselii  r  zLatexPrinter._print_besselic                 C  r  )NKr  r  ro   ro   rr   _print_besselkl  r  zLatexPrinter._print_besselkc                 C  r  )NYr  r  ro   ro   rr   _print_besselyo  r  zLatexPrinter._print_besselyc                 C  r  )Nr  r  r  ro   ro   rr   	_print_ynr  r  zLatexPrinter._print_ync                 C  r  )Nr   r  r  ro   ro   rr   	_print_jnu  r  zLatexPrinter._print_jnc                 C  r  )NzH^{(1)}r  r  ro   ro   rr   _print_hankel1x  r  zLatexPrinter._print_hankel1c                 C  r  )NzH^{(2)}r  r  ro   ro   rr   _print_hankel2{  r  zLatexPrinter._print_hankel2c                 C  r  )Nzh^{(1)}r  r  ro   ro   rr   
_print_hn1~  r  zLatexPrinter._print_hn1c                 C  r  )Nzh^{(2)}r  r  ro   ro   rr   
_print_hn2  r  zLatexPrinter._print_hn2r   c                 C  6   d|  |jd  }|d urd|||f S d||f S r  r  r   r   r   notationr   ro   ro   rr   _hprint_airy     zLatexPrinter._hprint_airyc                 C  r  )Nr:  r   z{%s^\prime}^{%s}%sz%s^\prime%sr  r  ro   ro   rr   _hprint_airy_prime  r  zLatexPrinter._hprint_airy_primec                 C  r  NAir  r  ro   ro   rr   _print_airyai  r  zLatexPrinter._print_airyaic                 C  r  NBir  r  ro   ro   rr   _print_airybi  r  zLatexPrinter._print_airybic                 C  r  r  r  r  ro   ro   rr   _print_airyaiprime  r  zLatexPrinter._print_airyaiprimec                 C  r  r  r  r  ro   ro   rr   _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   rY  apbqr  r  rm  ro   ro   rr   _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   rY  r  r  r   anr  aotherbotherr  rm  ro   ro   rr   _print_meijerg  s   
zLatexPrinter._print_meijergc                 C  r  )Nr:  r   z\eta^{%s}%sz\eta%sr  rm  ro   ro   rr   _print_dirichlet_eta  s   z!LatexPrinter._print_dirichlet_etac                 C  sV   t |jdkrdtt| j|j }n
d| |jd  }|d ur'd||f S d| S )Nr  r  r:  r   z\zeta^{%s}%sz\zeta%srY  r  r  r  r   rm  ro   ro   rr   _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)r  r   z\gamma%s^{%s}r  r  rm  ro   ro   rr   _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  r  r   r  rm  ro   ro   rr   _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r  r   r  )r   r   r   rq   r-  r   ro   ro   rr   _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   ro   ro   rr   _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   ro   ro   rr   _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   ro   ro   rr   _print_chebyshevt  
   zLatexPrinter._print_chebyshevtc                 C  r  )NzU_{%s}\left(%s\right)r  r  r  r  ro   ro   rr   _print_chebyshevu  r  zLatexPrinter._print_chebyshevuc                 C  r  )NzP_{%s}\left(%s\right)r  r  r  r  ro   ro   rr   _print_legendre  r  zLatexPrinter._print_legendrec                 C  r  )Nz'P_{%s}^{\left(%s\right)}\left(%s\right)r  r  r  r  ro   ro   rr   _print_assoc_legendre   r  z"LatexPrinter._print_assoc_legendrec                 C  r  )NzH_{%s}\left(%s\right)r  r  r  r  ro   ro   rr   _print_hermite  r  zLatexPrinter._print_hermitec                 C  r  )NzL_{%s}\left(%s\right)r  r  r  r  ro   ro   rr   _print_laguerre  r  zLatexPrinter._print_laguerrec                 C  r  )Nz'L_{%s}^{\left(%s\right)}\left(%s\right)r  r  r  r  ro   ro   rr   _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  mthetaphir   ro   ro   rr   
_print_Ynm  r  zLatexPrinter._print_Ynmc                 C  r  )NzZ_{%s}^{%s}\left(%s,%s\right)r  r  r  r  ro   ro   rr   
_print_Znm#  r  zLatexPrinter._print_Znmc           	      C  sB   t | j|\}}}|rdnd}|sdnd| }d||||||f S )Nz	^{\prime}r   r%  z%s%s\left(%s, %s, %s\right)%s)r  r   )	r   	characterr  r   r   r  r  r-  supro   ro   rr   __print_mathieu_functions*  s   z&LatexPrinter.__print_mathieu_functionsc                 C     | j d|j|dS )NCr  &_LatexPrinter__print_mathieu_functionsr  r  ro   ro   rr   _print_mathieuc0  r  zLatexPrinter._print_mathieucc                 C  r  )Nr   r  r  r  ro   ro   rr   _print_mathieus3  r  zLatexPrinter._print_mathieusc                 C     | j d|jd|dS )Nr  Tr   r   r  r  ro   ro   rr   _print_mathieucprime6     z!LatexPrinter._print_mathieucprimec                 C  r  )Nr   Tr  r  r  ro   ro   rr   _print_mathieusprime9  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 )NrE  r   r   r  r   z	%s%d / %dz%s\frac{%d}{%d})r  r  r   r   )r   r   signr  ro   ro   rr   _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   r  )r  r  ro   ro   rr   r  K  r  z,LatexPrinter._print_Order.<locals>.<genexpr>rE  ; r   z\rightarrow zO\left(%s\right))r   r   pointr  rY  	variablesr   r   rq   ro   ro   rr   _print_OrderI  s   zLatexPrinter._print_Orderr   c                 C  s,   | j d |}|d ur|S | j|j|dS )Nr   style)r   r   r(  r,  )r   r   r!  r,  ro   ro   rr   _print_SymbolY  s   zLatexPrinter._print_Symbolr,   tuple[str, list[str], list[str]]c                 C  s   |d u sd|v r|g g fS | j d r#|ddddg g }}}nt|\}}}t|}dd |D }d	d |D }|||fS )
Nr   r   r1  z\_r   z\^c                 S     g | ]}t |qS ro   	translate)r  r  ro   ro   rr   r%  j  r  z1LatexPrinter._split_super_sub.<locals>.<listcomp>c                 S  r$  ro   r%  )r  r  ro   ro   rr   r%  k  r  )r   r   r   r&  )r   r,  supersr  ro   ro   rr   _split_super_subb  s   

"
zLatexPrinter._split_super_substringc                 C  sR   |  |\}}}|dkrd|}|r|dd| 7 }|r'|dd| 7 }|S )Nr   \mathbf{{{}}}r%  r   r  )r(  r   r+  )r   r)  r!  r,  r'  r  ro   ro   rr   r(  n  s   
z!LatexPrinter._deal_with_super_subc                 C  sR   | j d r
d}d}nd}d}d||ddd	d
}d| |j||j | |jf S )Nr   z\gtz\lt><r  z\geqz\leqz\neq)z==r+  r,  z>=z<=z!=z%s %s %s)r   r   lhsrel_oprhs)r   r   gtltcharmapro   ro   rr   _print_Relational}  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/  r   r$  ro   rr   r%    s    z1LatexPrinter._print_Piecewise.<locals>.<listcomp>rD  z%s & \text{otherwise}r4  z\begin{cases} %s \end{cases}z \\)r  condr   r  r   r   r+  )r   r   ecpairsr   ro   r$  rr   _print_Piecewise  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 }|d| S )NrU  c                   r"  ro   r#  r  r$  ro   rr   r%    r&  z7LatexPrinter._print_matrix_contents.<locals>.<listcomp>r   r   r   smallmatrix
   Tmatrixarray \begin{%MATSTR%}%s\end{%MATSTR%}%MATSTR%rJ  r   r   }%sr  )rX  rowsr  r+  r   colsr   )r   r   linesliner   out_strro   r$  rr   _print_matrix_contents  s   ,
z#LatexPrinter._print_matrix_contentsc                 C  s@   |  |}| jd r| jd }| j| }d| | d | }|S )Nr   \left\right)rD  r   r   )r   r   rC  
left_delimright_delimro   ro   rr   _print_MatrixBase  s   




zLatexPrinter._print_MatrixBasec                 C  sF   | j |jtd dd}| |j d| |j }d| d| dS )Nru  Tr   rF  r   z}_{rn   )r   parentr    r   r   r   )r   r   matrix_part
index_partro   ro   rr   _print_MatrixElement  s   z!LatexPrinter._print_MatrixElementc                   sN    fdd} j |jtd ddd ||j|jj d ||j|jj d S )	Nc                   sZ   t | } | d dkr| d= | d dkrd | d< | d |kr!d | d< d fdd| D S )Nr  rE  r   :c                 3  s&    | ]}|d ur  |ndV  qd S Nr   r#  )r  xir$  ro   rr   r       $ zFLatexPrinter._print_MatrixSlice.<locals>.latexslice.<locals>.<genexpr>)r  r+  )r  r  r$  ro   rr   
latexslice  s   z3LatexPrinter._print_MatrixSlice.<locals>.latexsliceru  TrJ  \left[r'  \right])r   rK  r    rowslicer?  colslicer@  )r   r   rS  ro   r$  rr   _print_MatrixSlice  s   	zLatexPrinter._print_MatrixSlicec                 C  r  r   )r   blocksr   ro   ro   rr   _print_BlockMatrix  r  zLatexPrinter._print_BlockMatrixc                 C  sh   |j }ddlm}m} t||st||s|jrd| | S | |t|d}d|v r0d| S d| S )Nr   MatrixSymbolBlockMatrixz\left(%s\right)^{T}Tr   z%s^{T})	r;   sympy.matricesr\  r]  r  is_MatrixExprr   r   r   )r   r   r^  r\  r]  rq   ro   ro   rr   _print_Transpose  s   
zLatexPrinter._print_Transposec                 C  s   |j }d| | S )Nz!\operatorname{tr}\left(%s \right))r;   r   r   r   r^  ro   ro   rr   _print_Trace  s   zLatexPrinter._print_Tracec                 C  s   dddd}| | jd d}|j}ddlm}m} t||s0t||s0|jr0d| ||f S | 	|t
|d	}d
|v rCd||f S d||f S )Nz\daggerz\astz
\mathsf{H})r   star	hermitianr   r   r[  r  Tr   r  )r   r   r;   r^  r\  r]  r  r_  r   r   r   )r   r   style_to_latexr   r^  r\  r]  rq   ro   ro   rr   _print_Adjoint  s"   
zLatexPrinter._print_Adjointc                   sz   ddl m   fdd}tj} r5|d dkr$|dd  }n|d  |d< ddt|| S dt||S )	Nr   )MatMulc                   s0   t | trt |  s| S | tdS NF)r  r	   r   r   r   r  rg  r   r   ro   rr   rs     s    z,LatexPrinter._print_MatMul.<locals>.<lambda>rD  rE  r  r   )sympyrg  r  r  r  r+  r  )r   r   parensr  ro   ri  rr   _print_MatMul  s   
zLatexPrinter._print_MatMulc                 C  s0   t |}|j\}}| || d| || S )Nr   )r   r  r   )r   r   r   leftrightro   ro   rr   _print_DotProduct  s   
zLatexPrinter._print_DotProductc                 C  sN   |j }|jr ddlm} t||rd| |j S d| | S d| | S )Nr   )r]  rr  )r;   r_  &sympy.matrices.expressions.blockmatrixr]  r  rD  rY  r   )r   r   r^  r]  ro   ro   rr   _print_Determinant  s   
zLatexPrinter._print_Determinantc                 C  sz   |d ur!d| j |jd td dd| j |jd td dd|f S d| j |jd td dd| j |jd td ddf S )Nz\left(%s \bmod %s\right)^{%s}r   r	   TrJ  rE  z%s \bmod %sr  r  ro   ro   rr   
_print_Mod  s(   zLatexPrinter._print_Modc                   .   |j }td | j d fdd|D S )Nr   z \circ c                 3      | ]
} |d dV  qdS TrJ  Nro   rT  rk  precro   rr   r  ,      z6LatexPrinter._print_HadamardProduct.<locals>.<genexpr>r  r    r   r+  r   r   r  ro   rv  rr   _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  ro   ro   rr   _print_HadamardPower.  s   z!LatexPrinter._print_HadamardPowerc                   rs  )Nr   	 \otimes c                 3  rt  ru  ro   rT  rv  ro   rr   r  ;  rx  z7LatexPrinter._print_KroneckerProduct.<locals>.<genexpr>ry  rz  ro   rv  rr   _print_KroneckerProduct5  r|  z$LatexPrinter._print_KroneckerProductc                 C  sx   |j |j}}ddlm} t||s!|jr!d| || |f S | |}d|v r3d|| |f S d|| |f S )Nr   )r\  r  r   r  )r  r   r^  r\  r  r_  r   )r   r   r  r   r\  base_strro   ro   rr   _print_MatPow=  s   

zLatexPrinter._print_MatPowc                 C  s   | j || jd dS )Nr   r   )r"  r   r   ro   ro   rr   _print_MatrixSymbolJ  s   
z LatexPrinter._print_MatrixSymbolc                 C     | j d dkr	dS dS )Nr   r   0z
\mathbf{0}r   )r   Zro   ro   rr   _print_ZeroMatrixN     zLatexPrinter._print_ZeroMatrixc                 C  r  )Nr   r   1z
\mathbf{1}r  )r   Oro   ro   rr   _print_OneMatrixR  r  zLatexPrinter._print_OneMatrixc                 C  r  )Nr   r   z
\mathbb{I}z
\mathbf{I}r  )r   r  ro   ro   rr   _print_IdentityV  r  zLatexPrinter._print_Identityc                 C  s   |  |jd }d| S )Nr   zP_{%s}r  )r   Pperm_strro   ro   rr   _print_PermutationMatrixZ  s   z%LatexPrinter._print_PermutationMatrixr%   c              
   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|}|d
krM|ddd|jd   d }| jd rf| jd }| j| }d| | d | }|  dkrp|d S dd t|  d 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 < qq|d d }|  d dkr|| }|S )Nr   ro   r   r   r   r8  rD  r9  r:  r;  r<  r=  rJ  r   r   r>  r   rE  rF  r   c                 S  s   g | ]}g qS ro   ro   r  ro   ro   rr   r%  z  s    z1LatexPrinter._print_NDimArray.<locals>.<listcomp>rE  c                 S  s   g | ]}t t|qS ro   )r  rX  r  ro   ro   rr   r%  {      TrU  r  rT  rU  r  )rankr   r   shaper   r   rX  	itertoolsproductr  rY  r+  )r   r   r   	block_strrG  rH  	level_strshape_rangesouter_ievenback_outer_irC  ro   ro   rr   _print_NDimArray^  sf   





zLatexPrinter._print_NDimArray	index_mapdictc           	      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 )	NrF  rn   z{}^{z{}_{r   r  TF)r   is_upr  )	r   r,  r  r  rC  last_valenceprev_mapindexnew_valencero   ro   rr   _printer_tensor_indices  s0   

z$LatexPrinter._printer_tensor_indicesc                 C  s&   |j d j d }| }| ||i S r  )r  get_indicesr  )r   r   r,  r  ro   ro   rr   _print_Tensor  s   zLatexPrinter._print_Tensorc                 C  s0   |j jd jd }|j  }|j}| |||S r  )r   r  r  r  r  )r   r   r,  r  r  ro   ro   rr   _print_TensorElement  s   
z!LatexPrinter._print_TensorElementc                   s*      \}}|d fdd|D  S )Nr   c                   s   g | ]
} |t qS ro   )r   r   rT  r   r   ro   rr   r%        z/LatexPrinter._print_TensMul.<locals>.<listcomp>)!_get_args_for_traditional_printerr+  )r   r   r  r  ro   r  rr   _print_TensMul  s   zLatexPrinter._print_TensMulc                 C  sL   g }|j }|D ]}|| |t| q|  d|}|dd}|S )Nr9  z+ -r  )r  r  r   r   r  r+  r   )r   r   r  r  r  rq   ro   ro   rr   _print_TensAdd  s   
zLatexPrinter._print_TensAddc                 C  s"   d|j rdnd| |jd f S )Nz{}%s{%s}r   r1  r   )r  r   r  r   ro   ro   rr   _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 )
NrE  z"\frac{\partial}{\partial {%s}}{%s}r   r	   Fz\frac{\partial^{%s}}{%s}{%s}r   c                   rh  )z\partial {%s}r#  r  r$  ro   rr   r%    r8  z9LatexPrinter._print_PartialDerivative.<locals>.<listcomp>)rY  r  r   r   r   r    r+  r   ro   r$  rr   _print_PartialDerivative  s   z%LatexPrinter._print_PartialDerivativec                 C  r  r   )r   r,  r   ro   ro   rr   _print_ArraySymbol  r  zLatexPrinter._print_ArraySymbolc                   s2   d  |jtd dd fdd|jD f S )Nz{{%s}_{%s}}r  Tr'  c                   s   g | ]}  | qS ro   r#  r  r$  ro   rr   r%    r  z4LatexPrinter._print_ArrayElement.<locals>.<listcomp>)r   r,  r    r+  r  r   ro   r$  rr   _print_ArrayElement  s   z LatexPrinter._print_ArrayElementc                 C  re  )Nz
\mathbb{U}ro   r   ro   ro   rr   _print_UniversalSet  rg  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}r  r  ro   ro   rr   _print_frac  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 )Nr   rg  ;r   rF  Unknown Decimal SeparatorrE  r   z \  c                   r"  ro   r#  r  r$  ro   rr   r%    r&  z-LatexPrinter._print_tuple.<locals>.<listcomp>)r   r   rY  r   r   r+  )r   r   sepro   r$  rr   _print_tuple  s   zLatexPrinter._print_tuplec                       fdd|j D }d|S )Nc                   r"  ro   r#  r  r  r$  ro   rr   r%    r&  z5LatexPrinter._print_TensorProduct.<locals>.<listcomp>r~  r  r+  r   r   elementsro   r$  rr   _print_TensorProduct     
z!LatexPrinter._print_TensorProductc                   r  )Nc                   r"  ro   r#  r  r$  ro   rr   r%    r&  z4LatexPrinter._print_WedgeProduct.<locals>.<listcomp>z \wedge r  r  ro   r$  rr   _print_WedgeProduct  r  z LatexPrinter._print_WedgeProductc                 C  
   |  |S r   )r  r   ro   ro   rr   _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
)Nr   rg  z\left[ %s\right]z; \  c                   r"  ro   r#  r  r$  ro   rr   r%    r&  z,LatexPrinter._print_list.<locals>.<listcomp>r   , \  c                   r"  ro   r#  r  r$  ro   rr   r%    r&  r  )r   r+  r   r   ro   r$  rr   _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  )ri  keysr   r  r   r+  )r   r   r  r  r  valro   ro   rr   _print_dict  s    zLatexPrinter._print_dictc                 C  r  r   )r  r   ro   ro   rr   _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 )NrE  r   z\delta\left(%s\right)z+\delta^{\left( %s \right)}\left( %s \right)r  r  rm  ro   ro   rr   _print_DiracDelta$  s   zLatexPrinter._print_DiracDeltac                 C  sP   |  |jd |jd  }|  |jd }d||f }|d ur&d|||f }|S )Nr   rE  r  z${\left\langle %s \right\rangle}^{%s}z-{\left({\langle %s \rangle}^{%s}\right)}^{%s}r  )r   r   r   shiftrI  r   ro   ro   rr   _print_SingularityFunction.  s   z'LatexPrinter._print_SingularityFunctionc                   s6   d  fdd|jD }d| }|rd||f }|S )Nr'  c                 3  r  r   r#  rT  r$  ro   rr   r  7  r  z0LatexPrinter._print_Heaviside.<locals>.<genexpr>z\theta\left(%s\right)r  )r+  pargs)r   r   r   r  r   ro   r$  rr   _print_Heaviside6  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   rE  z\delta_{%s %s}z\delta_{%s, %s}r  )r   r  r   )r   r   r   r   r   r   ro   ro   rr   _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
  ro   ro   rr   r  J  s    z1LatexPrinter._print_LeviCivita.<locals>.<genexpr>z\varepsilon_{%s}r   r'  r  )r  r   r  r&  r+  )r   r   r   r  r   ro   ro   rr   _print_LeviCivitaH  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 \in r(  z\text{Domain on })r  r   r  r(  r  )r   r   ro   ro   rr   _print_RandomDomainR  s   




z LatexPrinter._print_RandomDomainc                 C  s   t |jtd}| |S )Nr  )ri  r  r   
_print_setr   rq   r  ro   ro   rr   _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  r   rg  r  r   r'  r  \left\{%s\right\})ri  r   r   r+  r  r   r   r  ro   ro   rr   r  a  s   zLatexPrinter._print_setc                   s
  fdd}t   jjr&jjr&jjr ddd f}nV ddd f}nNjjr7 d j d f}n=jjrIt}t|t| f}n+jd urqj	dk dkrZt
}njrnt}t|t| d f}n| S | S dd	 fd
d|D  d S )Nc                    s    j d dkr% j d dkr j d } n4dfdd j D } n& j d dkr>dfdd j d d D } ndfdd j D } d	|  d
S )Nr   r  rE  r'  c                 3  r  r   r#  rT  r$  ro   rr   r  u  r  zKLatexPrinter._print_Range.<locals>._print_symbolic_range.<locals>.<genexpr>c                 3  r  r   r#  rT  r$  ro   rr   r  x  r  c                 3  r  r   r#  rT  r$  ro   rr   r  z  r  z\text{Range}\left(rG  )r  r   r+  )cont)rq   r   ro   rr   _print_symbolic_rangeo  s   $z8LatexPrinter._print_Range.<locals>._print_symbolic_rangerD  r   rE  r  Tz\left\{r'  c                 3  &    | ]}| ur |nd V  qdS z\ldotsNr#  r  eldotsr   ro   rr   r    rR  z,LatexPrinter._print_Range.<locals>.<genexpr>z\right\})objectstartis_infinitestopstepis_positiveiternextis_emptyrI  r  is_iterabler+  )r   rq   r  printsetitro   )r  rq   r   rr   _print_Rangen  s0   

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   rE  r  z%s_{%s}r  r  )r   r   letterr   r   ro   ro   rr   __print_number_polynomial  s   z&LatexPrinter.__print_number_polynomialc                 C     |  |d|S Nr  &_LatexPrinter__print_number_polynomialr  ro   ro   rr   _print_bernoulli  r  zLatexPrinter._print_bernoullic                 C  r  )NGr  r  ro   ro   rr   _print_genocchi  r  zLatexPrinter._print_genocchic                   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 )Nr  z
B_{%s, %s}r   rE  r:  r'  c                 3  r  r   r#  r  r$  ro   rr   r    s    z+LatexPrinter._print_bell.<locals>.<genexpr>r  r  r  )rY  r  r   r+  r  )r   r   r   tex1tex2r   ro   r$  rr   _print_bell  s   
zLatexPrinter._print_bellc                 C  r  NFr  r  ro   ro   rr   _print_fibonacci  r  zLatexPrinter._print_fibonaccic                 C  ,   d|  |jd  }|d urd||f }|S )NzL_{%s}r   r  r  rm  ro   ro   rr   _print_lucas     zLatexPrinter._print_lucasc                 C  r  )NTr  r  ro   ro   rr   _print_tribonacci  r  zLatexPrinter._print_tribonaccic                 C  s4   |d u rd|  |jd  S d||  |jd f S )Nz\mu\left(%s\right)r   z\mu^{%s}\left(%s\right)r  r  ro   ro   rr   _print_mobius  s   zLatexPrinter._print_mobiusc                   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}r  r  rE  r  rT  r'  c                 3  r  r  r#  r  r  ro   rr   r    rR  z1LatexPrinter._print_SeqFormula.<locals>.<genexpr>rU  )r  rY  r  free_symbolsr  r   formular  r   r,  coeffr+  lengthr  r  r+  )r   rq   r  r  ro   r  rr   _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   r   r   r   z\left%s%s, %s\right%s)r  endr   	left_open
right_open)r   r   rm  rn  ro   ro   rr   _print_Interval  s   zLatexPrinter._print_Intervalc                 C  rT  )Nz \left\langle %s, %s\right\rangle)r   r   r   r   r   ro   ro   rr   _print_AccumulationBounds  s   z&LatexPrinter._print_AccumulationBoundsc                   (   t |  fdd|jD }d|S )Nc                      g | ]} | qS ro   r   r  rw  r   ro   rr   r%    r  z-LatexPrinter._print_Union.<locals>.<listcomp>z \cup r   r  r+  r   uargs_strro   r  rr   _print_Union     
zLatexPrinter._print_Unionc                   r	  )Nc                   r
  ro   r  r  r  ro   rr   r%  	  r  z2LatexPrinter._print_Complement.<locals>.<listcomp>z \setminus r  r  ro   r  rr   _print_Complement 	  r  zLatexPrinter._print_Complementc                   r	  )Nc                   r
  ro   r  r  r  ro   rr   r%  	  r  z4LatexPrinter._print_Intersection.<locals>.<listcomp>z \cap r  r  ro   r  rr   _print_Intersection	  r  z LatexPrinter._print_Intersectionc                   r	  )Nc                   r
  ro   r  r  r  ro   rr   r%  	  r  z;LatexPrinter._print_SymmetricDifference.<locals>.<listcomp>z \triangle r  r  ro   r  rr   _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 )NrE  r   z^{%d}r   c                 3  s    | ]	} | V  qd S r   r  )r  r  r  ro   rr   r  	  s    
z1LatexPrinter._print_ProductSet.<locals>.<genexpr>)r   rY  setsr#   r   r+  r   r  ro   r  rr   _print_ProductSet	  s    zLatexPrinter._print_ProductSetc                 C  re  )Nz	\emptysetro   r4  ro   ro   rr   _print_EmptySet	  rg  zLatexPrinter._print_EmptySetc                 C  re  )Nz
\mathbb{N}ro   r   r  ro   ro   rr   _print_Naturals	  rg  zLatexPrinter._print_Naturalsc                 C  re  )Nz\mathbb{N}_0ro   r  ro   ro   rr   _print_Naturals0	  rg  zLatexPrinter._print_Naturals0c                 C  re  Nz
\mathbb{Z}ro   r  ro   ro   rr   _print_Integers	  rg  zLatexPrinter._print_Integersc                 C  re  Nz
\mathbb{Q}ro   r  ro   ro   rr   _print_Rationals"	  rg  zLatexPrinter._print_Rationalsc                 C  re  Nz
\mathbb{R}ro   r  ro   ro   rr   _print_Reals%	  rg  zLatexPrinter._print_Realsc                 C  re  Nz
\mathbb{C}ro   r  ro   ro   rr   _print_Complexes(	  rg  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$  ro   rr   r  .	  s   & z/LatexPrinter._print_ImageSet.<locals>.<genexpr>r'  c                 s  s    | ]}d | V  qdS )	%s \in %sNro   )r  xyro   ro   rr   r  /	  r  z!\left\{%s\; \middle|\; %s\right\})rW   r   	signaturer  	base_setsr+  r   )r   rq   r   sigxysxinysro   r$  rr   _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"  ro   r#  r  ra  r$  ro   rr   r%  3	  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  base_setr   UniversalSetr   	conditionr   rq   
vars_printro   r$  rr   _print_ConditionSet2	  s   

z LatexPrinter._print_ConditionSetc                 C     |  |jd }d|S )Nr   z\mathcal{{P}}\left({}\right)r   r  r   )r   r   	arg_printro   ro   rr   _print_PowerSet>	  s   
zLatexPrinter._print_PowerSetc                   s8   d  fdd|jD }d |j| |jf S )Nr'  c                   r"  ro   r#  r-  r$  ro   rr   r%  C	  r&  z5LatexPrinter._print_ComplexRegion.<locals>.<listcomp>z)\left\{%s\; \middle|\; %s \in %s \right\})r+  r  r   r   r  r1  ro   r$  rr   _print_ComplexRegionB	  s   

z!LatexPrinter._print_ComplexRegionc                   s   dt  fdd|jD  S )Nr%  c                 3  r  r   r#  r  r$  ro   rr   r  J	  r  z/LatexPrinter._print_Contains.<locals>.<genexpr>)r  r  r4  ro   r$  rr   _print_ContainsI	     zLatexPrinter._print_Containsc                 C  s:   |j jtju r|jjtju r| |jS | | d S )Nz	 + \ldots)	r  r  r   r  bnr   a0r@  truncater   ro   ro   rr   _print_FourierSeriesL	  s   z!LatexPrinter._print_FourierSeriesc                 C  r  r   )r@  infiniter   ro   ro   rr   _print_FormalPowerSeriesQ	  r  z%LatexPrinter._print_FormalPowerSeriesc                 C  s
   d|j  S )Nz\mathbb{F}_{%s})modr   ro   ro   rr   _print_FiniteFieldT	  r   zLatexPrinter._print_FiniteFieldc                 C  re  r  ro   r   ro   ro   rr   _print_IntegerRingW	  rg  zLatexPrinter._print_IntegerRingc                 C  re  r  ro   r   ro   ro   rr   _print_RationalFieldZ	  rg  z!LatexPrinter._print_RationalFieldc                 C  re  r!  ro   r   ro   ro   rr   _print_RealField]	  rg  zLatexPrinter._print_RealFieldc                 C  re  r#  ro   r   ro   ro   rr   _print_ComplexField`	  rg  z LatexPrinter._print_ComplexFieldc                 C  ,   |  |j}dt| j |j}d||f S )Nr'  z%s\left[%s\right]r   domainr+  r  r(  r   r   rI  r(  ro   ro   rr   _print_PolynomialRingc	     z"LatexPrinter._print_PolynomialRingc                 C  rG  )Nr'  r*  rH  rJ  ro   ro   rr   _print_FractionFieldh	  rL  z!LatexPrinter._print_FractionFieldc                 C  s<   |  |j}dt| j |j}d}|jsd}d|||f S )Nr'  r   zS_<^{-1}z%s%s\left[%s\right])r   rI  r+  r  r(  is_Poly)r   r   rI  r(  invro   ro   rr   _print_PolynomialRingBasem	  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 )Nr   r   rE  r:  rf  r  r   )r  rf  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  r  
get_domainr3  )r   polyrb  r=  monomr   s_monomr   r   s_coeffs_termmodifierr   rQ  rI  r  r   ro   ro   rr   _print_Polyu	  sR   





zLatexPrinter._print_Polyc                 C  sJ   |j j}|dkr
d}| |j}|j}|tv rd|||f S d|||f S )NComplexRootOfCRootOfz\%s {\left(%s, %d\right)}z'\operatorname{%s} {\left(%s, %d\right)})r)  r*  r   r   r  r3  )r   r:   rb  r   r  ro   ro   rr   _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   funr   IdentityFunctionr  r3  r+  )r   r   rb  r  ro   ro   rr   _print_RootSum	  s   zLatexPrinter._print_RootSumc                 C  re  )N\omegaro   r   ro   ro   rr   _print_OrdinalOmega	  rg  z LatexPrinter._print_OrdinalOmegac                 C  sF   |j \}}|dkr|dkrd||S d|S |dkr!d|S dS )NrE  z{} \omega^{{{}}}z	{} \omegaz\omega^{{{}}}rc  )r  r   )r   r   r   mulro   ro   rr   _print_OmegaPower	  s   


zLatexPrinter._print_OmegaPowerc                   s   d  fdd|jD S )Nr9  c                   r"  ro   r#  rT  r$  ro   rr   r%  	  r&  z/LatexPrinter._print_Ordinal.<locals>.<listcomp>)r+  r  r   ro   r$  rr   _print_Ordinal	  s   zLatexPrinter._print_Ordinalc                 C  s   | j d }|| td|S )Nr   z	{%s}^{%d})r   r   r    )r   rV  r   ro   ro   rr   _print_PolyElement	  s   
zLatexPrinter._print_PolyElementc                 C  s:   |j dkr| |jS | |j}| |j }d||f S )NrE  r  )r  r   r  )r   r9   r  r  ro   ro   rr   _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 )NrE  r   zE_{%s}r  r*  r  )r   r   r   r  r  r   ro   ro   rr   _print_euler	  s   &zLatexPrinter._print_eulerc                 C  r  )NzC_{%s}r   r  r  rm  ro   ro   rr   _print_catalan	  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}r   rE  r   r  r   r   r  )r   r   rq   inversero   ro   rr   _print_UnifiedTransform	  s   >z$LatexPrinter._print_UnifiedTransformc                 C     |  |dS )NMrn  r   ro   ro   rr   _print_MellinTransform	  r  z#LatexPrinter._print_MellinTransformc                 C     |  |ddS )Nrp  Trq  r   ro   ro   rr   _print_InverseMellinTransform	  r  z*LatexPrinter._print_InverseMellinTransformc                 C  ro  )NLrq  r   ro   ro   rr   _print_LaplaceTransform	  r  z$LatexPrinter._print_LaplaceTransformc                 C  rs  )Nru  Trq  r   ro   ro   rr   _print_InverseLaplaceTransform
  r  z+LatexPrinter._print_InverseLaplaceTransformc                 C  ro  r  rq  r   ro   ro   rr   _print_FourierTransform
  r  z$LatexPrinter._print_FourierTransformc                 C  rs  )Nr  Trq  r   ro   ro   rr   _print_InverseFourierTransform
  r  z+LatexPrinter._print_InverseFourierTransformc                 C  ro  )NSINrq  r   ro   ro   rr   _print_SineTransform

  r  z!LatexPrinter._print_SineTransformc                 C  rs  )Nrz  Trq  r   ro   ro   rr   _print_InverseSineTransform
  r  z(LatexPrinter._print_InverseSineTransformc                 C  ro  )NCOSrq  r   ro   ro   rr   _print_CosineTransform
  r  z#LatexPrinter._print_CosineTransformc                 C  rs  )Nr}  Trq  r   ro   ro   rr   _print_InverseCosineTransform
  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   to_sympyr   reprr  ro   ro   rr   
_print_DMP
  s   
zLatexPrinter._print_DMPc                 C  r  r   )r  r  ro   ro   rr   
_print_DMF
  r   zLatexPrinter._print_DMFc                 C  s   |  t|jS r   r   r   r,  )r   r  ro   ro   rr   _print_Object"
  r  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   r%  r   rE  zW%s\left(%s\right)zW{0}_{{{1}}}\left({2}\right))r   r  rY  r   )r   r   r   arg0resultarg1ro   ro   rr   _print_LambertW%
  s   zLatexPrinter._print_LambertWc                 C     d | |jd S )Nz!\operatorname{{E}}\left[{}\right]r   rl  r   ro   ro   rr   _print_Expectation/
     zLatexPrinter._print_Expectationc                 C  r  )Nz#\operatorname{{Var}}\left({}\right)r   rl  r   ro   ro   rr   _print_Variance2
  r  zLatexPrinter._print_Variancec                   s    d d fdd|jD S )Nz#\operatorname{{Cov}}\left({}\right)r'  c                 3  r  r   r#  rT  r$  ro   rr   r  6
  r  z1LatexPrinter._print_Covariance.<locals>.<genexpr>)r   r+  r  r   ro   r$  rr   _print_Covariance5
  s    zLatexPrinter._print_Covariancec                 C  r  )Nz!\operatorname{{P}}\left({}\right)r   rl  r   ro   ro   rr   _print_Probability8
  r  zLatexPrinter._print_Probabilityc                 C  s$   |  |j}|  |j}d||f S )Nz%s\rightarrow %s)r   rI  codomain)r   morphismrI  r  ro   ro   rr   _print_Morphism;
  s   zLatexPrinter._print_Morphismc                 C  s&   |  |j|  |j}}d||f S )Nr  )r   r  den)r   r   r  r  ro   ro   rr   _print_TransferFunction@
  s   z$LatexPrinter._print_TransferFunctionc                   s(   t  j} fdd}dt||S )Nc                   s    | t dS rh  )r   r   r  r  ro   rr   rs   F
  s    z,LatexPrinter._print_Series.<locals>.<lambda>r   )r  r  r+  r  r   r   r  rk  ro   r  rr   _print_SeriesD
  s   
zLatexPrinter._print_Seriesc                   s@   ddl m  tjd d d } fdd}dt||S )Nr   )MIMOParallelrD  c                   s&   t |  r| tdS | S rh  )r  r   r   r   r  r  r   r   ro   rr   rs   M
  s
   
z0LatexPrinter._print_MIMOSeries.<locals>.<lambda>z\cdot)sympy.physics.control.ltir  r  r  r+  r  r  ro   r  rr   _print_MIMOSeriesJ
  s   zLatexPrinter._print_MIMOSeriesc                 C     d t| j|jS Nr9  r+  r  r   r  r   ro   ro   rr   _print_ParallelQ
  r  zLatexPrinter._print_Parallelc                 C  r  r  r  r   ro   ro   rr   _print_MIMOParallelT
  r  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SeriesrE  rD  rf  r  z\frac{%s}{%s %s %s})sympy.physics.controlr  r  sys1ra  r  r  r  sys2r   r  )r   r   r  r  r  tfnum_arg_listden_arg_list
den_term_1
den_term_2r  denom_1denom_2_signro   ro   rr   _print_FeedbackW
  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   )
MIMOSeriesrD  rf  r  z)\left(I_{\tau} %s %s\right)^{-1} \cdot %s)r  r  r   r  r  r  )r   r   r  inv_matr  r  ro   ro   rr   _print_MIMOFeedback{
  s
   z LatexPrinter._print_MIMOFeedbackc                 C  s   |  |j}d| S )Nz%s_\tau)r   	_expr_matra  ro   ro   rr   _print_TransferFunctionMatrix
  s   z*LatexPrinter._print_TransferFunctionMatrixc                 C  s   d |jj|jS )Nz\text{{{}}}_{{{}}})r   r)  r*  r  r   ro   ro   rr   
_print_DFT
  r  zLatexPrinter._print_DFTc                 C  s&   |  t|j}| |}d||f S )Nz%s:%s)r   r   r,  r  )r   r  pretty_namepretty_morphismro   ro   rr   _print_NamedMorphism
  s   
z!LatexPrinter._print_NamedMorphismc                 C  s"   ddl m} | ||j|jdS )Nr   )NamedMorphismid)sympy.categoriesr  r  rI  r  )r   r  r  ro   ro   rr   _print_IdentityMorphism
  s   
z$LatexPrinter._print_IdentityMorphismc                   s<    fdd|j D }|  d|d } |}|| S )Nc                   s   g | ]
}  t|jqS ro   r  )r  	componentr$  ro   rr   r%  
  s    z9LatexPrinter._print_CompositeMorphism.<locals>.<listcomp>z\circ rO  )r  reverser+  r  )r   r  component_names_listcomponent_namesr  ro   r$  rr   _print_CompositeMorphism
  s   

z%LatexPrinter._print_CompositeMorphismc                 C  s   d | t|jS Nr*  )r   r   r   r,  )r   r  ro   ro   rr   _print_Category
  r  zLatexPrinter._print_Categoryc                 C  s<   |j s	| tjS | |j }|jr|d| |j 7 }|S )Nz\Longrightarrow %s)premisesr   r   EmptySetconclusions)r   diagramlatex_resultro   ro   rr   _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}
r   r   rE  & r  
z\end{array}
)widthrX  heightlatex)r   gridr  r   r   ro   ro   rr   _print_DiagramGrid
  s   
zLatexPrinter._print_DiagramGridc                 C     d | |j| |jS )Nz{{{}}}^{{{}}})r   r   r  r  r   rp  ro   ro   rr   _print_FreeModule
  r:  zLatexPrinter._print_FreeModulec                   s   d d fdd|D S )N\left[ {} \right]rF  c                 3  "    | ]}d   | d V  qdS r   rn   Nr#  r
  r$  ro   rr   r  
      
z8LatexPrinter._print_FreeModuleElement.<locals>.<genexpr>)r   r+  )r   r  ro   r$  rr   _print_FreeModuleElement
  s   
z%LatexPrinter._print_FreeModuleElementc                   sJ   fddj D }dd  dd d fdd|D }d	|S )
Nc                   s    g | ]} fd d|D qS )c                   s   g | ]}  j|qS ro   )r   r  r  r
  r  r   ro   rr   r%  
      z<LatexPrinter._print_SubModule.<locals>.<listcomp>.<listcomp>ro   r  gr  ro   rr   r%  
  s     z1LatexPrinter._print_SubModule.<locals>.<listcomp>c                 S  rm   )Nr   rn   ro   rL   ro   ro   rr   rs   
  rt   z/LatexPrinter._print_SubModule.<locals>.<lambda>c                 S  rm   )Nz\left[ z \right]ro   r  ro   ro   rr   rs   
  rt   rF  c                 3  s0    | ]} d   fdd|D V  qdS )rF  c                 3  s    | ]} |V  qd S r   ro   r
  )curlyro   rr   r  
  r  z:LatexPrinter._print_SubModule.<locals>.<genexpr>.<genexpr>N)r+  r  )r  squarero   rr   r  
  s   . z0LatexPrinter._print_SubModule.<locals>.<genexpr>\left\langle {} \right\rangle)rQ  r+  r   r   r  rQ  
gens_latexro   )r  r  r   r  rr   _print_SubModule
  s
   
zLatexPrinter._print_SubModulec                   s$   d  fdd|jD }d|S )NrF  c                   s   g | ]}d   | d qS )r   rn   r#  r  r$  ro   rr   r%  
  r  z9LatexPrinter._print_SubQuotientModule.<locals>.<listcomp>r  )r+  rQ  r   )r   r  r  ro   r$  rr   _print_SubQuotientModule
  s   
z%LatexPrinter._print_SubQuotientModulec                   s8    fdd j jD }dfdd|D }d|S )Nc                   s   g | ]	\} j |qS ro   )r  r  r
  r  ro   rr   r%  
  r8  z>LatexPrinter._print_ModuleImplementedIdeal.<locals>.<listcomp>rF  c                 3  r  r  r#  r
  r$  ro   rr   r  
  s     z=LatexPrinter._print_ModuleImplementedIdeal.<locals>.<genexpr>r  )_modulerQ  r+  r   r  ro   r  rr   _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	   TrJ  )r   r    r  r$  ro   rr   r%  
  s    z2LatexPrinter._print_Quaternion.<locals>.<listcomp>r   c                 S  s   g | ]
\}}|d  | qS )r   ro   )r  r   r   ro   ro   rr   r%  
  r  rE  ijkr9  )r  r  r+  )r   r   rq   r  ro   r$  rr   _print_Quaternion
  s
   
&
zLatexPrinter._print_Quaternionc                 C  r  Nz\frac{{{}}}{{{}}})r   r   r  
base_ideal)r   Rro   ro   rr   _print_QuotientRing
     
z LatexPrinter._print_QuotientRingc                 C  s(   |  |j|}d||  |jjS )N{{{}}} + {{{}}})r   r  r  r   r  )r   r  x_latexro   ro   rr   _print_QuotientRingElement
  s   z'LatexPrinter._print_QuotientRingElementc                   sH    fdd j D }ddfdd|D }d| jjS )Nc                   s   g | ]	} j j|qS ro   )moduler  r  r
  r  ro   rr   r%  
  r8  z=LatexPrinter._print_QuotientModuleElement.<locals>.<listcomp>r  rF  c                 3  r  r  r#  r
  r$  ro   rr   r  
  r  z<LatexPrinter._print_QuotientModuleElement.<locals>.<genexpr>r  )datar   r+  r   r  killed_module)r   r  r  
data_latexro   r  rr   _print_QuotientModuleElement
  s   
z)LatexPrinter._print_QuotientModuleElementc                 C  r  r  )r   r   r  r  r  ro   ro   rr   _print_QuotientModule
  r  z"LatexPrinter._print_QuotientModulec                 C  s(   d | | | |j| |jS )Nz{{{}}} : {{{}}} \to {{{}}})r   r   _sympy_matrixrI  r  )r   rF  ro   ro   rr   _print_MatrixHomomorphism
  s   z&LatexPrinter._print_MatrixHomomorphismc                 C  sL   |  |jj\}}}d| }|r|dd| 7 }|r$|dd| 7 }|S )Nr3  r%  r   r  )r(  r,  r+  )r   manifoldr,  r'  r  ro   ro   rr   _print_Manifold
  s   zLatexPrinter._print_Manifoldc                 C  rT  )Nz\text{%s}_{%s})r   r,  r  )r   patchro   ro   rr   _print_Patch  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   coordsysro   ro   rr   _print_CoordSystem  s    zLatexPrinter._print_CoordSystemc                 C  s   d|  |j S )Nz\mathbb{\nabla}_{%s})r   _wrt)r   cvdro   ro   rr   _print_CovarDerivativeOp  r  z%LatexPrinter._print_CovarDerivativeOpc                 C  $   |j j|j j}d| t|S r  
_coord_sysr(  _indexr,  r   r   r   r   fieldr)  ro   ro   rr   _print_BaseScalarField     z#LatexPrinter._print_BaseScalarFieldc                 C  r  )Nz\partial_{{{}}}r  r  ro   ro   rr   _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))	_form_fieldr  r  r(  r  r,  r   r   r   )r   diffr  r)  ro   ro   rr   _print_Differential  s   


z LatexPrinter._print_Differentialc                 C  r4  )Nr   z"\operatorname{{tr}}\left({}\right)r5  )r   r  contentsro   ro   rr   	_print_Tr!  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)r  r  ro   ro   rr   _print_totient&  
   zLatexPrinter._print_totientc                 C  r  )Nz(\left(\lambda\left(%s\right)\right)^{%s}r   z\lambda\left(%s\right)r  r  ro   ro   rr   _print_reduced_totient,  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)rE  r   r:  z\sigma^{%s}%sz\sigma%sr  rm  ro   ro   rr   _print_divisor_sigma2     

z!LatexPrinter._print_divisor_sigmac                 C  r  )Nr  r  rE  r   r:  z\sigma^*^{%s}%sz
\sigma^*%sr  rm  ro   ro   rr   _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)r  r  ro   ro   rr   _print_primenuF  r  zLatexPrinter._print_primenuc                 C  r  )Nz'\left(\Omega\left(%s\right)\right)^{%s}r   z\Omega\left(%s\right)r  r  ro   ro   rr   _print_primeomegaL  r  zLatexPrinter._print_primeomegac                 C  s
   t |jS r   )r   r,  r   ro   ro   rr   
_print_StrR  r   zLatexPrinter._print_Strc                 C  rR  r   )r   r   r   ro   ro   rr   _print_floatU  r  zLatexPrinter._print_floatc                 C     t |S r   r   r   ro   ro   rr   
_print_intX  r6  zLatexPrinter._print_intc                 C  r  r   r  r   ro   ro   rr   
_print_mpz[  r6  zLatexPrinter._print_mpzc                 C  r  r   r  r   ro   ro   rr   
_print_mpq^  r6  zLatexPrinter._print_mpqc                 C  r  r   r  r   ro   ro   rr   _print_fmpza  r6  zLatexPrinter._print_fmpzc                 C  r  r   r  r   ro   ro   rr   _print_fmpqd  r6  zLatexPrinter._print_fmpqc                 C  s   d tt|jS )Nz"\operatorname{{Q}}_{{\text{{{}}}}})r   r   r   r,  r   ro   ro   rr   _print_Predicateg  r  zLatexPrinter._print_Predicatec                   s:   |j }|j} |}d fdd|D }d||f S )Nr'  c                   r"  ro   r#  r  r$  ro   rr   r%  n  r&  z8LatexPrinter._print_AppliedPredicate.<locals>.<listcomp>z%s(%s))r  	argumentsr   r+  )r   r   predr  
pred_latex
args_latexro   r$  rr   _print_AppliedPredicatej  s
   
z$LatexPrinter._print_AppliedPredicatec                   s   t  |}dt| S )Nz\mathtt{\text{%s}})superemptyPrinterr   r  r)  ro   rr   r+  q  s   zLatexPrinter.emptyPrinterr   r  )FF)r   r   )r   r   )r/  r0  )r   r   )r   r   )r  r   r   r   )r   r  )r  r   r   r   )r   r   r   r   r  )r  r   r   r   rP  )FN)r   )r   r   )r,  r   r   r#  )r)  r   r   r   )r   r%   )r  r  )F(C  r*  
__module____qualname__printmethodr   __annotations__r   r   r   r   r   r   r   r  r  r  r   r   r!  r.  r5  _print_BooleanTrue_print_BooleanFalser7  r@  rN  r_  rb  rs  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r)  r0  r6  rQ  rS  rU  propertyr`  rc  rd  rf  rk  
_print_Min
_print_Maxrn  rp  rq  rs  rw  ry  r  r  r  r  r  r~  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  _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
  r  r  r  r  r  r  r  r"  _print_RandomSymbolr(  r(  r3  r7  rD  rI  rN  rX  rZ  r`  rb  rf  rl  ro  rq  rr  r{  r}  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  _print_frozensetr  r  r  r  r  r  r  r  r  r  _print_SeqPer_print_SeqAdd_print_SeqMulr  r  r  r  r  r  r  r  r  r  r  r   r"  r$  r,  r3  r7  r8  r9  r>  r@  rB  rC  rD  rE  rF  rK  rM  rP  r\  r_  rb  rd  rf  rg  rh  ri  rj  rk  rn  rr  rt  rv  rw  rx  ry  r{  r|  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  _print_IDFTr  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+  __classcell__ro   ro   r,  rr   r      s  
 	
=

	!s-!
$ L

										
7



*9		
$	

r   c                 C  s   t | }|r	|S |  tv rd|   S | tv rd|  S tt tddD ]"}|  	|rHt| t|krHt| 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}}}'"
    r   T)r  r  N)tex_greek_dictionaryr   rZ  greek_letters_setother_symbolsri  r   r  rY  r  r&  )rq   r   r  ro   ro   rr   r&  x  s   
"r&  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.
    diff_operator: string, optional
        String to use for differential operator. Default is ``'d'``, to print in italic
        form. ``'rd'``, ``'td'`` are shortcuts for ``\mathrm{d}`` and ``\text{d}``.
    adjoint_style: string, optional
        String to use for the adjoint symbol. Defined options are ``'dagger'``
        (default),``'star'``, and ``'hermitian'``.

    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.

    )r   r   r   r   ro   ro   rr   r    s    Xr  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  rA  ro   ro   rr   print_latexq  s   rC  rE  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.

    eqnarrayz\begin{eqnarray}
z& = &z	\nonumberz
\end{eqnarray}TIEEEeqnarrayz\begin{IEEEeqnarray}{rCl}
z
\end{IEEEeqnarray}rD  z\begin{align*}
z= &r   z
\end{align*}FzUnknown environment: {}z\dotsrE  rf  z& & r  r  r  r   rD  r  z{:s} {:s}{:s} {:s} {:s}z{:s}{:s} {:s} {:s}Nro   )r   r   r   as_ordered_termsrY  rX  r  r   )r-  r/  terms_per_lineenvironmentuse_dotsr   r  r  
first_termnonumberend_termdoubleetr  r=  n_terms
term_countr   r>  
term_startterm_endr  ro   ro   rr   multiline_latexx  sn   C


rS  )rq   r   r   r   )rE  rD  F)J__doc__
__future__r   typingr   r   r   r  
sympy.corer   r   r   r	   r
   r   r   r   sympy.core.alphabetsr   sympy.core.containersr   sympy.core.functionr   r   r   sympy.core.operationsr   sympy.core.powerr   sympy.core.sortingr   sympy.core.sympifyr   r|  r   r   r   sympy.printing.precedencer   sympy.printing.printerr   r   sympy.printing.conventionsr   r   r   r    mpmath.libmp.libmpfr!   r"   rj  sympy.utilities.iterablesr#   r$   r7   sympy.tensor.arrayr%   sympy.vector.basisdependentr&   r3  r>  r@  r   r0  	frozensetr?  compiler  r   r   r&  r  rC  rS  ro   ro   ro   rr   <module>   sZ   (	
 !"#'	

                     

 Z