a
    CCCf#  ã                   @   sT   d dl mZ d dlZd dlmZ d dlmZmZ ddl	m
Z
mZ dgZdd
d„ZdS )é    )ÚIterableN)Ú_asarray_validated)Ú
block_diagÚLinAlgErroré   )Ú_compute_lworkÚget_lapack_funcsÚcossinFTc           -      C   s&  |s
|r|du rdnt |ƒ}|du r*dnt |ƒ}t| dd} tj| jŽ sZtd| j› ƒ‚| jd }||kst|dkrtd|› d| jd › d	ƒ‚||ks |dkr¼td
|› d| jd › d	ƒ‚| d|…d|…f | d|…|d…f | |d…d|…f | |d…|d…f f\}}	}
}n(t| tƒs,tdƒ‚nt| ƒdkrLtdt| ƒ› ƒ‚dd„ | D ƒ\}}	}
}t	g d¢||	|
|gƒD ](\}}|jd dkrxt|› dƒ‚qx|j\}}|j\}}|	j||fkràtd||f› d|	j› ƒ‚|
j||fkr
td||f› d|
j› ƒ‚|| || kr4td 
|| || ¡ƒ‚|| }tdd„ ||	|
|fD ƒƒ}|r`dnd}t||d g||	|
|gƒ\}}t||||d}|rª|d |d dœnd|i}|f ||	|
|||||d|d œ
|¤Ž^ }}}}}}}|j| }|dk rtd!| › d"|› ƒ‚|dkr,t|› d#|› ƒ‚|rD||f|||ffS t||ƒ}t||ƒ}t t |¡¡} t t |¡¡}!t|||| || ƒ}"t||ƒ|" }#t||| ƒ|" }$t|| |ƒ|" }%t|| || ƒ|" }&tjt |#|$|%|&|"g¡|jd$}'tj||f|jd$}(|'d|#…d|#…f |(d|#…d|#…f< |#|" })|#|" |$ }*|#|% |& d%|"  }+|#|% |& d%|"  |$ },|rˆ|'d|$…d|$…f n|'d|$…d|$…f  |(|)|*…|+|,…f< ||& |" })||& |" |%
  }*|#|" }+|#|" |% },|rü|'d|%…d|%…f  n|'d|%…d|%…f |(|)|*…|+|,…f< |'d|&…d|&…f |(|||& …|||& …f< | |(|#|#|" …|#|#|" …f< | |(||& ||& |" …|"|% |& d%|" |% |& …f< |#})|#|" }*|#|% |& |" }+|#|% |& d%|"  },|rÖ|!n|! |(|)|*…|+|,…f< |rø|! n|!|(||& ||& |" …|#|#|" …f< ||(|fS )&u»  
    Compute the cosine-sine (CS) decomposition of an orthogonal/unitary matrix.

    X is an ``(m, m)`` orthogonal/unitary matrix, partitioned as the following
    where upper left block has the shape of ``(p, q)``::

                                   â”Œ                   â”
                                   â”‚ I  0  0 â”‚ 0  0  0 â”‚
        â”Œ           â”   â”Œ         â”â”‚ 0  C  0 â”‚ 0 -S  0 â”‚â”Œ         â”*
        â”‚ X11 â”‚ X12 â”‚   â”‚ U1 â”‚    â”‚â”‚ 0  0  0 â”‚ 0  0 -I â”‚â”‚ V1 â”‚    â”‚
        â”‚ â”€â”€â”€â”€â”¼â”€â”€â”€â”€ â”‚ = â”‚â”€â”€â”€â”€â”¼â”€â”€â”€â”€â”‚â”‚â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¼â”€â”€â”€â”€â”€â”€â”€â”€â”€â”‚â”‚â”€â”€â”€â”€â”¼â”€â”€â”€â”€â”‚
        â”‚ X21 â”‚ X22 â”‚   â”‚    â”‚ U2 â”‚â”‚ 0  0  0 â”‚ I  0  0 â”‚â”‚    â”‚ V2 â”‚
        â””           â”˜   â””         â”˜â”‚ 0  S  0 â”‚ 0  C  0 â”‚â””         â”˜
                                   â”‚ 0  0  I â”‚ 0  0  0 â”‚
                                   â””                   â”˜

    ``U1``, ``U2``, ``V1``, ``V2`` are square orthogonal/unitary matrices of
    dimensions ``(p,p)``, ``(m-p,m-p)``, ``(q,q)``, and ``(m-q,m-q)``
    respectively, and ``C`` and ``S`` are ``(r, r)`` nonnegative diagonal
    matrices satisfying ``C^2 + S^2 = I`` where ``r = min(p, m-p, q, m-q)``.

    Moreover, the rank of the identity matrices are ``min(p, q) - r``,
    ``min(p, m - q) - r``, ``min(m - p, q) - r``, and ``min(m - p, m - q) - r``
    respectively.

    X can be supplied either by itself and block specifications p, q or its
    subblocks in an iterable from which the shapes would be derived. See the
    examples below.

    Parameters
    ----------
    X : array_like, iterable
        complex unitary or real orthogonal matrix to be decomposed, or iterable
        of subblocks ``X11``, ``X12``, ``X21``, ``X22``, when ``p``, ``q`` are
        omitted.
    p : int, optional
        Number of rows of the upper left block ``X11``, used only when X is
        given as an array.
    q : int, optional
        Number of columns of the upper left block ``X11``, used only when X is
        given as an array.
    separate : bool, optional
        if ``True``, the low level components are returned instead of the
        matrix factors, i.e. ``(u1,u2)``, ``theta``, ``(v1h,v2h)`` instead of
        ``u``, ``cs``, ``vh``.
    swap_sign : bool, optional
        if ``True``, the ``-S``, ``-I`` block will be the bottom left,
        otherwise (by default) they will be in the upper right block.
    compute_u : bool, optional
        if ``False``, ``u`` won't be computed and an empty array is returned.
    compute_vh : bool, optional
        if ``False``, ``vh`` won't be computed and an empty array is returned.

    Returns
    -------
    u : ndarray
        When ``compute_u=True``, contains the block diagonal orthogonal/unitary
        matrix consisting of the blocks ``U1`` (``p`` x ``p``) and ``U2``
        (``m-p`` x ``m-p``) orthogonal/unitary matrices. If ``separate=True``,
        this contains the tuple of ``(U1, U2)``.
    cs : ndarray
        The cosine-sine factor with the structure described above.
         If ``separate=True``, this contains the ``theta`` array containing the
         angles in radians.
    vh : ndarray
        When ``compute_vh=True`, contains the block diagonal orthogonal/unitary
        matrix consisting of the blocks ``V1H`` (``q`` x ``q``) and ``V2H``
        (``m-q`` x ``m-q``) orthogonal/unitary matrices. If ``separate=True``,
        this contains the tuple of ``(V1H, V2H)``.

    References
    ----------
    .. [1] Brian D. Sutton. Computing the complete CS decomposition. Numer.
           Algorithms, 50(1):33-65, 2009.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.linalg import cossin
    >>> from scipy.stats import unitary_group
    >>> x = unitary_group.rvs(4)
    >>> u, cs, vdh = cossin(x, p=2, q=2)
    >>> np.allclose(x, u @ cs @ vdh)
    True

    Same can be entered via subblocks without the need of ``p`` and ``q``. Also
    let's skip the computation of ``u``

    >>> ue, cs, vdh = cossin((x[:2, :2], x[:2, 2:], x[2:, :2], x[2:, 2:]),
    ...                      compute_u=False)
    >>> print(ue)
    []
    >>> np.allclose(x, u @ cs @ vdh)
    True

    Nr   T)Zcheck_finitez=Cosine Sine decomposition only supports square matrices, got r   z
invalid p=z, 0<p<z
 must holdz
invalid q=z, 0<q<zJWhen p and q are None, X must be an Iterable containing the subblocks of Xé   z?When p and q are None, exactly four arrays should be in X, got c                 s   s   | ]}t  |¡V  qd S )N)ÚnpZ
atleast_2d©Ú.0Úx© r   úW/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/linalg/_decomp_cossin.pyÚ	<genexpr>„   ó    zcossin.<locals>.<genexpr>)Úx11Úx12Úx21Úx22z can't be emptyz Invalid x12 dimensions: desired z, got z Invalid x21 dimensions: desired z‘The subblocks have compatible sizes but don't form a square array (instead they form a {}x{} array). This might be due to missing p, q arguments.c                 S   s   g | ]}t  |¡‘qS r   )r   Ziscomplexobjr   r   r   r   Ú
<listcomp>œ   r   zcossin.<locals>.<listcomp>ZuncsdZorcsdZ_lwork)ÚmÚpÚq)ÚlworkZlrworkr   F)
r   r   r   r   Z
compute_u1Z
compute_u2Zcompute_v1tZcompute_v2tZtransZsignszillegal value in argument z of internal z did not converge: )Údtypeé   )Úintr   r   ÚequalÚshapeÚ
ValueErrorÚ
isinstancer   ÚlenÚzipÚformatÚanyr   r   Útypecoder   r   ZdiagÚcosÚsinÚminÚeyeÚmaxr   Zzeros)-ÚXr   r   ZseparateZ	swap_signZ	compute_uZ
compute_vhr   r   r   r   r   ÚnameÚblockZmmpZmmqZcplxZdriverÚcsdZ	csd_lworkr   Z
lwork_argsÚ_ÚthetaÚu1Úu2Zv1hZv2hÚinfoÚmethod_nameÚUZVDHÚcÚsÚrZn11Zn12Zn21Zn22ZIdÚCSZxsZxeZysZyer   r   r   r	      sÈ    c
ÿ
Xÿ
ÿ

ÿÿý
ÿÿûú

ÿ


 $@@,4 0)NNFFTT)Úcollections.abcr   Únumpyr   Zscipy._lib._utilr   Zscipy.linalgr   r   Zlapackr   r   Ú__all__r	   r   r   r   r   Ú<module>   s     ÿ