ó
    Eñi)  ã                   ó0  • S r SS/rSSKrSSKJrJr  SSKJrJr    SS\S\	S	\	S-  S
\S-  S\4
S jjr
   SS\S\	S-  S	\	S-  S
\S-  S\\\\4   4
S jjr   SS\S\	S-  S	\	S-  S
\S-  S\\\\4   4
S jjr   SS\S\	S-  S\S	\	S\\\\4   4
S jjrg)zBImplement various linear algebra algorithms for low rank matrices.Úsvd_lowrankÚpca_lowranké    N)Ú_linalg_utilsÚTensor)Úhandle_torch_functionÚhas_torch_functionÚAÚqÚniterÚMÚreturnc                 óÀ  • Uc  SOUnU R                  5       (       d  [        R                  " U 5      OU R                  n[        R                  n[
        R                  " U R                  S   XU R                  S9nU" X5      nUb
  Xu" X65      -
  n[
        R                  R                  U5      R                  n[        U5       H•  n	U" U R                  U5      nUb  Xu" UR                  U5      -
  n[
        R                  R                  U5      R                  nU" X5      nUb
  Xu" X85      -
  n[
        R                  R                  U5      R                  nM—     U$ )a  Return tensor :math:`Q` with :math:`q` orthonormal columns such
that :math:`Q Q^H A` approximates :math:`A`. If :math:`M` is
specified, then :math:`Q` is such that :math:`Q Q^H (A - M)`
approximates :math:`A - M`. without instantiating any tensors
of the size of :math:`A` or :math:`M`.

.. note:: The implementation is based on the Algorithm 4.4 from
          Halko et al., 2009.

.. note:: For an adequate approximation of a k-rank matrix
          :math:`A`, where k is not known in advance but could be
          estimated, the number of :math:`Q` columns, q, can be
          chosen according to the following criteria: in general,
          :math:`k <= q <= min(2*k, m, n)`. For large low-rank
          matrices, take :math:`q = k + 5..10`.  If k is
          relatively small compared to :math:`min(m, n)`, choosing
          :math:`q = k + 0..2` may be sufficient.

.. note:: To obtain repeatable results, reset the seed for the
          pseudorandom number generator

Args::
    A (Tensor): the input tensor of size :math:`(*, m, n)`

    q (int): the dimension of subspace spanned by :math:`Q`
             columns.

    niter (int, optional): the number of subspace iterations to
                           conduct; ``niter`` must be a
                           nonnegative integer. In most cases, the
                           default value 2 is more than enough.

    M (Tensor, optional): the input tensor's mean of size
                          :math:`(*, m, n)`.

References::
    - Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
      structure with randomness: probabilistic algorithms for
      constructing approximate matrix decompositions,
      arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
      `arXiv <http://arxiv.org/abs/0909.4061>`_).
é   éÿÿÿÿ©ÚdtypeÚdevice)Ú
is_complexÚ_utilsÚget_floating_dtyper   ÚmatmulÚtorchÚrandnÚshaper   ÚlinalgÚqrÚQÚrangeÚmH)
r	   r
   r   r   r   r   ÚRÚXr   Ú_s
             ÚK/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/_lowrank.pyÚget_approximate_basisr$      s  € ðb ‘‰A E€EØ01·±·±ŒF×%Ò% aÔ(ÀAÇGÁG€EÜ�]‰]€Fä�Š�A—G‘G˜B‘K ¸¿¹ÑA€Añ
 	ˆq‹€AØ�}Ø��q“ÑˆÜ�‰�‰˜Ó×Ñ€AÜ�5Ž\ˆÙ�1—4‘4˜‹OˆØ‰=Ø�F˜1Ÿ4™4 “OÑ#ˆAÜ�L‰L�O‰O˜AÓ× Ñ ˆÙ�1‹LˆØ‰=Ø�F˜1“LÑ ˆAÜ�L‰L�O‰O˜AÓ× Ñ Šñ ð €Hó    c           	      ó.  • [         R                  R                  5       (       di  X4n[        [	        [
        U5      5      R                  [         R                  [        S5      45      (       d   [        U5      (       a  [        [        X@XUS9$ [        XX#S9$ )a*  Return the singular value decomposition ``(U, S, V)`` of a matrix,
batches of matrices, or a sparse matrix :math:`A` such that
:math:`A \approx U \operatorname{diag}(S) V^{\text{H}}`. In case :math:`M` is given, then
SVD is computed for the matrix :math:`A - M`.

.. note:: The implementation is based on the Algorithm 5.1 from
          Halko et al., 2009.

.. note:: For an adequate approximation of a k-rank matrix
          :math:`A`, where k is not known in advance but could be
          estimated, the number of :math:`Q` columns, q, can be
          chosen according to the following criteria: in general,
          :math:`k <= q <= min(2*k, m, n)`. For large low-rank
          matrices, take :math:`q = k + 5..10`.  If k is
          relatively small compared to :math:`min(m, n)`, choosing
          :math:`q = k + 0..2` may be sufficient.

.. note:: This is a randomized method. To obtain repeatable results,
          set the seed for the pseudorandom number generator

.. note:: In general, use the full-rank SVD implementation
          :func:`torch.linalg.svd` for dense matrices due to its 10x
          higher performance characteristics. The low-rank SVD
          will be useful for huge sparse matrices that
          :func:`torch.linalg.svd` cannot handle.

Args::
    A (Tensor): the input tensor of size :math:`(*, m, n)`

    q (int, optional): a slightly overestimated rank of A.

    niter (int, optional): the number of subspace iterations to
                           conduct; niter must be a nonnegative
                           integer, and defaults to 2

    M (Tensor, optional): the input tensor's mean of size
                          :math:`(*, m, n)`, which will be broadcasted
                          to the size of A in this function.

References::
    - Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
      structure with randomness: probabilistic algorithms for
      constructing approximate matrix decompositions,
      arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
      `arXiv <https://arxiv.org/abs/0909.4061>`_).

N)r
   r   r   )r   ÚjitÚis_scriptingÚsetÚmapÚtypeÚissubsetr   r   r   r   Ú_svd_lowrank)r	   r
   r   r   Ú
tensor_opss        r#   r   r   U   s~   € ôj �9‰9×!Ñ!×#Ñ#Ø�Vˆ
Ü”3”t˜ZÓ(Ó)×2Ñ2Ü�\‰\œ4 ›:Ð&÷
ñ 
ä  ×,Ñ,Ü(Ü˜Z¨aÀñð ô ˜ eÑ1Ð1r%   c                 óÒ  • Uc  SOUnU R                   SS  u  pE[        R                  nUb  UR                  U R	                  5       5      nXE:  a  U R
                  n Ub  UR
                  n[        XX#S9nU" UR
                  U 5      nUb  X†" UR
                  U5      -
  n[        R                  R                  USS9u  pšnUR
                  nUR                  U	5      n	XE:  a  XÉpÉXšU4$ )Né   éþÿÿÿ©r   r   F)Úfull_matrices)
r   r   r   Úbroadcast_toÚsizer   r$   r   r   Úsvd)r	   r
   r   r   ÚmÚnr   r   ÚBÚUÚSÚVhÚVs                r#   r-   r-   •   sÚ   € ð ‰Y‰˜A€AØ�7‰7�2�3ˆ<�D€AÜ�]‰]€FØ�}Ø�N‰N˜1Ÿ6™6›8Ó$ˆð 	ƒuØ�D‰DˆØ‰=Ø—‘ˆAä˜a¨%Ñ5€AÙˆq�t‰t�Q‹€AØ�}Ø��q—t‘t˜Q“ÑˆÜ�|‰|×Ñ °ÐÐ7�H€Aˆ"Ø
�‰€AØ	�‰�‹€AàƒuØˆ1à�ˆ7€Nr%   Úcenterc           	      ó˜  • [         R                  R                  5       (       d>  [        U 5      [         R                  La"  [        U 45      (       a  [        [        U 4XX#S9$ U R                  SS u  pEUc  [        SXE5      nO/US:¼  a  U[        XE5      ::  d  [        SU S[        XE5       35      eUS:¼  d  [        SU S	35      e[        R                  " U 5      nU(       d  [        XUSS
9$ [        R                  " U 5      (       Ga$  [        U R                  5      S:w  a  [        S5      e[         R                   R#                  U SS9U-  nUR%                  5       S   n[         R&                  " S[        U5      UR(                  UR*                  S9n	X‰S'   [         R,                  " X—R/                  5       US4X`R*                  S9n
[         R0                  " U R                  SS SU4-   X`R*                  S9n[         R                   R3                  X«5      R4                  n[        XX<S
9$ U R7                  SSS9n[        X-
  XSS
9$ )a;  Performs linear Principal Component Analysis (PCA) on a low-rank
matrix, batches of such matrices, or sparse matrix.

This function returns a namedtuple ``(U, S, V)`` which is the
nearly optimal approximation of a singular value decomposition of
a centered matrix :math:`A` such that :math:`A \approx U \operatorname{diag}(S) V^{\text{H}}`

.. note:: The relation of ``(U, S, V)`` to PCA is as follows:

            - :math:`A` is a data matrix with ``m`` samples and
              ``n`` features

            - the :math:`V` columns represent the principal directions

            - :math:`S ** 2 / (m - 1)` contains the eigenvalues of
              :math:`A^T A / (m - 1)` which is the covariance of
              ``A`` when ``center=True`` is provided.

            - ``matmul(A, V[:, :k])`` projects data to the first k
              principal components

.. note:: Different from the standard SVD, the size of returned
          matrices depend on the specified rank and q
          values as follows:

            - :math:`U` is m x q matrix

            - :math:`S` is q-vector

            - :math:`V` is n x q matrix

.. note:: To obtain repeatable results, reset the seed for the
          pseudorandom number generator

Args:

    A (Tensor): the input tensor of size :math:`(*, m, n)`

    q (int, optional): a slightly overestimated rank of
                       :math:`A`. By default, ``q = min(6, m,
                       n)``.

    center (bool, optional): if True, center the input tensor,
                             otherwise, assume that the input is
                             centered.

    niter (int, optional): the number of subspace iterations to
                           conduct; niter must be a nonnegative
                           integer, and defaults to 2.

References::

    - Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
      structure with randomness: probabilistic algorithms for
      constructing approximate matrix decompositions,
      arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
      `arXiv <http://arxiv.org/abs/0909.4061>`_).

)r
   r>   r   r1   Nr0   r   zq(=z>) must be non-negative integer and not greater than min(m, n)=zniter(=z) must be non-negative integerr2   r   z8pca_lowrank input is expected to be 2-dimensional tensor)r1   )Údimr   é   T)r@   Úkeepdim)r   r'   r(   r+   r   r   r   r   r   ÚminÚ
ValueErrorr   r   r-   Ú	is_sparseÚlenÚsparseÚsumÚindicesÚzerosr   r   Úsparse_coo_tensorÚvaluesÚonesÚmmÚmTÚmean)r	   r
   r>   r   r7   r8   r   ÚcÚcolumn_indicesrI   ÚC_tÚ	ones_m1_tr   ÚCs                 r#   r   r   ·   s  € ôD �9‰9×!Ñ!×#Ñ#Ü�‹7œ%Ÿ,™,Ò&Ô+=¸q¸d×+CÑ+CÜ(Ü˜a˜T 1°&ñð ð �W‰W�R�Sˆ\�F€Qà�yÜ��1‹L‰Ø�1‹f˜œc !›i›ÜØ�!�ÐRÔSVÐWXÓS\ÐR]Ð^ó
ð 	
ð �Q‹JÜ˜7 5 'Ð)GÐHÓIÐIä×%Ò% aÓ(€EæÜ˜A¨°Ñ6Ð6ä×Ò˜×ÒÜˆq�w‰w‹<˜1ÓÜÐWÓXÐXÜ�L‰L×Ñ˜Q EÐÐ*¨QÑ.ˆàŸ™› Q™ˆÜ—+’+ØÜ�ÓØ ×&Ñ&Ø!×(Ñ(ñ	
ˆð $�‰
Ü×%Ò%Ø—X‘X“Z ! Q ¨u¿X¹Xñ
ˆô —J’J˜qŸw™w s¨˜|¨q°!¨fÑ4¸EÏ(É(ÑSˆ	Ü�L‰L�O‰O˜CÓ+×.Ñ.ˆÜ˜A¨Ñ3Ð3à�F‰F�u dˆFÐ+ˆÜ˜A™E 1°TÑ:Ð:r%   )r   N)r0   r   N)NTr   )Ú__doc__Ú__all__r   r   r   r   Útorch.overridesr   r   Úintr$   Útupler   r-   Úboolr   © r%   r#   Ú<module>r]      si  ðÙ Hà˜-Ð
(€ó ß 1ß Eð Øñ	GØðGà
ðGð �‰:ðGð ��}ð	Gð
 õGðX ØØñ	=2Øð=2à
ˆT�zð=2ð �‰:ð=2ð ��}ð	=2ð
 ˆ6�6˜6Ð!Ñ"õ=2ðD ØØñ	Øðà
ˆT�zðð �‰:ðð ��}ð	ð
 ˆ6�6˜6Ð!Ñ"õðH ØØñ	n;Øðn;à
ˆT�zðn;ð ðn;ð ð	n;ð
 ˆ6�6˜6Ð!Ñ"ön;r%   