o
    §ƒðjR   ã                   @   sà   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gZeddd	œfed
diffZeeee	fZd#dd„Zefdd„Zd$dd„Zeeddfdd„Zdd„ ddfdd„ZdZdddœZed d„ d!defd"d„Zd!S )%é    )ÚBasic)ÚExpr)ÚSymbol)ÚIntegerÚRationalÚFloat)ÚsreprÚdotprintÚblueÚellipse)ÚcolorÚshaper   ÚblackFc                 C   sd   d}t | tƒst| ƒ}n| jst| ƒ}n| j}ttt|ƒƒ}dt| ƒj	d 
|¡f }|r0||f}|S )aN  A string that follows ```obj = type(obj)(*obj.args)``` exactly.

    Parameters
    ==========

    with_args : boolean, optional
        If ``True``, there will be a second argument for the return
        value, which is a tuple containing ``purestr`` applied to each
        of the subnodes.

        If ``False``, there will not be a second argument for the
        return.

        Default is ``False``

    Examples
    ========

    >>> from sympy import Float, Symbol, MatrixSymbol
    >>> from sympy import Integer # noqa: F401
    >>> from sympy.core.symbol import Str # noqa: F401
    >>> from sympy.printing.dot import purestr

    Applying ``purestr`` for basic symbolic object:
    >>> code = purestr(Symbol('x'))
    >>> code
    "Symbol('x')"
    >>> eval(code) == Symbol('x')
    True

    For basic numeric object:
    >>> purestr(Float(2))
    "Float('2.0', precision=53)"

    For matrix symbol:
    >>> code = purestr(MatrixSymbol('x', 2, 2))
    >>> code
    "MatrixSymbol(Str('x'), Integer(2), Integer(2))"
    >>> eval(code) == MatrixSymbol('x', 2, 2)
    True

    With ``with_args=True``:
    >>> purestr(Float(2), with_args=True)
    ("Float('2.0', precision=53)", ())
    >>> purestr(MatrixSymbol('x', 2, 2), with_args=True)
    ("MatrixSymbol(Str('x'), Integer(2), Integer(2))",
     ("Str('x')", 'Integer(2)', 'Integer(2)'))
    © z%s(%s)ú, )Ú
isinstancer   ÚstrÚargsr   ÚtupleÚmapÚpurestrÚtypeÚ__name__Újoin)ÚxÚ	with_argsÚsargsÚrvr   r   r   ú”/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/printing/dot.pyr      s   1


r   c                 C   s*   i }|D ]\}}t | |ƒr| |¡ q|S )aÚ   Merge style dictionaries in order

    Examples
    ========

    >>> from sympy import Symbol, Basic, Expr, S
    >>> from sympy.printing.dot import styleof
    >>> styles = [(Basic, {'color': 'blue', 'shape': 'ellipse'}),
    ...           (Expr,  {'color': 'black'})]

    >>> styleof(Basic(S(1)), styles)
    {'color': 'blue', 'shape': 'ellipse'}

    >>> x = Symbol('x')
    >>> styleof(x + 1, styles)  # this is an Expr
    {'color': 'black', 'shape': 'ellipse'}
    )r   Úupdate)ÚexprÚstylesÚstyleÚtypÚstyr   r   r   ÚstyleofN   s   

€r%   r   c                 C   s   |  dd„ t|  ¡ ƒD ƒ¡S )zÙ Print a dictionary of attributes

    Examples
    ========

    >>> from sympy.printing.dot import attrprint
    >>> print(attrprint({'color': 'blue', 'shape': 'ellipse'}))
    "color"="blue", "shape"="ellipse"
    c                 s   s   � | ]}d | V  qdS )z	"%s"="%s"Nr   )Ú.0Úitemr   r   r   Ú	<genexpr>q   s   € zattrprint.<locals>.<genexpr>)r   ÚsortedÚitems)ÚdÚ	delimiterr   r   r   Ú	attrprintg   s   
r-   r   Tc                 C   sd   t | |ƒ}t| tƒr| jst| jjƒ}n|| ƒ}||d< t| ƒ}|r*|dt|ƒ 7 }d|t|ƒf S )zé String defining a node

    Examples
    ========

    >>> from sympy.printing.dot import dotnode
    >>> from sympy.abc import x
    >>> print(dotnode(x))
    "Symbol('x')_()" ["color"="black", "label"="x", "shape"="ellipse"];
    Úlabelú_%sz
"%s" [%s];)	r%   r   r   Úis_Atomr   Ú	__class__r   r   r-   )r    r!   Ú	labelfuncÚposÚrepeatr"   r.   Úexpr_strr   r   r   Údotnodet   s   
r6   c                 C   ó   t | tƒ S ©N©r   r   ©r   r   r   r   Ú<lambda>Œ   ó    r;   c                    sX   || ƒrg S t | dd�\‰ }|r#ˆ dtˆƒ 7 ‰ ‡fdd„t|ƒD ƒ}‡ fdd„|D ƒS )a�   List of strings for all expr->expr.arg pairs

    See the docstring of dotprint for explanations of the options.

    Examples
    ========

    >>> from sympy.printing.dot import dotedges
    >>> from sympy.abc import x
    >>> for e in dotedges(x+2):
    ...     print(e)
    "Add(Integer(2), Symbol('x'))_()" -> "Integer(2)_(0,)";
    "Add(Integer(2), Symbol('x'))_()" -> "Symbol('x')_(1,)";
    T)r   r/   c                    s&   g | ]\}}d |t ˆ |f ƒf ‘qS )z%s_%s)r   )r&   ÚiÚa)r3   r   r   Ú
<listcomp>¡   s    ÿzdotedges.<locals>.<listcomp>c                    s   g | ]}d ˆ |f ‘qS )z"%s" -> "%s";r   )r&   r>   )r5   r   r   r?   £   s    )r   r   Ú	enumerate)r    Úatomr3   r4   Úarg_strsr   )r5   r3   r   ÚdotedgesŒ   s   
ÿrC   z|digraph{

# Graph style
%(graphstyle)s

#########
# Nodes #
#########

%(nodes)s

#########
# Edges #
#########

%(edges)s
}ÚTDÚout)ÚrankdirÚorderingc                 C   r7   r8   r9   r:   r   r   r   r;   »   r<   Nc              	      sd   t  ¡ }| |¡ g ‰g ‰d	‡ ‡‡‡‡‡‡‡fdd„	‰ˆ| dƒ tt|dd�d ˆ¡d ˆ¡dœ S )
a°	  DOT description of a SymPy expression tree

    Parameters
    ==========

    styles : list of lists composed of (Class, mapping), optional
        Styles for different classes.

        The default is

        .. code-block:: python

            (
                (Basic, {'color': 'blue', 'shape': 'ellipse'}),
                (Expr,  {'color': 'black'})
            )

    atom : function, optional
        Function used to determine if an arg is an atom.

        A good choice is ``lambda x: not x.args``.

        The default is ``lambda x: not isinstance(x, Basic)``.

    maxdepth : integer, optional
        The maximum depth.

        The default is ``None``, meaning no limit.

    repeat : boolean, optional
        Whether to use different nodes for common subexpressions.

        The default is ``True``.

        For example, for ``x + x*y`` with ``repeat=True``, it will have
        two nodes for ``x``; with ``repeat=False``, it will have one
        node.

        .. warning::
            Even if a node appears twice in the same object like ``x`` in
            ``Pow(x, x)``, it will still only appear once.
            Hence, with ``repeat=False``, the number of arrows out of an
            object might not equal the number of args it has.

    labelfunc : function, optional
        A function to create a label for a given leaf node.

        The default is ``str``.

        Another good option is ``srepr``.

        For example with ``str``, the leaf nodes of ``x + 1`` are labeled,
        ``x`` and ``1``.  With ``srepr``, they are labeled ``Symbol('x')``
        and ``Integer(1)``.

    **kwargs : optional
        Additional keyword arguments are included as styles for the graph.

    Examples
    ========

    >>> from sympy import dotprint
    >>> from sympy.abc import x
    >>> print(dotprint(x+2)) # doctest: +NORMALIZE_WHITESPACE
    digraph{
    <BLANKLINE>
    # Graph style
    "ordering"="out"
    "rankdir"="TD"
    <BLANKLINE>
    #########
    # Nodes #
    #########
    <BLANKLINE>
    "Add(Integer(2), Symbol('x'))_()" ["color"="black", "label"="Add", "shape"="ellipse"];
    "Integer(2)_(0,)" ["color"="black", "label"="2", "shape"="ellipse"];
    "Symbol('x')_(1,)" ["color"="black", "label"="x", "shape"="ellipse"];
    <BLANKLINE>
    #########
    # Edges #
    #########
    <BLANKLINE>
    "Add(Integer(2), Symbol('x'))_()" -> "Integer(2)_(0,)";
    "Add(Integer(2), Symbol('x'))_()" -> "Symbol('x')_(1,)";
    }

    r   c              	      s`   ˆ  t| ˆˆˆˆd�¡ ˆrˆ ˆkrd S ˆ t| ˆˆˆd�¡ ‡‡ ‡‡	fdd„t| jƒD ƒ d S )N)r2   r3   r4   )rA   r3   r4   c                    s.   g | ]\}}ˆ |ƒsˆ|ˆd  ˆ|f ƒ‘qS )é   r   )r&   r=   Úarg)rA   Údepthr3   Útraverser   r   r?   !  s   . z.dotprint.<locals>.traverse.<locals>.<listcomp>)Úappendr6   ÚextendrC   r@   r   )ÚerJ   r3   ©rA   Úedgesr2   ÚmaxdepthÚnodesr4   r!   rK   )rJ   r3   r   rK     s
   "zdotprint.<locals>.traverser   Ú
)r,   )Ú
graphstylerR   rP   N)r   )Ú_graphstyleÚcopyr   Útemplater-   r   )r    r!   rA   rQ   r4   r2   ÚkwargsrT   r   rO   r   r	   º   s   ]

þ)F)r   )Úsympy.core.basicr   Úsympy.core.exprr   Úsympy.core.symbolr   Úsympy.core.numbersr   r   r   Úsympy.printing.reprr   Ú__all__Údefault_stylesÚslotClassesr   r%   r-   r   r6   rC   rW   rU   r	   r   r   r   r   Ú<module>   s*    
þ
?
ÿ
þ