B
    [C                 @   s   d Z ddlmZmZ ddlZddlmZ ddlm	Z	 ddl
mZ ddl
mZ ddlmZ dd	lmZ d
d Zdd Zdd ZdZdddZdS )z7Tools for setting up printing in interactive sessions.     )print_functiondivisionN)LooseVersion)BytesIO)latex)preview)integer_types)debugc                s.   ddl }ddlm   fdd}||_dS )z.Setup printing in Python interactive session. r   N)builtinsc                s(   | dk	r$d _ t| f |  _ dS )zPython's pretty-printer display hook.

           This function was adapted from:

            http://www.python.org/dev/peps/pep-0217/

        N)_print)arg)r
   settingsstringify_func 9lib/python3.7/site-packages/sympy/interactive/printing.py_displayhook   s    z+_init_python_printing.<locals>._displayhook)syssympy.core.compatibilityr
   displayhook)r   r   r   r   r   )r
   r   r   r   _init_python_printing   s    r   c
                s  yddl m W n tk
r$   Y nX d|r4d}nd}||f d}d}d}d	|||||f }| td
 td |	pt fdd}fddfdd	fdd
fdd}
fdd}
fdd}fdd}ddl t jdkrddl	m
} ddlm} dd lm}m} dd!lm} ||tttttt|||gtt }| jjd" }x|D ]}||| qW | jjd# }|d$krtd% x||D ]}||| qW n`|d&krtd' xL|D ]}||| qW n0td( x&|D ]}||jkr|j| qW | jjd) }|d*krptd+ xL|D ]}||| qXW n0td, x4|D ]}||jkr~|j| q~W n| d-| dS ).z/Setup printing in IPython interactive session. r   )latex_to_pngz]\documentclass[%s]{article}
\pagestyle{empty}
\usepackage{amsmath,amsfonts}%s\begin{document}z\usepackage{euler} Ztightz0cm,0cm   z-T %s -D %d -bg %s -fg %s -O %szinit_printing: DVIOPTIONS:zinit_printing: PREAMBLE:c                s0   | r| |  n|  jj|  dS )z*caller for pretty, for use in IPython 0.11N)textlibpretty)r   pcycle)IPython_can_print_latexr   r   r   _print_plainA   s    z,_init_ipython_printing.<locals>._print_plainc          
      s\   t  }yt| dd| d W n4 tk
rR } ztddt|  W d d }~X Y nX | S )Npngr   )outputZviewerZoutputbufferpreamble
dvioptionszpng printing:z"_preview_wrapper exception raised:)r   r   	Exceptionr	   reprgetvalue)oZ
exprbuffere)r%   r$   r   r   _preview_wrapperH   s    
z0_init_ipython_printing.<locals>._preview_wrapperc          
      sV   |  dd} |  dd} y | S  tk
rP } ztdt| d S d }~X Y nX d S )Nz\operatornamer   z	\overlinez\barzmatplotlib exception caught:)replace
ValueErrorr	   r'   )r)   r*   )r   r   r   _matplotlib_wrapperU   s    z3_init_ipython_printing.<locals>._matplotlib_wrapperc                s   yddl m} ddlm} ddlm}m} ddlm} t	t
ttf}t |rt jdd |D ks~t jdd |D krd	S tfd
d D S t trt fdd D S t trd	S t |||||frdS t ttfrrdS d	S  tk
r   d	S X dS )zReturn True if type o can be printed with LaTeX.

        If o is a container type, this is True if and only if every element of
        o can be printed with LaTeX.
        r   )Basic)
MatrixBase)VectorDyadic)	NDimArrayc             s   s   | ]}|j V  qd S )N)__str__).0ir   r   r   	<genexpr>v   s    zC_init_ipython_printing.<locals>._can_print_latex.<locals>.<genexpr>c             s   s   | ]}|j V  qd S )N)__repr__)r5   r6   r   r   r   r7   w   s    Fc             3   s   | ]} |V  qd S )Nr   )r5   r6   )r    r   r   r7   y   s    c             3   s"   | ]} |o | V  qd S )Nr   )r5   r6   )r    r)   r   r   r7   {   s    TN)sympyr/   Zsympy.matricesr0   sympy.physics.vectorr1   r2   sympy.tensor.arrayr3   listtupleset	frozenset
isinstancetyper4   r8   alldictboolfloatr   RuntimeError)r)   r/   r0   r1   r2   r3   Zbuiltin_types)r    print_builtin)r)   r   r    c   s,    


z0_init_ipython_printing.<locals>._can_print_latexc          
      s|    | rx| fdi}y|S  t k
rv } z4tdt|d dkrb| fddi}|S d}~X Y nX dS )z
        A function that returns a png rendered by an external latex
        distribution, falling back to matplotlib rendering
        modezpreview failed with:z# Falling back to matplotlib backendinlineN)rF   r	   r'   )r)   sr*   )r    r.   r+   r   
latex_moder   r   r   _print_latex_png   s    
z0_init_ipython_printing.<locals>._print_latex_pngc                s(    | r$| fddi}|S dS )zD
        A function that returns a png rendered by mathtext
        rH   rI   Nr   )r)   rJ   )r    r.   r   r   r   r   _print_latex_matplotlib   s    z7_init_ipython_printing.<locals>._print_latex_matplotlibc                s2    | r.| fddi}| d}d| S dS )zW
        A function to generate the latex representation of sympy expressions.
        rH   Zplain$z$$%s$$N)strip)r)   rJ   )r    r   r   r   r   _print_latex_text   s    
z1_init_ipython_printing.<locals>._print_latex_textc                s6   | j jr& |}d|krt t| ntt| dS )zIPython's pretty-printer display hook, for use in IPython 0.10

           This function was adapted from:

            ipython/IPython/hooks.py:155

        
N)ZrcZpprintr   r'   )selfr   out)r   r   r   _result_display   s    
z/_init_ipython_printing.<locals>._result_displayNz0.11)r/   )r0   )r1   r2   )r3   z
text/plainz	image/png)Tr"   z"init_printing: using png formatterZ
matplotlibz)init_printing: using matplotlib formatterz*init_printing: not using any png formatterz
text/latex)TZmathjaxz&init_printing: using mathjax formatterz-init_printing: not using text/latex formatterZresult_display)ZIPython.lib.latextoolsr   ImportErrorsplitr	   default_latexr   V__version__Zsympy.core.basicr/   Zsympy.matrices.matricesr0   r:   r1   r2   r;   r3   rE   r=   r<   r>   r?   rC   r   Zdisplay_formatterZ
formattersZfor_typeZtype_printerspopZset_hook)ipr   	use_latexeuler	forecolor	backcolorfontsizerK   rG   latex_printerr   ZaddpackagesZ	imagesizeoffsetZ
resolutionZdvir!   rL   rM   rP   rT   r/   r0   r1   r2   r3   Zprintable_typesZplaintext_formatterclsZpng_formatterZlatex_formatterr   )r   r    r.   r+   r%   r   rK   r   r$   rG   r   r   r   _init_ipython_printing$   sx    

(	








rd   c             C   sd   dt jkrdS yddlm} W n: tk
rX   yddlm} W n tk
rR   dS X Y nX t| |S )z%Is a shell instance an IPython shell?r   Fr   )InteractiveShell)r   modulesZIPython.core.interactiveshellre   rU   ZIPython.iplibr@   )shellre   r   r   r   _is_ipython   s    
rh   FTBlackTransparent10pt	equation*c                s  ddl }ddlm} | r4|dk	r&|}qNddlm} n|dk	rB|}nddlm} d}|dkry
t }W n tk
rx   Y n
X |dk	}|r|st|}|rB| rBy@ddl	}t
|jdkrddlm} nddlm} dd	lm} W n tk
r   Y nPX t|||fsBd
d|jkrBdkr,td d|dkrBtd d}tsb|sb|jd n.| | r fdd}n fdd}|r|dd}|rtd t|||||	|
||||f
| nt|f| dS )a)  
    Initializes pretty-printer depending on the environment.

    Parameters
    ==========

    pretty_print: boolean
        If True, use pretty_print to stringify or the provided pretty
        printer; if False, use sstrrepr to stringify or the provided string
        printer.
    order: string or None
        There are a few different settings for this parameter:
        lex (default), which is lexographic order;
        grlex, which is graded lexographic order;
        grevlex, which is reversed graded lexographic order;
        old, which is used for compatibility reasons and for long expressions;
        None, which sets it to lex.
    use_unicode: boolean or None
        If True, use unicode characters;
        if False, do not use unicode characters.
    use_latex: string, boolean, or None
        If True, use default latex rendering in GUI interfaces (png and
        mathjax);
        if False, do not use latex rendering;
        if 'png', enable latex rendering with an external latex compiler,
        falling back to matplotlib if external compilation fails;
        if 'matplotlib', enable latex rendering with matplotlib;
        if 'mathjax', enable latex text generation, for example MathJax
        rendering in IPython notebook or text rendering in LaTeX documents
    wrap_line: boolean
        If True, lines will wrap at the end; if False, they will not wrap
        but continue as one line. This is only relevant if `pretty_print` is
        True.
    num_columns: int or None
        If int, number of columns before wrapping is set to num_columns; if
        None, number of columns before wrapping is set to terminal width.
        This is only relevant if `pretty_print` is True.
    no_global: boolean
        If True, the settings become system wide;
        if False, use just for this console/session.
    ip: An interactive console
        This can either be an instance of IPython,
        or a class that derives from code.InteractiveConsole.
    euler: boolean, optional, default=False
        Loads the euler package in the LaTeX preamble for handwritten style
        fonts (http://www.ctan.org/pkg/euler).
    forecolor: string, optional, default='Black'
        DVI setting for foreground color.
    backcolor: string, optional, default='Transparent'
        DVI setting for background color.
    fontsize: string, optional, default='10pt'
        A font size to pass to the LaTeX documentclass function in the
        preamble.
    latex_mode: string, optional, default='equation*'
        The mode used in the LaTeX printer. Can be one of:
        {'inline'|'plain'|'equation'|'equation*'}.
    print_builtin: boolean, optional, default=True
        If true then floats and integers will be printed. If false the
        printer will only print SymPy types.
    str_printer: function, optional, default=None
        A custom string printer function. This should mimic
        sympy.printing.sstrrepr().
    pretty_printer: function, optional, default=None
        A custom pretty printer. This should mimic sympy.printing.pretty().
    latex_printer: function, optional, default=None
        A custom LaTeX printer. This should mimic sympy.printing.latex().

    Examples
    ========

    >>> from sympy.interactive import init_printing
    >>> from sympy import Symbol, sqrt
    >>> from sympy.abc import x, y
    >>> sqrt(5)
    sqrt(5)
    >>> init_printing(pretty_print=True) # doctest: +SKIP
    >>> sqrt(5) # doctest: +SKIP
      ___
    \/ 5
    >>> theta = Symbol('theta') # doctest: +SKIP
    >>> init_printing(use_unicode=True) # doctest: +SKIP
    >>> theta # doctest: +SKIP
    \u03b8
    >>> init_printing(use_unicode=False) # doctest: +SKIP
    >>> theta # doctest: +SKIP
    theta
    >>> init_printing(order='lex') # doctest: +SKIP
    >>> str(y + x + y**2 + x**2) # doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_printing(order='grlex') # doctest: +SKIP
    >>> str(y + x + y**2 + x**2) # doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_printing(order='grevlex') # doctest: +SKIP
    >>> str(y * x**2 + x * y**2) # doctest: +SKIP
    x**2*y + x*y**2
    >>> init_printing(order='old') # doctest: +SKIP
    >>> str(x**2 + y**2 + x + y) # doctest: +SKIP
    x**2 + x + y**2 + y
    >>> init_printing(num_columns=10) # doctest: +SKIP
    >>> x**2 + x + y**2 + y # doctest: +SKIP
    x + y +
    x**2 + y**2
    r   N)Printer)r   )sstrreprFz1.0)TerminalInteractiveShell)InteractiveConsolezipython-consoler   z*init_printing: Setting use_unicode to TrueTz(init_printing: Setting use_latex to True)orderuse_unicode	wrap_linenum_columnsc                s    | dS )N)rq   rr   rs   rt   r   )expr)_stringify_funcrt   rq   rr   rs   r   r   <lambda>  s    zinit_printing.<locals>.<lambda>c                s    | dS )N)rq   r   )ru   )rv   rq   r   r   rw     s    rH   zMinit_printing: Mode is not able to be set due to internalsof IPython printing)r   Zsympy.printing.printerrm   Zsympy.printingr   rn   Zget_ipython	NameErrorrh   r   rX   rY   Z!IPython.terminal.interactiveshellro   Z*IPython.frontend.terminal.interactiveshellcoderp   rU   r@   joinargvr	   	NO_GLOBALZset_global_settingsrZ   rd   r   )Zpretty_printrq   rr   r\   rs   rt   Z	no_globalr[   r]   r^   r_   r`   rK   rG   Zstr_printerZpretty_printerra   r   r   rm   r   Z
in_ipythonr   ro   rp   Zmode_in_settingsr   )rv   rt   rq   rr   rs   r   init_printing  sd    n



r}   )TNNNNNFNFri   rj   rk   rl   TNNN)__doc__Z
__future__r   r   r   Zdistutils.versionr   rX   ior   r9   r   rW   r   r   r   Zsympy.utilities.miscr	   r   rd   rh   r|   r}   r   r   r   r   <module>   s(    K      