ó
Ë½÷Xc           @` så   d  Z  d d l m Z m Z m Z d d l Z d d l m Z m Z m Z d d l	 m
 Z
 m Z d d l m Z m Z d d l m Z d d	 l m Z d
 d d d g Z e e e e d d „ Z e e d „ Z d „  Z d „  Z d S(   s   SVD decomposition functions.i    (   t   divisiont   print_functiont   absolute_importN(   t   zerost   r_t   diagi   (   t   LinAlgErrort   _datacopied(   t   get_lapack_funcst   _compute_lwork(   t   _asarray_validated(   t   string_typest   svdt   svdvalst   diagsvdt   ortht   gesddc         C` st  t  |  d | ƒ} t | j ƒ d k r6 t d ƒ ‚ n  | j \ } } | pW t | |  ƒ } t | t ƒ sx t d ƒ ‚ n  | d k rš t d | f ƒ ‚ n  | | d f }	 t |	 | f ƒ \ }
 } t	 | | j d	 | j d
 d | d | ƒ} |
 | d | d | d | d | ƒ\ } } } } | d	 k r9t
 d ƒ ‚ n  | d	 k  rYt d | ƒ ‚ n  | rl| | | f S| Sd S(   s6
  
    Singular Value Decomposition.

    Factorizes the matrix a into two unitary matrices U and Vh, and
    a 1-D array s of singular values (real, non-negative) such that
    ``a == U*S*Vh``, where S is a suitably shaped matrix of zeros with
    main diagonal s.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to decompose.
    full_matrices : bool, optional
        If True, `U` and `Vh` are of shape ``(M,M)``, ``(N,N)``.
        If False, the shapes are ``(M,K)`` and ``(K,N)``, where
        ``K = min(M,N)``.
    compute_uv : bool, optional
        Whether to compute also `U` and `Vh` in addition to `s`.
        Default is True.
    overwrite_a : bool, optional
        Whether to overwrite `a`; may improve performance.
        Default is False.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
    lapack_driver : {'gesdd', 'gesvd'}, optional
        Whether to use the more efficient divide-and-conquer approach
        (``'gesdd'``) or general rectangular approach (``'gesvd'``)
        to compute the SVD. MATLAB and Octave use the ``'gesvd'`` approach.
        Default is ``'gesdd'``.

        .. versionadded:: 0.18

    Returns
    -------
    U : ndarray
        Unitary matrix having left singular vectors as columns.
        Of shape ``(M,M)`` or ``(M,K)``, depending on `full_matrices`.
    s : ndarray
        The singular values, sorted in non-increasing order.
        Of shape (K,), with ``K = min(M, N)``.
    Vh : ndarray
        Unitary matrix having right singular vectors as rows.
        Of shape ``(N,N)`` or ``(K,N)`` depending on `full_matrices`.

    For ``compute_uv=False``, only `s` is returned.

    Raises
    ------
    LinAlgError
        If SVD computation does not converge.

    See also
    --------
    svdvals : Compute singular values of a matrix.
    diagsvd : Construct the Sigma matrix, given the vector s.

    Examples
    --------
    >>> from scipy import linalg
    >>> a = np.random.randn(9, 6) + 1.j*np.random.randn(9, 6)
    >>> U, s, Vh = linalg.svd(a)
    >>> U.shape, Vh.shape, s.shape
    ((9, 9), (6, 6), (6,))

    >>> U, s, Vh = linalg.svd(a, full_matrices=False)
    >>> U.shape, Vh.shape, s.shape
    ((9, 6), (6, 6), (6,))
    >>> S = linalg.diagsvd(s, 6, 6)
    >>> np.allclose(a, np.dot(U, np.dot(S, Vh)))
    True

    >>> s2 = linalg.svd(a, compute_uv=False)
    >>> np.allclose(s, s2)
    True

    t   check_finitei   s   expected matrixs   lapack_driver must be a stringR   t   gesvds2   lapack_driver must be "gesdd" or "gesvd", not "%s"t   _lworki    i   t
   compute_uvt   full_matricest   lworkt   overwrite_as   SVD did not converges1   illegal value in %d-th argument of internal gesddN(   R   R   (   R
   t   lent   shapet
   ValueErrorR   t
   isinstanceR   t	   TypeErrorR   R	   R   (   t   aR   R   R   R   t   lapack_drivert   a1t   mt   nt   funcst   gesXdt   gesXd_lworkR   t   ut   st   vt   info(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/decomp_svd.pyR      s0    Pc         C` sl   t  |  d | ƒ}  |  j r7 t |  d d d | d t ƒSt |  j ƒ d k r[ t d ƒ ‚ n t j d ƒ Sd S(   s‘  
    Compute singular values of a matrix.

    Parameters
    ----------
    a : (M, N) array_like
        Matrix to decompose.
    overwrite_a : bool, optional
        Whether to overwrite `a`; may improve performance.
        Default is False.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    s : (min(M, N),) ndarray
        The singular values, sorted in decreasing order.

    Raises
    ------
    LinAlgError
        If SVD computation does not converge.

    Notes
    -----
    ``svdvals(a)`` only differs from ``svd(a, compute_uv=False)`` by its
    handling of the edge case of empty ``a``, where it returns an
    empty sequence:

    >>> a = np.empty((0, 2))
    >>> from scipy.linalg import svdvals
    >>> svdvals(a)
    array([], dtype=float64)

    See also
    --------
    svd : Compute the full singular value decomposition of a matrix.
    diagsvd : Construct the Sigma matrix, given the vector s.

    R   R   i    R   i   s   expected matrixN(	   R
   t   sizeR   t   FalseR   R   R   t   numpyt   empty(   R   R   R   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/decomp_svd.pyR      s    +	c         C` s‘   t  |  ƒ } | j j } t |  ƒ } | | k rT t d | t | | | f | ƒ f S| | k r t | t | | | f | ƒ f St d ƒ ‚ d S(   sœ  
    Construct the sigma matrix in SVD from singular values and size M, N.

    Parameters
    ----------
    s : (M,) or (N,) array_like
        Singular values
    M : int
        Size of the matrix whose singular values are `s`.
    N : int
        Size of the matrix whose singular values are `s`.

    Returns
    -------
    S : (M, N) ndarray
        The S-matrix in the singular value decomposition

    s   -1s   Length of s must be M or N.N(   R   t   dtypet   charR   R   R   R   (   R&   t   Mt   Nt   partt   typt   MorN(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/decomp_svd.pyR   ¶   s    $!c   
      C` s—   t  |  d t ƒ\ } } } |  j \ } } t j t ƒ j } t | | ƒ t j | ƒ | } t j	 | | k d t
 ƒ} | d d … d | … f }	 |	 S(   s‚  
    Construct an orthonormal basis for the range of A using SVD

    Parameters
    ----------
    A : (M, N) array_like
        Input array

    Returns
    -------
    Q : (M, K) ndarray
        Orthonormal basis for the range of A.
        K = effective rank of A, as determined by automatic cutoff

    See also
    --------
    svd : Singular value decomposition of a matrix

    R   R-   N(   R   R*   R   R+   t   finfot   floatt   epst   maxt   amaxt   sumt   int(
   t   AR%   R&   t   vhR/   R0   R6   t   tolt   numt   Q(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/decomp_svd.pyR   Ö   s     (   t   __doc__t
   __future__R    R   R   R+   R   R   R   t   miscR   R   t   lapackR   R	   t   decompR
   t   scipy._lib.sixR   t   __all__t   TrueR*   R   R   R   R   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/linalg/decomp_svd.pyt   <module>   s   	p5	 