o
    §ƒðj  ã                   @   sF   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 d
dd	„Z
dS )z5 Functions to support rewriting of SymPy expressions é    )ÚExpr)Úask)Úsubs)ÚrebuildÚunify© Nc                    s   d‡ ‡‡‡‡fdd„	}|S )aÜ   Rewrite rule.

    Transform expressions that match source into expressions that match target
    treating all ``variables`` as wilds.

    Examples
    ========

    >>> from sympy.abc import w, x, y, z
    >>> from sympy.unify.rewrite import rewriterule
    >>> from sympy import default_sort_key
    >>> rl = rewriterule(x + y, x**y, [x, y])
    >>> sorted(rl(z + 3), key=default_sort_key)
    [3**z, z**3]

    Use ``condition`` to specify additional requirements.  Inputs are taken in
    the same order as is found in variables.

    >>> rl = rewriterule(x + y, x**y, [x, y], lambda x, y: x.is_integer)
    >>> list(rl(z + 3))
    [3**z]

    Use ``assume`` to specify additional requirements using new assumptions.

    >>> from sympy.assumptions import Q
    >>> rl = rewriterule(x + y, x**y, [x, y], assume=Q.integer(x))
    >>> list(rl(z + 3))
    [3**z]

    Assumptions for the local context are provided at rule runtime

    >>> list(rl(w + z, Q.integer(z)))
    [z**w]
    Tc                 3   sr   � t ˆ| i ˆd�D ]-‰ ˆrˆ‡ fdd„ˆD ƒŽ sq	ˆr$tˆ ˆ ¡|ƒs$q	tˆ ƒˆƒ}t|tƒr3t|ƒ}|V  q	d S )N)Ú	variablesc                    s   g | ]}ˆ   ||¡‘qS r   )Úget)Ú.0Úvar©Úmatchr   ú•/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/unify/rewrite.pyÚ
<listcomp>/   s    z3rewriterule.<locals>.rewrite_rl.<locals>.<listcomp>)r   r   Úxreplacer   Ú
isinstancer   r   )ÚexprÚassumptionsÚexpr2©ÚassumeÚ	conditionÚsourceÚtargetr   r   r   Ú
rewrite_rl,   s   €ÿ
÷zrewriterule.<locals>.rewrite_rlN)Tr   )r   r   r   r   r   r   r   r   r   Úrewriterule   s   $r   )r   NN)Ú__doc__Úsympy.core.exprr   Úsympy.assumptionsr   Úsympy.strategies.toolsr   Úsympy.unify.usympyr   r   r   r   r   r   r   Ú<module>   s    