Ë
    qwj…%  ã                   ó|   — d dl mZ d dlZd dlmZmZ d dlmZm	Z	 ddl
mZmZ dgZ	 	 dd„Z ed	d
dd«      d„ «       Zy)é    )ÚIterableN)Ú_asarray_validatedÚ_apply_over_batch)Ú
block_diagÚLinAlgErroré   )Ú_compute_lworkÚget_lapack_funcsÚcossinc           
      óÀ  — |s|rú|€dn
t        |«      }|€dn
t        |«      }t        | d¬«      } t        j                  | j                  dd Ž st        d| j                  dd › �«      ‚| 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|…d|…f   | dd|…|d…f   | d|d…d|…f   | d|d…|d…f   f\  }}	}
}nNt        | t        «      st        d«      ‚t        | «      dk7  rt        dt        | «      › �«      ‚d„ | D «       \  }}	}
}t        ||	|
|||||¬«      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.

    The documentation is written assuming array arguments are of specified
    "core" shapes. However, array argument(s) of this function may have additional
    "batch" dimensions prepended to the core shape. In this case, the array is treated
    as a batch of lower-dimensional slices; see :ref:`linalg_batch` for details.

    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_finiteéþÿÿÿz=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     ú`/var/www/html/newmanjeet/manjet/venv/lib/python3.12/site-packages/scipy/linalg/_decomp_cossin.pyÚ	<genexpr>zcossin.<locals>.<genexpr>‰   s   è ø€ Ð:¹°1œbŸm™m¨A×.¹ùs   ‚!)ÚseparateÚ	swap_signÚ	compute_uÚ
compute_vh)
Úintr   r   ÚequalÚshapeÚ
ValueErrorÚ
isinstancer   ÚlenÚ_cossin)ÚXÚpÚqr   r   r   r   ÚmÚx11Úx12Úx21Úx22s               r   r   r      s¯  € ñP 	‰AØ�‰A¤ A£ˆØ�‰A¤ A£ˆÜ˜q¨tÔ4ˆÜ�x‰x˜Ÿ™  ˜Ñ&Üð /Ø/0¯w©w°r°s¨|¨nð>ó ?ð ?à�G‰G�B‰KˆØ�Š6�Q˜!’VÜ˜z¨!¨¨F°1·7±7¸2±;°-¸zÐJÓKÐKØ�Š6�Q˜!’VÜ˜z¨!¨¨F°1·7±7¸2±;°-¸zÐJÓKÐKà  R a R¨¨!¨ ™n¨a°°R°a°R¸¹°©nØ  Q¡R¨¨!¨ ™n¨a°°Q±R¸¹°©nð>ÑˆˆS�#‘sä˜œ8Ô$Üð :ó ;ð 	;ô ˆq‹6�QŠ;Üð 5Ü58¸³V°Hð>ó ?ð ?á:¹Ó:ÑˆˆS�#�sä�3˜˜S #°ÀIØ&°:ô?ð ?ó    )r&   é   )r'   r+   )r(   r+   )r)   r+   c                 ó6  — 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	        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 c c}w )N)r&   r'   r(   r)   r   r   z 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)r%   r#   r$   )ÚlworkÚlrworkr0   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: )Údtyper+   © )Úzipr   r   Úanyr   Úiscomplexobjr
   r	   Útypecoder   r   ÚdiagÚcosÚsinÚminÚeyeÚmaxr8   Úzeros)-r&   r'   r(   r)   r   r   r   r   ÚnameÚblockr#   r$   ÚmmpÚmmqr%   r   ÚcplxÚdriverÚcsdÚ	csd_lworkr0   Ú
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–  € ô Ò7Ø  c¨3Ð/ö1‰ˆˆeà�;‰;�q‰>˜QÓÜ ˜v _Ð5Ó6Ð6ð1ð �9‰9�D€A€qØ�y‰y�H€Cˆà
‡y�y�Q˜�HÒÜÐ;¸QÀ¸H¸:ð F Ø #§	¡	˜{ð,ó -ð 	-ð ‡y�y�S˜!�HÒÜÐ;¸SÀ!¸H¸:ð F Ø #§	¡	˜{ð,ó -ð 	-ð 	ˆ3�w�!�c‘'ÒÜð à #™g˜Y a¨¨C© yð 1;ð;ó <ð 	<ð
 	
ˆC‰€Aä¨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   Ã*P)NNFFTT)Úcollections.abcr   Únumpyr   Úscipy._lib._utilr   r   Úscipy.linalgr   r   Úlapackr	   r
   Ú__all__r   r!   r9   r*   r   Ú<module>rk      sL   ðÝ $Û ç Bß 0ß 4àˆ*€ð (-Ø7;óA?ñH �:˜z¨:°zÓBñZó CñZr*   