ó
¦–Õ\c           @` sÐ   d  d l  m Z m Z m Z d  d l m Z d  d l m Z d  d l Z	 d d l
 m Z d d l m Z d d	 l
 m Z d d
 l m Z d d l m Z d d l m Z d „  Z d „  Z d „  Z d „  Z d S(   i    (   t   absolute_importt   divisiont   print_function(   t   product(   t   mulNi   (   t   Arrayi   (   t   tokenize(   t   flatten(   t   reduce(   t   HighLevelGraph(   t   Mc         ` sÐ  t  d „  | Dƒ ƒ s t ‚ t |  ƒ d } t | ƒ d } g  t t |  ƒ ƒ D] } d  ^ qO } g  t t | ƒ ƒ D] } d  ^ qt } x1| d k s¡ | d k r¹|  | | | k rë | | | | <| | | | <| d 8} | d 8} q‰ n  |  | } | | }	 | d k r"d | | <| d 8} q‰ |	 d k rEd | | <| d 8} q‰ | |	 k  r‘| d }
 x: |
 d k r—t t |  |
 | d !ƒ |	 k  r—|
 d 8}
 q^Wt t |  |
 | d !ƒ |	 k rÇt d ƒ ‚ n  x0 t |
 d | d ƒ D] } |  | f | | <qßWt t t t | |
 d | d !ƒ ƒ } t	 | |
 | ƒ | |
 <t t |  |
 d | d !ƒ ‰ t
 ‡ f d †  | |
 Dƒ ƒ | | <| d 8} |
 d } q‰ | |	 k r‰ | d } x: | d k rãt t | | | d !ƒ | k  rã| d 8} qªWt t | | | d !ƒ | k rt d ƒ ‚ n  t t | | d | d !ƒ ‰  t | | ˆ  ƒ | | <x0 t | d | d ƒ D] } | | f | | <q`Wt
 ‡  f d †  | | Dƒ ƒ | | <| d } | d 8} q‰ q‰ Wt
 | ƒ t
 | ƒ f S(	   Nc         s` s   |  ] } t  | t ƒ Vq d  S(   N(   t
   isinstancet   tuple(   t   .0t   c(    (    s1   lib/python2.7/site-packages/dask/array/reshape.pys	   <genexpr>   s    i   i    s   Shapes not compatiblec         3` s   |  ] } ˆ  | Vq d  S(   N(    (   R   R   (   t   prod(    s1   lib/python2.7/site-packages/dask/array/reshape.pys	   <genexpr>4   s    c         3` s   |  ] } | ˆ  Vq d  S(   N(    (   R   R   (   t   cs(    s1   lib/python2.7/site-packages/dask/array/reshape.pys	   <genexpr>G   s    (   i   (   i   (   t   allt   AssertionErrort   lent   ranget   NoneR   R   t
   ValueErrort   mapt   expand_tupleR   t   contract_tuple(   t   inshapet   outshapet   inchunkst   iit   oit   it   result_inchunkst   result_outchunkst   dint   doutt   ileftt   chunk_reductiont   oleft(    (   R   R   s1   lib/python2.7/site-packages/dask/array/reshape.pyt   reshape_rechunk   s`    %%






/ '$

/ $
c         C` s¸   | d k r |  Sg  } xw |  D]o } | } t  | | d ƒ } x7 | d | k ru | j t | ƒ ƒ | t | ƒ 8} q? W| r | j | ƒ q q Wt |  ƒ t | ƒ k s® t ‚ t | ƒ S(   sÖ   

    >>> expand_tuple((2, 4), 2)
    (1, 1, 2, 2)

    >>> expand_tuple((2, 4), 3)
    (1, 1, 1, 1, 2)

    >>> expand_tuple((3, 4), 2)
    (1, 2, 2, 2)

    >>> expand_tuple((7, 4), 3)
    (2, 2, 3, 1, 1, 2)
    i   i   (   t   maxt   appendt   intt   sumR   R   (   t   chunkst   factort   outR   t   xt   part(    (    s1   lib/python2.7/site-packages/dask/array/reshape.pyR   O   s    c         C` s   t  |  ƒ | d k s t ‚ g  } d } xL |  D]D } | | 7} | | } | | } | | } | r/ | j | ƒ q/ q/ Wt | ƒ S(   s˜    Return simple chunks tuple such that factor divides all elements

    Examples
    --------

    >>> contract_tuple((2, 2, 8, 4), 4)
    (4, 8, 4)
    i    (   R+   R   R)   R   (   R,   R-   R.   t   residualt   chunkt   divt   good(    (    s1   lib/python2.7/site-packages/dask/array/reshape.pyR   n   s    	



c         ` s  d d l  m } t t | | ƒ ƒ } g  | D] } | d k r, | ^ q, } t | ƒ t | ƒ k  rð t | ƒ t | ƒ d k r t d ƒ ‚ n  t | ƒ d k r² |  j d k r² |  S| |  j t t	 | d ƒ ƒ ‰  t ‡  f d †  | Dƒ ƒ } n  t
 j t |  j ƒ ƒ rt d |  j ƒ ‚ n  t t	 | d ƒ |  j k rGt d ƒ ‚ n  |  j | k rZ|  Sd t |  | ƒ } |  j d k rt t |  j ƒ  ƒ ƒ } i t j | | f | f d t | ƒ 6} t d
 „  | Dƒ ƒ } t j | | d |  g ƒ}	 t |	 | | d |  j ƒSt |  j | |  j ƒ \ }
 } |  j |
 ƒ } t t | j g g  |
 D] } t t | ƒ ƒ ^ qKŒ ƒ } t t | g g  | D] } t t | ƒ ƒ ^ q‚Œ ƒ } t t | Œ  ƒ } d „  t  | | | ƒ Dƒ } t j | | d | g ƒ}	 t |	 | | d |  j ƒS(   s²   Reshape array to new shape

    This is a parallelized version of the ``np.reshape`` function with the
    following limitations:

    1.  It assumes that the array is stored in `row-major order`_
    2.  It only allows for reshapings that collapse or merge dimensions like
        ``(1, 2, 3, 4) -> (1, 6, 4)`` or ``(64,) -> (4, 4, 4)``

    .. _`column-major order`: https://en.wikipedia.org/wiki/
                              Row-_and_column-major_order

    When communication is necessary this algorithm depends on the logic within
    rechunk.  It endeavors to keep chunk sizes roughly the same when possible.

    See Also
    --------
    dask.array.rechunk
    numpy.reshape
    i   (   t   sanitize_indexiÿÿÿÿs&   can only specify one unknown dimensionc         3` s'   |  ] } | d  k r ˆ  n | Vq d S(   iÿÿÿÿN(    (   R   t   s(   t   missing_size(    s1   lib/python2.7/site-packages/dask/array/reshape.pys	   <genexpr>¦   s    s/   Array chunk size or shape is unknown. shape: %ss)   total size of new array must be unchangeds   reshape-i    c         s` s   |  ] } | f Vq d  S(   N(    (   R   t   d(    (    s1   lib/python2.7/site-packages/dask/array/reshape.pys	   <genexpr>¶   s    t   dependenciest   dtypec         S` s.   i  |  ]$ \ } } } t  j | | f | “ q S(    (   R
   t   reshape(   R   t   at   bt   shape(    (    s1   lib/python2.7/site-packages/dask/array/reshape.pys
   <dictcomp>Â   s   	 (   i    (!   t   slicingR5   R   R   R   R   t   ndimt   sizeR   R   t   npt   isnanR+   R>   R   t   npartitionst   nextR   t   __dask_keys__R
   R;   R	   t   from_collectionsR   R:   R'   R,   t   rechunkt   listR   t   nameR   t   zip(   R/   R>   R5   R6   t   known_sizesRJ   t   keyt   dskR,   t   graphR   t	   outchunkst   x2R   t   in_keyst   out_keyst   shapes(    (   R7   s1   lib/python2.7/site-packages/dask/array/reshape.pyR;   …   s>    %!*:7(   t
   __future__R    R   R   t	   itertoolsR   t   operatorR   t   numpyRB   t   coreR   t   baseR   R   t   compatibilityR   t   highlevelgraphR	   t   utilsR
   R'   R   R   R;   (    (    (    s1   lib/python2.7/site-packages/dask/array/reshape.pyt   <module>   s   	?		