ó
¡¼™\c           @  s›   d  d l  m Z m Z d  d l m Z d  d l m Z m Z d  d l j	 j
 Z
 d  d l m Z d „  Z e d „ Z e d „ Z e d	 „ Z e d
 „ Z d S(   iÿÿÿÿ(   t   print_functiont   division(   t   partial(   t   chaint   minimizeN(   t   yieldifyc         C  s   |  S(   N(    (   t   x(    (    s4   lib/python2.7/site-packages/sympy/strategies/tree.pyt   <lambda>   t    c      	   C  sS   xF | D]> } t  |  | ƒ r | | t t t d | d | ƒ|  ƒ Œ  Sq W| |  ƒ S(   sm   Apply functions onto recursive containers (tree)

    join - a dictionary mapping container types to functions
      e.g. ``{list: minimize, tuple: chain}``

    Keys are containers/iterables.  Values are functions [a] -> a.

    Examples
    ========

    >>> from sympy.strategies.tree import treeapply
    >>> tree = [(3, 2), (4, 1)]
    >>> treeapply(tree, {list: max, tuple: min})
    2

    >>> add = lambda *args: sum(args)
    >>> def mul(*args):
    ...     total = 1
    ...     for arg in args:
    ...         total *= arg
    ...     return total
    >>> treeapply(tree, {list: mul, tuple: add})
    25
    t   joint   leaf(   t
   isinstancet   mapR   t	   treeapply(   t   treeR	   R
   t   typ(    (    s4   lib/python2.7/site-packages/sympy/strategies/tree.pyR   
   s
    c         K  s0   t  t d | ƒ} t |  i | t 6t t 6|  S(   sÕ   Execute a strategic tree.  Select alternatives greedily

    Trees
    -----

    Nodes in a tree can be either

    function - a leaf
    list     - a selection among operations
    tuple    - a sequence of chained operations

    Textual examples
    ----------------

    Text: Run f, then run g, e.g. ``lambda x: g(f(x))``
    Code: ``(f, g)``

    Text: Run either f or g, whichever minimizes the objective
    Code: ``[f, g]``

    Textx: Run either f or g, whichever is better, then run h
    Code: ``([f, g], h)``

    Text: Either expand then simplify or try factor then foosimp. Finally print
    Code: ``([(expand, simplify), (factor, foosimp)], print)``

    Objective
    ---------

    "Better" is determined by the objective keyword.  This function makes
    choices to minimize the objective.  It defaults to the identity.

    Examples
    ========

    >>> from sympy.strategies.tree import greedy
    >>> inc    = lambda x: x + 1
    >>> dec    = lambda x: x - 1
    >>> double = lambda x: 2*x

    >>> tree = [inc, (dec, double)] # either inc or dec-then-double
    >>> fn = greedy(tree)
    >>> fn(4)  # lowest value comes from the inc
    5
    >>> fn(1)  # lowest value comes from dec then double
    0

    This function selects between options in a tuple.  The result is chosen that
    minimizes the objective function.

    >>> fn = greedy(tree, objective=lambda x: -x)  # maximize
    >>> fn(4)  # highest value comes from the dec then double
    6
    >>> fn(1)  # highest value comes from the inc
    2

    Greediness
    ----------

    This is a greedy algorithm.  In the example:

        ([a, b], c)  # do either a or b, then do c

    the choice between running ``a`` or ``b`` is made without foresight to c
    t	   objective(   R   R   R   t   listR   t   tuple(   R   R   t   kwargst   optimize(    (    s4   lib/python2.7/site-packages/sympy/strategies/tree.pyt   greedy)   s    Bc         C  s'   t  |  i t j t 6t j t 6d | ƒS(   sœ   Execute a strategic tree.  Return all possibilities.

    Returns a lazy iterator of all possible results

    Exhaustiveness
    --------------

    This is an exhaustive algorithm.  In the example

        ([a, b], [c, d])

    All of the results from

        (a, c), (b, c), (a, d), (b, d)

    are returned.  This can lead to combinatorial blowup.

    See sympy.strategies.greedy for details on input
    R
   (   R   t   brancht	   multiplexR   R   R   (   R   R
   (    (    s4   lib/python2.7/site-packages/sympy/strategies/tree.pyt
   allresultsn   s     c           s   ‡  ‡ ‡ f d †  S(   Nc           s%   t  t t ˆ ˆ   |  ƒ ƒ d ˆ ƒS(   Nt   key(   t   minR   R   (   t   expr(   R   R   R   (    s4   lib/python2.7/site-packages/sympy/strategies/tree.pyR   †   s   (    (   R   R   R   (    (   R   R   R   s4   lib/python2.7/site-packages/sympy/strategies/tree.pyt   brute…   s    (   t
   __future__R    R   t	   functoolsR   t   sympy.strategiesR   R   t   sympy.strategies.brancht
   strategiesR   R   t   identityR   R   R   R   (    (    (    s4   lib/python2.7/site-packages/sympy/strategies/tree.pyt   <module>   s   	E