
    EdX                     ~    d 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 Zd	 Zd
 ZdZ	 	 	 	 	 	 	 ddZdS )z7Tools for setting up printing in interactive sessions.     )version_tuple)BytesIO)latex)preview)debug)	Printablec                 8     ddl }ddl fd}||_        dS )z.Setup printing in Python interactive session. r   Nc                 X    | &d_         t           | fi            | _         dS dS )zPython's pretty-printer display hook.

           This function was adapted from:

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

        N)_print)argbuiltinssettingsstringify_funcs    :lib/python3.11/site-packages/sympy/interactive/printing.py_displayhookz+_init_python_printing.<locals>._displayhook   sI      	HJ..1111222HJJJ	 	    )sysr   displayhook)r   r   r   r   r   s   ``  @r   _init_python_printingr      sG    JJJOOO       #COOOr   c                   
 !"#$%&'( 	 ddl m' n# t          $ r Y nw xY w=| j                                        }|dk    rdn|dk    rdndt          d	           |d
k    rdz  %nd%d}d}t          d
z            }d||||fz  }|                                #d
z  }dd                    |          g$t          d#           t          d$           |	pt          &fd}#%fd!$%fd"'
fd d((fd !&fd}"&fd} &fd}&fd}fd }ddl
t          j                  t          d!          k    rpt          t          t          t           t"          t$          t&          g}| j        j        d"         }|t,          gz   D ]}|                    ||           | j        j        d#         }|d$v r7t          d%           |D ]}|                    ||           |t,          _        nMt          d&           |D ]%}||j        v r|j                            |           &t,          j        t,          _        | j        j        d'         }|d(v r7t          d)           |D ]}|                    ||           |t,          _        n|d*k    r7t          d+           |D ]}|                    ||           |t,          _        nMt          d,           |D ]%}||j        v r|j                            |           &t,          j        t,          _        | j        j        d-         }|d.v r8t          d/           |D ]}|                    ||           |t,          _        dS t          d0           |D ]%}||j        v r|j                            |           &t,          j        t,          _        dS |                     d1|           dS )2z/Setup printing in IPython interactive session. r   )latex_to_pngNlightbgBlacklinuxWhiteGrayz*init_printing: Automatic foreground color:svgz
\special{color %s} tightz0cm,0cm   z-T %s -D %d -bg %s -fg %s -O %sg @z
--no-fontsz
--scale={}zinit_printing: DVIOPTIONS:zinit_printing: DVIOPTIONS_SVG:c                      |           r |                      |                      dS |                     j        j                            |                      dS )z*caller for pretty, for use in IPython 0.11N)textlibpretty)r   pcycleIPython
_can_printr   s      r   _print_plainz,_init_ipython_printing.<locals>._print_plainO   s`    :c?? 	3FF>>#&&'''''FF7;%,,S1122222r   c           
          t                      }	 t          | dd|           n1# t          $ r$}t          ddt	          |                      d }~ww xY w|                                S )Npngr   outputviewereuleroutputbufferextra_preamble
dvioptionsfontsizezpng printing:"_preview_wrapper exception raised:)r   r   	Exceptionr   reprgetvalue)o
exprbufferer3   r0   r2   r4   s      r   _preview_wrapperz0_init_ipython_printing.<locals>._preview_wrapperV   s    YY
	AeIU!+N)H> > > > >  	 	 	/#Gq''  		
 ""$$$   ) 
AAAc           
         t                      }	 t          | dd|           n1# t          $ r$}t          ddt	          |                      d }~ww xY w|                                                    d          S )Nr   r   r-   zsvg printing:r5   zutf-8)r   r   r6   r   r7   r8   decode)r9   r:   r;   dvioptions_svgr0   r2   r4   s      r   _svg_wrapperz,_init_ipython_printing.<locals>._svg_wrapperc   s    YY
	AeIU!+N-B B B B B  	 	 	/#Gq''  		
 ""$$++G444r=   c                     	 	  |           S # t           $ r  |           cY S w xY w# t          $ r(}t          dt          |                     Y d }~d S d }~ww xY w)N)colorscalezmatplotlib exception caught:)	TypeError
ValueErrorr   r7   )r9   r;   	forecolorr   rD   s     r   _matplotlib_wrapperz3_init_ipython_printing.<locals>._matplotlib_wrapperp   s    	'#|AYeDDDD ' ' '#|A&&&' 	 	 	0$q'':::44444	s$    *- *- 
AAA)_latex	_sympystr_pretty
_sympyreprc                 t    	 t           t          t          t          f}t	           |          r]t                     j        d |D             vs t                     j        d |D             vrdS t          fd D                       S t	           t                    rt           fd D                       S t	           t                    rdS t	           t                    rdS t           fdD                       rdS t	           t          t          f          rrdS dS # t          $ r Y dS w xY w)zReturn True if type o can be printed with one of the SymPy printers.

        If o is a container type, this is True if and only if every element of
        o can be printed in this way.
        c              3   $   K   | ]}|j         V  d S N)__str__.0is     r   	<genexpr>z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>   s$      +M+M!AI+M+M+M+M+M+Mr   c              3   $   K   | ]}|j         V  d S rO   )__repr__rQ   s     r   rT   z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>   s$      ,O,OAQZ,O,O,O,O,O,Or   Fc              3   .   K   | ]} |          V  d S rO    )rR   rS   r)   s     r   rT   z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>   s+      44Q::a==444444r   c              3   P   K   | ] } |          o |                   V  !d S rO   rX   )rR   rS   r)   r9   s     r   rT   z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>   s>      II!::a===ZZ!-=-=IIIIIIr   Tc              3   8   K   | ]}t          |          V  d S rO   )hasattr)rR   hookr9   s     r   rT   z=_init_ipython_printing.<locals>._can_print.<locals>.<genexpr>   s-      AA$WQ%%AAAAAAr   )listtupleset	frozenset
isinstancetyperP   rV   alldictboolr   anyfloatintRuntimeError)r9   builtin_typesr)   print_builtinprinting_hookss   ` r   r)   z*_init_ipython_printing.<locals>._can_print   sw   	 "5#y9M!]++  GGO+M+M}+M+M+MM !GG$,O,O,O,O,OO! 54444!444444At$$ IIIIIqIIIIIIAt$$ 	uAy)) tAAAA.AAAAA tAs|,,  t5 	 	 	55	s6   A*D) 0D) 0D) <D) D) *D) D) )
D76D7c                     |           rs | fdi}dk    rd|z  }	  |          S # t           $ rD}t          dt          |          d           dk    r | fddi} |          cY d}~S d}~ww xY wdS )z
        A function that returns a png rendered by an external latex
        distribution, falling back to matplotlib rendering
        modeplain$\displaystyle %s$preview failed with:z# Falling back to matplotlib backendinlineNri   r   r7   )	r9   sr;   r)   rH   r<   r   
latex_moder   s	      r   _print_latex_pngz0_init_ipython_printing.<locals>._print_latex_png   s    
 :a== 	.a55j5H55AW$ .)A-.''*** . . .,d1gg;= = =) <a;;h;(;;A**1--------.	. 	.s   
/ 
A=9A82A=8A=c                      |           rX | fdi}dk    rd|z  }	  |          S # t           $ r)}t          dt          |          d           Y d}~dS d}~ww xY wdS )zz
        A function that returns a svg rendered by an external latex
        distribution, no fallback available.
        rn   ro   rp   rq   z No fallback available.Nrs   )r9   rt   r;   r)   rA   r   ru   r   s      r   _print_latex_svgz0_init_ipython_printing.<locals>._print_latex_svg   s    
 :a== 	1a55j5H55AW$ .)A-1#|A& 1 1 1,d1gg/1 1 1 1 1 1 1 1 11	1 	1s   
/ 
A"AA"c                 J     |           r | fddi} |          S dS )zD
        A function that returns a png rendered by mathtext
        rn   rr   NrX   )r9   rt   r)   rH   r   r   s     r   _print_latex_matplotlibz7_init_ipython_printing.<locals>._print_latex_matplotlib   sL     :a== 	*a33h3(33A&&q)))	* 	*r   c                 N     |           r | fdi}dk    rd|z  S |S dS )zW
        A function to generate the latex representation of SymPy expressions.
        rn   ro   rp   NrX   )r9   rt   r)   r   ru   r   s     r   _print_latex_textz1_init_ipython_printing.<locals>._print_latex_text   sX     :a== 	a55j5H55AW$ 1,q00H		 	r   c                     | j         j        r. |          }d|v rt                       t          |           dS 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)rcpprintr   r7   )selfr   outr   s      r   _result_displayz/_init_ipython_printing.<locals>._result_display   s^     7> 	 .%%Cs{ #JJJJJ$s))r   z0.11z
text/plainzimage/svg+xml)r   z"init_printing: using svg formatterz*init_printing: not using any svg formatterz	image/png)Tr,   z"init_printing: using png formatter
matplotlibz)init_printing: using matplotlib formatterz*init_printing: not using any png formatterz
text/latex)Tmathjaxz&init_printing: using mathjax formatterz-init_printing: not using text/latex formatterresult_display)IPython.lib.latextoolsr   ImportErrorcolorslowerr   roundsplitformatdefault_latexr(   r   __version__rg   r^   r]   r_   r`   rd   rh   display_formatter
formattersr   for_type
_repr_svg_type_printerspop_repr_disabled
_repr_png__repr_latex_set_hook))ipr   	use_latexr0   rG   	backcolorr4   ru   rk   latex_printerrD   r   rC   	imagesizeoffset
resolutiondvi	svg_scaler*   rv   rx   rz   r|   r   printable_typesplaintext_formatterclssvg_formatterpng_formatterlatex_formatterr(   r)   rH   r<   rA   r3   r@   r2   r   r   rl   s)    ` `` ``` ``                  @@@@@@@@@@@r   _init_ipython_printingr   !   s   7777777     	G	!!I 	IIg 	II I:IFFFE 09<IFs5y!!J
,:y)V0= =CJuI"L$7$7	$B$BCN	
&
333	
*N;;;*]E3 3 3 3 3 3 3% % % % % % % %5 5 5 5 5 5 5 5       FN! ! ! ! ! ! !L. . . . . . . . . .$1 1 1 1 1 1 1 1 1* * * * * * * *           $ NNNW())]6-B-BB C7 !%sItSI 2=lK
 #i[0 	< 	<C((l;;;;,7H	! 	<6777& > >&&s,<====#3I  >???& 9 9 -55 9!/33C888#,#;I ,7D% 	<6777& > >&&s,<====#3I  ,& 	<=>>>& E E&&s,CDDDD#:I  >???& 9 9 -55 9!/33C888#,#;I .9,G)) 	>:;;;& A A((.?@@@@%6I"""ABBB& ; ; /77 ;#155c:::%.%=I""" 	$o66666s    
))c                     ddl m} d|vrdS 	 ddlm} n*# t          $ r 	 ddlm} n# t          $ r Y Y dS w xY wY nw xY wt          | |          S )z%Is a shell instance an IPython shell?r   )modulesr(   F)InteractiveShell)r   r   IPython.core.interactiveshellr   r   IPython.iplibra   )shellr   r   s      r   _is_ipythonr   3  s      u	BBBBBBB   	6666666 	 	 	 555	 76 e-...s$    
<'<
6<6<<FTNTransparent10ptro         ?c                 $   ddl }ddlm} | r||}nddlm} n||}nddlm} d}|$	 t                      }|du}n# t          $ r Y nw xY w|r|st          |          }|r| r	 ddl	}t          |j                  t          d          k    rddlm} nddlm} dd	lm} t#          |||f          sBd
d                    |j                  vr&t)          d           d|t)          d           d}n# t*          $ r Y nw xY wt,          s|s|                               n|| r
fd}nfd}|r@|                    dd          }|rt)          d           t3          |||||	|
|||||fi | dS t5          |fi | dS )a1  
    Initializes pretty-printer depending on the environment.

    Parameters
    ==========

    pretty_print : bool, default=True
        If ``True``, use :func:`~.pretty_print` to stringify or the provided pretty
        printer; if ``False``, use :func:`~.sstrrepr` to stringify or the provided string
        printer.
    order : string or None, default='lex'
        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 : bool or None, default=None
        If ``True``, use unicode characters;
        if ``False``, do not use unicode characters;
        if ``None``, make a guess based on the environment.
    use_latex : string, bool, or None, default=None
        If ``True``, use default LaTeX rendering in GUI interfaces (png and
        mathjax);
        if ``False``, do not use LaTeX rendering;
        if ``None``, make a guess based on the environment;
        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;
        if ``'svg'``, enable LaTeX rendering with an external latex compiler,
        no fallback
    wrap_line : bool
        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, default=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 : bool, default=False
        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 : bool, optional, default=False
        Loads the euler package in the LaTeX preamble for handwritten style
        fonts (http://www.ctan.org/pkg/euler).
    forecolor : string or None, optional, default=None
        DVI setting for foreground color. ``None`` means that either ``'Black'``,
        ``'White'``, or ``'Gray'`` will be selected based on a guess of the IPython
        terminal color setting. See notes.
    backcolor : string, optional, default='Transparent'
        DVI setting for background color. See notes.
    fontsize : string or int, optional, default='10pt'
        A font size to pass to the LaTeX documentclass function in the
        preamble. Note that the options are limited by the documentclass.
        Consider using scale instead.
    latex_mode : string, optional, default='plain'
        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
        :func:`~.sstrrepr()`.
    pretty_printer : function, optional, default=None
        A custom pretty printer. This should mimic :func:`~.pretty()`.
    latex_printer : function, optional, default=None
        A custom LaTeX printer. This should mimic :func:`~.latex()`.
    scale : float, optional, default=1.0
        Scale the LaTeX output when using the ``'png'`` or ``'svg'`` backends.
        Useful for high dpi screens.
    settings :
        Any additional settings for the ``latex`` and ``pretty`` commands can
        be used to fine-tune the output.

    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

    Notes
    =====

    The foreground and background colors can be selected when using ``'png'`` or
    ``'svg'`` LaTeX rendering. Note that before the ``init_printing`` command is
    executed, the LaTeX rendering is handled by the IPython console and not SymPy.

    The colors can be selected among the 68 standard colors known to ``dvips``,
    for a list see [1]_. In addition, the background color can be
    set to  ``'Transparent'`` (which is the default value).

    When using the ``'Auto'`` foreground color, the guess is based on the
    ``colors`` variable in the IPython console, see [2]_. Hence, if
    that variable is set correctly in your IPython console, there is a high
    chance that the output will be readable, although manual settings may be
    needed.


    References
    ==========

    .. [1] https://en.wikibooks.org/wiki/LaTeX/Colors#The_68_standard_colors_known_to_dvips

    .. [2] https://ipython.readthedocs.io/en/stable/config/details.html#terminal-colors

    See Also
    ========

    sympy.printing.latex
    sympy.printing.pretty

    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                       | fd|S )Nr   rX   )exprr   _stringify_funcr   r   r   r   s     r   <lambda>zinit_printing.<locals>.<lambda>  s8    ,_T 99D7@9D9 9 08	9 9 r   c                      | fdi|S )Nr   rX   )r   r   r   r   s     r   r   zinit_printing.<locals>.<lambda>%  s+    *3 *3 %*3)1*3 *3 r   rn   zMinit_printing: Mode is not able to be set due to internalsof IPython printing)r   sympy.printing.printerr   sympy.printingr%   r   get_ipython	NameErrorr   r(   r   r   !IPython.terminal.interactiveshellr   *IPython.frontend.terminal.interactiveshellcoder   ra   joinargvr   r   	NO_GLOBALset_global_settingsr   r   r   )pretty_printr   r   r   r   r   	no_globalr   r0   rG   r   r4   ru   rk   str_printerpretty_printerr   rD   r   r   r   r   
in_ipythonr(   r   r   mode_in_settingsr   s    `` ``                     @r   init_printingr   H  s   v JJJ...... 	B 	@+NN??????? 	B(NNAAAAAA J	 *	*B D.JJ  	 	 	D	
 
 %* % __
 %l %	%NNN W011]55I5II `VVVVVVV______//////
 b#57O"PQQ %)1B1BB% 'FGGG"&K %DEEE $I  	 	 	D	  3Y 3##%[.7[ 	$ 	R 	R 	R 	R ) 
	39 9 9 9 9 9 9 9NN3 3 3 3 3   
:#<<55 	) ( ) ) )r>9e()Xz,mU	+ 	+ "*	+ 	+ 	+ 	+ 	+
 	n9999999s#   A 
AA)<C: :
DD)TNNNNNFNFNr   r   ro   TNNNr   )__doc__sympy.external.importtoolsr   ior   sympy.printing.latexr   r   sympy.printing.previewr   sympy.utilities.miscr   sympy.printing.defaultsr   r   r   r   r   r   rX   r   r   <module>r      s    = = 4 4 4 4 4 4       7 7 7 7 7 7 * * * * * * & & & & & & - - - - - -# # #*P7 P7 P7d/ / /& 	=A>BCG4:4837,/j: j: j: j: j: j:r   