
    (phx<                         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  ppnU(       d  U(       a  U4nU(       a  X4-  nU(       a  X4-  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                  R/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/_onenormest.pyr   r      sd   T 	AwwqzQWWQZLMM
 	

AvZZ 0 3 : :2;;q> JK
v%.'#j.>.>*??A A:***2!&.'#l.@.@*AAC C99\*a*q({#$(8CC(J%: IdNFdNF
    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    771:
"7Nan%B!''!*!"5RXXFAkzN:qwwqz:>$(Qz\):$;AjL! ?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A1fINAHr'   c                 T    [         R                  " [         R                  " U 5      SS9$ )Nr   r   )r   maxr   )r;   s    r&   _max_abs_axis1r@      s    66"&&)!$$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1aggaj*-FF266!al+,159AFA . Hr'   c                 @    [         R                  " U [        S9nSX!'   U$ )Nr+   r   )r   r-   float)r   ir    s      r&   r   r      s    
% AAD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     	vv{agg(MNN	
A66!<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.    SS;qss;;;  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1QT7++Xs    #Tc              3   <   >#    U  H  n[        TU5      v   M     g 7frM   rN   rO   s     r&   rQ   rY      s     73a#Aq))3rS   F)r	   rU   r.   rT   )rF   r;   r<   r   r   r    s    `   @r&   column_needs_resamplingr[      sU     77DA	!Q$A

>U1X
>>>}7133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    ii11771:6q81<AadGr'   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   X4$ [        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^   Nz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1u99$$QaC$9!;a?!QR%qMA FF	A
(C
JJ(//2311	ddG!JJ)00341 6!#a&"&&1f9qF|*LMM2 6M1 jjmDbD!"1%cFqA'q62AadG  6%fQi;; =>>%fQi166 =>> 61X)!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
[        X5        M     [        U5       H7  n
[        X5      (       d  M  [        X5        US-  n[        X5      (       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OUnUnX:  a  GO[!        U5      nA[#        UU5      (       a  GOUS:  aH  [        U5       H9  n
[        XU5      (       d  M  [        X5        US-  n[        XU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,                  " X5      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 )+)"-qy?@@1u:;;	
AvBCC FJ 	e$A 	1uq!A A!  qA)!//%a
 *!// 
 qMAxx)HG
!u%A	A
C
JJ(//23!a ffTl4=AFAvv;!V)A6cWnC9!3Au==q5 1X-aE::#A)!OJ .aE::  JJ)0034!16c!f(+ jjmDbD!"21S]?388:q5 wws2Aw)--// 773)D..#te*c$i!89CqA'q62AadG  bq'2773r7H556>>8W"56	Q{ | 	!X&A1f((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'   