§
    ŠŠtj)  ã                   ó4  — d Z ddgZddlZddlmZmZ ddlmZmZ 	 	 dded	e	d
e	dz  dedz  def
d„Z
	 	 	 dded	e	dz  d
e	dz  dedz  deeeef         f
d„Z	 	 	 dded	e	dz  d
e	dz  dedz  deeeef         f
d„Z	 	 	 dded	e	dz  ded
e	deeeef         f
d„ZdS )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                 ó–  — |€dn|}|                       ¦   «         st          j        | ¦  «        n| j        }t          j        }t          j        | j        d         ||| j        ¬¦  «        } || |¦  «        }|�| |||¦  «        z
  }t
          j	         
                    |¦  «        j        }t          |¦  «        D ]Ž}	 || j        |¦  «        }|�| ||j        |¦  «        z
  }t
          j	         
                    |¦  «        j        } || |¦  «        }|�| |||¦  «        z
  }t
          j	         
                    |¦  «        j        }Œ�|S )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>`_).
    Nr	   éÿÿÿÿ©Ú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
             úL/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_lowrank.pyÚget_approximate_basisr$      s9  € ðb �ˆAˆA E€EØ01·²±´ÐK�FÔ% aÑ(Ô(Ð(ÀAÄG€EÝŒ]€FåŒ�A”G˜B”K ¨%¸¼ÐAÑAÔA€Að
 	ˆˆq�!‰Œ€AØ€}Ø���q˜!‘”ÑˆÝŒ�Š˜ÑÔÔ€AÝ�5‰\Œ\ð !ð !ˆØˆF�1”4˜‰OŒOˆØˆ=Ø�F�F˜1œ4 ‘O”OÑ#ˆAÝŒL�OŠO˜AÑÔÔ ˆØˆF�1�a‰LŒLˆØˆ=Ø�F�F˜1˜a‘L”LÑ ˆAÝŒL�OŠO˜AÑÔÔ ˆˆØ€Hó    é   c                 óZ  — t           j                             ¦   «         s{| |f}t          t	          t
          |¦  «        ¦  «                             t           j        t          d¦  «        f¦  «        s)t          |¦  «        rt          t          || |||¬¦  «        S t          | |||¬¦  «        S )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×!Ò!Ñ#Ô#ð Ø˜�Vˆ
Ý•3•t˜ZÑ(Ô(Ñ)Ô)×2Ò2ÝŒ\�4 ™:œ:Ð&ñ
ô 
ð 	å  Ñ,Ô,ð	õ )Ý˜Z¨¨a°uÀðñ ô ð õ ˜˜Q e¨qÐ1Ñ1Ô1Ð1r%   c                 óÜ  — |€dn|}| j         dd …         \  }}t          j        }|�'|                     |                      ¦   «         ¦  «        }||k     r| j        } |�|j        }t          | |||¬¦  «        } ||j        | ¦  «        }|�| ||j        |¦  «        z
  }t          j         	                    |d¬¦  «        \  }	}
}|j        }|                     |	¦  «        }	||k     r||	}}	|	|
|fS )Nr&   éþÿÿÿ©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�2�3�3Œ<�D€A€qÝŒ]€FØ€}Ø�NŠN˜1Ÿ6š6™8œ8Ñ$Ô$ˆð 	ˆ1‚u€uØŒDˆØˆ=Ø”ˆAå˜a ¨%°1Ð5Ñ5Ô5€AØˆˆqŒt�Q‰Œ€AØ€}Ø���q”t˜Q‘”ÑˆÝŒ|×Ò °ÐÑ7Ô7�H€A€qˆ"Ø
Œ€AØ	�Š�‰Œ€Aàˆ1‚u€uØ�!ˆ1ˆàˆa�ˆ7€Nr%   TÚcenterc           	      óú  — t           j                             ¦   «         sFt          | ¦  «        t           j        ur+t          | f¦  «        rt          t          | f| |||¬¦  «        S | j        dd…         \  }}|€t          d||¦  «        }n=|dk    r|t          ||¦  «        k    s#t          d|› dt          ||¦  «        › �¦  «        ‚|dk    st          d|› d	�¦  «        ‚t          j        | ¦  «        }|st          | ||d¬
¦  «        S t          j        | ¦  «        �r1t          | j        ¦  «        dk    rt          d¦  «        ‚t           j                             | d¬¦  «        |z  }|                     ¦   «         d         }t          j        dt          |¦  «        |j        |j        ¬¦  «        }	||	d<   t          j        |	|                     ¦   «         |df|| j        ¬¦  «        }
t          j        | j        dd…         d|fz   || j        ¬¦  «        }t           j                             |
|¦  «        j        }t          | |||¬
¦  «        S |                      dd¬¦  «        }t          | |z
  ||d¬
¦  «        S )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   Nr&   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×!Ò!Ñ#Ô#ð Ý�‰7Œ7�%œ,Ð&Ð&Õ+=¸q¸dÑ+CÔ+CÐ&Ý(Ý˜a˜T 1¨°&Àðñ ô ð ð ŒW�R�S�SŒ\�F€Qˆà€yÝ��1�a‰LŒLˆˆØ�1Šfˆf˜�c ! Q™iœiš˜ÝØ^�!Ð^Ð^ÕSVÐWXÐZ[ÑS\ÔS\Ð^Ð^ñ
ô 
ð 	
ð �QŠJˆJÝÐH 5ÐHÐHÐHÑIÔIÐIåÔ% aÑ(Ô(€Eàð 7Ý˜A˜q¨°Ð6Ñ6Ô6Ð6åÔ˜ÑÔñ ;ÝˆqŒw‰<Œ<˜1ÒÐÝÐWÑXÔXÐXÝŒL×Ò˜Q EÐÑ*Ô*¨QÑ.ˆàŸš™œ QœˆÝ”+ØÝ�ÑÔØ Ô&Ø!Ô(ð	
ñ 
ô 
ˆð $ˆ�‰
ÝÔ%Ø�Q—X’X‘Z”Z ! Q ¨u¸Q¼Xð
ñ 
ô 
ˆõ ”J˜qœw s¨ sœ|¨q°!¨fÑ4¸EÈ!Ì(ÐSÑSÔSˆ	ÝŒL�OŠO˜C Ñ+Ô+Ô.ˆÝ˜A˜q¨°Ð3Ñ3Ô3Ð3à�FŠF�u dˆFÑ+Ô+ˆÝ˜A ™E 1¨E°TÐ:Ñ:Ô:Ð:r%   )r	   N)r&   r	   N)NTr	   )Ú__doc__Ú__all__r   r   r   r   Útorch.overridesr   r   Úintr$   Útupler   r.   Úboolr   © r%   r#   ú<module>r]      s   ðØ HÐ Hà˜-Ð
(€ð €€€Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø EÐ EÐ EÐ EÐ EÐ EÐ EÐ Eð Øð	Gð GØðGà
ðGð �‰:ðGð ��}ð	Gð
 ðGð Gð Gð GðX ØØð	=2ð =2Øð=2à
ˆT�zð=2ð �‰:ð=2ð ��}ð	=2ð
 ˆ6�6˜6Ð!Ô"ð=2ð =2ð =2ð =2ðD ØØð	ð Øðà
ˆT�zðð �‰:ðð ��}ð	ð
 ˆ6�6˜6Ð!Ô"ðð ð ð ðH ØØð	n;ð n;Øðn;à
ˆT�zðn;ð ðn;ð ð	n;ð
 ˆ6�6˜6Ð!Ô"ðn;ð n;ð n;ð n;ð n;ð n;r%   