ó
    Eñiz<  ã                   óŒ   • S r SSKrSSKJr  S/rSS jrS r\S 5       r\S 5       r	S	 r
S
 rS rS rSS jrS rS rS rS rg)zSparse block 1-norm estimator.
é    N)ÚaslinearoperatorÚ
onenormestc                 ó  • [        U 5      n U R                  S   U R                  S   :w  a  [        S5      eU R                  S   nX:¼  aï  [        R                  " [        U 5      R                  [        R                  " U5      5      5      nUR                  XU4:w  a"  [        SS[        UR                  5      -   5      e[        U5      R                  SS9nUR                  U4:w  a"  [        SS[        UR                  5      -   5      e[        R                  " U5      n[        XX5      n	USS2U4   n
Xx   nO[        X R                  X5      u  p¹p¬nU(       d  U(       a  U4nU(       a  Xé4-  nU(       a  Xê4-  nU$ U$ )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                  Ú\/home/mande/repo/quber/.venv/lib/python3.13/site-packages/scipy/sparse/linalg/_onenormest.pyr   r      sd  € ôT 	˜Ó€AØ‡w�wˆq�z�Q—W‘W˜Q‘ZÓÜÐLÓMÐMð
 	
�‰�‰
€AØƒvÜ—Z’ZÔ 0°Ó 3× :Ñ :¼2¿;º;Àq»>Ó JÓKˆ
Ø×Ñ ˜vÓ%ÜÐ.Ø'¬#¨j×.>Ñ.>Ó*?Ñ?óAð Aä˜:“×*Ñ*°Ð*Ð2ˆØ×Ñ ! Ó&ÜÐ.Ø'¬#¨l×.@Ñ.@Ó*AÑAóCð Cä—9’9˜\Ó*ˆÜ˜aÓ*ˆØ’q˜(�{Ñ#ˆØÑ$‰ä(8¸¿C¹CÀÓ(JÑ%ˆ�˜:ö –IØ�ˆÞØ�d‰NˆFÞØ�d‰NˆFØˆàˆ
ó    c                 ó   ^ ^• SmUU 4S jnU$ )z…
Decorator for an elementwise function, to apply it blockwise along
first dimension, to avoid excessive memory usage in temporaries.
é   c                 ó>  >• U R                   S   T:  a  T" U 5      $ T" U S T 5      n[        R                  " U R                   S   4UR                   SS  -   UR                  S9nXS T& A[	        TU R                   S   T5       H  nT" XUT-    5      X#UT-   & M     U$ )Nr   r   ©Údtype)r	   r   Úzerosr,   Úrange)ÚxÚy0ÚyÚjÚ
block_sizeÚfuncs       €€r&   ÚwrapperÚ%_blocked_elementwise.<locals>.wrapper€   s¡   ø€ Ø�7‰7�1‰:˜
Ó"Ù˜“7ˆNá�a˜˜�nÓ%ˆBÜ—’˜!Ÿ'™' !™*˜¨¯©°!°"¨Ñ5¸R¿X¹XÑFˆAØˆkˆzˆNØÜ˜: q§w¡w¨q¡z°:Ö>�Ù$(¨¨Q¨z©\Ð):Ó$;��A�j‘LÒ!ñ ?àˆHr'   © )r4   r5   r3   s   ` @r&   Ú_blocked_elementwiser8   y   s   ù€ ð
 €Jö
ð €Nr'   c                 óf   • U R                  5       nSXS:H  '   U[        R                  " U5      -  nU$ )a!  
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=   Ž   s0   € ð 	
�‰‹€AØ€Aˆ1�f�IØŒ�Š�‹�N€AØ€Hr'   c                 óT   • [         R                  " [         R                  " U 5      SS9$ )Nr   r   )r   Úmaxr   )r;   s    r&   Ú_max_abs_axis1r@   Ÿ   s   € ä�6Š6”"—&’&˜“) !Ñ$Ð$r'   c           	      óÀ   • SnS n[        SU R                  S   U5       H;  n[        R                  " [        R                  " XX1-    5      SS9nUc  UnM7  X$-  nM=     U$ )Nr)   r   r   )r.   r	   r   r   r   )r;   r3   Úrr2   r1   s        r&   Ú_sum_abs_axis0rC   ¤   s_   € Ø€JØ€AÜ�1�a—g‘g˜a‘j *Ö-ˆÜ�FŠF”2—6’6˜!˜a™lÐ+Ó,°1Ñ5ˆØ‰9ØŠAà‰FŠAñ .ð €Hr'   c                 ó@   • [         R                  " U [        S9nSX!'   U$ )Nr+   r   )r   r-   Úfloat)r   Úir    s      r&   r   r   °   s   € Ü
�Š�œ%Ñ €AØ€A�DØ€Hr'   c                 ó¼   • U R                   S:w  d  U R                  UR                  :w  a  [        S5      eU R                  S   n[        R                  " X5      U:H  $ )Nr   z2expected conformant vectors with entries in {-1,1}r   )Úndimr	   r
   r   Údot)r    r!   r   s      r&   Úvectors_are_parallelrJ   ¶   sJ   € ð 	‡v�v�ƒ{�a—g‘g §¡Ó(ÜÐMÓNÐNØ	�‰�‰
€AÜ�6Š6�!‹<˜1ÑÐr'   c                 óx   ^• U R                    H)  m[        U4S jUR                    5       5      (       a  M)    g   g)Nc              3   ó<   >#   • U  H  n[        TU5      v •  M     g 7f©N©rJ   ©Ú.0r!   r    s     €r&   Ú	<genexpr>Ú;every_col_of_X_is_parallel_to_a_col_of_Y.<locals>.<genexpr>Â   s   øé € Ð;²s°!Ô'¨¨1×-Ð-²sùó   ƒFT)ÚTÚany)r;   r<   r    s     @r&   Ú(every_col_of_X_is_parallel_to_a_col_of_YrV   À   s.   ø€ Ø�SŒSˆÜÔ;°q·s²sÓ;×;Ó;Ùñ ð r'   c                 óÐ   ^^• TR                   u  p4TS S 2U 4   m[        UU4S j[        U 5       5       5      (       a  gUb%  [        U4S jUR                   5       5      (       a  gg)Nc              3   óJ   >#   • U  H  n[        TTS S 2U4   5      v •  M     g 7frM   rN   )rP   r2   r;   r    s     €€r&   rQ   Ú*column_needs_resampling.<locals>.<genexpr>Í   s$   øé € Ð
>²X°Ô  1¢Q¨ T¡7×+Ð+²Xùs   ƒ #Tc              3   ó<   >#   • U  H  n[        TU5      v •  M     g 7frM   rN   rO   s     €r&   rQ   rY   Ð   s   øé € Ð7²3¨aÔ# A q×)Ð)²3ùrS   F)r	   rU   r.   rT   )rF   r;   r<   r   r   r    s    `   @r&   Úcolumn_needs_resamplingr[   Ç   sU   ù€ ð �7‰7�D€AØ	Š!ˆQˆ$‰€AÜ
Õ
>´U¸1´XÓ
>×>Ñ>ØØ�}ÜÔ7°1·3²3Ó7×7Ñ7ØØr'   c                 óv   • [         R                  R                  SSUR                  S   S9S-  S-
  US S 2U 4'   g )Nr   é   ©Úsizer   )r   ÚrandomÚrandintr	   )rF   r;   s     r&   Úresample_columnrb   Õ   s7   € Ü�i‰i×Ñ  1¨1¯7©7°1©:ÐÐ6°qÑ8¸1Ñ<€A‚aˆ€d‚Gr'   c                 óD   • [         R                  " X5      =(       d    X:  $ rM   )r   Úallclose)ÚaÚbs     r&   Úless_than_or_closerg   Ù   s   € Ü�;Š;�qÓ×' ¡Ð'r'   c           	      ó   • [        U 5      n[        U5      nUR                  S   n[        R                  " XR45      nUS:”  a2  [        R                  R                  SSXRS-
  4S9S-  S-
  USS2SS24'   U[        U5      -  nSnSnSn	[        U5      n
 [        R                  " UR                  U5      5      n[        U5      n[        R                  " U5      nUR                  5         USSS2   n[        U5      n[        R                  " UR                  U5      5      n[        U5      nU	S:¼  aA  [        [!        U5      [        R"                  " USS2U4   USS2U4   5      5      (       a   XÊ4$ [        R$                  " U5      SSS2   SU n
UU
   n[        U5       H  n['        XZU   5      USS2U4'   M     U	S:¼  aD  [        US   US   5      (       d  [)        S5      e[        US   US   5      (       d  [)        S5      eU	S:¼  a4  [        U5       H%  n[        UU   UU   5      (       a  M  [)        S	5      e   UnUnU	S-  n	GM»  )
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^   Néÿÿÿÿzinvariant (2.2) is violatedé   zinvariant (2.3) is violated)r   r	   r   Úonesr`   ra   rE   r.   r   r   rC   r   Úsortr=   r@   rg   r?   rI   Úargsortr   r   )r   ÚATr   ÚA_linear_operatorÚAT_linear_operatorr   r;   Úg_prevÚh_prevÚkÚindr<   ÚgÚbest_jÚSÚZÚhr2   s                     r&   Ú_algorithm_2_2rz   Ý   sB  € ôN )¨Ó+ÐÜ)¨"Ó-ÐØ×Ñ Ñ"€Aô 	�Š��‹€AØˆ1ƒuÜ—9‘9×$Ñ$ Q¨°°a±C°Ð$Ð9¸!Ñ;¸aÑ?ˆŠ!ˆQ‰Rˆ%‰ØŒˆq‹�M€Að €FØ€FØ	€AÜ
�‹(€CØ
Ü�JŠJÐ(×/Ñ/°Ó2Ó3ˆÜ˜1ÓˆÜ—’˜1“ˆØ	�‰ŒØ‰d�ˆd‰GˆÜ˜!ÓˆÜ�JŠJÐ)×0Ñ0°Ó3Ó4ˆÜ˜1Óˆð �‹6Ü!¤# a£&¬"¯&ª&°²1°f°9±¸qÂÀFÀ¹|Ó*L×MÑMØð2 ˆ6€Mô1 �jŠj˜‹m™D˜b˜DÑ! " 1Ð%ˆØˆc‰FˆÜ�q–ˆAÜ'¨¨q©6Ó2ˆAŠa�ˆd‹Gñ ð �‹6Ü% f¨Q¡i°¸±×;Ñ;ÜÐ =Ó>Ð>Ü% f¨Q¡i°°1±×6Ñ6ÜÐ =Ó>Ð>ð �‹6Ü˜1–X�Ü)¨!¨A©$°°q±	×:Ó:Ü#Ð$AÓBÐBñ ð
 ˆØˆØ	ˆQ‰ˆòU r'   c                 óR  • [        U 5      n[        U5      nUS:  a  [        S5      eUS:  a  [        S5      eU R                  S   nX&:¼  a  [        S5      eSnSn[        R                  " Xb4[
        S9n	US:”  ad  [        SU5       H  n
[        X©5        M     [        U5       H7  n
[        X©5      (       d  M  [        X©5        US-  n[        X©5      (       a  M"  M9     U	[        U5      -  n	[        R                  " S[        R                  S9nSn[        R                  " Xb4[
        S9nSnSn [        R                  " UR                  U	5      5      nUS-  n[        U5      n[        R                  " U5      n[        R                  " U5      nUU:”  d  US:X  a  US:¼  a  UU   nUSS2U4   nUS:¼  a
  UU::  a  UnGOÔUnUnXã:”  a  GOÉ[!        U5      nA[#        UU5      (       a  GOªUS:”  aH  [        U5       H9  n
[        X­U5      (       d  M  [        X­5        US-  n[        X­U5      (       a  M#  M;     A[        R                  " UR                  U5      5      nUS-  n[%        U5      nAUS:¼  a  [        U5      UW   :X  a  GO[        R&                  " U5      SSS	2   SU[)        U5      -    R+                  5       nAUS:”  ac  [        R,                  " USU U5      R/                  5       (       a  O�[        R,                  " Xû5      n[        R0                  " UU)    UU   45      n[        U5       H  n[3        XoU   5      U	SS2U4'   M     USU [        R,                  " USU U5      )    n[        R0                  " UU45      nUS-  nGMe  [3        UW5      nUUWXx4$ )
aj  
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+   Nri   )r   r
   r	   r   rk   rE   r.   rb   r[   r-   Úintpr   r   rC   r?   r   r=   rV   r@   rm   Úlenr:   ÚisinÚallÚconcatenater   )r   rn   r   r   ro   rp   r   r#   r$   r;   rF   Úind_histÚest_oldrw   rs   rt   r<   Úmagsr"   rv   Úind_bestr!   ÚS_oldrx   ry   Úseenr2   Únew_indr    s                                r&   r   r   D  s€  € ôR )¨Ó+ÐÜ)¨"Ó-ÐØˆqƒyÜÐ?Ó@Ð@Øˆ1ƒuÜÐ:Ó;Ð;Ø	�‰�‰
€AØƒvÜÐBÓCÐCð €FØ€Jô 	�Š��œeÑ$€Að 	ˆ1ƒuÜ�q˜!–ˆAô ˜AÖ!ñ ô �q–ˆAÜ)¨!×/Ó/Ü Ô%Ø˜a‘�
ô *¨!×/Õ/ñ ð
 Œˆq‹�M€Aä�xŠx˜¤§¡Ñ)€HØ€GÜ
�Š�!�œuÑ%€AØ	€AØ
€CØ
Ü�JŠJÐ(×/Ñ/°Ó2Ó3ˆØ�!‰ˆÜ˜aÓ ˆÜ�fŠf�T‹lˆÜ—’˜4“ˆØ�‹=˜A ›FØ�A‹vØ˜v™;�Ø’!�V�)‘ˆAà�‹6�c˜W“nØˆCÙØˆØˆØ‹9ÙÜ˜!ÓˆØä3°A°u×=Ñ=ÙØˆq‹5ô ˜1–X�Ü-¨a°E×:Ó:Ü# AÔ)Ø !‘O�Jô .¨a°E×:Õ:ñ ð ä�JŠJÐ)×0Ñ0°Ó3Ó4ˆØ�!‰ˆÜ˜1ÓˆØà�‹6”c˜!“f  (¡Ó+Ùô �jŠj˜‹m™D˜b˜DÑ!Ð"2 1¤S¨£]¡?Ð3×8Ñ8Ó:ˆØØˆq‹5ô �wŠw�s˜2˜A�w Ó)×-Ñ-×/Ñ/Øô —7’7˜3Ó)ˆDÜ—.’. # t e¡*¨c°$©iÐ!8Ó9ˆCÜ�q–ˆAÜ'¨¨q©6Ó2ˆAŠa�ˆd‹Gñ ð �b�q�'œ2Ÿ7š7 3 r¨ 7¨HÓ5Ð5Ñ6ˆÜ—>’> 8¨WÐ"5Ó6ˆØ	ˆQ‰ˆò{ ô| 	˜!˜XÓ&€AØ��1�fÐ(Ð(r'   )r]   é   FFrM   )Ú__doc__Únumpyr   Úscipy.sparse.linalgr   Ú__all__r   r8   r=   r@   rC   r   rJ   rV   r[   rb   rg   rz   r   r7   r'   r&   Ú<module>r�      s   ðñó Ý 0ð ˆ.€ôkò\ð* ñó ðð  ñ%ó ð%ò	òòòôò=ò(òdóNO)r'   