
    Cpj.                         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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                  [/var/www/html/pdf-tiff/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_columnra      s7    ii11771:6q81<AadGr'   c                 D    [         R                  " X5      =(       d    X:  $ rM   )r   allclose)abs     r&   less_than_or_closerf      s    ;;q''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+   N)r   r
   r	   r   onesrE   r.   ra   r[   r-   intpr   r   rC   r?   r   r=   rV   r@   argsortlenr:   isinallconcatenater   )r   ATr   r   A_linear_operatorAT_linear_operatorr   r#   r$   r;   rF   ind_histest_oldSkindr<   magsr"   best_jind_bestr!   S_oldZhseenr2   new_indr    s                                r&   r   r      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[   ra   rf   r   r7   r'   r&   <module>r      sx     0 .k\*    % %	=(O)r'   