§
    fŠtjz<  ã                   ó–   — d Z ddlZddlmZ dgZdd„Zd	„ Zed
„ ¦   «         Zed„ ¦   «         Z	d„ Z
d„ Zd„ Zd„ Zdd„Zd„ Zd„ Zd„ Zd„ ZdS )zSparse block 1-norm estimator.
é    N)ÚaslinearoperatorÚ
onenormesté   é   Fc                 ó  — t          | ¦  «        } | j        d         | j        d         k    rt          d¦  «        ‚| j        d         }||k    �rt          j        t          | ¦  «                             t          j        |¦  «        ¦  «        ¦  «        }|j        ||fk    r%t          ddt          |j        ¦  «        z   ¦  «        ‚t          |¦  «         
                    d¬¦  «        }|j        |fk    r%t          ddt          |j        ¦  «        z   ¦  «        ‚t          j        |¦  «        }t          ||¦  «        }	|dd…|f         }
||         }nt          | | j        ||¦  «        \  }}	}
}}|s|r|f}|r||	fz  }|r||
fz  }|S |S )a¾	  
    Compute a lower bound of the 1-norm of a sparse array.

    Parameters
    ----------
    A : ndarray or other linear operator
        A linear operator that can be transposed and that can
        produce matrix products.
    t : int, optional
        A positive parameter controlling the tradeoff between
        accuracy versus time and memory usage.
        Larger values take longer and use more memory
        but give more accurate output.
    itmax : int, optional
        Use at most this many iterations.
    compute_v : bool, optional
        Request a norm-maximizing linear operator input vector if True.
    compute_w : bool, optional
        Request a norm-maximizing linear operator output vector if True.

    Returns
    -------
    est : float
        An underestimate of the 1-norm of the sparse array.
    v : ndarray, optional
        The vector such that ||Av||_1 == est*||v||_1.
        It can be thought of as an input to the linear operator
        that gives an output with particularly large norm.
    w : ndarray, optional
        The vector Av which has relatively large 1-norm.
        It can be thought of as an output of the linear operator
        that is relatively large in norm compared to the input.

    Notes
    -----
    This is algorithm 2.4 of [1]_.

    In [2]_ it is described as follows.
    "This algorithm typically requires the evaluation of
    about 4t matrix-vector products and almost invariably
    produces a norm estimate (which is, in fact, a lower
    bound on the norm) correct to within a factor 3."

    .. versionadded:: 0.13.0

    References
    ----------
    .. [1] Nicholas J. Higham and Francoise Tisseur (2000),
           "A Block Algorithm for Matrix 1-Norm Estimation,
           with an Application to 1-Norm Pseudospectra."
           SIAM J. Matrix Anal. Appl. Vol. 21, No. 4, pp. 1185-1201.

    .. [2] Awad H. Al-Mohy and Nicholas J. Higham (2009),
           "A new scaling and squaring algorithm for the matrix exponential."
           SIAM J. Matrix Anal. Appl. Vol. 31, No. 3, pp. 970-989.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csc_array
    >>> from scipy.sparse.linalg import onenormest
    >>> A = csc_array([[1., 0., 0.], [5., 8., 2.], [0., -1., 0.]], dtype=float)
    >>> A.toarray()
    array([[ 1.,  0.,  0.],
           [ 5.,  8.,  2.],
           [ 0., -1.,  0.]])
    >>> onenormest(A)
    9.0
    >>> np.linalg.norm(A.toarray(), ord=1)
    9.0
    r   é   z1expected the operator to act like a square matrixzinternal error: zunexpected shape ©ÚaxisN)r   ÚshapeÚ
ValueErrorÚnpÚasarrayÚmatmatÚidentityÚ	ExceptionÚstrÚabsÚsumÚargmaxÚelementary_vectorÚ_onenormest_coreÚH)ÚAÚtÚitmaxÚ	compute_vÚ	compute_wÚnÚ
A_explicitÚcol_abs_sumsÚargmax_jÚvÚwÚestÚnmultsÚ
nresamplesÚresults                  ú]/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/sparse/linalg/_onenormest.pyr   r      s´  € õT 	˜ÑÔ€AØ„wˆq„z�Q”W˜Q”ZÒÐÝÐLÑMÔMÐMð
 	
Œ�Œ
€AØˆA‚v�vÝ”ZÕ 0°Ñ 3Ô 3× :Ò :½2¼;Àq¹>¼>Ñ JÔ JÑKÔKˆ
ØÔ  1˜vÒ%Ð%ÝÐ.Ø'­#¨jÔ.>Ñ*?Ô*?Ñ?ñAô Að Aå˜:‘”×*Ò*°Ð*Ñ2Ô2ˆØÔ ! Ò&Ð&ÝÐ.Ø'­#¨lÔ.@Ñ*AÔ*AÑAñCô Cð Cå”9˜\Ñ*Ô*ˆÝ˜a Ñ*Ô*ˆØ�q�q�q˜(�{Ô#ˆØ˜8Ô$ˆˆå(8¸¸A¼CÀÀEÑ(JÔ(JÑ%ˆˆQ��6˜:ð ð �Ið Ø�ˆØð 	Ø�q�d‰NˆFØð 	Ø�q�d‰NˆFØˆàˆ
ó    c                 ó   ‡ ‡— dŠˆˆ fd„}|S )z‘
    Decorator for an elementwise function, to apply it blockwise along
    first dimension, to avoid excessive memory usage in temporaries.
    é   c                 ó^  •— | j         d         ‰k     r ‰| ¦  «        S  ‰| d ‰…         ¦  «        }t          j        | j         d         f|j         dd …         z   |j        ¬¦  «        }||d ‰…<   ~t	          ‰| j         d         ‰¦  «        D ] } ‰| ||‰z   …         ¦  «        |||‰z   …<   Œ!|S )Nr   r   ©Údtype)r   r   Úzerosr.   Úrange)ÚxÚy0ÚyÚjÚ
block_sizeÚfuncs       €€r(   Úwrapperz%_blocked_elementwise.<locals>.wrapper€   sÇ   ø€ ØŒ7�1Œ:˜
Ò"Ð"Ø�4˜‘7”7ˆNà��a˜˜˜”nÑ%Ô%ˆBÝ”˜!œ' !œ*˜¨¬°!°"°"¬Ñ5¸R¼XÐFÑFÔFˆAØˆAˆkˆzˆk‰NØÝ˜: q¤w¨q¤z°:Ñ>Ô>ð <ð <�Ø$( D¨¨1¨Q¨z©\¨>Ô):Ñ$;Ô$;��!�A�j‘L�.Ñ!Ð!ØˆHr)   © )r6   r7   r5   s   ` @r(   Ú_blocked_elementwiser9   y   s0   øø€ ð
 €Jð
ð 
ð 
ð 
ð 
ð 
ð €Nr)   c                 ón   — |                       ¦   «         }d||dk    <   |t          j        |¦  «        z  }|S )a9  
    This should do the right thing for both real and complex matrices.

    From Higham and Tisseur:
    "Everything in this section remains valid for complex matrices
    provided that sign(A) is redefined as the matrix (aij / |aij|)
    (and sign(0) = 1) transposes are replaced by conjugate transposes."

    r   r   )Úcopyr   r   )ÚXÚYs     r(   Úsign_round_upr>   Ž   s4   € ð 	
�Š‰Œ€AØ€A€aˆ1‚f�IØ�Œ�‰Œ�N€AØ€Hr)   c                 óR   — t          j        t          j        | ¦  «        d¬¦  «        S )Nr   r	   )r   Úmaxr   )r<   s    r(   Ú_max_abs_axis1rA   Ÿ   s   € åŒ6•"”&˜‘)”) !Ð$Ñ$Ô$Ð$r)   c           	      óÆ   — d}d }t          d| j        d         |¦  «        D ]?}t          j        t          j        | |||z   …         ¦  «        d¬¦  «        }|€|}Œ:||z  }Œ@|S )Nr+   r   r	   )r0   r   r   r   r   )r<   r5   Úrr4   r3   s        r(   Ú_sum_abs_axis0rD   ¤   st   € Ø€JØ€AÝ�1�a”g˜a”j *Ñ-Ô-ð ð ˆÝŒF•2”6˜!˜A˜a 
™l˜NÔ+Ñ,Ô,°1Ð5Ñ5Ô5ˆØˆ9ØˆAˆAà�‰FˆAˆAØ€Hr)   c                 óF   — t          j        | t          ¬¦  «        }d||<   |S )Nr-   r   )r   r/   Úfloat)r   Úir"   s      r(   r   r   °   s$   € Ý
Œ��%Ð Ñ Ô €AØ€A€a�DØ€Hr)   c                 ó¢   — | j         dk    s| j        |j        k    rt          d¦  «        ‚| j        d         }t          j        | |¦  «        |k    S )Nr   z2expected conformant vectors with entries in {-1,1}r   )Úndimr   r   r   Údot)r"   r#   r   s      r(   Úvectors_are_parallelrK   ¶   sL   € ð 	„v�‚{€{�a”g ¤Ò(Ð(ÝÐMÑNÔNÐNØ	Œ�Œ
€AÝŒ6�!�Q‰<Œ<˜1ÒÐr)   c                 ób   ‡— | j         D ]%Št          ˆfd„|j         D ¦   «         ¦  «        s dS Œ&dS )Nc              3   ó8   •K  — | ]}t          ‰|¦  «        V — Œd S ©N©rK   ©Ú.0r#   r"   s     €r(   ú	<genexpr>z;every_col_of_X_is_parallel_to_a_col_of_Y.<locals>.<genexpr>Â   s.   øè è € Ð;Ð;°!Õ'¨¨1Ñ-Ô-Ð;Ð;Ð;Ð;Ð;Ð;r)   FT)ÚTÚany)r<   r=   r"   s     @r(   Ú(every_col_of_X_is_parallel_to_a_col_of_YrU   À   sL   ø€ ØŒSð ð ˆÝÐ;Ð;Ð;Ð;°q´sÐ;Ñ;Ô;Ñ;Ô;ð 	Ø�5�5ð	àˆ4r)   c                 óÔ   ‡‡— ‰j         \  }}‰d d …| f         Št          ˆˆfd„t          | ¦  «        D ¦   «         ¦  «        rdS |�"t          ˆfd„|j        D ¦   «         ¦  «        rdS dS )Nc              3   óL   •K  — | ]}t          ‰‰d d …|f         ¦  «        V — Œd S rN   rO   )rQ   r4   r<   r"   s     €€r(   rR   z*column_needs_resampling.<locals>.<genexpr>Í   s:   øè è € Ð
>Ð
>°Õ  1 Q Q Q¨ T¤7Ñ+Ô+Ð
>Ð
>Ð
>Ð
>Ð
>Ð
>r)   Tc              3   ó8   •K  — | ]}t          ‰|¦  «        V — Œd S rN   rO   rP   s     €r(   rR   z*column_needs_resampling.<locals>.<genexpr>Ð   s.   øè è € Ð7Ð7¨aÕ# A qÑ)Ô)Ð7Ð7Ð7Ð7Ð7Ð7r)   F)r   rT   r0   rS   )rG   r<   r=   r   r   r"   s    `   @r(   Úcolumn_needs_resamplingrY   Ç   s‹   øø€ ð Œ7�D€A€qØ	ˆ!ˆ!ˆ!ˆQˆ$Œ€AÝ
Ð
>Ð
>Ð
>Ð
>Ð
>µU¸1±X´XÐ
>Ñ
>Ô
>Ñ>Ô>ð ØˆtØ€}ÝÐ7Ð7Ð7Ð7°1´3Ð7Ñ7Ô7Ñ7Ô7ð 	Ø�4Øˆ5r)   c                 óz   — t           j                             dd|j        d         ¬¦  «        dz  dz
  |d d …| f<   d S )Nr   r   ©Úsizer   )r   ÚrandomÚrandintr   )rG   r<   s     r(   Úresample_columnr_   Õ   s>   € ÝŒi×Ò  1¨1¬7°1¬:ÐÑ6Ô6°qÑ8¸1Ñ<€A€a€a€aˆ€d�G€G€Gr)   c                 ó8   — t          j        | |¦  «        p| |k     S rN   )r   Úallclose)ÚaÚbs     r(   Úless_than_or_closerd   Ù   s   € ÝŒ;�q˜!ÑÔÐ'  Q¢Ð'r)   c           	      óT  — t          | ¦  «        }t          |¦  «        }|j        d         }t          j        ||f¦  «        }|dk    r6t          j                             dd||dz
  f¬¦  «        dz  dz
  |dd…dd…f<   |t          |¦  «        z  }d}d}d}	t          |¦  «        }
	 t          j        | 	                    |¦  «        ¦  «        }t          |¦  «        }t          j        |¦  «        }|                     ¦   «          |ddd…         }t          |¦  «        }t          j        | 	                    |¦  «        ¦  «        }t          |¦  «        }|	dk    rFt          t!          |¦  «        t          j        |dd…|f         |dd…|f         ¦  «        ¦  «        r�nt          j        |¦  «        ddd…         d|…         }
||
         }t          |¦  «        D ]}t'          ||
|         ¦  «        |dd…|f<   Œ |	dk    rVt          |d         |d         ¦  «        st)          d¦  «        ‚t          |d         |d         ¦  «        st)          d¦  «        ‚|	d	k    r=t          |¦  «        D ]-}t          ||         ||         ¦  «        st)          d
¦  «        ‚Œ.|}|}|	dz  }	�Œ ||
fS )a"  
    This is Algorithm 2.2.

    Parameters
    ----------
    A : ndarray or other linear operator
        A linear operator that can produce matrix products.
    AT : ndarray or other linear operator
        The transpose of A.
    t : int, optional
        A positive parameter controlling the tradeoff between
        accuracy versus time and memory usage.

    Returns
    -------
    g : sequence
        A non-negative decreasing vector
        such that g[j] is a lower bound for the 1-norm
        of the column of A of jth largest 1-norm.
        The first entry of this vector is therefore a lower bound
        on the 1-norm of the linear operator A.
        This sequence has length t.
    ind : sequence
        The ith entry of ind is the index of the column A whose 1-norm
        is given by g[i].
        This sequence of indices has length t, and its entries are
        chosen from range(n), possibly with repetition,
        where n is the order of the operator A.

    Notes
    -----
    This algorithm is mainly for testing.
    It uses the 'ind' array in a way that is similar to
    its usage in algorithm 2.4. This algorithm 2.2 may be easier to test,
    so it gives a chance of uncovering bugs related to indexing
    which could have propagated less noticeably to algorithm 2.4.

    r   r   r   r[   NTéÿÿÿÿzinvariant (2.2) is violatedé   zinvariant (2.3) is violated)r   r   r   Úonesr]   r^   rF   r0   r   r   rD   r   Úsortr>   rA   rd   r@   rJ   Úargsortr   r   )r   ÚATr   ÚA_linear_operatorÚAT_linear_operatorr   r<   Úg_prevÚh_prevÚkÚindr=   ÚgÚbest_jÚSÚZÚhr4   s                     r(   Ú_algorithm_2_2rw   Ý   sÄ  € õN )¨Ñ+Ô+ÐÝ)¨"Ñ-Ô-ÐØÔ Ô"€Aõ 	Œ��A�‰Œ€AØˆ1‚u€uÝ”9×$Ò$ Q¨°°A°a±C°Ð$Ñ9Ô9¸!Ñ;¸aÑ?ˆˆ!ˆ!ˆ!ˆQˆRˆRˆ%‰Ø�ˆq‰Œ�M€Að €FØ€FØ	€AÝ
�‰(Œ(€Cð*ÝŒJÐ(×/Ò/°Ñ2Ô2Ñ3Ô3ˆÝ˜1ÑÔˆÝ”˜1‘”ˆØ	�Š‰ŒˆØˆdˆd�ˆdŒGˆÝ˜!ÑÔˆÝŒJÐ)×0Ò0°Ñ3Ô3Ñ4Ô4ˆÝ˜1ÑÔˆð �Š6ˆ6Ý!¥# a¡&¤&­"¬&°°1°1°1°f°9´¸qÀÀÀÀFÀ¼|Ñ*LÔ*LÑMÔMð ÙÝŒj˜‰mŒm˜D˜D˜b˜DÔ! " 1 "Ô%ˆØˆcŒFˆÝ�q‘”ð 	3ð 	3ˆAÝ'¨¨3¨q¬6Ñ2Ô2ˆAˆaˆaˆa�ˆd‰GˆGð �Š6ˆ6Ý% f¨Q¤i°¸´Ñ;Ô;ð ?ÝÐ =Ñ>Ô>Ð>Ý% f¨Q¤i°°1´Ñ6Ô6ð ?ÝÐ =Ñ>Ô>Ð>ð �Š6ˆ6Ý˜1‘X”Xð Cð C�Ý)¨!¨A¬$°°q´	Ñ:Ô:ð CÝ#Ð$AÑBÔBÐBðCð ˆØˆØ	ˆQ‰ˆñU*ðZ ˆcˆ6€Mr)   c                 ó  — t          | ¦  «        }t          |¦  «        }|dk     rt          d¦  «        ‚|dk     rt          d¦  «        ‚| j        d         }||k    rt          d¦  «        ‚d}d}t          j        ||ft
          ¬¦  «        }	|dk    rjt          d|¦  «        D ]}
t          |
|	¦  «         Œt          |¦  «        D ]7}
t          |
|	¦  «        r%t          |
|	¦  «         |dz  }t          |
|	¦  «        °%Œ8|	t          |¦  «        z  }	t          j	        dt          j
        ¬¦  «        }d}t          j	        ||ft
          ¬¦  «        }d}d}	 t          j        |                     |	¦  «        ¦  «        }|dz  }t          |¦  «        }t          j        |¦  «        }t          j        |¦  «        }||k    s|dk    r|dk    r||         }|dd…|f         }|dk    r
||k    r|}�n	|}|}||k    r�nýt!          |¦  «        }~t#          ||¦  «        r�nÛ|dk    rIt          |¦  «        D ]9}
t          |
||¦  «        r&t          |
|¦  «         |dz  }t          |
||¦  «        °&Œ:~t          j        |                     |¦  «        ¦  «        }|dz  }t%          |¦  «        }~|dk    rt          |¦  «        ||         k    r�n.t          j        |¦  «        ddd
…         d|t)          |¦  «        z   …                              ¦   «         }~|dk    rht          j        |d|…         |¦  «                             ¦   «         rn°t          j        ||¦  «        }t          j        ||          ||         f¦  «        }t          |¦  «        D ]}t3          |||         ¦  «        |	dd…|f<   Œ |d|…         t          j        |d|…         |¦  «                  }t          j        ||f¦  «        }|dz  }�Œ¢t3          ||¦  «        }|||||fS )aî  
    Compute a lower bound of the 1-norm of a sparse array.

    Parameters
    ----------
    A : ndarray or other linear operator
        A linear operator that can produce matrix products.
    AT : ndarray or other linear operator
        The transpose of A.
    t : int, optional
        A positive parameter controlling the tradeoff between
        accuracy versus time and memory usage.
    itmax : int, optional
        Use at most this many iterations.

    Returns
    -------
    est : float
        An underestimate of the 1-norm of the sparse array.
    v : ndarray, optional
        The vector such that ||Av||_1 == est*||v||_1.
        It can be thought of as an input to the linear operator
        that gives an output with particularly large norm.
    w : ndarray, optional
        The vector Av which has relatively large 1-norm.
        It can be thought of as an output of the linear operator
        that is relatively large in norm compared to the input.
    nmults : int, optional
        The number of matrix products that were computed.
    nresamples : int, optional
        The number of times a parallel column was observed,
        necessitating a re-randomization of the column.

    Notes
    -----
    This is algorithm 2.4.

    r   z$at least two iterations are requiredr   zat least one column is requiredr   z't should be smaller than the order of Ar-   NTrf   )r   r   r   r   rh   rF   r0   r_   rY   r/   Úintpr   r   rD   r@   r   r>   rU   rA   rj   Úlenr;   ÚisinÚallÚconcatenater   )r   rk   r   r   rl   rm   r   r%   r&   r<   rG   Úind_histÚest_oldrt   rp   rq   r=   Úmagsr$   rs   Úind_bestr#   ÚS_oldru   rv   Úseenr4   Únew_indr"   s                                r(   r   r   D  sY  € õR )¨Ñ+Ô+ÐÝ)¨"Ñ-Ô-ÐØˆq‚y€yÝÐ?Ñ@Ô@Ð@Øˆ1‚u€uÝÐ:Ñ;Ô;Ð;Ø	Œ�Œ
€AØˆA‚v€vÝÐBÑCÔCÐCð €FØ€Jõ 	Œ��A��eÐ$Ñ$Ô$€Að 	ˆ1‚u€uÝ�q˜!‘”ð 	"ð 	"ˆAõ ˜A˜qÑ!Ô!Ð!Ð!Ý�q‘”ð 	 ð 	 ˆAÝ)¨!¨QÑ/Ô/ð  Ý  1Ñ%Ô%Ð%Ø˜a‘�
õ *¨!¨QÑ/Ô/ð  øð �ˆq‰Œ�M€AåŒx˜¥¤Ð)Ñ)Ô)€HØ€GÝ
Œ�!�Q��uÐ%Ñ%Ô%€AØ	€AØ
€Cð=ÝŒJÐ(×/Ò/°Ñ2Ô2Ñ3Ô3ˆØ�!‰ˆÝ˜aÑ Ô ˆÝŒf�T‰lŒlˆÝ”˜4‘”ˆØ�Š=ˆ=˜A šF˜FØ�AŠvˆvØ˜vœ;�Ø�!�!�!�V�)”ˆAà�Š6ˆ6�c˜W’n�nØˆCÙØˆØˆØˆuŠ9ˆ9ÙÝ˜!ÑÔˆØå3°A°uÑ=Ô=ð 	ÙØˆqŠ5ˆ5õ ˜1‘X”Xð $ð $�Ý-¨a°°EÑ:Ô:ð $Ý# A qÑ)Ô)Ð)Ø !‘O�Jõ .¨a°°EÑ:Ô:ð $øð åŒJÐ)×0Ò0°Ñ3Ô3Ñ4Ô4ˆØ�!‰ˆÝ˜1ÑÔˆØà�Š6ˆ6•c˜!‘f”f  (¤Ò+Ð+Ùõ Œj˜‰mŒm˜D˜D˜b˜DÔ!Ð"2 1¥S¨¡]¤]¡?Ð"2Ô3×8Ò8Ñ:Ô:ˆØØˆqŠ5ˆ5õ Œw�s˜2˜A˜2”w Ñ)Ô)×-Ò-Ñ/Ô/ð Øõ ”7˜3 Ñ)Ô)ˆDÝ”. # t e¤*¨c°$¬iÐ!8Ñ9Ô9ˆCÝ�q‘”ð 	3ð 	3ˆAÝ'¨¨3¨q¬6Ñ2Ô2ˆAˆaˆaˆa�ˆd‰GˆGà�b�q�b”'�2œ7 3 r¨ r¤7¨HÑ5Ô5Ð5Ô6ˆÝ”> 8¨WÐ"5Ñ6Ô6ˆØ	ˆQ‰ˆñ{=õ| 	˜!˜XÑ&Ô&€AØ��1�f˜jÐ(Ð(r)   )r   r   FFrN   )Ú__doc__Únumpyr   Úscipy.sparse.linalgr   Ú__all__r   r9   r>   rA   rD   r   rK   rU   rY   r_   rd   rw   r   r8   r)   r(   ú<module>r‰      s7  ððð ð Ð Ð Ð Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0ð ˆ.€ðkð kð kð kð\ð ð ð* ðð ñ Ôðð  ð%ð %ñ Ôð%ð	ð 	ð 	ðð ð ðð ð ðð ð ðð ð ð ð=ð =ð =ð(ð (ð (ðdð dð dðNO)ð O)ð O)ð O)ð O)r)   