ó
¦–Õ\c           @` så  d  d l  m Z m Z m Z d  d l Z d  d l m Z d  d l m Z d  d l	 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 m Z m Z d
 d l m Z d
 d l m Z d „  Z d „  Z d „  Z d „  Z e e  d „ Z! e  d „ Z" d d d „ Z# d  e  d „ Z$ d  e  d „ Z% d „  Z& d „  Z' d „  Z( d „  Z) e d „ Z* e d „ Z+ d „  Z, d  „  Z- e d! „ Z. d" „  Z/ d# „  Z0 d$ „  Z1 e e
 j2 j3 ƒ e  e  e d% „ ƒ Z3 d S(&   i    (   t   absolute_importt   divisiont   print_functionN(   t   wraps(   t   Numberi   (   t   tokenize(   t	   blockwise(   t   apply(   t   HighLevelGraphi   (   t   dotmanyt   Arrayt   concatenate(   t   eye(   t   RandomStatec         c` s6   d } x) |  D]! } | } | | 7} | | f Vq Wd  S(   Ni    (    (   t   itt   totalt   xt   total_previous(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _cumsum_blocks   s
    
c         C` s   |  d |  d | f S(   Ni   (    (   t   lastt   new(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _cumsum_part   s    c         C` sD   t  |  | g ƒ } t j | ƒ r' |  n | } t j | ƒ r@ | S| S(   N(   t   mint   npt   isnan(   t   mt   nt   k_0t   k_1(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _nanmin   s    c         C` sP   |  j  d d k r< t j d ƒ t j d |  j  d f ƒ f St j j |  ƒ Sd S(   sê   
    A wrapper for np.linalg.qr that handles arrays with 0 rows

    Notes: Created for tsqr so as to manage cases with uncertain
    array dimensions. In particular, the case where arrays have
    (uncertain) chunks with 0 rows.
    i    i   N(   i    i    (   t   shapeR   t   zerost   linalgt   qr(   t   a(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _wrapped_qr%   s    	)c   X      ` sª  t  ˆ  j d ƒ t  ˆ  j d ƒ } } t ˆ  j d ƒ ˆ  j d d } } ˆ  j d k od | d k sˆ t d j ˆ  j ˆ  j ƒ ƒ ‚ n  d t ˆ  | ƒ } ˆ  j \ } ‰ | d f }	 t	 j
 j t	 j d d5 d ˆ  j ƒ ƒ \ }
 } ˆ  j ƒ  j j ƒ  } ˆ  j ƒ  j j ƒ  } d | ‰	 t t ˆ	 d	 ˆ  j d	 d
 i |	 ˆ  j 6ƒ} | | ˆ	 <ˆ  j ƒ  | ˆ	 <d | d ‰ t ‡ ‡	 f d †  t |	 d ƒ Dƒ ƒ } | | ˆ <ˆ	 h | ˆ <d | d ‰
 t ‡	 ‡
 f d †  t |	 d ƒ Dƒ ƒ } | | ˆ
 <ˆ	 h | ˆ
 <| | } t d „  ˆ  j Dƒ ƒ } t	 j | | ƒ } | d4 k rA| n | d | k } | oft | ƒ d k } | rD| rD| rDg  } g  } d } x… t ˆ  j d ƒ D]p \ } } | } t | | ƒ } | } | | | k rô| j | ƒ g  } d } n  | j | | f ƒ | | 7} q¡Wt  | ƒ d k r7| j | ƒ n  d | d ‰ t ‡
 ‡ f d †  t | ƒ Dƒ ƒ } | | ˆ <ˆ
 h | ˆ <t g  | D] } t  t! d „  | ƒ ƒ ^ q‹ƒ }  t" | | ƒ }! t# |! ˆ d t  |  ƒ ˆ f d |  ˆ f d | j ƒ}" t$ |" d | ƒ\ ‰ }# t% j& ˆ j' j |# j' j ƒ } t% j& ˆ j' j |# j' j ƒ } d | d ‰ t ‡ ‡ ‡ f d †  t | ƒ Dƒ ƒ }$ |$ | ˆ <ˆ j ƒ  | ˆ <d | }% i |# j d d f |% d d f 6}& |& | |% <|# j ƒ  | |% <d | d }' t t	 j( |' d	 ˆ d	 ˆ d	 d
 i |	 ˆ 6|	 ˆ 6ƒ}( |( | |' <ˆ ˆ |' h | |' <ng  t |	 d ƒ D] }) ˆ
 |) d f ^ qU}* d | d }+ i t	 j) t |* f f |+ d d f 6}, |, | |+ <ˆ
 h | |+ <d | d }- t t	 j
 j |- d	 |+ d	 d
 i d6 |+ 6ƒ}. |. | |- <|+ h | |- <d | d ‰ i t* j+ |- d d f d f ˆ d d f 6}/ |/ | ˆ <|- h | ˆ <t d „  ˆ  j Dƒ ƒ síg  ˆ  j d D] }0 t |0 ˆ ƒ ^ q}1 g  t, |1 ƒ D], }0 t- |0 d |0 d ƒ t- d ˆ ƒ f ^ q©}2 i  }3 t. ƒ  }4 nNd | d ‰ ‡  ‡ f d †  t |	 d ƒ Dƒ }5 d | d  ‰ i t* j+ t/ ˆ  j d d f d f d f ˆ 6}6 d! | d ‰ i d ˆ d f g ˆ d f 6}7 |7 j0 ‡ ‡ f d" †  t d |	 d ƒ Dƒ ƒ d# | d$ ‰ ‡ ‡ ‡ f d% †  t |	 d ƒ Dƒ }8 t% j& |6 |5 |7 |8 ƒ }3 ˆ  j ˆ ˆ h }4 g  t |	 d ƒ D] }) ˆ |) f ^ q#}2 |3 | d& | <|4 | d& | <d | d ‰ t ‡ ‡ f d' †  t |2 ƒ Dƒ ƒ }$ |$ | ˆ <ˆ d& | h | ˆ <d( | d }' t t	 j( |' d	 ˆ d	 ˆ d	 d
 i |	 ˆ 6|	 ˆ 6ƒ}( |( | |' <ˆ ˆ h | |' <d | d) }% i t* j+ |- d d f d f |% d d f 6}& |& | |% <|- h | |% <| sÏt	 j1 ˆ  j d ƒ p‹	t d* „  ˆ  j d Dƒ ƒ }9 t	 j1 ˆ  j d ƒ p¾	t d+ „  ˆ  j d Dƒ ƒ }: |9 r
|: r
ˆ  j }; ˆ  j d t	 j2 f f }< t	 j2 t	 j2 f }= t	 j2 f t	 j2 f f }> nR|9 rc
|: rc
ˆ  j }; ˆ  j d ˆ f f }< ˆ ˆ f }= ˆ ˆ f }> n|9 r¿
|: r¿
ˆ  j }; ˆ  j d t	 j2 f f }< t	 j2 t	 j2 f }= t	 j2 f t	 j2 f f }> n¯ ˆ  j d ˆ  j d k râ
ˆ  j n ˆ  j d ˆ  j d f }; ˆ  j d ˆ  j d k rˆ  j n ˆ  j d ˆ  j d f }< ˆ  j d ˆ  j d k r_ˆ ˆ f n ˆ  j }= |= }> t" | | ƒ }! t# |! |' d |; d |< d |
 j ƒ}? t# |! |% d |= d |> d | j ƒ}@ |? |@ f Sd, | d- }A t t	 j
 j3 |A d	 |% d	 d
 i d7 |% 6ƒ}B d | d. }C i t* j+ |A d d f d f |C d d f 6}D d | d/ }E i t* j+ |A d d f d f |E d f 6}F d | d0 }G i t* j+ |A d d f d f |G d d f 6}H d | d1 }I t t4 |I d	 |' d2 |C d3 d
 i |	 |' 6d8 |C 6ƒ}J |B | |A <|% h | |A <|D | |C <|A h | |C <|J | |I <|' |C h | |I <|F | |E <|A h | |E <|H | |G <|A h | |G <t	 j
 j3 t	 j d d9 d ˆ  j ƒ ƒ \ }K }L }M t5 | ˆ ƒ }N | }O t	 j1 |N ƒ sÆt |N ƒ n |N }P |P }Q |P }R ˆ }S t |R |S ƒ }T t" | | ƒ }! t# |! |I d |O |P f d ˆ  j d |P f f d |K j ƒ}U t# |! |E d |Q f d |Q f f d |L j ƒ}V t# |! |G d |T |T f d ˆ f ˆ f f d |M j ƒ}W |U |V |W f Sd4 S(:   s°   Direct Tall-and-Skinny QR algorithm

    As presented in:

        A. Benson, D. Gleich, and J. Demmel.
        Direct QR factorizations for tall-and-skinny matrices in
        MapReduce architectures.
        IEEE International Conference on Big Data, 2013.
        https://arxiv.org/abs/1301.1071

    This algorithm is used to compute both the QR decomposition and the
    Singular Value Decomposition.  It requires that the input array have a
    single column of blocks, each of which fit in memory.

    Parameters
    ----------
    data: Array
    compute_svd: bool
        Whether to compute the SVD rather than the QR decomposition
    _max_vchunk_size: Integer
        Used internally in recursion to set the maximum row dimension
        of chunks in subsequent recursive calls.

    Notes
    -----
    With ``k`` blocks of size ``(m, n)``, this algorithm has memory use that
    scales as ``k * m * n``.

    The implementation here is the recursive variant due to the ultimate
    need for one "single core" QR decomposition. In the non-recursive version
    of the algorithm, given ``k`` blocks, after ``k`` ``m * n`` QR
    decompositions, there will be a "single core" QR decomposition that will
    have to work with a ``(k * n, n)`` matrix.

    Here, recursion is applied as necessary to ensure that ``k * n`` is not
    larger than ``m`` (if ``m / n >= 2``). In particular, this is done
    to ensure that single core computations do not have to work on blocks
    larger than ``(m, n)``.

    Where blocks are irregular, the above logic is applied with the "height" of
    the "tallest" block used in place of ``m``.

    Consider use of the ``rechunk`` method to control this behavior. Blocks
    that are as tall as possible are recommended.

    See Also
    --------
    dask.array.linalg.qr - Powered by this algorithm
    dask.array.linalg.svd - Powered by this algorithm
    dask.array.linalg.sfqr - Variant for short-and-fat arrays
    i    i   i   s  Input must have the following properties:
  1. Have two dimensions
  2. Have only one column of blocks

Note: This function (tsqr) supports QR decomposition in the case of
tall-and-skinny matrices (single column chunk/block; see qr)Current shape: {},
Current chunksize: {}t   -R   t   dtypeR!   t   ijt	   numblockst   getitems   -q1c         3` s9   |  ]/ } ˆ  | d  f t  j ˆ | d  f d  f f Vq d S(   i    N(   t   operatorR(   (   t   .0t   i(   t
   name_q_st1t   name_qr_st1(    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>‰   s   s   -r1c         3` s9   |  ]/ } ˆ | d  f t  j ˆ  | d  f d f f Vq d S(   i    i   N(   R)   R(   (   R*   R+   (   R-   t
   name_r_st1(    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>‘   s   c         s` s+   |  ]! } | D] } t  j | ƒ Vq q d  S(   N(   R   R   (   R*   t   cst   c(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>   s    t   stackc      	   3` s[   |  ]Q \ } } ˆ | d  f t  j t g  | D] \ } } ˆ  | d  f ^ q+ f f f Vq d S(   i    N(   R   t   vstackt   tuple(   R*   R+   t   sub_block_infot   idxt   _(   R.   t   name_r_stacked(    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>¹   s   c         S` s   |  d S(   Ni   (    (   R   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   <lambda>Â   t    t   chunkst   _max_vchunk_sizes   getitem-s   -q2c      	   3` s²   |  ]¨ \ } } t  g  | D] } | d  ^ q t g  | D] } | d ^ q6 ƒ ƒ D]X \ } } ˆ | d  f t j ˆ j | d  f t | d  | d ƒ t d  ˆ  ƒ f f f VqP q d S(   i    i   N(   t   zipR   R)   R(   t   namet   slice(   R*   R+   R4   R   t   jt   e(   R   t
   name_q_st2t   q_inner(    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>Ï   s   	s   r-inner-s   dot-s   -q3s   -qr2s   -q2-auxc         s` s+   |  ]! } | D] } t  j | ƒ Vq q d  S(   N(   R   R   (   R*   R/   R0   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>ÿ   s    c         ` s:   i  |  ]0 } t  t ˆ  j | d  f d f f ˆ | f “ q S(   i    R   (   R   t   getattrR=   (   R*   R+   (   t   datat	   name_q2bs(    s0   lib/python2.7/site-packages/dask/array/linalg.pys
   <dictcomp>  s   	s   -nt   cumsumc         ` s8   i  |  ]. } t  ˆ | d  f ˆ  | f f ˆ | f “ q S(   i   (   R   (   R*   R+   (   RE   t	   name_q2cs(    s0   lib/python2.7/site-packages/dask/array/linalg.pys
   <dictcomp>  s   	R>   s   -qc         ` sC   i  |  ]9 } t  t t ˆ | f f t d  ˆ f g f ˆ  | f “ q S(   i    (   R3   R   R>   (   R*   R+   (   t   name_blockslicet   name_nRG   (    s0   lib/python2.7/site-packages/dask/array/linalg.pys
   <dictcomp>  s   	s   q-blocksizesc         3` s?   |  ]5 \ } } ˆ  | d  f t  j ˆ d  d  f | f f Vq d S(   i    N(   R)   R(   (   R*   R+   t   b(   RA   t   name_q_st2_aux(    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>-  s   t   dots   -r2c         s` s   |  ] } t  j | ƒ Vq d  S(   N(   R   R   (   R*   R0   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>B  s    c         s` s   |  ] } t  j | ƒ Vq d  S(   N(   R   R   (   R*   R0   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pys	   <genexpr>C  s    t   svds   -2s   -u2s   -s2s   -v2s   -u4t   ikt   kjN(   i   i   (   i   i   (   i   i   (   i   i   (   i   i   (6   t   lenR:   t   maxt   ndimt
   ValueErrort   formatR   t	   chunksizeR   R   R    R!   t   onesR%   t   __dask_graph__t   layerst   copyt   dependenciesR   R#   R=   t   __dask_layers__t   dictt   ranget   anyt   ceilt   Nonet   intt	   enumerateR   t   appendR3   t   sumt   mapR   R
   t   tsqrt   toolzt   merget   daskRL   R2   R)   R(   R   R>   t   setRC   t   updateR   t   nanRM   R	   R   (X   RD   t   compute_svdR;   t   nrt   nct   cr_maxt   cct   tokenR   R'   t   qqt   rrRX   RZ   t
   dsk_qr_st1t	   dsk_q_st1t	   dsk_r_st1t   single_core_compute_mt   chunks_well_definedt   prospective_blockst   meaningful_reduction_possiblet   can_distributet
   all_blockst
   curr_blockt   curr_block_szR5   t   a_mt   m_qt   n_qt   m_rt   dsk_r_stackedR4   t   vchunks_rstackedt   grapht	   r_stackedt   r_innert	   dsk_q_st2t
   name_r_st2t	   dsk_r_st2t
   name_q_st3t	   dsk_q_st3R+   t   to_stackt   name_r_st1_stackedt   dsk_r_st1_stackedt   name_qr_st2t
   dsk_qr_st2t   dsk_q_st2_auxR@   t   q2_block_sizest   block_slicest   dsk_q_blockslicest   depst   dsk_q2_shapest   dsk_nt   dsk_q2_cumsumt   dsk_block_slicest   is_unknown_mt   is_unknown_nt   q_shapet   q_chunkst   r_shapet   r_chunkst   qt   rt   name_svd_st2t   dsk_svd_st2t
   name_u_st2t	   dsk_u_st2t
   name_s_st2t	   dsk_s_st2t
   name_v_st2t	   dsk_v_st2t
   name_u_st4t	   dsk_u_st4t   uut   sst   vvht   kt   m_ut   n_ut   n_st   m_vht   n_vht   d_vht   ut   st   vh(    (   RD   R   RH   RI   RE   RG   R,   RA   RK   R-   R.   R7   RB   s0   lib/python2.7/site-packages/dask/array/linalg.pyRf   4   s’   4'%-




" 	
1*

"

,	

	"
)<	%		)

+
33			==/
	"		"




0!:-6c      
   ` s„  t  ˆ  j d ƒ t  ˆ  j d ƒ } } ˆ  j d d ˆ  j d d } } ˆ  j d k oz | d k oz | | k pz | d k sŒ t d ƒ ‚ n  | pŸ d t ˆ  ƒ } | d 7} ˆ  j \ } } t j j t j	 d d d ˆ  j
 ƒ ƒ \ }	 }
 ˆ  j ƒ  j j ƒ  } ˆ  j ƒ  j j ƒ  } | d	 } | d
 ‰ i ˆ  j d d f | d d f 6| | <ˆ  j ƒ  | | <‡  ‡ f d †  t | d ƒ Dƒ | ˆ <ˆ  j ƒ  | ˆ <| d } | d } | d } i t j j | d d f f | d d f 6| | <| h | | <i t j | d d f d f | d d f 6| | <| h | | <i t j | d d f d f | d d f 6| | <| h | | <t | | ƒ } t | | d | t | | ƒ f d | t | | ƒ f d |	 j
 ƒ} t | | d t | | ƒ | f d | | f d |
 j
 ƒ} | g } | d k rht | ˆ d t | | ƒ | | f d | ˆ  j d d f d |
 j
 ƒ} | j | j j | ƒ ƒ n  t | d d ƒ} | | f S(   s$   Direct Short-and-Fat QR

    Currently, this is a quick hack for non-tall-and-skinny matrices which
    are one chunk tall and (unless they are one chunk wide) have chunks
    that are wider than they are tall

    Q [R_1 R_2 ...] = [A_1 A_2 ...]

    it computes the factorization Q R_1 = A_1, then computes the other
    R_k's in parallel.

    Parameters
    ----------
    data: Array

    See Also
    --------
    dask.array.linalg.qr - Main user API that uses this function
    dask.array.linalg.tsqr - Variant for tall-and-skinny case
    i    i   i   sà  Input must have the following properties:
  1. Have two dimensions
  2. Have only one row of blocks
  3. Either one column of blocks or (first) chunk size on cols
     is at most that on rows (e.g.: for a 5x20 matrix,
     chunks=((5), (8,4,8)) is fine, but chunks=((5), (4,8,8)) is not;
     still, prefer something simple like chunks=(5,10) or chunks=5)

Note: This function (sfqr) supports QR decomposition in the case
of short-and-fat matrices (single row chunk/block; see qr)s   sfqr-R6   R   R%   t   A_1t   A_restc         ` s2   i  |  ]( } ˆ  j  d  d | f ˆ d  | f “ q S(   i    i   (   R=   (   R*   R5   (   RD   t   name_A_rest(    s0   lib/python2.7/site-packages/dask/array/linalg.pys
   <dictcomp>Ï  s   	t   Q_R_1t   Qt   R_1R:   t   axis(   i   i   (   RP   R:   RR   RS   R   R   R   R    R!   RV   R%   RW   RX   RY   RZ   R=   R[   R]   R)   R(   R   R
   R   Rc   t   TRL   R   (   RD   R=   Rn   Ro   t   crRq   t   prefixR   R   Rs   Rt   RX   RZ   t   name_A_1t	   name_Q_R1t   name_Qt   name_R_1R†   R¿   RÀ   t   RsR¼   t   R(    (   RD   R½   s0   lib/python2.7/site-packages/dask/array/linalg.pyt   sfqr˜  sT    '#
-

&


///6-	<i
   i   c         C` s   t  t | | | ƒ |  ƒ S(   s    Compression level to use in svd_compressed

    Given the size ``n`` of a space, compress that that to one of size
    ``q`` plus oversampling.

    The oversampling allows for greater flexibility in finding an
    appropriate subspace, a low value is often enough (10 is already a
    very conservative choice, it can be further reduced).
    ``q + oversampling`` should not be larger than ``n``.  In this
    specific implementation, ``q + oversampling`` is at least
    ``min_subspace_size``.

    >>> compression_level(100, 10)
    20
    (   R   RQ   (   R   R¢   t   oversamplingt   min_subspace_size(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   compression_levelô  s    c         C` s°   |  j  d } t | | ƒ } t | ƒ } | j d | | f d |  j d | f f ƒ } |  j | ƒ } x/ t | ƒ D]! }	 |  j |  j j | ƒ ƒ } qr Wt | ƒ \ } }
 | j S(   sa   Randomly sample matrix to find most active subspace

    This compression matrix returned by this algorithm can be used to
    compute both the QR decomposition and the Singular Value
    Decomposition.

    Parameters
    ----------
    data: Array
    q: int
        Size of the desired subspace (the actual size will be bigger,
        because of oversampling, see ``da.linalg.compression_level``)
    n_power_iter: int
        number of power iterations, useful when the singular values of
        the input matrix decay very slowly.

    References
    ----------
    N. Halko, P. G. Martinsson, and J. A. Tropp.
    Finding structure with randomness: Probabilistic algorithms for
    constructing approximate matrix decompositions.
    SIAM Rev., Survey and Review section, Vol. 53, num. 2,
    pp. 217-288, June 2011
    https://arxiv.org/abs/0909.4061
    i   t   sizeR:   (	   R   RÎ   R   t   standard_normalR:   RL   R]   RÂ   Rf   (   RD   R¢   t   n_power_itert   seedR   t
   comp_levelt   statet   omegat   mat_hR?   R6   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   compression_matrix  s    c   	      C` s²   t  |  | d | d | ƒ} | j |  ƒ } t | j d t ƒ\ } } } | j j | ƒ } | j } | d d … d | … f } | |  } | d | … d d … f } | | | f S(   sé   Randomly compressed rank-k thin Singular Value Decomposition.

    This computes the approximate singular value decomposition of a large
    array.  This algorithm is generally faster than the normal algorithm
    but does not provide exact results.  One can balance between
    performance and accuracy with input parameters (see below).

    Parameters
    ----------
    a: Array
        Input array
    k: int
        Rank of the desired thin SVD decomposition.
    n_power_iter: int
        Number of power iterations, useful when the singular values
        decay slowly. Error decreases exponentially as n_power_iter
        increases. In practice, set n_power_iter <= 4.

    Examples
    --------

    >>> u, s, vt = svd_compressed(x, 20)  # doctest: +SKIP

    Returns
    -------
    u:  Array, unitary / orthogonal
    s:  Array, singular values in decreasing order (largest first)
    v:  Array, unitary / orthogonal

    References
    ----------
    N. Halko, P. G. Martinsson, and J. A. Tropp.
    Finding structure with randomness: Probabilistic algorithms for
    constructing approximate matrix decompositions.
    SIAM Rev., Survey and Review section, Vol. 53, num. 2,
    pp. 217-288, June 2011
    https://arxiv.org/abs/0909.4061
    RÑ   RÒ   Rm   N(   R×   RL   Rf   RÂ   t   True(	   R"   R±   RÑ   RÒ   t   compt   a_compressedt   vR¹   R¸   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   svd_compressed-  s    '	
c         C` so   t  |  j d ƒ d k r< t  |  j d ƒ d k r< t |  ƒ St  |  j d ƒ d k r_ t |  ƒ St d ƒ ‚ d S(   s©  
    Compute the qr factorization of a matrix.

    Examples
    --------

    >>> q, r = da.linalg.qr(x)  # doctest: +SKIP

    Returns
    -------

    q:  Array, orthonormal
    r:  Array, upper-triangular

    See Also
    --------

    np.linalg.qr : Equivalent NumPy Operation
    dask.array.linalg.tsqr: Implementation for tall-and-skinny arrays
    dask.array.linalg.sfqr: Implementation for short-and-fat arrays
    i   i    s}  qr currently supports only tall-and-skinny (single column chunk/block; see tsqr)
and short-and-fat (single row chunk/block; see sfqr) matrices

Consider use of the rechunk method. For example,

x.rechunk({0: -1, 1: 'auto'}) or x.rechunk({0: 'auto', 1: -1})

which rechunk one shorter axis to a single chunk, while allowing
the other axis to automatically grow/shrink appropriately.N(   RP   R:   Rf   RË   t   NotImplementedError(   R"   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyR!   _  s    2

c         C` s   t  |  d t ƒS(   sÆ  
    Compute the singular value decomposition of a matrix.

    Examples
    --------

    >>> u, s, v = da.linalg.svd(x)  # doctest: +SKIP

    Returns
    -------

    u:  Array, unitary / orthogonal
    s:  Array, singular values in decreasing order (largest first)
    v:  Array, unitary / orthogonal

    See Also
    --------

    np.linalg.svd : Equivalent NumPy Operation
    dask.array.linalg.tsqr: Implementation for tall-and-skinny arrays
    Rm   (   Rf   RØ   (   R"   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyRM   …  s    c         C` s%   d d  l  } | j j |  | d t ƒS(   Ni    t   lower(   t   scipy.linalgR    t   solve_triangularRØ   (   R"   RJ   t   scipy(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _solve_triangular_lowerž  s    c      	   C` s—  d d l  } |  j d k r* t d ƒ ‚ n  |  j \ } } | | k rT t d ƒ ‚ n  t t |  j d |  j d ƒ ƒ d k s“ d } t | ƒ ‚ n  t |  j d ƒ } t |  j d ƒ } t |  ƒ } d | } d	 | }	 d
 | }
 d | } d | } d | } d | } d | } d | } i  } xÉt t	 | | ƒ ƒ D]²} |  j
 | | f } | d k rág  } x^ t | ƒ D]P } | | | | | f } t j | | | f | | | f f | | <| j | ƒ qrWt j | t | f f } n  | j j | f | | | | f <xõ t | d | ƒ D]à } t j | | | f |  j
 | | f f } | d k rÏg  } x^ t | ƒ D]P } | | | | | f } t j |
 | | f | | | f f | | <| j | ƒ q`Wt j | t | f f } n  t |
 | | f | f | | | | f <qWxò t | d | ƒ D]Ý } |  j
 | | f } | d k r²g  } x^ t | ƒ D]P } | | | | | f } t j | | | f | | | f f | | <| j | ƒ qCWt j | t | f f } n  t j t | | | f t j | f f f | | | | f <qWq;Wx—t t	 | | ƒ ƒ D]€} xwt t	 | | ƒ ƒ D]`} | | k r+t j | | | f d f | |	 | | f <t j | | | f d f | |
 | | f <t j | | | f d f | | | | f <t j |	 | | f |
 | | f f | | | | f <t j | | | f f | | | | f <t j |	 | | f f | | | | f <q#| | k r÷t j |  j d | |  j d | f f | |	 | | f <t j | | | f | | | f f | |
 | | f <t j |  j d | |  j d | f f | | | | f <| | | f | | | | f <q#t j |  j d | |  j d | f f | |	 | | f <t j |  j d | |  j d | f f | |
 | | f <| | | f | | | | f <q#WqW| j j t j d d d |  j ƒ ƒ \ } } } t j |	 | d |  g ƒ} t | |	 d |  j d |  j d | j ƒ} t j |
 | d |  g ƒ} t | |
 d |  j d |  j d | j ƒ} t j | | d |  g ƒ} t | | d |  j d |  j d | j ƒ} | | | f S(   s!  
    Compute the lu decomposition of a matrix.

    Examples
    --------

    >>> p, l, u = da.linalg.lu(x)  # doctest: +SKIP

    Returns
    -------

    p:  Array, permutation matrix
    l:  Array, lower triangular matrix with unit diagonal.
    u:  Array, upper triangular matrix
    i    Ni   s/   Dimension must be 2 to perform lu decompositions9   Input must be a square matrix to perform lu decompositioni   sq   All chunks must be a square matrix to perform lu decomposition. Use .rechunk method to change the size of chunks.s   lu-lu-s   lu-p-s   lu-l-s   lu-u-s	   lu-p-inv-s   lu-l-permute-s   lu-u-transpose-s   lu-plu-dot-s
   lu-lu-dot-R   R%   RZ   R:   (   i   i   (   Rß   RR   RS   R   RP   Rj   R:   R   R]   R   R=   R   RL   Rc   R)   t   subRd   R    t   luRâ   t	   transposeR(   R   RV   R%   R   t   from_collectionsR
   (   R"   Rá   t   xdimt   ydimt   msgt   vdimt   hdimRr   t   name_lut   name_pt   name_lt   name_ut
   name_p_invt   name_l_permutedt   name_u_transposedt   name_plu_dott   name_lu_dott   dskR+   t   targett   prevst   pt   prevR?   R±   t   ppt   llR®   R†   t   lR¸   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyRä   £  s¢    *








('(&(*(((1%(81888$0***c      	   ` s¿  d d l  } |  j d k r* t d ƒ ‚ n  ˆ  j d k r” |  j d ˆ  j d k rb t d ƒ ‚ n  |  j d ˆ  j d k r  d } t | ƒ ‚ q  n t d ƒ ‚ t |  j d ƒ } ˆ  j d k rÈ d n t ˆ  j d ƒ } t |  ˆ  | ƒ } d	 | ‰ d
 | } ‡  f d †  }	 ‡  ‡ f d †  }
 i  } | r'xýt | ƒ D]å } xÜ t | ƒ D]Î } |	 | | ƒ } | d k rôg  } xa t | ƒ D]S } | | | | | f } t j	 |  j
 | | f |
 | | ƒ f | | <| j | ƒ q‚Wt j | t | f f } n  t |  j
 | | f | f | |
 | | ƒ <qNWq;Wnxt | ƒ D]ö } xí t | ƒ D]ß } |	 | | ƒ } | | d k  røg  } xh t | d | ƒ D]S } | | | | | f } t j	 |  j
 | | f |
 | | ƒ f | | <| j | ƒ q†Wt j | t | f f } n  | j j |  j
 | | f | f | |
 | | ƒ <qGWq4Wt j ˆ | d |  ˆ  g ƒ} t t j d d g d d g g d |  j ƒt j d d g d ˆ  j ƒƒ } t | ˆ d ˆ  j d ˆ  j d | j ƒS(   sõ  
    Solve the equation `a x = b` for `x`, assuming a is a triangular matrix.

    Parameters
    ----------
    a : (M, M) array_like
        A triangular matrix
    b : (M,) or (M, N) array_like
        Right-hand side matrix in `a x = b`
    lower : bool, optional
        Use only data contained in the lower triangle of `a`.
        Default is to use upper triangle.

    Returns
    -------
    x : (M,) or (M, N) array
        Solution to the system `a x = b`. Shape of return matches `b`.
    i    Ni   s   a must be 2 dimensionali   s'   a.shape[1] and b.shape[0] must be equals\   a.chunks[1] and b.chunks[0] must be equal. Use .rechunk method to change the size of chunks.s   b must be 1 or 2 dimensionals   solve-triangular-s   solve-tri-dot-c         ` s0   ˆ  j  d k r ˆ  j |  f Sˆ  j |  | f Sd  S(   Ni   (   RR   R=   (   R+   R?   (   RJ   (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _b_initO  s    c         ` s*   ˆ  j  d k r ˆ |  f Sˆ |  | f Sd  S(   Ni   (   RR   (   R+   R?   (   RJ   R=   (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _keyU  s    
RZ   R%   R   R:   (   Rß   RR   RS   R   R:   RP   R   R]   R   RL   R=   Rc   R)   Rã   Rd   Râ   R    Rà   R   Ræ   t   arrayR%   R
   (   R"   RJ   RÞ   Rá   Ré   t   vchunkst   hchunksRr   t	   name_mdotRý   Rþ   Rõ   R+   R?   Rö   R÷   R±   Rù   R†   t   res(    (   RJ   R=   s0   lib/python2.7/site-packages/dask/array/linalg.pyRà   $  sZ    (

+3+6*!c         C` sd   | r t  |  ƒ \ } } n' t |  ƒ \ } } } | j j | ƒ } t | | d t ƒ} t | | ƒ S(   sq  
    Solve the equation ``a x = b`` for ``x``. By default, use LU
    decomposition and forward / backward substitutions. When ``sym_pos`` is
    ``True``, use Cholesky decomposition.

    Parameters
    ----------
    a : (M, M) array_like
        A square matrix.
    b : (M,) or (M, N) array_like
        Right-hand side matrix in ``a x = b``.
    sym_pos : bool
        Assume a is symmetric and positive definite. If ``True``, use Cholesky
        decomposition.

    Returns
    -------
    x : (M,) or (M, N) Array
        Solution to the system ``a x = b``.  Shape of the return matches the
        shape of `b`.
    RÞ   (   t	   _choleskyRä   RÂ   RL   Rà   RØ   (   R"   RJ   t   sym_posRü   R¸   Rø   t   uy(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   solve{  s    c         C` s+   t  |  t |  j d d |  j d d ƒƒ S(   s  
    Compute the inverse of a matrix with LU decomposition and
    forward / backward substitutions.

    Parameters
    ----------
    a : array_like
        Square matrix to be inverted.

    Returns
    -------
    ainv : Array
        Inverse of the matrix `a`.
    i    R:   (   R  R   R   R:   (   R"   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   invš  s    c         C` s"   d d  l  } | j j |  d t ƒS(   Ni    RÞ   (   Rß   R    t   choleskyRØ   (   R"   Rá   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _cholesky_lower¬  s    c         C` s$   t  |  ƒ \ } } | r | S| Sd S(   s×  
    Returns the Cholesky decomposition, :math:`A = L L^*` or
    :math:`A = U^* U` of a Hermitian positive-definite matrix A.

    Parameters
    ----------
    a : (M, M) array_like
        Matrix to be decomposed
    lower : bool, optional
        Whether to compute the upper or lower triangular Cholesky
        factorization.  Default is upper-triangular.

    Returns
    -------
    c : (M, M) Array
        Upper- or lower-triangular Cholesky factor of `a`.
    N(   R  (   R"   RÞ   Rü   R¸   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyR	  ±  s    c      	   C` s  d d l  } |  j d k r* t d ƒ ‚ n  |  j \ } } | | k rT t d ƒ ‚ n  t t |  j d |  j d ƒ ƒ d k s“ d } t | ƒ ‚ n  t |  j d ƒ } t |  j d ƒ } t |  ƒ } d | } d	 | }	 d
 | }
 i  } xet | ƒ D]W} xNt | ƒ D]@} | | k  rrt	 j
 |  j d | |  j d | f f | | | | f <| | | f | |
 | | f <q	| | k r_|  j | | f } | d k rg  } x^ t | ƒ D]P } |	 | | | | f } t	 j | | | f |
 | | f f | | <| j | ƒ q¯Wt j | t | f f } n  t | f | | | | f <t	 j | | | f f | |
 | | f <q	|  j | | f } | d k rÿg  } x^ t | ƒ D]P } |	 | | | | f } t	 j | | | f |
 | | f f | | <| j | ƒ qWt j | t | f f } n  t | | | f | f | |
 | | f <t	 j |
 | | f f | | | | f <q	Wqö Wt j |
 | d |  g ƒ} t j | | d |  g ƒ} | j j t	 j d d g d d g g d |  j ƒƒ } t | | d |  j d |  j d | j ƒ} t | |
 d |  j d |  j d | j ƒ} | | f S(   sq   
    Private function to perform Cholesky decomposition, which returns both
    lower and upper triangulars.
    i    Ni   s5   Dimension must be 2 to perform cholesky decompositions?   Input must be a square matrix to perform cholesky decompositioni   sw   All chunks must be a square matrix to perform cholesky decomposition. Use .rechunk method to change the size of chunks.s	   cholesky-s   cholesky-lt-dot-s   cholesky-upper-RZ   i   R%   R   R:   (   Rß   RR   RS   R   RP   Rj   R:   R   R]   R   R   R=   RL   Rc   R)   Rã   Rd   R
  Rå   Râ   R   Ræ   R    R	  Rÿ   R%   R
   (   R"   Rá   Rç   Rè   Ré   Rê   Rë   Rr   R=   t   name_lt_dott
   name_upperRõ   R+   R?   Rö   R÷   Rø   Rù   t   graph_uppert   graph_lowert   choRÞ   t   upper(    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyR  Ë  s`    *


8(((%-6**c         C` s   |  d  d  d … j  ƒ  |  S(   Niÿÿÿÿ(   t   sort(   R   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   _sort_decreasing  s    c      	   C` sõ  t  |  ƒ \ } } t | | j j | ƒ ƒ } | |  j | ƒ } | d j d t ƒ } t |  | ƒ } d | } i t j j	 | j
 d d f f | f 6} t j | | d | g ƒ}	 t |	 | d d d d d t ƒ}
 d	 | } | j } i t t j t j j t j | j
 d d f | j
 d d f f f f f | d f 6} t j | | d | g ƒ}	 t j j t j d
 d g d
 d g g d |  j ƒt j d d
 g d | j ƒƒ \ } } } } t |	 | d | j d f d | j d d | j ƒ} | | |
 | f S(   sA  
    Return the least-squares solution to a linear matrix equation using
    QR decomposition.

    Solves the equation `a x = b` by computing a vector `x` that
    minimizes the Euclidean 2-norm `|| b - a x ||^2`.  The equation may
    be under-, well-, or over- determined (i.e., the number of
    linearly independent rows of `a` can be less than, equal to, or
    greater than its number of linearly independent columns).  If `a`
    is square and of full rank, then `x` (but for round-off error) is
    the "exact" solution of the equation.

    Parameters
    ----------
    a : (M, N) array_like
        "Coefficient" matrix.
    b : (M,) array_like
        Ordinate or "dependent variable" values.

    Returns
    -------
    x : (N,) Array
        Least-squares solution. If `b` is two-dimensional,
        the solutions are in the `K` columns of `x`.
    residuals : (1,) Array
        Sums of residuals; squared Euclidean 2-norm for each column in
        ``b - a*x``.
    rank : Array
        Rank of matrix `a`.
    s : (min(M, N),) Array
        Singular values of `a`.
    i   t   keepdimss   lstsq-rank-i    RZ   R   R:   R%   s   lstsq-singular-i   (    (    (   R!   Rà   RÂ   RL   Rd   RØ   R   R   R    t   matrix_rankR=   R   Ræ   R
   Ra   R  t   sqrtt   eigvalst   lstsqRÿ   R%   R   (   R"   RJ   R¢   R£   R   t	   residualsRr   t   rnamet   rdskR†   t   rankt   snamet   rtt   sdskR6   R¯   R¹   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyR    s*    !
(!
		=0-c         C` s"  | d  k r$ t t |  j ƒ ƒ } n- t | t ƒ rE t | ƒ f } n t | ƒ } t | ƒ d k rr t d ƒ ‚ n  | d k r¨ d  } t | ƒ d k r¨ t d ƒ ‚ q¨ n  |  j	 t
 j |  j t ƒ ƒ } | d  k rû t | ƒ d j d | d | ƒ d } n#| d	 k rit | ƒ d k r(t d ƒ ‚ n  |  j d k rFt d
 ƒ ‚ n  t |  ƒ d d  j d | ƒ } nµ| t
 j k rt | ƒ } t | ƒ d k r±| j d | d | ƒ } q| j d | d d t ƒ j d | d d t ƒ } | t k r| j d | ƒ } qn| t
 j k r¦t | ƒ } t | ƒ d k rP| j d | d | ƒ } q| j d | d d t ƒ j d | d d t ƒ } | t k r| j d | ƒ } qnx| d k r t | ƒ d k rÓt d ƒ ‚ n  | d k j	 | j ƒ j d | d | ƒ } n| d k r›t | ƒ } t | ƒ d k rE| j d | d | ƒ } q| j d | d d t ƒ j d | d d t ƒ } | t k r| j d | ƒ } qnƒt | ƒ d k r| d k rt | ƒ j d | d d t ƒ j d | d d t ƒ } | t k r| j d | ƒ } qn	t | ƒ d k rt| d k rt|  j d k rQt d
 ƒ ‚ n  t |  ƒ d d  j d | ƒ } nª t | ƒ d k rÓ| d k rÓ|  j d k r°t d
 ƒ ‚ n  t |  ƒ d d  j d | ƒ } nK t | ƒ d k rôt d ƒ ‚ n  t | ƒ | j d | d | ƒ d | } | S(   Ni   s&   Improper number of dimensions to norm.t   froi   s   Invalid norm order for vectors.RÁ   R  g      à?t   nucs+   SVD based norm not implemented for ndim > 2i    s    Invalid norm order for matrices.iÿÿÿÿiþÿÿÿg      ð?(   R`   R3   R]   RR   t
   isinstanceR   Ra   RP   RS   t   astypeR   t   promote_typesR%   t   floatt   absRd   RÝ   RM   t   infRQ   RØ   t   Falset   squeezeR   (   R   t   ordRÁ   R  R£   (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   normZ  sz    )#22-28##*(4   t
   __future__R    R   R   R)   t	   functoolsR   t   numbersR   t   numpyR   Rg   t   baseR   R   t   compatibilityR   t   highlevelgraphR   t   coreR	   R
   R   t   creationR   t   randomR   R   R   R   R#   R'  R`   Rf   RË   RÎ   R×   RÜ   R!   RM   Râ   Rä   Rà   R  R  R
  R	  R  R  R  R    R*  (    (    (    s0   lib/python2.7/site-packages/dask/array/linalg.pyt   <module>   sH   				ÿ e\&2	&			W			I		A