
    _Mhx<                         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/test/jupyter/venv/lib/python3.11/site-packages/scipy/sparse/linalg/_onenormest.pyr   r      s   T 	AwqzQWQZLMMM
 	

AAvvZ 0 3 3 : :2;q>> J JKK
1v%%.'#j.>*?*??A A A:***22!&&.'#l.@*A*AAC C C9\**a**qqq({#8$(8ACE(J(J%Q6:  I  	qdNF 	qdNF
    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    71:
""477Nan%%B!'!*!""5RXFFFAAkzkN:qwqz:>> < <$(D1Qz\>):$;$;!AjL.!!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Aa1fINA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1agaj*--  F26!Aa
lN+,,15559AAFAAHr)   c                 F    t          j        | t                    }d||<   |S )Nr-   r   )r   r/   float)r   ir"   s      r(   r   r      s$    
%   AAaD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{{ag((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  ;;;;qs;;;;; 	55	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:      
>
>1QQQT7++
>
>
>
>
>
>r)   Tc              3   8   K   | ]}t          |          V  d S rN   rO   rP   s     r(   rR   z*column_needs_resampling.<locals>.<genexpr>   s.      77a#Aq))777777r)   F)r   rT   r0   rS   )rG   r<   r=   r   r   r"   s    `   @r(   column_needs_resamplingrY      s     7DAq	!!!Q$A

>
>
>
>
>U1XX
>
>
>>> t}777713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1171:66q81<AaaadGGGr)   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[   NTz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1uu9$$QAaC$99!;a?!!!QRR%qMA FF	A
((C*J(//223311	dddG!J)0033441 66!#a&&"&111f9qF|*L*LMM jmmDDbD!"1"%cFq 	3 	3A'3q622AaaadGG 66%fQi;; ? =>>>%fQi166 ? =>>> 661XX C C)!A$q	:: C#$ABBBC 	QU*Z c6Mr)   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nt!          |          }~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nt          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 )++)"--qyy?@@@1uu:;;;	
AAvvBCCC FJ 	Ae$$$A 	1uuq! 	" 	"A Aq!!!!q 	  	 A)!Q//  1%%%a
 *!Q//   qMAx)))HG
!Qu%%%A	A
C=J(//2233!a  fTll4==AFFAvvv;!!!V)A66cWnnCu99!3Au== 	q55 1XX $ $-aE:: $#Aq)))!OJ .aE:: $ J)003344!166c!ff(++ jmmDDbD!"21S]]?"2388::q55 ws2A2w))--//  73))D.#te*c$i!899Cq 	3 	3A'3q622AaaadGGbqb'273rr7H5556>8W"566	Q{=| 	!X&&A1fj((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)   