B
    ™‘[ä€  ã               @   sÀ   d Z ddlmZ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 dd
lmZmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZddd„Zddd„ZeZdS )z
A MathML printer.
é    )Úprint_functionÚdivision)ÚsympifyÚSÚMul)Ú_coeff_isneg)Úgreeks)Úrangeé   )ÚPrinter)Úgreek_unicode)Úsplit_super_subÚrequires_partialc               @   s<   e Zd ZdZdddœZddd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚMathMLPrinterBasez^Contains common code required for MathMLContentPrinter and
    MathMLPresentationPrinter.
    Nzutf-8)ÚorderÚencodingc                sN   t  ˆ|¡ ddlm}m} |ƒ ˆ_G dd„ d|ƒ‰ ‡ ‡fdd„}|ˆj_d S )Nr   )ÚDocumentÚTextc               @   s   e Zd Zddd„ZdS )z+MathMLPrinterBase.__init__.<locals>.RawTextÚ c             S   s    | j r| d || j |¡¡ d S )Nz{}{}{})ÚdataÚwriteÚformat)ÚselfÚwriterÚindentÚ	addindentÚnewl© r   ú4lib/python3.7/site-packages/sympy/printing/mathml.pyÚwritexml"   s    z4MathMLPrinterBase.__init__.<locals>.RawText.writexmlN)r   r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   ÚRawText!   s   r#   c                s   ˆ ƒ }| |_ ˆj|_|S )N)r   ÚdomZownerDocument)r   Úr)r#   r   r   r   ÚcreateRawTextNode&   s    z5MathMLPrinterBase.__init__.<locals>.createRawTextNode)r   Ú__init__Úxml.dom.minidomr   r   r$   ÚcreateTextNode)r   Úsettingsr   r   r&   r   )r#   r   r   r'      s    zMathMLPrinterBase.__init__c             C   s,   t  | |¡}| ¡ }| dd¡}| ¡ }|S )z2
        Prints the expression as MathML.
        ÚasciiÚxmlcharrefreplace)r   Ú_printZtoxmlÚencodeÚdecode)r   ÚexprZmathMLZunistrZxmlbstrZresr   r   r   Údoprint.   s
    zMathMLPrinterBase.doprintc                sV   ddl m}m}m‰ m‰ d‡ ‡fdd„	}|j| _||_d‡fdd„	}|j| _||_d S )	Nr   )ÚElementr   ÚNodeÚ_write_datar   c       	         s  |  |d | j ¡ |  ¡ }t| ¡ ƒ}| ¡  x4|D ],}|  d| ¡ ˆ||| jƒ |  d¡ q6W | jrü|  d¡ t| jƒdkr®| jd j	ˆ j
kr®| jd  |ddd¡ n8|  |¡ x"| jD ]}| ||| ||¡ qÀW |  |¡ |  d| j|f ¡ n|  d	| ¡ d S )
Nú<z %s="ú"ú>r
   r   r   z</%s>%sz/>%s)r   ZtagNameZ_get_attributesÚlistÚkeysÚsortÚvalueZ
childNodesÚlenZnodeTypeZ	TEXT_NODEr   )	r   r   r   r   r   ZattrsZa_namesZa_nameZnode)r3   r4   r   r   r   B   s(    



z/MathMLPrinterBase.apply_patch.<locals>.writexmlc                s   ˆ |d|| j |f ƒ d S )Nz%s%s%s)r   )r   r   r   r   r   )r4   r   r   r   a   s    )r   r   r   )r   r   r   )r(   r2   r   r3   r4   r   Ú_Element_writexml_oldÚ_Text_writexml_old)r   r2   r   r   r   )r3   r4   r   Úapply_patch8   s    zMathMLPrinterBase.apply_patchc             C   s$   ddl m}m} | j|_| j|_d S )Nr   )r2   r   )r(   r2   r   r=   r   r>   )r   r2   r   r   r   r   Úrestore_patchf   s    zMathMLPrinterBase.restore_patch)N)	r    r!   r"   Ú__doc__Z_default_settingsr'   r1   r?   r@   r   r   r   r   r      s   

.r   c               @   sÞ   e Zd ZdZdZdd„ Zdd„ Zd6d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d0d1„ Zd2d3„ Zd4d5„ ZdS )7ÚMathMLContentPrinterz}Prints an expression to the Content MathML markup language.

    References: https://www.w3.org/TR/MathML2/chapter4.html
    Z_mathml_contentc             C   sv   dddddddddd	d
ddddddddddddddddddœ}x&|j jD ]}|j}||krH|| S qHW |j j}| ¡ S )z)Returns the MathML tag for an expression.ÚplusÚtimesÚdiffÚcnZpowerÚciÚintÚsumÚsinÚcosÚtanÚcotÚarcsinÚarcsinhÚarccosÚarccoshÚarctanÚarctanhÚarccotZlnÚeqZneqZgeqZleqÚgtÚlt)ZAddr   Ú
DerivativeÚNumberrH   ZPowÚSymbolÚIntegralÚSumrJ   rK   rL   rM   ÚasinÚasinhÚacosÚacoshÚatanÚatanhÚacotÚatan2ÚlogÚEqualityÚ
UnequalityÚGreaterThanÚLessThanÚStrictGreaterThanÚStrictLessThan)Ú	__class__Ú__mro__r    Úlower)r   ÚeÚ	translateÚclsÚnr   r   r   Ú
mathml_tags   sD    zMathMLContentPrinter.mathml_tagc       	      C   sD  t |ƒr<| j d¡}| | j d¡¡ | |  | ¡¡ |S ddlm} ||ƒ\}}|tjk	r | j d¡}| | j d¡¡ | |  	|¡¡ | |  	|¡¡ |S | 
¡ \}}|tjkrÐt|ƒdkrÐ|  	|d ¡S | jdkrèt |¡ ¡ }| j d¡}| | j d¡¡ |dkr | |  	|¡¡ x|D ]}| |  	|¡¡ q&W |S )	NÚapplyÚminusr   )ÚfractionÚdivider
   ÚoldrD   )r   r$   ÚcreateElementÚappendChildÚ
_print_MulÚsympy.simplifyrv   r   ÚOner-   Úas_coeff_mulr<   r   r   Ú
_from_argsÚas_ordered_factors)	r   r0   Úxrv   ÚnumerÚdenomÚcoeffÚtermsÚtermr   r   r   r{   œ   s2    



zMathMLContentPrinter._print_MulNc             C   s  | j ||d}|  |d ¡}g }x¢|dd … D ]’}t|ƒr| j d¡}| | j d¡¡ | |¡ | |  | ¡¡ |}||d krÀ| |¡ q.| |¡ |  |¡}||d kr.| |  |¡¡ q.W t|ƒdkrÔ|S | j d¡}| | j d¡¡ x"t|ƒdkr| | d¡¡ qôW |S )N)r   r   r
   rt   ru   éÿÿÿÿrC   )	Ú_as_ordered_termsr-   r   r$   ry   rz   Úappendr<   Úpop)r   r0   r   ÚargsZlastProcessedZ	plusNodesÚargr   r   r   r   Ú
_print_Add¿   s.    


zMathMLContentPrinter._print_Addc          	   C   sf   | j  d¡}xTt|jƒD ]F}| j  d¡}x*t|jƒD ]}| |  |||f ¡¡ q4W | |¡ qW |S )NZmatrixZ	matrixrow)r$   ry   r	   ÚrowsÚcolsrz   r-   )r   Úmr   ÚiZx_rÚjr   r   r   Ú_print_MatrixBaseÛ   s    z&MathMLContentPrinter._print_MatrixBasec             C   s°   |j dkr2| j d¡}| | j t|jƒ¡¡ |S | j d¡}| | j d¡¡ | j d¡}| | j t|jƒ¡¡ | j d¡}| | j t|j ƒ¡¡ | |¡ | |¡ |S )Nr
   rF   rt   rw   )Úqr$   ry   rz   r)   ÚstrÚp)r   ro   r   ÚxnumZxdenomr   r   r   Ú_print_Rationalä   s    


z$MathMLContentPrinter._print_Rationalc             C   s–   | j  d¡}| | j  |  |¡¡¡ | j  d¡}| j  d¡}| |  |jd ¡¡ | |  |jd ¡¡ | |¡ | |¡ | |  |jd ¡¡ |S )Nrt   ÚbvarÚlowlimitr
   é   r   )r$   ry   rz   rs   r-   r‹   )r   ro   r   Úx_1Úx_2r   r   r   Ú_print_Limitö   s    

z!MathMLContentPrinter._print_Limitc             C   s   | j  d¡S )NZ
imaginaryi)r$   ry   )r   ro   r   r   r   Ú_print_ImaginaryUnit  s    z)MathMLContentPrinter._print_ImaginaryUnitc             C   s   | j  d¡S )NZ
eulergamma)r$   ry   )r   ro   r   r   r   Ú_print_EulerGamma  s    z&MathMLContentPrinter._print_EulerGammac             C   s"   | j  d¡}| | j  d¡¡ |S )zvWe use unicode #x3c6 for Greek letter phi as defined here
        http://www.w3.org/2003/entities/2007doc/isogrk1.htmlrF   u   Ï†)r$   ry   rz   r)   )r   ro   r   r   r   r   Ú_print_GoldenRatio
  s    z'MathMLContentPrinter._print_GoldenRatioc             C   s   | j  d¡S )NZexponentiale)r$   ry   )r   ro   r   r   r   Ú_print_Exp1  s    z MathMLContentPrinter._print_Exp1c             C   s   | j  d¡S )NZpi)r$   ry   )r   ro   r   r   r   Ú	_print_Pi  s    zMathMLContentPrinter._print_Pic             C   s   | j  d¡S )NÚinfinity)r$   ry   )r   ro   r   r   r   Ú_print_Infinity  s    z$MathMLContentPrinter._print_Infinityc             C   s4   | j  d¡}| | j  d¡¡ | | j  d¡¡ |S )Nrt   ru   r¤   )r$   ry   rz   )r   ro   r   r   r   r   Ú_print_Negative_Infinity  s    z-MathMLContentPrinter._print_Negative_Infinityc                s*   ‡ ‡‡fdd„‰t ˆ jƒ}| ¡  ˆ|ƒS )Nc                s8  ˆj  d¡}| ˆj  ˆ ˆ ¡¡¡ ˆj  d¡}| ˆ | d d ¡¡ | |¡ t| d ƒdkr¾ˆj  d¡}| ˆ | d d ¡¡ | |¡ ˆj  d¡}| ˆ | d d ¡¡ | |¡ t| d ƒdkrüˆj  d¡}| ˆ | d d ¡¡ | |¡ t| ƒdkr| ˆ ˆ j¡¡ n| ˆ| dd … ƒ¡ |S )	Nrt   r™   r   é   rš   r
   Zuplimitr›   )r$   ry   rz   rs   r-   r<   Úfunction)Úlimitsr   Z	bvar_elemÚlow_elemÚup_elem)ro   Ú
lime_recurr   r   r   r¬   !  s(    



z8MathMLContentPrinter._print_Integral.<locals>.lime_recur)r8   r©   Úreverse)r   ro   r©   r   )ro   r¬   r   r   Ú_print_Integral   s    
z$MathMLContentPrinter._print_Integralc             C   s
   |   |¡S )N)r®   )r   ro   r   r   r   Ú
_print_Sum=  s    zMathMLContentPrinter._print_Sumc                sb  ˆ j  ˆ  |¡¡}‡ fdd„}ttƒ}dd„ ‰t|jƒ\}}}ˆ|ƒ}‡fdd„|D ƒ}‡fdd„|D ƒ}ˆ j  d¡}| ˆ j  |¡¡ t	|ƒd	krät	|ƒd	kr´| ˆ j  |¡¡ n.ˆ j  d
¡}	|	 |¡ |	 ||ƒ¡ | |	¡ nzt	|ƒd	kr"ˆ j  d¡}
|
 |¡ |
 ||ƒ¡ | |
¡ n<ˆ j  d¡}| |¡ | ||ƒ¡ | ||ƒ¡ | |¡ |S )Nc                s´   t | ƒdkrŠˆ j d¡}xlt| ƒD ]`\}}|dkrZˆ j d¡}| ˆ j d¡¡ | |¡ ˆ j d¡}| ˆ j |¡¡ | |¡ q"W |S ˆ j d¡}| ˆ j | d ¡¡ |S d S )Nr
   zmml:mrowr   zmml:moú zmml:mi)r<   r$   ry   Ú	enumeraterz   r)   )ÚitemsÚmrowr‘   ÚitemÚmoÚmi)r   r   r   ÚjoinE  s    
z0MathMLContentPrinter._print_Symbol.<locals>.joinc             S   s   | t krt  | ¡S | S d S )N)r   Úget)Úsr   r   r   rp   X  s    
z5MathMLContentPrinter._print_Symbol.<locals>.translatec                s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úsup)rp   r   r   ú
<listcomp>`  s    z6MathMLContentPrinter._print_Symbol.<locals>.<listcomp>c                s   g | ]}ˆ |ƒ‘qS r   r   )rº   Úsub)rp   r   r   r¼   a  s    zmml:mir   zmml:msubzmml:msupzmml:msubsup)
r$   ry   rs   Úsetr   r   Únamerz   r)   r<   )r   ÚsymrG   r·   Úgreek_lettersr¿   ÚsupersÚsubsÚmnameÚmsubÚmsupÚmsubsupr   )r   rp   r   Ú_print_SymbolB  s8    



z"MathMLContentPrinter._print_Symbolc             C   sê   |j jrš|j jdkrš| j d¡}| | j d¡¡ |j jdkr„| j d¡}| j d¡}| | j t|j jƒ¡¡ | |¡ | |¡ | |  	|j
¡¡ |S | j d¡}| j |  |¡¡}| |¡ | |  	|j
¡¡ | |  	|j ¡¡ |S )Nr
   rt   Úrootr›   ZdegreerG   )ÚexpÚis_Rationalr–   r$   ry   rz   r”   r)   r•   r-   Úbasers   )r   ro   r   ZxmldegZxmlcirœ   r   r   r   Ú
_print_Pow{  s"    


zMathMLContentPrinter._print_Powc             C   s,   | j  |  |¡¡}| | j  t|ƒ¡¡ |S )N)r$   ry   rs   rz   r)   r•   )r   ro   r   r   r   r   Ú_print_Number  s    z"MathMLContentPrinter._print_Numberc             C   s€   | j  d¡}|  |¡}t|ƒr"d}| | j  |¡¡ | j  d¡}x|jD ]}| |  |¡¡ qHW | |¡ | |  |j¡¡ |S )Nrt   Zpartialdiffr™   )r$   ry   rs   r   rz   Ú	variablesr-   r0   )r   ro   r   Zdiff_symbolrœ   rÀ   r   r   r   Ú_print_Derivative•  s    

z&MathMLContentPrinter._print_Derivativec             C   sH   | j  d¡}| | j  |  |¡¡¡ x|jD ]}| |  |¡¡ q,W |S )Nrt   )r$   ry   rz   rs   r‹   r-   )r   ro   r   rŒ   r   r   r   Ú_print_Function¤  s
    z$MathMLContentPrinter._print_Functionc             C   s4   | j  |  |¡¡}x|D ]}| |  |¡¡ qW |S )N)r$   ry   rs   rz   r-   )r   ro   r   rŒ   r   r   r   Ú_print_Basic«  s    
z!MathMLContentPrinter._print_Basicc             C   sL   | j  d¡}| j  |  |¡¡}| |¡ x|jD ]}| |  |¡¡ q0W |S )Nrt   )r$   ry   rs   rz   r‹   r-   )r   ro   r   rœ   rŒ   r   r   r   Ú_print_AssocOp±  s    
z#MathMLContentPrinter._print_AssocOpc             C   sL   | j  d¡}| | j  |  |¡¡¡ | |  |j¡¡ | |  |j¡¡ |S )Nrt   )r$   ry   rz   rs   r-   ÚlhsÚrhs)r   ro   r   r   r   r   Ú_print_Relational¹  s
    z&MathMLContentPrinter._print_Relationalc             C   s.   | j  d¡}x|D ]}| |  |¡¡ qW |S )zfMathML reference for the <list> element:
        http://www.w3.org/TR/MathML2/chapter4.html#contm.listr8   )r$   ry   rz   r-   )r   ÚseqÚdom_elementr´   r   r   r   Ú_print_listÀ  s    
z MathMLContentPrinter._print_listc             C   s,   | j  |  |¡¡}| | j  t|ƒ¡¡ |S )N)r$   ry   rs   rz   r)   r•   )r   r–   rØ   r   r   r   Ú
_print_intÈ  s    zMathMLContentPrinter._print_int)N)r    r!   r"   rA   Úprintmethodrs   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   rB   l   s6   )#
	9rB   c               @   sÎ   e Zd ZdZdZdd„ Zdd„ Zd2d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d0d1„ ZdS )3ÚMathMLPresentationPrinterz‚Prints an expression to the Presentation MathML markup language.

    References: https://www.w3.org/TR/MathML2/chapter3.html
    Z_mathml_presentationc             C   sr   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œ}x&|j jD ]}|j}||krD|| S qDW |j j}| ¡ S )z)Returns the MathML tag for an expression.z&InvisibleTimes;Úmnz&#x2192;z&dd;r¶   z&int;z&#x2211;rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   ú=z&#x2260;z&#x2265;z&#x2264;r7   r5   )r   rY   ZLimitrX   rH   rZ   r[   r\   rJ   rK   rL   rM   r]   r^   r_   r`   ra   rb   rc   rd   rf   rg   rh   ri   rj   rk   )rl   rm   r    rn   )r   ro   rp   rq   rr   r   r   r   rs   Õ  s@    z$MathMLPresentationPrinter.mathml_tagc                sd   ‡ fdd„}ˆ j  d¡}t|ƒrVˆ j  d¡}| ˆ j  d¡¡ | |¡ || |ƒ}n
|||ƒ}|S )Nc                sL  ddl m} || ƒ\}}|tjk	rZˆ j d¡}ˆ  |¡}ˆ  |¡}| |¡ | |¡ |S |  ¡ \}}	|tjkrŠt	|	ƒdkrŠˆ  |	d ¡S ˆ j
dkr¢t |	¡ ¡ }	|dkrìˆ  |¡}
ˆ j d¡}| ˆ j ˆ  | ¡¡¡ | |
¡ | |¡ xZ|	D ]R}ˆ  |¡}
| |
¡ ||	d ksòˆ j d¡}| ˆ j ˆ  | ¡¡¡ | |¡ qòW |S )Nr   )rv   Úmfracr
   rx   rµ   r‡   )r|   rv   r   r}   r$   ry   r-   rz   r~   r<   r   r   r   r€   r)   rs   )r0   r³   rv   r‚   rƒ   Úfracr—   Zxdenr„   r…   r   Úyr†   )r   r   r   Úmultiplyþ  s8    











z6MathMLPresentationPrinter._print_Mul.<locals>.multiplyr³   rµ   ú-)r$   ry   r   rz   r)   )r   r0   râ   r³   r   r   )r   r   r{   ü  s    "

z$MathMLPresentationPrinter._print_MulNc             C   s¸   | j  d¡}| j||d}| |  |d ¡¡ x„|dd … D ]t}t|ƒrt| j  d¡}| | j  d¡¡ |  | ¡}n(| j  d¡}| | j  d¡¡ |  |¡}| |¡ | |¡ q<W |S )Nr³   )r   r   r
   rµ   rã   ú+)r$   ry   rˆ   rz   r-   r   r)   )r   r0   r   r³   r‹   rŒ   r   rá   r   r   r   r   +  s    

z$MathMLPresentationPrinter._print_Addc          	   C   s’   | j  d¡}| j  d¡}xjt|jƒD ]\}| j  d¡}x@t|jƒD ]2}| j  d¡}| |  |||f ¡¡ | |¡ q@W | |¡ q$W | |¡ |S )NÚmfencedZmtableZmtrZmtd)r$   ry   r	   rŽ   r   rz   r-   )r   r   ÚbracÚtabler‘   r   r’   rá   r   r   r   r“   ?  s    
z+MathMLPresentationPrinter._print_MatrixBasec             C   sž   |j dkr2| j d¡}| | j t|jƒ¡¡ |S | j d¡}| j d¡}| | j t|jƒ¡¡ | |¡ | j d¡}| | j t|j ƒ¡¡ | |¡ |S )Nr
   rÝ   rß   )r”   r$   ry   rz   r)   r•   r–   )r   ro   r   ZnumZdenr   r   r   r˜   L  s    


z)MathMLPresentationPrinter._print_Rationalc       	      C   sÜ   | j  d¡}| j  d¡}| j  d¡}| | j  d¡¡ | j  d¡}|  |jd ¡}| j  d¡}| | j  |  |¡¡¡ |  |jd ¡}| |¡ | |¡ | |¡ | |¡ | |¡ | |¡ | |  |jd ¡¡ |S )	Nr³   Úmunderr¶   Zlimr
   rµ   r›   r   )r$   ry   rz   r)   r-   r‹   rs   )	r   ro   r³   rè   r¶   r   rœ   Zarrowr   r   r   r   rž   [  s"    





z&MathMLPresentationPrinter._print_Limitc             C   s"   | j  d¡}| | j  d¡¡ |S )Nr¶   z&ImaginaryI;)r$   ry   rz   r)   )r   ro   r   r   r   r   rŸ   q  s    z.MathMLPresentationPrinter._print_ImaginaryUnitc             C   s"   | j  d¡}| | j  d¡¡ |S )zvWe use unicode #x3c6 for Greek letter phi as defined here
        http://www.w3.org/2003/entities/2007doc/isogrk1.htmlr¶   u   Ï†)r$   ry   rz   r)   )r   ro   r   r   r   r   r¡   v  s    z,MathMLPresentationPrinter._print_GoldenRatioc             C   s"   | j  d¡}| | j  d¡¡ |S )Nr¶   z&ExponentialE;)r$   ry   rz   r)   )r   ro   r   r   r   r   r¢   }  s    z%MathMLPresentationPrinter._print_Exp1c             C   s"   | j  d¡}| | j  d¡¡ |S )Nr¶   z&pi;)r$   ry   rz   r)   )r   ro   r   r   r   r   r£   ‚  s    z#MathMLPresentationPrinter._print_Pic             C   s"   | j  d¡}| | j  d¡¡ |S )Nr¶   z&#x221E;)r$   ry   rz   r)   )r   ro   r   r   r   r   r¥   ‡  s    z)MathMLPresentationPrinter._print_Infinityc             C   sN   | j  d¡}| j  d¡}| | j  d¡¡ |  | ¡}| |¡ | |¡ |S )Nr³   rµ   rã   )r$   ry   rz   r)   r¥   )r   ro   r³   rá   r   r   r   r   r¦   Œ  s    

z2MathMLPresentationPrinter._print_Negative_Infinityc             C   sÄ  t |jƒ}t|d ƒdkrŒ| j d¡}|  |d d ¡}|  |d d ¡}| j d¡}| | j |  |¡¡¡ | |¡ | |¡ | |¡ t|d ƒdkrÖ| j d¡}| j d¡}| | j |  |¡¡¡ | |¡ | j d¡}| j d¡}| | j d¡¡ tt	|d d ƒƒdkr@| j d	¡}	|	 |  |d d ¡¡ n|  |d d ¡}	| |¡ tt	|j
ƒƒdkr„| |  |j
¡¡ n(| j d	¡}
|
 |  |j
¡¡ | |
¡ | |¡ | |	¡ |S )
Nr   r§   rÇ   r
   r›   rµ   r³   z&dd;rå   )r8   r©   r<   r$   ry   r-   rz   r)   rs   r•   r¨   )r   ro   r©   Úsubsuprª   r«   Zintegralr³   rE   ÚvarÚfencer   r   r   r®   •  s>    








z)MathMLPresentationPrinter._print_Integralc             C   s@  t |jƒ}| j d¡}|  |d d ¡}|  |d d ¡}| j d¡}| | j |  |¡¡¡ | j d¡}|  |d d ¡}| j d¡}	|	 | j d¡¡ | |¡ | |	¡ | |¡ | |¡ | |¡ | |¡ | j d¡}
|
 |¡ tt	|j
ƒƒdkr|
 |  |j
¡¡ n(| j d¡}| |  |j
¡¡ |
 |¡ |
S )	NZ
munderoverr   r
   r›   rµ   r³   rÞ   rå   )r8   r©   r$   ry   r-   rz   r)   rs   r<   r•   r¨   )r   ro   r©   ré   rª   r«   ZsummandZlowrê   Zequalr³   rë   r   r   r   r¯   »  s0    








z$MathMLPresentationPrinter._print_Sumc                s\  ˆ j  d¡}‡ fdd„}ttƒ}dd„ ‰t|jƒ\}}}ˆ|ƒ}‡fdd„|D ƒ}‡fdd„|D ƒ}ˆ j  d¡}| ˆ j  |¡¡ t|ƒd	krÞt|ƒd	kr®| ˆ j  |¡¡ n.ˆ j  d
¡}	|	 |¡ |	 ||ƒ¡ | |	¡ nzt|ƒd	krˆ j  d¡}
|
 |¡ |
 ||ƒ¡ | |
¡ n<ˆ j  d¡}| |¡ | ||ƒ¡ | ||ƒ¡ | |¡ |S )Nr¶   c                s´   t | ƒdkrŠˆ j d¡}xlt| ƒD ]`\}}|dkrZˆ j d¡}| ˆ j d¡¡ | |¡ ˆ j d¡}| ˆ j |¡¡ | |¡ q"W |S ˆ j d¡}| ˆ j | d ¡¡ |S d S )Nr
   r³   r   rµ   r°   r¶   )r<   r$   ry   r±   rz   r)   )r²   r³   r‘   r´   rµ   r¶   )r   r   r   r·   Ý  s    
z5MathMLPresentationPrinter._print_Symbol.<locals>.joinc             S   s   | t krt  | ¡S | S d S )N)r   r¸   )r¹   r   r   r   rp   ð  s    
z:MathMLPresentationPrinter._print_Symbol.<locals>.translatec                s   g | ]}ˆ |ƒ‘qS r   r   )rº   r»   )rp   r   r   r¼   ø  s    z;MathMLPresentationPrinter._print_Symbol.<locals>.<listcomp>c                s   g | ]}ˆ |ƒ‘qS r   r   )rº   r½   )rp   r   r   r¼   ù  s    r   rÅ   rÆ   rÇ   )	r$   ry   r¾   r   r   r¿   rz   r)   r<   )r   rÀ   r   r·   rÁ   r¿   rÂ   rÃ   rÄ   rÅ   rÆ   rÇ   r   )r   rp   r   rÈ   Ú  s8    



z'MathMLPresentationPrinter._print_Symbolc             C   s.  |j jstt|jƒƒdkrz| j d¡}| j d¡}| |  |j¡¡ | |¡ | j d¡}| |¡ | |  |j ¡¡ |S |j j	rú|j j
dkrú|j jdkr¸| j d¡}| |  |j¡¡ |j jdkrö| j d¡}| |  |j¡¡ | |  |j j¡¡ |S | j d¡}| |  |j¡¡ | |  |j ¡¡ |S )Nr
   r³   rå   rÆ   r›   ZmsqrtZmroot)rÊ   Zis_negativer<   r•   rÌ   r$   ry   rz   r-   rË   r–   r”   )r   ro   r³   r   r   r   r   rÍ     s,    

z$MathMLPresentationPrinter._print_Powc             C   s,   | j  |  |¡¡}| | j  t|ƒ¡¡ |S )N)r$   ry   rs   rz   r)   r•   )r   ro   r   r   r   r   rÎ   .  s    z'MathMLPresentationPrinter._print_Numberc       	      C   sp  | j  d¡}| j  d¡}t|ƒrR| | j  d¡¡ | j  d¡}| | j  d¡¡ n<| | j  |  |¡¡¡ | j  d¡}| | j  |  |¡¡¡ | j  d¡}| |  |j¡¡ | j  d¡}| |¡ | |¡ xž|jD ]”}| j  d¡}| j  d¡}| j  d¡}t|ƒr| | j  d¡¡ n| | j  |  |¡¡¡ |  |¡}| |¡ | |¡ | |¡ | |¡ |}qÔW |S )Nr³   rµ   z&#x2202;rå   rß   )	r$   ry   r   rz   r)   rs   r-   r0   rÏ   )	r   ro   r³   r   rá   ræ   rÀ   rà   r   r   r   r   rÐ   3  s8    







z+MathMLPresentationPrinter._print_Derivativec             C   st   | j  d¡}| j  d¡}| | j  |  |¡¡¡ | j  d¡}x|jD ]}| |  |¡¡ qDW | |¡ | |¡ |S )Nr³   r¶   rå   )r$   ry   rz   r)   rs   r‹   r-   )r   ro   r³   r   rá   rŒ   r   r   r   rÑ   V  s    

z)MathMLPresentationPrinter._print_Functionc             C   s\   | j  d¡}| j  d¡}| | j  |  |¡¡¡ | |¡ x|D ]}t |  |¡¡ q@W |S )Nr³   r¶   )r$   ry   rz   r)   rs   r   r-   )r   ro   r³   r¶   rŒ   r   r   r   rÒ   a  s    

z&MathMLPresentationPrinter._print_Basicc             C   s^   | j  d¡}| j  d¡}| | j  |  |¡¡¡ | |¡ x|jD ]}| |  |¡¡ qBW |S )Nr³   r¶   )r$   ry   r‰   r)   rs   rz   r‹   r-   )r   ro   r³   r¶   rŒ   r   r   r   rÓ   j  s    
z(MathMLPresentationPrinter._print_AssocOpc             C   sb   | j  d¡}| |  |j¡¡ | j  d¡}| | j  |  |¡¡¡ | |¡ | |  |j¡¡ |S )Nr³   rµ   )r$   ry   rz   r-   rÔ   r)   rs   rÕ   )r   ro   r³   r   r   r   r   rÖ   s  s    
z+MathMLPresentationPrinter._print_Relationalc             C   s,   | j  |  |¡¡}| | j  t|ƒ¡¡ |S )N)r$   ry   rs   rz   r)   r•   )r   r–   rØ   r   r   r   rÚ   |  s    z$MathMLPresentationPrinter._print_int)N)r    r!   r"   rA   rÛ   rs   r{   r   r“   r˜   rž   rŸ   r¡   r¢   r£   r¥   r¦   r®   r¯   rÈ   rÍ   rÎ   rÐ   rÑ   rÒ   rÓ   rÖ   rÚ   r   r   r   r   rÜ   Î  s2   '/
	&9#			rÜ   Úcontentc             K   s(   |dkrt |ƒ | ¡S t|ƒ | ¡S dS )z‹Returns the MathML representation of expr. If printer is presentation then
     prints Presentation MathML else prints content MathML.
    ÚpresentationN)rÜ   r1   rB   )r0   Úprinterr*   r   r   r   Úmathml‚  s    rï   c             K   sL   |dkrt |ƒ}nt|ƒ}| t| ƒ¡}| ¡  | ¡ }| ¡  t|ƒ dS )a*  
    Prints a pretty representation of the MathML code for expr. If printer is
    presentation then prints Presentation MathML else prints content MathML.

    Examples
    ========

    >>> ##
    >>> from sympy.printing.mathml import print_mathml
    >>> from sympy.abc import x
    >>> print_mathml(x+1) #doctest: +NORMALIZE_WHITESPACE
    <apply>
        <plus/>
        <ci>x</ci>
        <cn>1</cn>
    </apply>
    >>> print_mathml(x+1, printer='presentation')
    <mrow>
        <mi>x</mi>
        <mo>+</mo>
        <mn>1</mn>
    </mrow>

    rí   N)rÜ   rB   r-   r   r?   Ztoprettyxmlr@   Úprint)r0   rî   r*   r¹   ZxmlZ
pretty_xmlr   r   r   Úprint_mathmlŒ  s    
rñ   N)rì   )rì   )rA   Z
__future__r   r   Zsympyr   r   r   Zsympy.core.functionr   Zsympy.core.alphabetsr   Zsympy.core.compatibilityr	   rî   r   Zpretty.pretty_symbologyr   Zconventionsr   r   r   rB   rÜ   rï   rñ   ZMathMLPrinterr   r   r   r   Ú<module>   s&   \  d   7


%