Ë
    âQ(h#  ã                   óV   — 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y)
é    )ÚIterableN)Ú_asarray_validated)Ú
block_diagÚLinAlgErroré   )Ú_compute_lworkÚget_lapack_funcsÚcossinc                 ó¼
  — |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\  }}	}
}�n*t        | t        «      st        d«      ‚t        | «      dk7  rt        dt        | «      › �«      ‚d„ | D «       \  }}	}
}t        g d¢||	|
|g«      D ]%  \  }}|j                  d   dk(  sŒt        |› d�«      ‚ |j                  \  }}|j                  \  }}|	j                  ||fk7  rt        d||f› d|	j                  › �«      ‚|
j                  ||fk7  rt        d||f› d|
j                  › �«      ‚||z   ||z   k7  rt        d||z   › d||z   › d�«      ‚||z   }t        ||	|
|fD �cg c]  }t        j                  |«      ‘Œ c}«      }|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kD  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        j0                  ||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 c c}w )&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   óF   K  — | ]  }t        j                  |«      –— Œ y ­w)N)ÚnpÚ
atleast_2d)Ú.0Úxs     úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/scipy/linalg/_decomp_cossin.pyú	<genexpr>zcossin.<locals>.<genexpr>„   s   è ø€ Ò:°1œbŸm™m¨A×.Ñ:ùs   ‚!)Ú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.Ú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é   © )Úintr   r   ÚequalÚshapeÚ
ValueErrorÚ
isinstancer   ÚlenÚzipÚanyÚiscomplexobjr	   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Úmmqr   Ú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£ˆÜ˜q¨tÔ4ˆÜ�x‰x˜Ÿ™Ñ!Üð /Ø/0¯w©w¨ið9ó :ð :à�G‰G�A‰JˆØ�Š6�Q˜!’VÜ˜z¨!¨¨F°1·7±7¸1±:°,¸jÐIÓJÐJØ�Š6�Q˜!’VÜ˜z¨!¨¨F°1·7±7¸1±:°,¸jÐIÓJÐJà˜r ˜r 2 A 2˜v™Y¨¨"¨1¨"¨a©b¨&©	°1°Q±R¸¸!¸°V±9¸aÀÁÀAÁBÀ¹iÐGÑˆˆS�#’sÜ˜œ8Ô$Üð :ó ;ð 	;ô ˆq‹6�QŠ;Üð 5Ü58¸³V°Hð>ó ?ð ?ñ ;¸Ô:ÑˆˆS�#�sÜÒ;Ø # S¨#¨sÐ3ó5ò 	;‰KˆD�%à�{‰{˜1‰~ Ó"Ü  D 6¨Ð!9Ó:Ð:ð	;ð �y‰y‰ˆˆ1Ø—9‘9‰ˆˆSà�9‰9˜˜C˜Ò ÜÐ?ÀÀCÀ¸zð J$Ø$'§I¡I ;ð0ó 1ð 1ð �9‰9˜˜a˜Ò ÜÐ?ÀÀaÀ¸zð J$Ø$'§I¡I ;ð0ó 1ð 1ð ˆs‰7�a˜#‘gÒÜð "à"# c¡' ¨!¨A°©G¨9ð 5?ð?ó @ð @ð
 �‰Gˆä¨S°#°s¸CÐ,@ÖA q”—‘ Õ"ÒAÓB€DÙ‰W '€FÜ% v¨v¸Ñ/@Ð&AØ'*¨C°°cÐ&:ó<�N€Cˆä˜9¨¨Q°!Ô4€EÙ=A˜E !™H°°a±Ò9Ø˜EÐ"ð á(+ð ):°¸À#È3Ø7@Ø7@Ø8BØ8BØ27¸yñ):ð /9ñ):Ñ%€Qˆˆr�2�s˜C ð —,‘, Ñ'€KØˆa‚xÜÐ5°t°e°Wð =(Ø(3 }ð6ó 7ð 	7àˆa‚xÜ˜[˜MÐ)<¸T¸FÐCÓ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€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˜˜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˜˜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¨A¨2€B€rˆ"€uˆb�ˆe€|Ñá1:¨A©2À€B€qˆ3�wˆq�3‰w˜‰{Ð˜C  a¡˜KÐ'Ñ(àˆb�#ˆ:ÐùòC Bs   È-U)NNFFTT)Úcollections.abcr   Únumpyr   Úscipy._lib._utilr   Úscipy.linalgr   r   Úlapackr   r	   Ú__all__r
   r)   ó    r   ú<module>rg      s)   ðÝ $Û å /ß 0ß 4àˆ*€ð (-Ø7;ôRrf   