§
    ´³Ùg#  ã                   óX   — 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
„ZdS )é    )ÚIterableN)Ú_asarray_validated)Ú
block_diagÚLinAlgErroré   )Ú_compute_lworkÚget_lapack_funcsÚcossinFTc                 ó4  — |s|�r|€dnt          |¦  «        }|€dnt          |¦  «        }t          | d¬¦  «        } t          j        | j        Ž st          d| j        › �¦  «        ‚| j        d         }||k    s|dk    r!t          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\  }}	}
}�n9t          | t          ¦  «        st          d¦  «        ‚t          | ¦  «        dk    rt          dt          | ¦  «        › �¦  «        ‚d„ | D ¦   «         \  }}	}
}t          g d¢||	|
|g¦  «        D ](\  }}|j        d         dk    rt          |› d�¦  «        ‚Œ)|j        \  }}|j        \  }}|	j        ||fk    rt          d||f› d|	j        › �¦  «        ‚|
j        ||fk    rt          d||f› d|
j        › �¦  «        ‚||z   ||z   k    rt          d||z   › d||z   › d�¦  «        ‚||z   }t          d„ ||	|
|fD ¦   «         ¦  «        }|rdnd}t          ||dz   g||	|
|g¦  «        \  }}t          ||||¬¦  «        }|r|d         |d         dœnd|i} |d&||	|
|||||d|d œ
|¤Ž�^ }}}}}}}|j        |z   }|dk     rt          d!| › d"|› �¦  «        ‚|dk    rt          |› d#|› �¦  «        ‚|r	||f|||ffS t          ||¦  «        }t          ||¦  «        }t          j        t          j        |¦  «        ¦  «        } t          j        t          j        |¦  «        ¦  «        }!t'          ||||z
  ||z
  ¦  «        }"t'          ||¦  «        |"z
  }#t'          |||z
  ¦  «        |"z
  }$t'          ||z
  |¦  «        |"z
  }%t'          ||z
  ||z
  ¦  «        |"z
  }&t          j        t          j        |#|$|%|&|"g¦  «        |j        ¬$¦  «        }'t          j        ||f|j        ¬$¦  «        }(|'d|#…d|#…f         |(d|#…d|#…f<   |#|"z   })|#|"z   |$z   }*|#|%z   |&z   d%|"z  z   }+|#|%z   |&z   d%|"z  z   |$z   },|r|'d|$…d|$…f         n|'d|$…d|$…f          |(|)|*…|+|,…f<   ||&z   |"z   })||&z   |"z   |%
 z   }*|#|"z   }+|#|"z   |%z   },|r|'d|%…d|%…f          n|'d|%…d|%…f         |(|)|*…|+|,…f<   |'d|&…d|&…f         |(|||&z   …|||&z   …f<   | |(|#|#|"z   …|#|#|"z   …f<   | |(||&z   ||&z   |"z   …|#|"z   |%z   |&z   d%|"z  |#z   |%z   |&z   …f<   |#})|#|"z   }*|#|%z   |&z   |"z   }+|#|%z   |&z   d%|"z  z   },|r|!n|! |(|)|*…|+|,…f<   |r|! n|!|(||&z   ||&z   |"z   …|#|#|"z   …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)Ú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              3   ó>   K  — | ]}t          j        |¦  «        V — Œd S )N)ÚnpÚ
atleast_2d©Ú.0Úxs     úQ/granary/edev/quantum/lib/python3.11/site-packages/scipy/linalg/_decomp_cossin.pyú	<genexpr>zcossin.<locals>.<genexpr>„   s,   è è € Ð:Ð:°1�bœm¨AÑ.Ô.Ð:Ð:Ð:Ð:Ð:Ð:ó    )Úx11Úx12Úx21Úx22z can't be emptyz Invalid x12 dimensions: desired z, got z Invalid x21 dimensions: desired zWThe subblocks have compatible sizes but don't form a square array (instead they form a r   z5 array). This might be due to missing p, q arguments.c                 ó6   — g | ]}t          j        |¦  «        ‘ŒS © )r   Úiscomplexobjr   s     r   ú
<listcomp>zcossin.<locals>.<listcomp>œ   s"   € ÐAÐAÐA q•” Ñ"Ô"ÐAÐAÐAr   ÚuncsdÚorcsdÚ_lwork)ÚmÚpÚq)ÚlworkÚlrworkr%   F)
r   r   r   r   Ú
compute_u1Ú
compute_u2Úcompute_v1tÚcompute_v2tÚtransÚsignszillegal value in argument z of internal z did not converge: )Údtypeé   r   )Úintr   r   ÚequalÚshapeÚ
ValueErrorÚ
isinstancer   ÚlenÚzipÚanyr	   r   Útypecoder   r   ÚdiagÚcosÚsinÚminÚeyeÚmaxr-   Úzeros)-ÚXr#   r$   ÚseparateÚ	swap_signÚ	compute_uÚ
compute_vhr"   r   r   r   r   ÚnameÚblockÚmmpÚmmqÚcplxÚdriverÚcsdÚ	csd_lworkr%   Ú
lwork_argsÚ_ÚthetaÚu1Úu2Úv1hÚv2hÚinfoÚmethod_nameÚUÚVDHÚcÚsÚrÚn11Ún12Ún21Ún22ÚIdÚCSÚxsÚxeÚysÚyes-                                                r   r
   r
      s  € ðF 	ð ,ˆAñ ,Ø�ˆAˆA¥ A¡¤ˆØ�ˆAˆA¥ A¡¤ˆÝ˜q¨tÐ4Ñ4Ô4ˆÝŒx˜œÐ!ð 	:Ýð 9Ø/0¬wð9ð 9ñ :ô :ð :àŒG�AŒJˆØ�Š6ˆ6�Q˜!’V�VÝÐI¨!ÐIÐI°1´7¸1´:ÐIÐIÐIÑJÔJÐJØ�Š6ˆ6�Q˜!’V�VÝÐI¨!ÐIÐI°1´7¸1´:ÐIÐIÐIÑJÔJÐJà˜r ˜r 2 A 2˜vœY¨¨"¨1¨"¨a¨b¨b¨&¬	°1°Q°R°R¸¸!¸°V´9¸aÀÀÀÀAÀBÀBÀ¼iÐGÑˆˆS�#�s‘sÝ˜�8Ñ$Ô$ð Ýð :ñ ;ô ;ð 	;õ ˆq‰6Œ6�QŠ;ˆ;Ýð >Ý58¸±V´Vð>ð >ñ ?ô ?ð ?ð ;Ð:¸Ð:Ñ:Ô:ÑˆˆS�#�sÝÐ;Ð;Ð;Ø # S¨#¨sÐ3ñ5ô 5ð 	;ð 	;‰KˆD�%àŒ{˜1Œ~ Ò"Ð"Ý  DÐ!9Ð!9Ð!9Ñ:Ô:Ð:ð #àŒy‰ˆˆ1Ø”9‰ˆˆSàŒ9˜˜C˜Ò Ð Ýð 0ÀÀCÀð 0ð 0Ø$'¤Ið0ð 0ñ 1ô 1ð 1ð Œ9˜˜a˜Ò Ð Ýð 0ÀÀaÀð 0ð 0Ø$'¤Ið0ð 0ñ 1ô 1ð 1ð ˆs‰7�a˜#‘gÒÐÝð ?à"# c¡'ð?ð ?à,-°©Gð?ð ?ð ?ñ @ô @ð @ð
 �‰GˆåÐAÐA¨S°#°s¸CÐ,@ÐAÑAÔAÑBÔB€DØÐ)ˆWˆW '€FÝ% v¨v¸Ñ/@Ð&AØ'*¨C°°cÐ&:ñ<ô <�N€Cˆå˜9¨¨Q°!Ð4Ñ4Ô4€EØ=Að #˜E !œH°°a´Ð9Ð9Ð9Ø˜EÐ"ð à(+¨ð ):°¸À#È3Ø7@Ø7@Ø8BØ8BØ27¸yð):ð ):ð /9ð):ð ):Ñ%€Qˆˆr�2�s˜C ð ”, Ñ'€KØˆa‚x€xÝð 6°t°eð 6ð 6Ø(3ð6ð 6ñ 7ô 7ð 	7àˆa‚x€xÝ˜[ÐCÐC¸TÐCÐCÑDÔDÐDàð +Ø�Bˆx˜  c 
Ð*Ð*å�2�rÑÔ€AÝ
�S˜#Ñ
Ô
€Cõ 	Œ•”�u‘”ÑÔ€AÝ
Œ•”�u‘”ÑÔ€AÝˆAˆq�!�a‘%˜˜Q™ÑÔ€AÝ
ˆa�‰)Œ)�a‰-€CÝ
ˆa��Q‘‰-Œ-˜!Ñ
€CÝ
ˆa�!‰e�Q‰-Œ-˜!Ñ
€CÝ
ˆa�!‰e�Q˜‘UÑ
Ô
˜aÑ
€CÝ	Œ•”˜˜S # s¨AÐ.Ñ/Ô/°u´{Ð	CÑ	CÔ	C€BÝ	Œ�1�a�& ¤Ð	,Ñ	,Ô	,€Bà˜˜˜˜d˜s˜d˜
”^€B€tˆ€tˆTˆcˆT€z�Nà	ˆq‰€BØ	ˆq‰�3‰€BØ	ˆs‰�S‰˜1˜q™5Ñ	 €BØ	ˆs‰�S‰˜1˜q™5Ñ	  3Ñ	&€BØ*3ÐH˜˜4˜C˜4  # ˜:œ˜¸"¸T¸c¸TÀ4ÀCÀ4¸Z¼.¸€B€rˆ2€vˆr�"ˆu€}Ñà	
ˆS‰�1‰€BØ	
ˆS‰�1‰˜�uÑ	€BØ	ˆq‰€BØ	ˆq‰�3‰€BØ*3ÐG˜˜4˜C˜4  # ˜:œ��¸¸D¸S¸DÀ$À3À$¸J¼€B€rˆ"€uˆb�ˆe€|Ñà! $ 3 $¨¨¨ *œ~€B€qˆˆS‰€y�!�A˜‘G�)ÐÑØ#$€B€sˆ3�‰7€{�C˜˜a™�KÐÑ ØKL€B€qˆ3�wˆq�3‰w˜‰{Ð˜C !™G c™M¨CÑ/°°A±¸±¸cÑ0AÀCÑ0GÐGÐGÑHà	€BØ	ˆq‰€BØ	ˆs‰�S‰˜1Ñ	€BØ	ˆs‰�S‰˜1˜q™5Ñ	 €BØ%Ð-�q�q¨A¨2€B€rˆ"€uˆb�ˆe€|Ñà1:Ð+A¨A¨2¨2À€B€qˆ3�wˆq�3‰w˜‰{Ð˜C  a¡˜KÐ'Ñ(àˆb�#ˆ:Ðr   )NNFFTT)Úcollections.abcr   Únumpyr   Úscipy._lib._utilr   Úscipy.linalgr   r   Úlapackr   r	   Ú__all__r
   r   r   r   ú<module>rj      s˜   ðØ $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð à /Ð /Ð /Ð /Ð /Ð /Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4àˆ*€ð (-Ø7;ðRð Rð Rð Rð Rð Rr   