
|VE              $   @   sP  d  Z  d Z d d d d d g Z d d l Z d d	 l m Z d d l Z d d
 l m	 Z	 m
 Z
 d d l m Z m Z m Z d d l m Z m Z d d l m Z d d l m Z m Z d d l Z d d l m Z d d d d d d d d i Z d d d d d d d d i Z d d d d d d d d d d  d d! d d" d# d$ d d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d d2 d3 d4 d5 d6 i Z e Z e j   Z d7 e d. <e Z  d d d d8 d d d d d d  d d! d d9 d# d$ d d: d& d' d( d) d* d; d, d- d. d< d0 d1 d d2 d3 d= d5 d6 i Z! e! Z" d d d d> d d  d d! d d" d d? d& d' d( d@ d* dA d, dB d. d/ d0 d1 d dC d3 dD dE dF dG dH i Z# e# j   Z$ dI e$ d <dJ e$ dE <dK e$ dG <d d d dL d d  d d! d dM d d? d& d' d( d@ d* dN d, dO d. dP d0 d1 d dC d3 dD dE dQ dG dR i Z% e% j   Z& dS e& dE <dT e& dG <d d d d  d d! d d9 d d: d& d' d( d@ d* dU d, d- d. d< d0 d1 d d4 dE dV dG dW dX dC dY dZ i Z' e' j   Z( d[ e( dE <d\ e( dY <d e! d e" i Z) d e d e d e d e  i Z* d e' d e( i Z+ d e# d e$ d e% d e& i Z, d] d^ d_ d` da g Z- d] d^ db dc dd de g Z. Gdf d   d e/  Z0 Gdg d   d e0  Z1 Gdh di   di e2  Z3 Gdj dk   dk e3  Z4 Gdl dm   dm e3  Z5 dn do   Z6 Gdp dq   dq e
  Z7 Gdr ds   ds e
  Z8 Gdt du   du e
  Z9 Gdv dw   dw e
  Z: dx d dy dz  Z; dx d d{ d|  Z< d& d d d] d d d d d} d d d d~ d  Z= d& d d d] d d d d d} d d d d d  Z> d& d d d d  Z? e j@ j@ e d  k r4d d l mA ZA mB ZB mC ZC n e= e> e? f \ ZA ZB ZC d S)a8  
This contains a copy of the future version of
scipy.sparse.linalg.eigen.arpack.eigsh
It's an upgraded wrapper of the ARPACK library which
allows the use of shift-invert mode for symmetric matrices.


Find a few eigenvectors and eigenvalues of a matrix.


Uses ARPACK: http://www.caam.rice.edu/software/ARPACK/

zrestructuredtext eneigseigshsvdsArpackErrorArpackNoConvergence    N)_arpack)aslinearoperatorLinearOperator)identity
isspmatrixisspmatrix_csr)	lu_factorlu_solve)isdense)gmressplu)LooseVersionfsdFcDz      zNormal exit.   zMaximum number of iterations taken. All possible eigenvalues of OP has been found. IPARAM(5) returns the number of wanted converged Ritz values.   zONo longer an informational error. Deprecated starting with release 2 of ARPACK.   zNo shifts could be applied during a cycle of the Implicitly restarted Arnoldi iteration. One possibility is to increase the size of NCV relative to NEV. zN must be positive.zNEV must be positive.z)NCV-NEV >= 2 and less than or equal to N.   zRThe maximum number of Arnoldi update iterations allowed must be greater than zero.z8 WHICH must be one of 'LM', 'SM', 'LR', 'SR', 'LI', 'SI'   zBMAT must be one of 'I' or 'G'.   z5Length of private work array WORKL is not sufficient.   z0Error return from LAPACK eigenvalue calculation;	   zStarting vector is zero.
   zIPARAM(7) must be 1,2,3,4.   z.IPARAM(7) = 1 and BMAT = 'G' are incompatible.z"IPARAM(1) must be equal to 0 or 1.   z&NEV and WHICH = 'BE' are incompatible.i'  zCould not build an Arnoldi factorization. IPARAM(5) returns the size of the current Arnoldi factorization. The user is advised to check that enough workspace and array storage has been allocated.zIPARAM(7) must be 1,2,3.zRMaximum number of iterations taken. All possible eigenvalues of OP has been found.z9NCV must be greater than NEV and less than or equal to N.z4WHICH must be one of 'LM', 'SM', 'LA', 'SA' or 'BE'.z`Error return from trid. eigenvalue calculation; Informational error from LAPACK routine dsteqr .zIPARAM(7) must be 1,2,3,4,5.z'NEV and WHICH = 'BE' are incompatible. at  The Schur form computed by LAPACK routine dlahqr could not be reordered by LAPACK routine dtrsen. Re-enter subroutine dneupd  with IPARAM(5)NCV and increase the size of the arrays DR and DI to have dimension at least dimension NCV and allocate at least NCV columns for Z. NOTE: Not necessary if Z and V share the same space. Please notify the authors if this error occurs.z7WHICH must be one of 'LM', 'SM', 'LR', 'SR', 'LI', 'SI'z5Length of private work WORKL array is not sufficient.zdError return from calculation of a real Schur form. Informational error from LAPACK routine dlahqr .z^Error return from calculation of eigenvectors. Informational error from LAPACK routine dtrevc.z HOWMNY = 'S' not yet implementedz1HOWMNY must be one of 'A' or 'P' if RVEC = .true.   z<DNAUPD  did not find any eigenvalues to sufficient accuracy.   zDNEUPD got a different count of the number of converged Ritz values than DNAUPD got.  This indicates the user probably made an error in passing data from DNAUPD to DNEUPD or that the data was modified before entering DNEUPDav  The Schur form computed by LAPACK routine slahqr could not be reordered by LAPACK routine strsen . Re-enter subroutine dneupd  with IPARAM(5)=NCV and increase the size of the arrays DR and DI to have dimension at least dimension NCV and allocate at least NCV columns for Z. NOTE: Not necessary if Z and V share the same space. Please notify the authors if this error occurs.z;SNAUPD did not find any eigenvalues to sufficient accuracy.zSNEUPD got a different count of the number of converged Ritz values than SNAUPD got.  This indicates the user probably made an error in passing data from SNAUPD to SNEUPD or that the data was modified before entering SNEUPDak  The Schur form computed by LAPACK routine csheqr could not be reordered by LAPACK routine ztrsen. Re-enter subroutine zneupd with IPARAM(5)=NCV and increase the size of the array D to have dimension at least dimension NCV and allocate at least NCV columns for Z. NOTE: Not necessary if Z and V share the same space. Please notify the authors if this error occurs.z)NCV-NEV >= 1 and less than or equal to N.zLError return from LAPACK eigenvalue calculation. This should never happened.z^Error return from calculation of eigenvectors. Informational error from LAPACK routine ztrevc.zIPARAM(7) must be 1,2,3z;ZNAUPD did not find any eigenvalues to sufficient accuracy.zZNEUPD got a different count of the number of converged Ritz values than ZNAUPD got.  This indicates the user probably made an error in passing data from ZNAUPD to ZNEUPD or that the data was modified before entering ZNEUPDz;CNAUPD did not find any eigenvalues to sufficient accuracy.zCNEUPD got a different count of the number of converged Ritz values than CNAUPD got.  This indicates the user probably made an error in passing data from CNAUPD to CNEUPD or that the data was modified before entering CNEUPDz]Error return from trid. eigenvalue calculation; Information error from LAPACK routine dsteqr.z<DSAUPD  did not find any eigenvalues to sufficient accuracy.z1HOWMNY must be one of 'A' or 'S' if RVEC = .true.      zDSEUPD  got a different count of the number of converged Ritz values than DSAUPD  got.  This indicates the user probably made an error in passing data from DSAUPD  to DSEUPD  or that the data was modified before entering  DSEUPD.z<SSAUPD  did not find any eigenvalues to sufficient accuracy.zSSEUPD  got a different count of the number of converged Ritz values than SSAUPD  got.  This indicates the user probably made an error in passing data from SSAUPD  to SSEUPD  or that the data was modified before entering  SSEUPD.LMSMLASABELRSRLISIc               @   s%   e  Z d  Z d Z e d d  Z d S)r   z
    ARPACK error
    c             C   s0   | j  | d  } t j |  d | | f  d  S)NzUnknown errorzARPACK error %d: %s)getRuntimeError__init__)selfinfoinfodictmsg r;   O/s/python-3.5.2/amd64_ubu14/lib/python3.5/site-packages/sklearn/utils/arpack.pyr6     s    zArpackError.__init__N)__name__
__module____qualname____doc___NAUPD_ERRORSr6   r;   r;   r;   r<   r     s   c               @   s"   e  Z d  Z d Z d d   Z d S)r   z
    ARPACK iteration did not converge

    Attributes
    ----------
    eigenvalues : ndarray
        Partial result. Converged eigenvalues.
    eigenvectors : ndarray
        Partial result. Converged eigenvectors.

    c             C   s/   t  j |  d d | i  | |  _ | |  _ d  S)Nr   rB   )r   r6   eigenvalueseigenvectors)r7   r:   rC   rD   r;   r;   r<   r6   *  s    	zArpackNoConvergence.__init__N)r=   r>   r?   r@   r6   r;   r;   r;   r<   r     s   c            	   @   s=   e  Z d  Z d d d d d d d d d  Z d d   Z d S)	_ArpackParamsr   Nr+   r   c             C   s  | d k r t  d |   | d  k r2 | d } | d k rN t  d |   | d k rf t  d   | d  k	 r t j | d d |  _ d	 } n t j | |  |  _ d } | d  k r d |  _ n	 | |  _ | d  k r d
 | d	 } t | |  } t j | | f |  |  _ t j d d  |  _ d	 } | |  _	 | |  j d <| |  j d
 <d	 |  j d <| |  j d <| |  _
 |
 |  _ | |  _ | |  _ | |  _ |	 |  _ | |  _ | |  _ d |  _ d |  _ d  S)Nr   zk must be positive, k=%dr$   z$maxiter must be positive, maxiter=%dZfdFDz)matrix type must be 'f', 'd', 'F', or 'D'copyTr   r   r%   intr   r    F)
ValueErrornparrayresidzerossigmaminviparammodentolkmaxiterncvwhichtpr8   	convergedido)r7   rR   rT   rX   rQ   rM   rV   v0rU   rW   rS   r8   Zishftsr;   r;   r<   r6   1  sJ    
												z_ArpackParams.__init__c             C   s   d } |  j  d } |  j  d } y |  j d  \ } } Wnb t k
 r } zB d | | f } t j d  } t j |  j d f  } d } WYd  d  } ~ Xn Xt | | | |  j f | |   d  S)Nz<No convergence (%d iterations, %d/%d eigenvectors converged)r   r   Tz%s [%s]r   )r   )rP   extractr   rI   rL   rR   r   rT   )r7   r:   k_okZnum_iterevvecerrr;   r;   r<   _raise_no_convergenceg  s    z#_ArpackParams._raise_no_convergence)r=   r>   r?   r6   ra   r;   r;   r;   r<   rE   0  s   5rE   c               @   sO   e  Z d  Z d d d d d d d d d d d 	 Z d d   Z d	 d
   Z d S)_SymmetricArpackParamsr   Nr+   r   c                sr  | d k rx  d  k r$ t  d     d  k	 r< t  d    d  k	 rT t  d     _ d d    _ d  _ n| d k r d  k r t  d	     d  k r t  d
    d  k r t  d     f d d    _   _   _    _ d  _ n| d k r d  k	 r/t  d    d  k rGt  d     d  k r  _   _ d d    _ d  _ q   f d d    _   _    _ d  _ n]| d k r@ d  k rt  d     d  k	 rt  d    d  k r
t  d     _   f d d    _   _ d  _ n | d k r d  k rdt  d    d  k r|t  d     _   _   d  k r   f d d    _ d d    _ d  _ q     f d d    _    _ d  _ n t  d |   | t k r8t  d d  j t    | | k rTt  d! |   t	 j
  | | | |  |	 |
 | | |   j | k s j | k rt  d"  j   t j d |  j   _ t j  j  j d#  j   _ t  j } | d+ k rt  d&   t j | d'  _ t j | d(  _ t |  _ t |  _ t j d) d*   _ d  S),Nr   z#matvec must be specified for mode=1z'M_matvec cannot be specified for mode=1z*Minv_matvec cannot be specified for mode=1c             S   s   |  S)Nr;   )xr;   r;   r<   <lambda>  s    z1_SymmetricArpackParams.__init__.<locals>.<lambda>Ir   z#matvec must be specified for mode=2z%M_matvec must be specified for mode=2z(Minv_matvec must be specified for mode=2c                s      |    S)Nr;   )rc   )Minv_matvecmatvecr;   r<   rd     s    Gr   z'matvec must not be specified for mode=3z(Minv_matvec must be specified for mode=3c             S   s   |  S)Nr;   )rc   r;   r;   r<   rd     s    c                s      |    S)Nr;   )rc   )M_matvecrf   r;   r<   rd     s    r   z#matvec must be specified for mode=4z)M_matvec must not be specified for mode=4z(Minv_matvec must be specified for mode=4c                s    j    |    S)N)OPa)rc   )rg   r7   r;   r<   rd     s    r   z#matvec must be specified for mode=5z(Minv_matvec must be specified for mode=5c                s      |    |   S)Nr;   )rc   )rf   rg   rM   r;   r<   rd     s    c             S   s   |  S)Nr;   )rc   r;   r;   r<   rd     s    c                s     |      |    S)Nr;   )rc   )ri   rf   rg   rM   r;   r<   rd     s   zmode=%i not implementedzwhich must be one of %s z!k must be less than rank(A), k=%dzncv must be k<ncv<=n, ncv=%sr"   r   r   z Input matrix is not real-valued.ZsaupdZseupdr%   rG   )r   r   )rH   OPBbmatrj   OPbA_matvec_SEUPD_WHICHjoinrE   r6   rV   rI   rL   rX   workdworkl
_type_convr   __dict___arpack_solver_arpack_extract_SAUPD_ERRORSiterate_infodict_SEUPD_ERRORSextract_infodictipntr)r7   rR   rT   rX   rg   rQ   ri   rf   rM   rV   r[   rU   rW   rS   ltrr;   )ri   rf   rg   r7   rM   r<   r6   v  s    6													&z_SymmetricArpackParams.__init__c             C   s  |  j  |  j |  j |  j |  j |  j |  j |  j |  j |  j	 |  j
 |  j |  j  \ |  _ |  _ |  _ |  _ |  _	 |  _ t |  j	 d d |  j	 d d |  j  } t |  j	 d d |  j	 d d |  j  } |  j d	 k r |  j |  j
 |  |  j
 | <n|  j d k rQ|  j d k r=|  j |  j
 |  |  j
 | <q|  j d k r|  j |  j
 |  |  j
 | <|  j |  j
 |  |  j
 | <q|  j d k rt |  j	 d d |  j	 d d |  j  } |  j |  j
 |  } |  j | |  j |  j
 |  |  j
 | <qt |  j	 d d |  j	 d d |  j  } |  j |  j
 |  |  j
 | <n |  j d k r|  j |  j
 |  |  j
 | <nm |  j d k rt d   nO d |  _ |  j d k rn4 |  j d k r|  j   n t |  j d |  j  d  S)
Nr   r   r   r   r   z/ARPACK requested user shifts.  Assure ISHIFT==0Tr9   rB   )rw   rZ   rn   rW   rT   rS   rK   rO   rP   r}   rs   rt   r8   slicerR   rl   rQ   ro   rj   rp   rM   rm   rH   rY   ra   r   rz   )r7   xsliceysliceBxsliceZAxr;   r;   r<   iterate  s<    B,,   ,,  	z_SymmetricArpackParams.iteratec       	      C   s  | } d } d } t  j |  j d  } |  j | | | |  j |  j |  j |  j |  j |  j	 |  j
 |  j d d  |  j |  j d d |  j  |  j |  \ } } } | d k r t | d |  j  |  j d } | d  |  } | d  d   d  |  f } | r| | f S| Sd  S)Nr   ArG   r!   r   r9   r   )rI   rL   rV   rx   rM   rn   rW   rT   rS   rK   rO   rP   r}   rs   rR   rt   r   r|   )	r7   return_eigenvectorsZrvecierrhowmnysselectr   r   r]   r;   r;   r<   r\   7  s$    
z_SymmetricArpackParams.extract)r=   r>   r?   r6   r   r\   r;   r;   r;   r<   rb   u  s
   (rb   c               @   sO   e  Z d  Z d d d d d d d d d d d 	 Z d d   Z d	 d
   Z d S)_UnsymmetricArpackParamsr   Nr+   r   c                s  | d k rx  d  k r$ t  d     d  k	 r< t  d    d  k	 rT t  d     _ d d    _ d  _ n| d k r d  k r t  d	     d  k r t  d
    d  k r t  d     f d d    _   _   _    _ d  _ n!| d" k r d  k r/t  d    d  k rGt  d     _ | d k r| d k rt  _ qt  d   n9 | d k r f d d    _ n  f d d    _   d  k rd d    _ d  _  j  _ q,   _ d  _    f d d    _ n t  d |   | t k rQt  d d j t    | | d k rqt  d |   t	 j
  | | | | | |	 |
 | | |   j | k s j | d k rt  d  j   t j d |  j   _ t j d  j  j d  j   _ t  j } t j | d  _ t j | d  _ t |  _ t |  _ t j d d    _  j d! k rt j  j  j j     _ n	 d   _ d  S)#Nr   z#matvec must be specified for mode=1z'M_matvec cannot be specified for mode=1z*Minv_matvec cannot be specified for mode=1c             S   s   |  S)Nr;   )rc   r;   r;   r<   rd   }  s    z3_UnsymmetricArpackParams.__init__.<locals>.<lambda>re   r   z#matvec must be specified for mode=2z%M_matvec must be specified for mode=2z(Minv_matvec must be specified for mode=2c                s      |    S)Nr;   )rc   )rf   rg   r;   r<   rd     s    rh   r   r   z*matvec must be specified for mode in (3,4)z/Minv_matvec must be specified for mode in (3,4)ZDFzmode=4 invalid for complex Ac                s   t  j   |    S)N)rI   real)rc   )rf   r;   r<   rd     s    c                s   t  j   |    S)N)rI   imag)rc   )rf   r;   r<   rd     s    c             S   s   |  S)Nr;   )rc   r;   r;   r<   rd     s    c                s    j    |    S)N)rj   )rc   )ri   r7   r;   r<   rd     s    zmode=%i not implementedz!Parameter which must be one of %srk   z#k must be less than rank(A)-1, k=%dzncv must be k+1<ncv<=n, ncv=%sZnaupdZneupdr'   rG   ZFD)r   r   )rH   rl   rm   rn   rj   ro   rg   _NEUPD_WHICHrr   rE   r6   rV   rI   rL   rX   rs   rt   ru   r   rv   rw   rx   rA   rz   _NEUPD_ERRORSr|   r}   lowerrwork)r7   rR   rT   rX   rg   rQ   ri   rf   rM   rV   r[   rU   rW   rS   r~   r;   )ri   rf   rg   r7   r<   r6   O  s~    %								"*$z!_UnsymmetricArpackParams.__init__c             C   s  |  j  d k r |  j |  j |  j |  j |  j |  j |  j |  j |  j	 |  j
 |  j |  j |  j  \ |  _ |  _ |  _ |  _	 |  _
 |  _ n~ |  j |  j |  j |  j |  j |  j |  j |  j |  j	 |  j
 |  j |  j |  j |  j  \ |  _ |  _ |  _ |  _	 |  _
 |  _ t |  j
 d d |  j
 d d |  j  } t |  j
 d d |  j
 d d |  j  } |  j d	 k r|  j |  j |  |  j | <n&|  j d k r|  j d
 k r|  j |  j |  |  j | <qt |  j
 d d |  j
 d d |  j  } |  j |  j |  |  j | <n |  j d k rH|  j |  j |  |  j | <nm |  j d k rft d   nO d |  _ |  j d k rn4 |  j d k r|  j   n t |  j d |  j  d  S)Nfdr   r   r   r   z/ARPACK requested user shifts.  Assure ISHIFT==0Tr9   rB   )r   r   )rX   rw   rZ   rn   rW   rT   rS   rK   rO   rP   r}   rs   rt   r8   r   r   rR   rl   rQ   rj   rm   rH   rY   ra   r   rz   )r7   r   r   r   r;   r;   r<   r     s8    36,,  ,  	z _UnsymmetricArpackParams.iteratec             C   s  |  j  |  j } } d } d } t j |  j d  } t j |  j  } t j |  j  } t j d |  j |  j  }	 |  j d k rt j | d |  j  }
 t j | d |  j  } t j | | d f |  j  } |  j	 | | | | | |	 |  j
 |  j | |  j |  j |  j |  j |  j |  j |  j |  j  \ }
 } } } | d k r^t | d |  j  |  j d } |
 d	 | } | j |  j j    } | d k rqd } x| | k rmt | | j  d k r`| | k  rV| d  d   | f d	 | d  d   | d f | d  d   | f <| d  d   | f j   | d  d   | d f <| d 7} n
 | d 8} | d 7} qWn)d } x | | k rt | | j  d k rt j | d  d   | f |  j | d  d   | f   | | <n| | k  r| d  d   | f d	 | d  d   | d f | d  d   | f <| d  d   | f j   | d  d   | d f <t j | d  d   | f |  j | d  d   | f   t j | d  d   | d f |  j | d  d   | d f   d	 t j | d  d   | f |  j | d  d   | d f   t j | d  d   | d f |  j | d  d   | f   | | <| | j   | | d <| d 7} n
 | d 8} | d 7} qzW| | k r| d  |  } | d  d   d  |  f } qt j | d
 t |  j } |  j d k rt j | j  } n? |  j d k r?t j t | j   } n t j t |   } |  j d k r| | | d   } | d  d   | | d   f } |  j d k r| | d  |  } | d  d   | d  |  f } n |  j	 | | | |  j |	 |  j
 |  j | |  j |  j |  j |  j |  j |  j |  j |  j  |  \ } } } | d k rit | d |  j  |  j d } | d  |  } | d  d   d  |  f } | r| | f S| Sd  S)Nr   r   rG   r   r   r   r9   r   y              ?decimalsr0   r1   r2   r3   r+   r,   )r0   r1   )r2   r3   )r0   zLMr2   )r1   r,   r3   )!rT   rR   rI   rL   rV   r   rM   r   rX   rx   rn   rW   rS   rK   rO   rP   r}   rs   rt   r8   r   r|   astypeupperabs	conjugatedotrg   conjround_ndigitsargsortr   )r7   r   rT   rR   r   r   r   ZsigmarZsigmaiZworkevZdrZdizrZ	nreturnedr   r   iZrdindr]   r;   r;   r<   r\     s    B0
BB0$#)
!#!
z _UnsymmetricArpackParams.extract)r=   r>   r?   r6   r   r\   r;   r;   r;   r<   r   N  s
   v(r   c             C   sE   t  |   }  t |  d  sA t j |  j d  } |  | j |  _ |  S)Ndtyper   )r   hasattrrI   rL   shaper   )mrc   r;   r;   r<   _aslinearoperator_with_dtypet  s
    r   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)SpLuInvzl
    SpLuInv:
       helper class to repeatedly solve M*x=b
       using a sparse LU-decopposition of M
    c             C   sQ   t  |  |  _ t j |  | j |  j d | j t j |  j t j	  |  _
 d  S)Nr   )r   M_lur	   r6   r   _matvecr   rI   
issubdtypecomplexfloatingisreal)r7   Mr;   r;   r<   r6     s    "zSpLuInv.__init__c             C   sk   |  j  rW t j | j t j  rW |  j j t j |   d |  j j t j |   S|  j j |  Sd  S)Ny              ?)	r   rI   r   r   r   r   solver   r   )r7   rc   r;   r;   r<   r     s    !zSpLuInv._matvecN)r=   r>   r?   r@   r6   r   r;   r;   r;   r<   r   |  s   r   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)LuInvzd
    LuInv:
       helper class to repeatedly solve M*x=b
       using an LU-decomposition of M
    c             C   s5   t  |  |  _ t j |  | j |  j d | j d  S)Nr   )r   r   r	   r6   r   r   r   )r7   r   r;   r;   r<   r6     s    zLuInv.__init__c             C   s   t  |  j |  S)N)r   r   )r7   rc   r;   r;   r<   r     s    zLuInv._matvecN)r=   r>   r?   r@   r6   r   r;   r;   r;   r<   r     s   r   c               @   s4   e  Z d  Z d Z e d d d  Z d d   Z d S)IterInvzb
    IterInv:
       helper class to repeatedly solve M*x=b
       using an iterative method.
    r   c             C   s   | d k r! t  j | j  j } | |  _ | |  _ | |  _ t | d  rW | j } n# t  j | j	 d  } | | j } t
 j |  | j	 |  j d | d  S)Nr   r   r   )rI   finfor   epsr   ifuncrS   r   rL   r   r	   r6   r   )r7   r   r   rS   r   rc   r;   r;   r<   r6     s    			zIterInv.__init__c             C   sP   |  j  |  j | d |  j \ } } | d k rL t d |  j  j | f   | S)NrS   r   z?Error in inverting M: function %s did not converge (info = %i).)r   r   rS   rH   r=   )r7   rc   br8   r;   r;   r<   r     s
    $zIterInv._matvecN)r=   r>   r?   r@   r   r6   r   r;   r;   r;   r<   r     s   r   c               @   sL   e  Z d  Z d Z e d d d  Z d d   Z d d   Z d	 d
   Z d S)	IterOpInvzo
    IterOpInv:
       helper class to repeatedly solve [A-sigma*M]*x = b
       using an iterative method
    r   c             C   s   | d k r! t  j | j  j } | |  _ | |  _ | |  _ | |  _ | |  _ t  j	 | j
 d  } | d  k r |  j |  j } t |  j j
 |  j d | |  _ n3 |  j |  j } t |  j j
 |  j d | |  _ t j |  | j
 |  j d | d  S)Nr   r   r   )rI   r   r   r   r   r   rM   r   rS   rL   r   mult_func_M_Noner	   rl   	mult_funcr6   r   )r7   r   r   rM   r   rS   rc   r   r;   r;   r<   r6     s$    							zIterOpInv.__init__c             C   s'   |  j  j |  |  j |  j j |  S)N)r   rg   rM   r   )r7   rc   r;   r;   r<   r     s    zIterOpInv.mult_funcc             C   s   |  j  j |  |  j | S)N)r   rg   rM   )r7   rc   r;   r;   r<   r     s    zIterOpInv.mult_func_M_Nonec             C   sP   |  j  |  j | d |  j \ } } | d k rL t d |  j  j | f   | S)NrS   r   zIError in inverting [A-sigma*M]: function %s did not converge (info = %i).)r   rl   rS   rH   r=   )r7   rc   r   r8   r;   r;   r<   r     s
    $zIterOpInv._matvecN)	r=   r>   r?   r@   r   r6   r   r   r   r;   r;   r;   r<   r     s
   r   Fc             C   sd   t  |   r t |   j St |   rM t |   r@ | r@ |  j }  t |   j St |  d | j Sd  S)NrS   )r   r   rg   r   r   Tr   r   )r   	symmetricrS   r;   r;   r<   get_inv_matvec  s    	r   c             C   s  | d k r" t  |  d | d | S| d  k r,t |   r t j |  j t j  sg t j |  d k ry t j |   }  n
 |  d }  |  j d  d  |  j	 d d  | 8<t
 |   j St |   r
|  | t |  j	 d  }  | r t |   r |  j }  t |  j    j St t |   | | d | j Sn t |   rFt |   s`t |  rt |  rt t |   t |  | d | j St |   st |  rt
 |  | |  j S|  | | } | rt |  r| j } t | j    j Sd  S)Nr   r   rS   y                r   )r   r   rI   r   r   r   r   rF   flatr   r   rg   r   r
   r   r   r   Ztocscr   r   )r   r   rM   r   rS   rl   r;   r;   r<   get_OPinv_matvec  s8    
%	
	r   Tc             C   sH  |  j  d |  j  d k r0 t d |  j  f   | d k	 r | j  |  j  k rj t d | j  |  j  f   t j | j  j j   t j |  j  j j   k r t j d  |  j  d } | d k s | | k r t d   | d k rt |   j	 } | d k	 rt d   | d k	 r)t d	   | d k rbd } d } d } |
 d k	 rt d
   qd } |
 d k rt
 | d d d | } n t |
  }
 |
 j	 } t |  j	 } n5t j |  j t j  r| d k	 rt d   d } no | d k s| j   d k rd } nH | j   d k rRt j |  d k rIt d   d } n t d   t |   j	 } |
 d k	 rt d   | d k rt |  | | d d d | } n t |  } | j	 } | d k rd } n t |  j	 } t | | |  j j | | | | | | | | | |  } x | j s:| j   q$W| j |	  S)a(  
    Find k eigenvalues and eigenvectors of the square matrix A.

    Solves ``A * x[i] = w[i] * x[i]``, the standard eigenvalue problem
    for w[i] eigenvalues with corresponding eigenvectors x[i].

    If M is specified, solves ``A * x[i] = w[i] * M * x[i]``, the
    generalized eigenvalue problem for w[i] eigenvalues
    with corresponding eigenvectors x[i]

    Parameters
    ----------
    A : An N x N matrix, array, sparse matrix, or LinearOperator representing     the operation A * x, where A is a real or complex square matrix.

    k : int, default 6
        The number of eigenvalues and eigenvectors desired.
        `k` must be smaller than N. It is not possible to compute all
        eigenvectors of a matrix.

    return_eigenvectors : boolean, default True
        Whether to return the eigenvectors along with the eigenvalues.

    M : An N x N matrix, array, sparse matrix, or LinearOperator representing
        the operation M*x for the generalized eigenvalue problem
          ``A * x = w * M * x``
        M must represent a real symmetric matrix.  For best results, M should
        be of the same type as A.  Additionally:
         * If sigma==None, M is positive definite
         * If sigma is specified, M is positive semi-definite
        If sigma==None, eigs requires an operator to compute the solution
        of the linear equation `M * x = b`. This is done internally via a
        (sparse) LU decomposition for an explicit matrix M, or via an
        iterative solver for a general linear operator.  Alternatively,
        the user can supply the matrix or operator Minv, which gives
        x = Minv * b = M^-1 * b

    sigma : real or complex
        Find eigenvalues near sigma using shift-invert mode.  This requires
        an operator to compute the solution of the linear system
        `[A - sigma * M] * x = b`, where M is the identity matrix if
        unspecified. This is computed internally via a (sparse) LU
        decomposition for explicit matrices A & M, or via an iterative
        solver if either A or M is a general linear operator.
        Alternatively, the user can supply the matrix or operator OPinv,
        which gives x = OPinv * b = [A - sigma * M]^-1 * b.
        For a real matrix A, shift-invert can either be done in imaginary
        mode or real mode, specified by the parameter OPpart ('r' or 'i').
        Note that when sigma is specified, the keyword 'which' (below)
        refers to the shifted eigenvalues w'[i] where:
         * If A is real and OPpart == 'r' (default),
            w'[i] = 1/2 * [ 1/(w[i]-sigma) + 1/(w[i]-conj(sigma)) ]
         * If A is real and OPpart == 'i',
            w'[i] = 1/2i * [ 1/(w[i]-sigma) - 1/(w[i]-conj(sigma)) ]
         * If A is complex,
            w'[i] = 1/(w[i]-sigma)

    v0 : array
        Starting vector for iteration.

    ncv : integer
        The number of Lanczos vectors generated
        `ncv` must be greater than `k`; it is recommended that ``ncv > 2*k``.

    which : string ['LM' | 'SM' | 'LR' | 'SR' | 'LI' | 'SI']
        Which `k` eigenvectors and eigenvalues to find:
         - 'LM' : largest magnitude
         - 'SM' : smallest magnitude
         - 'LR' : largest real part
         - 'SR' : smallest real part
         - 'LI' : largest imaginary part
         - 'SI' : smallest imaginary part
        When sigma != None, 'which' refers to the shifted eigenvalues w'[i]
        (see discussion in 'sigma', above).  ARPACK is generally better
        at finding large values than small values.  If small eigenvalues are
        desired, consider using shift-invert mode for better performance.

    maxiter : integer
        Maximum number of Arnoldi update iterations allowed

    tol : float
        Relative accuracy for eigenvalues (stopping criterion)
        The default value of 0 implies machine precision.

    return_eigenvectors : boolean
        Return eigenvectors (True) in addition to eigenvalues

    Minv : N x N matrix, array, sparse matrix, or linear operator
        See notes in M, above.
        
    OPinv : N x N matrix, array, sparse matrix, or linear operator
        See notes in sigma, above.
    OPpart : 'r' or 'i'.
        See notes in sigma, above

    Returns
    -------
    w : array
        Array of k eigenvalues.

    v : array
        An array of `k` eigenvectors.
        ``v[:, i]`` is the eigenvector corresponding to the eigenvalue w[i].

    Raises
    ------
    ArpackNoConvergence
        When the requested convergence is not obtained.

        The currently converged eigenvalues and eigenvectors can be found
        as ``eigenvalues`` and ``eigenvectors`` attributes of the exception
        object.

    See Also
    --------
    eigsh : eigenvalues and eigenvectors for symmetric matrix A
    svds : singular value decomposition for a matrix A

    Examples
    --------
    Find 6 eigenvectors of the identity matrix:

    >>> from sklearn.utils.arpack import eigs
    >>> id = np.identity(13)
    >>> vals, vecs = eigs(id, k=6)
    >>> vals
    array([ 1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j])
    >>> vecs.shape
    (13, 6)

    Notes
    -----
    This function is a wrapper to the ARPACK [1]_ SNEUPD, DNEUPD, CNEUPD,
    ZNEUPD, functions which use the Implicitly Restarted Arnoldi Method to
    find the eigenvalues and eigenvectors [2]_.

    References
    ----------
    .. [1] ARPACK Software, http://www.caam.rice.edu/software/ARPACK/
    .. [2] R. B. Lehoucq, D. C. Sorensen, and C. Yang,  ARPACK USERS GUIDE:
       Solution of Large Scale Eigenvalue Problems by Implicitly Restarted
       Arnoldi Methods. SIAM, Philadelphia, PA, 1998.
    r   r   z!expected square matrix (shape=%s)Nz#wrong M dimensions %s, should be %szZM does not have the same type precision as A. This may adversely affect ARPACK convergencez!k must be between 1 and rank(A)-1z0OPinv should not be specified with sigma = None.z=OPpart should not be specified with sigma = None or complex Az+Minv should not be specified with M = None.r   r   TrS   z;OPpart should not be specified with sigma=None or complex Ar   rr   z%OPpart cannot be 'i' if sigma is realr   zOPpart must be one of ('r','i')z*Minv should not be specified when sigma isF)r   rH   rI   r   charr   warningswarnr   rg   r   r   r   r   r   r   rY   r   r\   )r   rT   r   rM   rW   r[   rV   rU   rS   r   MinvOPinvZOPpartrR   rg   rQ   ri   rf   paramsr;   r;   r<   _eigs  sr    6							r   normalc             C   s}  t  j |  j t  j  r | d k r4 t d |   | d k rO t d   n' | d k rd d } n | d k rv d } t |  | d	 | d
 | d | d | d | d | d | d |	 d |
 d | 
} |	 r | d j | d f S| j S|  j d |  j d k rt d |  j f   | d k	 r| j |  j k rMt d | j |  j f   t  j | j  j j	   t  j |  j  j j	   k rt
 j d  |  j d } | d k s| | k rt d   | d k rt |   }  |  j } | d k	 rt d   | d k r3d } d } d } |
 d k	 rt d   q d } |
 d k r`t | d d d | } n t |
  }
 |
 j } t |  j } n|
 d k	 rt d   | d k r&d  } d } | d k rt |  | | d d d | } n t |  } | j } | d k rd } q t |  } | j } n | d! k rd" } | d k ret |  | | d d d | } n t |  j } t |   j } d } n | d# k rd$ } t |   j } | d k rt |  | | d d d | } n t |  j } | d k rd } q t |  j } n t d% |   t | | |  j j | | | | | | | | | |  } x | j so| j   qYW| j |	  S)&a  
    Find k eigenvalues and eigenvectors of the real symmetric square matrix
    or complex hermitian matrix A.

    Solves ``A * x[i] = w[i] * x[i]``, the standard eigenvalue problem for
    w[i] eigenvalues with corresponding eigenvectors x[i].

    If M is specified, solves ``A * x[i] = w[i] * M * x[i]``, the
    generalized eigenvalue problem for w[i] eigenvalues
    with corresponding eigenvectors x[i]


    Parameters
    ----------
    A : An N x N matrix, array, sparse matrix, or LinearOperator representing
        the operation A * x, where A is a real symmetric matrix
        For buckling mode (see below) A must additionally be positive-definite
    k : integer
        The number of eigenvalues and eigenvectors desired.
        `k` must be smaller than N. It is not possible to compute all
        eigenvectors of a matrix.

    M : An N x N matrix, array, sparse matrix, or linear operator representing
        the operation M * x for the generalized eigenvalue problem
          ``A * x = w * M * x``.
        M must represent a real, symmetric matrix.  For best results, M should
        be of the same type as A.  Additionally:
         * If sigma == None, M is symmetric positive definite
         * If sigma is specified, M is symmetric positive semi-definite
         * In buckling mode, M is symmetric indefinite.
        If sigma == None, eigsh requires an operator to compute the solution
        of the linear equation `M * x = b`. This is done internally via a
        (sparse) LU decomposition for an explicit matrix M, or via an
        iterative solver for a general linear operator.  Alternatively,
        the user can supply the matrix or operator Minv, which gives
        x = Minv * b = M^-1 * b
    sigma : real
        Find eigenvalues near sigma using shift-invert mode.  This requires
        an operator to compute the solution of the linear system
        `[A - sigma * M] x = b`, where M is the identity matrix if
        unspecified.  This is computed internally via a (sparse) LU
        decomposition for explicit matrices A & M, or via an iterative
        solver if either A or M is a general linear operator.
        Alternatively, the user can supply the matrix or operator OPinv,
        which gives x = OPinv * b = [A - sigma * M]^-1 * b.
        Note that when sigma is specified, the keyword 'which' refers to
        the shifted eigenvalues w'[i] where:
         - if mode == 'normal',
             w'[i] = 1 / (w[i] - sigma)
         - if mode == 'cayley',
             w'[i] = (w[i] + sigma) / (w[i] - sigma)
         - if mode == 'buckling',
             w'[i] = w[i] / (w[i] - sigma)
        (see further discussion in 'mode' below)
    v0 : array
        Starting vector for iteration.
    ncv : integer
        The number of Lanczos vectors generated
        ncv must be greater than k and smaller than n;
        it is recommended that ncv > 2*k
    which : string ['LM' | 'SM' | 'LA' | 'SA' | 'BE']
        If A is a complex hermitian matrix, 'BE' is invalid.
        Which `k` eigenvectors and eigenvalues to find
         - 'LM' : Largest (in magnitude) eigenvalues
         - 'SM' : Smallest (in magnitude) eigenvalues
         - 'LA' : Largest (algebraic) eigenvalues
         - 'SA' : Smallest (algebraic) eigenvalues
         - 'BE' : Half (k/2) from each end of the spectrum
                  When k is odd, return one more (k/2+1) from the high end
        When sigma != None, 'which' refers to the shifted eigenvalues w'[i]
        (see discussion in 'sigma', above).  ARPACK is generally better
        at finding large values than small values.  If small eigenvalues are
        desired, consider using shift-invert mode for better performance.
    maxiter : integer
        Maximum number of Arnoldi update iterations allowed
    tol : float
        Relative accuracy for eigenvalues (stopping criterion).
        The default value of 0 implies machine precision.
    Minv : N x N matrix, array, sparse matrix, or LinearOperator
        See notes in M, above
    OPinv : N x N matrix, array, sparse matrix, or LinearOperator
        See notes in sigma, above.
    return_eigenvectors : boolean
        Return eigenvectors (True) in addition to eigenvalues
    mode : string ['normal' | 'buckling' | 'cayley']
        Specify strategy to use for shift-invert mode.  This argument applies
        only for real-valued A and sigma != None.  For shift-invert mode,
        ARPACK internally solves the eigenvalue problem
        ``OP * x'[i] = w'[i] * B * x'[i]``
        and transforms the resulting Ritz vectors x'[i] and Ritz values w'[i]
        into the desired eigenvectors and eigenvalues of the problem
        ``A * x[i] = w[i] * M * x[i]``.
        The modes are as follows:
          - 'normal'   : OP = [A - sigma * M]^-1 * M
                         B = M
                         w'[i] = 1 / (w[i] - sigma)
          - 'buckling' : OP = [A - sigma * M]^-1 * A
                         B = A
                         w'[i] = w[i] / (w[i] - sigma)
          - 'cayley'   : OP = [A - sigma * M]^-1 * [A + sigma * M]
                         B = M
                         w'[i] = (w[i] + sigma) / (w[i] - sigma)
        The choice of mode will affect which eigenvalues are selected by
        the keyword 'which', and can also impact the stability of
        convergence (see [2] for a discussion)

    Returns
    -------
    w : array
        Array of k eigenvalues
    v : array
        An array of k eigenvectors
        The v[i] is the eigenvector corresponding to the eigenvector w[i]

    Raises
    ------
    ArpackNoConvergence
        When the requested convergence is not obtained.

        The currently converged eigenvalues and eigenvectors can be found
        as ``eigenvalues`` and ``eigenvectors`` attributes of the exception
        object.

    See Also
    --------
    eigs : eigenvalues and eigenvectors for a general (nonsymmetric) matrix A
    svds : singular value decomposition for a matrix A

    Notes
    -----
    This function is a wrapper to the ARPACK [1]_ SSEUPD and DSEUPD
    functions which use the Implicitly Restarted Lanczos Method to
    find the eigenvalues and eigenvectors [2]_.

    Examples
    --------
    >>> from sklearn.utils.arpack import eigsh
    >>> id = np.identity(13)
    >>> vals, vecs = eigsh(id, k=6)
    >>> vals # doctest: +SKIP
    array([ 1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j,  1.+0.j])
    >>> print(vecs.shape)
    (13, 6)

    References
    ----------
    .. [1] ARPACK Software, http://www.caam.rice.edu/software/ARPACK/
    .. [2] R. B. Lehoucq, D. C. Sorensen, and C. Yang,  ARPACK USERS GUIDE:
       Solution of Large Scale Eigenvalue Problems by Implicitly Restarted
       Arnoldi Methods. SIAM, Philadelphia, PA, 1998.
    r   z,mode=%s cannot be used with complex matrix Ar/   z/which='BE' cannot be used with complex matrix Ar-   r0   r.   r1   r   rM   rW   r[   rV   rU   rS   r   r   r   r   r   z!expected square matrix (shape=%s)Nz#wrong M dimensions %s, should be %szZM does not have the same type precision as A. This may adversely affect ARPACK convergencez!k must be between 1 and rank(A)-1z0OPinv should not be specified with sigma = None.z+Minv should not be specified with M = None.r   r   Tz*Minv should not be specified when sigma isr   Zbucklingr   Zcayleyr   zunrecognized mode '%s')rI   r   r   r   rH   r   r   r   r   r   r   r   r   rg   r   r   rb   rY   r   r\   )r   rT   r   rM   rW   r[   rV   rU   rS   r   r   r   rQ   retrR   rg   ri   rf   r   r;   r;   r<   _eigsh  s    
	$	6							r   c                s'  t  |  t j  p t |   s- t j |   }  |  j \ } } t j |  j t j  ri d d   } t	 } n d d   } t
 } | | k r |    | |    n |   | |     t  d  r    f d d   } n    f d d   } t d | d	   j d
   j d   j d f  }	 t j   7 t j d t  | |	 d | d | d \ }
 } Wd QRXt j |
  } | | k r| } t   d  r  j |  | } n t j   |  | } | |  } nM | } t   d  r|   j |  |  } n | t j   |  |  } | | | f S)a  Compute k singular values/vectors for a sparse matrix using ARPACK.

    Parameters
    ----------
    A : sparse matrix
        Array to compute the SVD on
    k : int, optional
        Number of singular values and vectors to compute.
    ncv : integer
        The number of Lanczos vectors generated
        ncv must be greater than k+1 and smaller than n;
        it is recommended that ncv > 2*k
    tol : float, optional
        Tolerance for singular values. Zero (default) means machine precision.

    Notes
    -----
    This is a naive implementation using an eigensolver on A.H * A or
    A * A.H, depending on which one is more efficient.

    c             S   s   |  j  j   S)N)r   r   )rc   r;   r;   r<   rd   *  s    z_svds.<locals>.<lambda>c             S   s   |  j  S)N)r   )rc   r;   r;   r<   rd   -  s    r   c                s    j    j  |    S)N)r   )rc   )XXHr;   r<   matvec_XH_X8  s    z_svds.<locals>.matvec_XH_Xc                s   t  j  t  j   |    S)N)rI   r   )rc   )r   r   r;   r<   r   ;  s    rg   r   r   r   ignorerT   rS   r   N)
isinstancerI   ndarrayr   asarrayr   r   r   r   r   r   r   r	   r   catch_warningssimplefilterDeprecationWarningsqrtr   )r   rT   rV   rS   rR   r   hermZeigensolverr   ZXH_XeigvalsZeigvecr   rO   uZvhr;   )r   r   r<   _svds  sB    	)r   z0.10)r   r   r   )Dr@   __docformat____all__r   Z scipy.sparse.linalg.eigen.arpackr   numpyrI   Zscipy.sparse.linalg.interfacer   r	   Zscipy.sparser
   r   r   Zscipy.linalgr   r   Zscipy.sparse.sputilsr   Zscipy.sparse.linalgr   r   scipydistutils.versionr   ru   r   ZDNAUPD_ERRORSZSNAUPD_ERRORSrF   ZZNAUPD_ERRORSZCNAUPD_ERRORSZDSAUPD_ERRORSZSSAUPD_ERRORSZDNEUPD_ERRORSZSNEUPD_ERRORSZZNEUPD_ERRORSZCNEUPD_ERRORSZDSEUPD_ERRORSZSSEUPD_ERRORSry   rA   r{   r   rq   r   r5   r   r   objectrE   rb   r   r   r   r   r   r   r   r   r   r   r   versionr   r   r   r;   r;   r;   r<   <module>   s.   
	E '-% K