
    -i$$                         S r SSKrSSKJrJrJr  SSKrSSKr	SSK
r	SSKJrJr   " S S\	R                  R                  R                   5      r " S S5      r " S	 S
5      rg)z8Test functions for the sparse.linalg._onenormest module
    N)assert_allcloseassert_equalassert_)_onenormest_core_algorithm_2_2c                   @    \ rS rSrSrS rS rS rS r\	S 5       r
Srg	)
MatrixProductOperator   z(
This is purely for onenormest testing.
c                    UR                   S:w  d  UR                   S:w  a  [        S5      eUR                  S   UR                  S   :w  a  [        S5      eXl        X l        SU l         UR                  S   UR                  S   4U l        g )N   z'expected ndarrays representing matrices   r   zincompatible shapes)ndim
ValueErrorshapeAB)selfr   r   s      \/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/linalg/tests/test_onenormest.py__init__MatrixProductOperator.__init__   st    66Q;!&&A+FGG771:#233	ggaj!''!*-
    c                     [         R                  " U R                  [         R                  " U R                  U5      5      $ Nnpdotr   r   r   xs     r   _matvecMatrixProductOperator._matvec   &    vvdffbffTVVQ/00r   c                     [         R                  " [         R                  " XR                  5      U R                  5      $ r   r   r   s     r   _rmatvecMatrixProductOperator._rmatvec   s$    vvbffQ'00r   c                     [         R                  " U R                  [         R                  " U R                  U5      5      $ r   r   )r   Xs     r   _matmatMatrixProductOperator._matmat!   r!   r   c                 j    [        U R                  R                  U R                  R                  5      $ r   )r	   r   Tr   )r   s    r   r*   MatrixProductOperator.T$   s    $TVVXXtvvxx88r   )r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r#   r'   propertyr*   __static_attributes__ r   r   r	   r	      s/    .111 9 9r   r	   c                      \ rS rSr\R
                  R                  S 5       r\R
                  R                  S 5       rS r	\R
                  R                  S 5       r
S rS r\R
                  R                  S 5       rS	 rS
rg)TestOnenormest)   c                    [         R                  R                  S5        SnSnSnSn/ n/ n/ n/ n[        U5       H  n	[        R
                  R                  [         R                  R                  X"5      5      n
[        XR                  X5      u  ppnUR                  U5        UR                  [        R
                  R                  U
S5      5        UR                  U5        UR                  U5        M     [         R                  " U[        S9n[         R                  " U[        S9n[         R                  " XV-
  5      U-  nXV-  n[        S[         R                   " U5      s=:  =(       a    S	:  Os  5        [#        [         R$                  " U5      S5        [        S
[         R                   " U5      s=:  =(       a    S:  Os  5        [         R&                  " US:  5      nU[        U5      -  n[        SUs=:  =(       a    S:  Os  5        [        S[         R                   " U5      s=:  =(       a
    S:  5        g s  5        g )N  r   d        r   dtypeGz?g      ?皙?g?+=?gffffff?      @      @)r   randomseedrangescipylinalginvrandnr   r*   appendnormarrayfloatabsr   meanr   maxcount_nonzeror   tnitmaxnsamplesobservedexpected
nmult_listnresample_listir   estvwnmults
nresamplesrelative_errorsunderestimation_rationexactproportion_exacts                       r   test_onenormest_table_3_t_2*TestOnenormest.test_onenormest_table_3_t_2+   s    			t
xA  !67A,<QQ,N)CAzOOC OOELL--a34f%!!*- ! 88HE288HE2&&!45@ !) 3rww45;;;< 	RVVN+Q/rww~.4445 !!/E"9:!E(O3&---. 	bggj)//C/0/0r   c                    [         R                  R                  S5        SnSnSnSn/ n/ n/ n/ n[        U5       H  n	[         R                  R	                  SSX"4S9n
[        XR                  X5      u  ppnUR                  U5        UR                  [        R                  R                  U
S	5      5        UR                  U5        UR                  U5        M     [         R                  " U[        S
9n[         R                  " U[        S
9n[         R                  " XV-
  5      U-  nXV-  n[        S[         R                  " U5      s=:  =(       a    S:  Os  5        [!        [         R"                  " U5      S5        [         R$                  " US:  5      nU[        U5      -  n[        SUs=:  =(       a    S:  Os  5        [        S[         R                  " U5      s=:  =(       a
    S:  5        g s  5        g )Nr8      r9   r:   r;   r   )sizer   r<   rA   r>   r   r@   g333333?g      ?rB   rC   )r   rD   rE   rF   randintr   r*   rK   rG   rH   rL   rM   rN   rO   r   rP   r   rQ   rR   rS   s                       r   test_onenormest_table_4_t_7*TestOnenormest.test_onenormest_table_4_t_7S   s    			t
xA		!!"aqf!5A,<QQ,N)CAzOOC OOELL--a34f%!!*- ! 88HE288HE2&&!45@ !) 3rww45<<<= 	RVVN+Q/ !!/E"9:!E(O3'..$./ 	bggj)//C/0/0r   c           	         SnSnSnSn[         R                  R                  [        R                  " U5      U[        R
                  " USS9-  -   5      * n[        R                  " S/S/US-
  -  -   5      n[        R                  " [        U5       Vs/ s H  ot* U-  PM
     sn5      n[         R                  R                  Xh5      * n	[        XY5        [        XR                  X5      u  ppn[         R                  R                  U	S5      nX-  n[        USSS	9  [        US
5        [        US5        [         R                  R                  R                  XUS9n[        U
U5        g s  snf )Nr   r9   r:   g!?)kr   r?   g-C6?rtol   )rT   rV   )rG   rH   rI   r   identityeyerM   rF   toeplitzr   r   r*   rL   r   sparse
onenormest)r   rT   rU   rV   alphar   	first_colr\   	first_rowr   r]   r^   r_   r`   ra   exact_valueunderest_ratio	est_plains                     r   test_onenormest_table_5_t_1*TestOnenormest.test_onenormest_table_5_t_1z   s.   \\bkk!nuRVVA^/CCDDHHaSA3!9_-	HH58<8avk8<=	\\""988(8CC(J%:ll''1-*48VR Z#LL''2212G	Y' =s   E+c                    [         R                  R                  S5        SnSnSnSn/ n/ n/ n/ n[        U5       H  n	[         R                  R	                  X"5      S[         R                  R	                  X"5      -  -   n
[
        R                  R                  U
5      n[        XR                  X5      u  ppnUR                  U5        UR                  [
        R                  R                  US5      5        UR                  U5        UR                  U5        M     [         R                  " U[        S9n[         R                  " U[        S9n[         R                  " XV-
  5      U-  nXV-  n[         R                  " U5      n[!        S	Us=:  =(       a    S
:  Os  5        [         R"                  " U5      n[%        US5        [         R&                  " US:  5      nU[        U5      -  n[!        SUs=:  =(       a    S:  Os  5        [         R                  " U5      n[!        SUs=:  =(       a
    S:  5        g s  5        g )Nr8   r   r9   r:   r;   y              ?r   r<   rA   r>   r   r@   gffffff?g?   )r   rD   rE   rF   randrG   rH   rI   r   r*   rK   rL   rM   rN   rO   rP   r   rQ   r   rR   )r   rT   rU   rV   rW   rX   rY   rZ   r[   r\   A_invr   r]   r^   r_   r`   ra   rb   rc   underestimation_ratio_meanmax_nresamplesrd   re   
mean_nmults                           r   test_onenormest_table_6_t_2*TestOnenormest.test_onenormest_table_6_t_2   s    			t
xAIINN1(2		q0D+DDE  'A,<QQ,N)CAzOOC OOELL--a34f%!!*- ! 88HE288HE2&&!45@ !) 3%'WW-B%C"188D89 /^Q' !!/E"9:!E(O3&,,,- WWZ(
J"""#"#r   c                 n    [         R                  " X5      n[        R                  R	                  US5      $ )Nr   )r   r   rG   rH   rL   )r   r   r   Cs       r   _help_product_norm_slow&TestOnenormest._help_product_norm_slow   s&    FF1L||  A&&r   c                 X    SnSn[        X5      n[        XUR                  X45      u  pgpn
U$ )Nr   r:   )r	   r   r*   )r   r   r   rT   rV   Dr]   r^   r_   r`   ra   s              r   _help_product_norm_fast&TestOnenormest._help_product_norm_fast   s2    !!'(8CC(J%:
r   c                 \   [         R                  R                  S5        SnSn[         R                  R                  X5      n[         R                  R                  X!5      nU R	                  X45      nU R                  X45      n[        XVs=:*  =(       a    SU-  :*  Os  SUS SUS 35        g )Nr8   ip     zfast: gz
exact:)r   rD   rE   rJ   r   r   r   )r   rU   rp   r   r   fast_estimater|   s          r   test_onenormest_linear_operator.TestOnenormest.test_onenormest_linear_operator   s     			tIIOOA!IIOOA!44Q:2218??-?q)+aA	Cr   c                    [         R                  R                  S5        [        R                  R                  SSS5      n[        R                  R                  UR                  5       S5      n[        R                  R                  R                  USS9u  p4[        R                  R                  R                  USS9u  pV[        R                  R                  R                  USSS9u  pxn	[        X2S	S
9  [        [         R                  R                  UR                  U5      S5      U[         R                  R                  US5      -  S	S
9  [        UR                  U5      US	S
9  g )Nr8   2   g?r   T)	compute_v)	compute_w)r   r   g&.>rq   )r   rD   rE   rG   rw   r   rH   rL   toarrayrx   r   r   )
r   r   s0s1r^   s2r_   s3v2w2s
             r   test_returnsTestOnenormest.test_returns   s   
		tLLb"c*\\qyy{A.##..qD.A##..qD.A\\((33AQU3V
T*		quuQx3R		q!8L5LSWXa!$/r   r3   N)r,   r-   r.   r/   pytestmarkxslowrf   rm   r   r   r   r   slowr   r   r2   r3   r   r   r5   r5   )   s    [[%1 %1N [[$1 $1L(* [[+$ +$Z'
 [[C C 0r   r5   c                   L    \ rS rSr\R
                  R                  S 5       rSrg)TestAlgorithm_2_2   c                 B   [         R                  R                  S5      nSnSn[        U5       Hm  nUR	                  SS5      nUR	                  SS5      n[
        R                  R                  UR                  X"5      5      n[        XfR                  U5      u  pxMo     g )Nr8      r9   r   r   
   r6   )r   rD   RandomStaterF   rl   rG   rH   rI   rJ   r   r*   )	r   rngrU   rW   r\   rT   r   r   inds	            r   test_randn_inv TestAlgorithm_2_2.test_randn_inv   s    ii##D)xA Aq!A B#A   11A $AssA.FAs !r   r3   N)	r,   r-   r.   r/   r   r   thread_unsafer   r2   r3   r   r   r   r      s    [[/ /r   r   )r0   numpyr   numpy.testingr   r   r   r   scipy.linalgrG   scipy.sparse.linalgscipy.sparse.linalg._onenormestr   r   rw   rH   LinearOperatorr	   r5   r   r3   r   r   <module>r      sT     @ @    L9ELL//>> 9:}0 }0@/ /r   