B
    ed                 @   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   st   xnt | i dD ]Z r2 fddD  s2qrHt |sHqt }t|trft|}|V  qW d S )N)	variablesc                s   g | ]}  ||qS r   )get).0var)matchr   `/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/unify/rewrite.py
<listcomp>/   s    z3rewriterule.<locals>.rewrite_rl.<locals>.<listcomp>)r   r   Zxreplacer   
isinstancer   r   )exprZassumptionsZexpr2)assume	conditionsourcetargetr   )r   r   
rewrite_rl,   s    
zrewriterule.<locals>.rewrite_rl)Tr   )r   r   r   r   r   r   r   )r   r   r   r   r   r   rewriterule   s    $r   )r   NN)__doc__Zsympy.core.exprr   Zsympy.assumptionsr   Zsympy.strategies.toolsr   Zsympy.unify.usympyr   r   r   r   r   r   r   <module>   s
   