ó
”¼\c           @  s7  d  Z  d d l m Z m Z d d l m Z m Z m Z m Z d d l	 m
 Z
 m Z d d l m Z m Z m Z d d l m Z m Z m Z d d l m Z m Z m Z d d l m Z e g Z e
 e e g Z e g Z d	   Z d
   Z d   Z d   Z  d   Z! d d  Z" d   Z# d   Z$ d d d  Z& d S(   s    SymPy interface to Unification engine

See sympy.unify for module level docstring
See sympy.unify.core for algorithmic docstring i’’’’(   t   print_functiont   division(   t   Basict   Addt   Mult   Pow(   t   AssocOpt	   LatticeOp(   t   MatAddt   MatMult
   MatrixExpr(   t   Uniont   Intersectiont	   FiniteSet(   t   Compoundt   Variablet   CondVariable(   t   corec           s2   t  t t t t t f } t   f d   | D  S(   Nc         3  s   |  ] } t    |  Vq d  S(   N(   t
   issubclass(   t   .0t   aop(   t   op(    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>   s    (   R   R   R	   R   R   R   t   any(   R   t	   assoc_ops(    (   R   s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   sympy_associative   s    c           s/   t  t t t t f } t   f d   | D  S(   Nc         3  s   |  ] } t    |  Vq d  S(   N(   R   (   R   t   cop(   R   (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>   s    (   R   R   R   R   R   R   (   R   t   comm_ops(    (   R   s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   sympy_commutative   s    c         C  s   t  |  t  o t |  j  S(   N(   t
   isinstanceR   R   R   (   t   x(    (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   is_associative   s    c         C  sS   t  |  t  s t St |  j  r& t St |  j t  rO t d   |  j	 D  Sd  S(   Nc         s  s   |  ] } t  |  j Vq d  S(   N(   t	   constructt   is_commutative(   R   t   arg(    (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>$   s    (
   R   R   t   FalseR   R   t   TrueR   R   t   allt   args(   R   (    (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pyR       s    c           s     f d   } | S(   Nc           s.   t  |     p- t  |  t  o- t |  j    S(   N(   R   R   R   R   (   R   (   t   typ(    s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt	   matchtype'   s    (    (   R&   R'   (    (   R&   s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   mk_matchtype&   s    c           su   |    k r t  |   St |  t  t f  r/ |  St |  t  sH |  j rL |  St |  j t   f d   |  j D   S(   s%    Turn a SymPy object into a Compound c         3  s   |  ] } t  |    Vq d  S(   N(   t   deconstruct(   R   R!   (   t	   variables(    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>5   s    (	   R   R   R   R   t   is_AtomR   t	   __class__t   tupleR%   (   t   sR*   (    (   R*   s1   lib/python2.7/site-packages/sympy/unify/usympy.pyR)   ,   s    
	c           sĀ   t    t t f  r   j St    t  s/   St   f d   t D  rj   j d t t	 t
   j   St   f d   t D  r„ t j   j t	 t
   j   S  j t	 t
   j    Sd S(   s%    Turn a Compound into a SymPy object c         3  s!   |  ] } t    j |  Vq d  S(   N(   R   R   (   R   t   cls(   t   t(    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>=   s    t   evaluatec         3  s!   |  ] } t    j |  Vq d  S(   N(   R   R   (   R   R/   (   R0   (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>?   s    N(   R   R   R   R!   R   R   t   eval_false_legalR   R"   t   mapR   R%   t   basic_new_legalR   t   __new__(   R0   (    (   R0   s1   lib/python2.7/site-packages/sympy/unify/usympy.pyR   7   s    c         C  s   t  t |    S(   sY    Rebuild a SymPy expression

    This removes harm caused by Expr-Rules interactions
    (   R   R)   (   R.   (    (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   rebuildD   s    c      	   +  s    f d     | p i  } t    f d   | j   D  } t j   |     |  | d t d t | } x) | D]! } t  d   | j   D  Vqt Wd S(   s^   Structural unification of two expressions/patterns

    Examples
    ========

    >>> from sympy.unify.usympy import unify
    >>> from sympy import Basic, cos
    >>> from sympy.abc import x, y, z, p, q

    >>> next(unify(Basic(1, 2), Basic(1, x), variables=[x]))
    {x: 2}

    >>> expr = 2*x + y + z
    >>> pattern = 2*p + q
    >>> next(unify(expr, pattern, {}, variables=(p, q)))
    {p: x, q: y + z}

    Unification supports commutative and associative matching

    >>> expr = x + y + z
    >>> pattern = p + q
    >>> len(list(unify(expr, pattern, {}, variables=(p, q))))
    12

    Symbols not indicated to be variables are treated as literal,
    else they are wild-like and match anything in a sub-expression.

    >>> expr = x*y*z + 3
    >>> pattern = x*y + 3
    >>> next(unify(expr, pattern, {}, variables=[x, y]))
    {x: y, y: x*z}

    The x and y of the pattern above were in a Mul and matched factors
    in the Mul of expr. Here, a single symbol matches an entire term:

    >>> expr = x*y + 3
    >>> pattern = p + 3
    >>> next(unify(expr, pattern, {}, variables=[p]))
    {p: x*y}

    c           s   t  |     S(   N(   R)   (   R   (   R*   (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   <lambda>u   t    c         3  s-   |  ]# \ } }   |    |  f Vq d  S(   N(    (   R   t   kt   v(   t   decons(    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>w   s    R   R    c         s  s-   |  ]# \ } } t  |  t  |  f Vq d  S(   N(   R   (   R   R9   R:   (    (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pys	   <genexpr>~   s    N(   t   dictt   itemsR   t   unifyR   R    (   R   t   yR.   R*   t   kwargst   dst   d(    (   R;   R*   s1   lib/python2.7/site-packages/sympy/unify/usympy.pyR>   K   s    *"	N(    (    ('   t   __doc__t
   __future__R    R   t
   sympy.coreR   R   R   R   t   sympy.core.operationsR   R   t   sympy.matricesR   R	   R
   t   sympy.sets.setsR   R   R   t   sympy.unify.coreR   R   R   t   sympy.unifyR   R4   R2   t   illegalR   R   R   R    R(   R)   R   R6   t   NoneR>   (    (    (    s1   lib/python2.7/site-packages/sympy/unify/usympy.pyt   <module>   s&   "									