Exemplo n.º 1
0
def unify(x, y, s=None, **kwargs):
    """ Structural unification of two expressions/patterns

    Examples
    ========

    >>> from sympy.unify.usympy import unify
    >>> from sympy import Wild
    >>> from sympy.abc import x, y, z
    >>> from sympy.core.compatibility import next
    >>> expr = 2*x + y + z
    >>> pattern = 2*Wild('p') + Wild('q')
    >>> next(unify(expr, pattern, {}))
    {p_: x, q_: y + z}

    >>> expr = x + y + z
    >>> pattern = Wild('p') + Wild('q')
    >>> len(list(unify(expr, pattern, {})))
    12
    """
    s = s or {}
    s = dict((deconstruct(k), deconstruct(v)) for k, v in s.items())

    ds = core.unify(deconstruct(x), deconstruct(y), s,
                                                is_associative=is_associative,
                                                is_commutative=is_commutative,
                                                **kwargs)
    for d in ds:
        yield dict((construct(k), construct(v)) for k, v in d.items())
Exemplo n.º 2
0
def main(n):
    C1 = C('Add', [i for i in range(8)])
    C2 = C('Add', [Variable(i) for i in range(8)])

    for idx in range(n):
        lst = []
        for elem in core.unify(C1, C2, {}):
            lst.append(elem)

    return lst
Exemplo n.º 3
0
def main(n):
    C1 = C('Add', [i for i in range(8)])
    C2 = C('Add', [Variable(i) for i in range(8)])

    for idx in range(n):
        lst = []
        for elem in core.unify(C1, C2, {}):
            lst.append(elem)

    return lst
Exemplo n.º 4
0
def unify(x, y, s=None, variables=(), **kwargs):
    """ 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
    >>> from sympy.core.compatibility import next

    >>> 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}

    """
    decons = lambda x: deconstruct(x, variables)
    s = s or {}
    s = dict((decons(k), decons(v)) for k, v in s.items())

    ds = core.unify(decons(x),
                    decons(y),
                    s,
                    is_associative=is_associative,
                    is_commutative=is_commutative,
                    **kwargs)
    for d in ds:
        yield dict((construct(k), construct(v)) for k, v in d.items())
Exemplo n.º 5
0
def unify(x, y, s=None, variables=(), **kwargs):
    """ 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
    >>> from sympy.core.compatibility import next

    >>> 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}

    """
    decons = lambda x: deconstruct(x, variables)
    s = s or {}
    s = dict((decons(k), decons(v)) for k, v in s.items())

    ds = core.unify(decons(x), decons(y), s,
                                     is_associative=is_associative,
                                     is_commutative=is_commutative,
                                     **kwargs)
    for d in ds:
        yield dict((construct(k), construct(v)) for k, v in d.items())
Exemplo n.º 6
0
def unify(x, y, s=None, variables=(), **kwargs):
    """ Structural unification of two expressions/patterns

    Examples
    ========

    >>> from sympy.unify.usympy import unify
    >>> from sympy import Basic
    >>> from sympy.abc import x, y, z, p, q
    >>> from sympy.core.compatibility import next

    >>> 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

    Wilds may be specified directly in the call to unify

    """
    decons = lambda x: deconstruct(x, variables)
    s = s or {}
    s = dict((decons(k), decons(v)) for k, v in s.items())

    ds = core.unify(decons(x),
                    decons(y),
                    s,
                    is_associative=is_associative,
                    is_commutative=is_commutative,
                    **kwargs)
    for d in ds:
        yield dict((construct(k), construct(v)) for k, v in d.items())
Exemplo n.º 7
0
def unify(x, y, s=None, variables=(), **kwargs):
    """ Structural unification of two expressions/patterns

    Examples
    ========

    >>> from sympy.unify.usympy import unify
    >>> from sympy import Basic
    >>> from sympy.abc import x, y, z, p, q
    >>> from sympy.core.compatibility import next

    >>> 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

    Wilds may be specified directly in the call to unify

    """
    decons = lambda x: deconstruct(x, variables)
    s = s or {}
    s = dict((decons(k), decons(v)) for k, v in s.items())

    ds = core.unify(decons(x), decons(y), s,
                                     is_associative=is_associative,
                                     is_commutative=is_commutative,
                                     **kwargs)
    for d in ds:
        yield dict((construct(k), construct(v)) for k, v in d.items())
Exemplo n.º 8
0
def unify(a, b, s={}):
    return core.unify(a, b, s=s, is_associative=is_associative,
                          is_commutative=is_commutative)
Exemplo n.º 9
0
def unify(a, b, s={}):
    return core.unify(a,
                      b,
                      s=s,
                      is_associative=is_associative,
                      is_commutative=is_commutative)
Exemplo n.º 10
0
def unify(a, b, s={}):
    return core.unify(a, b, s)
Exemplo n.º 11
0
def unify(a, b, s={}):
    return core.unify(a, b, s)