§
    Qzni�*  ã                   ó|  — d Z ddgZddlmZmZ ddlZddlmZ ddlmZ	 dd	l
mZmZ 	 ddededee         dee         def
d„Z	 	 	 ddedee         dee         dee         deeeef         f
d„Z	 	 	 ddedee         dee         dee         deeeef         f
d„Z	 ddedee         dededeeeef         f
d„ZdS )zCImplement various linear algebra algorithms for low rank matrices.
Úsvd_lowrankÚpca_lowranké    )ÚOptionalÚTupleN)ÚTensoré   )Ú_linalg_utils)Úhandle_torch_functionÚhas_torch_functioné   ÚAÚqÚniterÚMÚreturnc                 óÄ  — |€dn|}| j         dd…         \  }}t          j        | ¦  «        }t          j        }t	          j        |||| j        ¬¦  «        }t          j        | ¦  «        }	|€�t          j         	                     || |¦  «        ¦  «        j
        }
t          |¦  «        D ]^}t          j         	                     ||	|
¦  «        ¦  «        j
        }
t          j         	                     || |
¦  «        ¦  «        j
        }
Œ_n×t          j        |¦  «        }t          j         	                     || |¦  «         |||¦  «        z
  ¦  «        j
        }
t          |¦  «        D ]x}t          j         	                     ||	|
¦  «         |||
¦  «        z
  ¦  «        j
        }
t          j         	                     || |
¦  «         |||
¦  «        z
  ¦  «        j
        }
Œy|
S )a8  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`.

    .. 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
              choosen 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:`(*, 1, 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)ÚshapeÚ_utilsÚget_floating_dtypeÚmatmulÚtorchÚrandnr   ÚtransjugateÚlinalgÚqrÚQÚrange)r   r   r   r   ÚmÚnr   r   ÚRÚA_Hr    ÚiÚM_Hs                ún/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/torch/_lowrank.pyÚget_approximate_basisr)      s¤  € ðZ �ˆAˆA E€EØŒ7�2�3�3Œ<�D€A€qÝÔ% aÑ(Ô(€EÝŒ]€FåŒ�A�q ¨a¬hÐ7Ñ7Ô7€Aõ Ô
˜QÑ
Ô
€CØ€yÝŒL�OŠO˜F˜F 1 a™LœLÑ)Ô)Ô+ˆÝ�u‘”ð 	0ð 	0ˆAÝ”—’   s¨A¡¤Ñ/Ô/Ô1ˆAÝ”—’   q¨!¡¤Ñ-Ô-Ô/ˆAˆAð	0õ Ô  Ñ#Ô#ˆÝŒL�OŠO˜F˜F 1 a™LœL¨6¨6°!°Q©<¬<Ñ7Ñ8Ô8Ô:ˆÝ�u‘”ð 	?ð 	?ˆAÝ”—’   s¨A¡¤°°¸¸Q±´Ñ ?Ñ@Ô@ÔBˆAÝ”—’   q¨!¡¤¨v¨v°a¸©|¬|Ñ ;Ñ<Ô<Ô>ˆAˆ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 diag(S) V^T`. 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:: To obtain repeatable results, reset the seed for the
              pseudorandom number generator

    .. note:: The input is assumed to be a low-rank matrix.

    .. note:: In general, use the full-rank SVD implementation
              :func:`torch.linalg.svd` for dense matrices due to its 10-fold
              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:`(*, 1, 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 <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   T   s­   € õZ Œ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        }|€d }nt          j        |¦  «        }t          j        | ¦  «        }||k     s||k    �rt	          ||||¬¦  «        }	t          j        |	¦  «        }
|€ || |
¦  «        }n || |
¦  «         |||
¦  «        z
  }|j         d         |k    sJ |j         |f¦   «         ‚|j         d         |k    sJ |j         |f¦   «         ‚|j         d         |j         d         k    sJ |j         ¦   «         ‚t          j                             |d¬¦  «        \  }}}|j	        }|	                     |¦  «        }�nt	          | |||¬¦  «        }	t          j        |	¦  «        }
|€ |||
¦  «        }n |||
¦  «         |||
¦  «        z
  }t          j        |¦  «        }|j         d         |k    sJ |j         |f¦   «         ‚|j         d         |k    sJ |j         |f¦   «         ‚|j         d         |j         d         k    sJ |j         ¦   «         ‚t          j                             |d¬¦  «        \  }}}|j	        }|	                     |¦  «        }|||fS )Nr+   r   ©r   r   éÿÿÿÿF)Úfull_matrices)
r   r   r   Ú	transposer)   Ú	conjugater   r   ÚsvdÚmH)r   r   r   r   r"   r#   r   ÚM_tÚA_tr    ÚQ_cÚB_tÚUÚSÚVhÚVÚBs                    r(   r3   r3   Œ   s‘  € ð ˆYˆˆ˜A€AØŒ7�2�3�3Œ<�D€A€qÝŒ]€FØ€yØˆˆåÔ˜qÑ!Ô!ˆÝ
Ô
˜1Ñ
Ô
€Cð 	ˆ1‚u€u��A’‘õ " # q°¸Ð=Ñ=Ô=ˆÝÔ˜qÑ!Ô!ˆØˆ9Ø�&˜˜C‘.”.ˆCˆCà�&˜˜C‘.”. 6 6¨!¨S¡>¤>Ñ1ˆCØŒy˜Œ} Ò!Ð!Ð! C¤I¨q >Ñ!Ô!Ð!ØŒy˜Œ} Ò!Ð!Ð! C¤I¨q >Ñ!Ô!Ð!ØŒy˜Œ} ¤	¨"¤Ò-Ð-Ð-¨s¬yÑ-Ô-Ð-Ý”<×#Ò# C°uÐ#Ñ=Ô=‰ˆˆ1ˆbØŒEˆØ�HŠH�Q‰KŒKˆ‰å! ! Q¨e°qÐ9Ñ9Ô9ˆÝÔ˜qÑ!Ô!ˆØˆ9Ø��s˜CÑ Ô ˆAˆAà��s˜CÑ Ô  6 6¨#¨sÑ#3Ô#3Ñ3ˆAÝÔ˜qÑ!Ô!ˆØŒy˜Œ} Ò!Ð!Ð! C¤I¨q >Ñ!Ô!Ð!ØŒy˜Œ} Ò!Ð!Ð! C¤I¨q >Ñ!Ô!Ð!ØŒy˜Œ} ¤	¨"¤Ò-Ð-Ð-¨s¬yÑ-Ô-Ð-Ý”<×#Ò# C°uÐ#Ñ=Ô=‰ˆˆ1ˆbØŒEˆØ�HŠH�Q‰KŒKˆàˆ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        | ¦  «        �r>t          | 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        t           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 = U diag(S) V^T`.

    .. 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   rF   r   r   Nr+   r   zq(=z>) must be non-negative integer and not greater than min(m, n)=zniter(=z) must be non-negative integerr6   r   z8pca_lowrank input is expected to be 2-dimensional tensor)r   )Údimr   r   T)rH   Úkeepdim)r   r-   r.   r1   r   r   r
   r   r   ÚminÚ
ValueErrorr   r   r3   Ú	is_sparseÚlenÚsparseÚsumÚindicesÚzerosr   r   Úsparse_coo_tensorÚvaluesÚonesr9   ÚmmÚmean)r   r   rF   r   r"   r#   r   ÚcÚcolumn_indicesrP   ÚC_tÚ	ones_m1_tr   ÚCs                 r(   r   r   ¿   s’  € õ~ Œ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ˆ	ÝÔ�Uœ\Ÿ_š_¨S°)Ñ<Ô<Ñ=Ô=ˆÝ˜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__Útypingr   r   r   r   Ú r	   r   Ú	overridesr
   r   Úintr)   r   r3   Úboolr   © r*   r(   ú<module>rd      s  ððð ð ˜-Ð
(€à "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @ð HLðCð CØðCØðCØ& sœmðCØ4<¸VÔ4DðCàðCð Cð Cð CðP ØØð	52ð 52Øð52à�„}ð52ð �CŒ=ð52ð �Ôð	52ð
 ˆ6�6˜6Ð!Ô"ð52ð 52ð 52ð 52ðt ØØð	0ð 0Øð0à�„}ð0ð �CŒ=ð0ð �Ôð	0ð
 ˆ6�6˜6Ð!Ô"ð0ð 0ð 0ð 0ðh KLðk;ð k;Øðk;Ø˜3”-ðk;Ø04ðk;ØDGðk;à
ˆ6�6˜6Ð!Ô"ðk;ð k;ð k;ð k;ð k;ð k;r*   