
    g$$                         d Z ddlZddlmZmZmZ ddlZddlZ	ddl
Z	ddlmZmZ  G d de	j                  j                  j                         Z G d d      Z G d	 d
      Zy)z8Test functions for the sparse.linalg._onenormest module
    N)assert_allcloseassert_equalassert_)_onenormest_core_algorithm_2_2c                   8    e Zd ZdZd Zd Zd Zd Zed        Z	y)MatrixProductOperatorz0
    This is purely for onenormest testing.
    c                    |j                   dk7  s|j                   dk7  rt        d      |j                  d   |j                  d   k7  rt        d      || _        || _        d| _         |j                  d   |j                  d   f| _        y )N   z'expected ndarrays representing matrices   r   zincompatible shapes)ndim
ValueErrorshapeAB)selfr   r   s      \/var/www/html/venv/lib/python3.12/site-packages/scipy/sparse/linalg/tests/test_onenormest.py__init__zMatrixProductOperator.__init__   sx    66Q;!&&A+FGG771:#233	ggaj!''!*-
    c                 ~    t        j                  | j                  t        j                  | j                  |            S Nnpdotr   r   r   xs     r   _matveczMatrixProductOperator._matvec   &    vvdffbffTVVQ/00r   c                 ~    t        j                  t        j                  || j                        | j                        S r   r   r   s     r   _rmatveczMatrixProductOperator._rmatvec   s&    vvbffQ'00r   c                 ~    t        j                  | j                  t        j                  | j                  |            S r   r   )r   Xs     r   _matmatzMatrixProductOperator._matmat!   r   r   c                 j    t        | j                  j                  | j                  j                        S r   )r	   r   Tr   )r   s    r   r%   zMatrixProductOperator.T$   s    $TVVXXtvvxx88r   N)
__name__
__module____qualname____doc__r   r   r    r#   propertyr%    r   r   r	   r	      s/    .111 9 9r   r	   c                      e Zd Zej                  j
                  d        Zej                  j
                  d        Zd Zej                  j
                  d        Z	d Z
d Zej                  j                  d        Zd Zy	)
TestOnenormestc                 v   t         j                  j                  d       d}d}d}d}g }g }g }g }t        |      D ]  }	t        j
                  j                  t         j                  j                  ||            }
t        |
|
j                  ||      \  }}}}}|j                  |       |j                  t        j
                  j                  |
d             |j                  |       |j                  |        t        j                  |t              }t        j                  |t              }t        j                  ||z
        |z  }||z  }t        dt        j                   |      cxk  xr d	k  nc        t#        t        j$                  |      d       t        d
t        j                   |      cxk  xr dk  nc        t        j&                  |dk        }|t        |      z  }t        d|cxk  xr dk  nc        t        dt        j                   |      cxk  xr
 dk         y c        y )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_2z*TestOnenormest.test_onenormest_table_3_t_2+   s    			t
x 	.A  A!67A,<QQ,N)CAvzOOC OOELL--a34f%!!*-	. 88HE288HE2&&H!45@ !)8 3rww45;;< 	RVVN+Q/rww~.445 !!/E"9:!E(O3&--. 	bggj)/C/0/0r   c                    t         j                  j                  d       d}d}d}d}g }g }g }g }t        |      D ]  }	t         j                  j	                  dd||f      }
t        |
|
j                  ||      \  }}}}}|j                  |       |j                  t        j                  j                  |
d	             |j                  |       |j                  |        t        j                  |t        
      }t        j                  |t        
      }t        j                  ||z
        |z  }||z  }t        dt        j                  |      cxk  xr dk  nc        t!        t        j"                  |      d       t        j$                  |dk        }|t        |      z  }t        d|cxk  xr dk  nc        t        dt        j                  |      cxk  xr
 dk         y c        y )Nr/      r0   r1   r2   r   )sizer   r3   r8   r5   r   r7   g333333?g      ?r9   r:   )r   r;   r<   r=   randintr   r%   rB   r>   r?   rC   rD   rE   rF   r   rG   r   rH   rI   rJ   s                       r   test_onenormest_table_4_t_7z*TestOnenormest.test_onenormest_table_4_t_7S   s    			t
x 	.A		!!"aq!f!5A,<QQ,N)CAvzOOC OOELL--a34f%!!*-	. 88HE288HE2&&H!45@ !)8 3rww45<<= 	RVVN+Q/ !!/E"9:!E(O3'.$./ 	bggj)/C/0/0r   c                    d}d}d}d}t         j                  j                  t        j                  |      |t        j
                  |d      z  z          }t        j                  dgdg|dz
  z  z         }t        j                  t        |      D cg c]  }| |z  
 c}      }t         j                  j                  ||       }	t        ||	       t        |	|	j                  ||      \  }
}}}}t         j                  j                  |	d      }|
|z  }t        |dd	       t        |d
       t        |d       t         j                  j                  j                  |	||      }t        |
|       y c c}w )Nr   r0   r1   g!?)kr   r6   g-C6?rtol   )rK   rM   )r>   r?   r@   r   identityeyerD   r=   toeplitzr   r   r%   rC   r   sparse
onenormest)r   rK   rL   rM   alphar   	first_colrS   	first_rowr   rT   rU   rV   rW   rX   exact_valueunderest_ratio	est_plains                     r   test_onenormest_table_5_t_1z*TestOnenormest.test_onenormest_table_5_t_1z   s<   \\bkk!nuRVVA^/CCDDHHaSA3!9_-	HH58<avk<=	\\""9i881(8ACCE(J%Q6:ll''1-{*48VR Z#LL''2212G	Y' =s   E6c                 v   t         j                  j                  d       d}d}d}d}g }g }g }g }t        |      D ]  }	t         j                  j	                  ||      dt         j                  j	                  ||      z  z   }
t
        j                  j                  |
      }t        ||j                  ||      \  }}}}}|j                  |       |j                  t
        j                  j                  |d             |j                  |       |j                  |        t        j                  |t              }t        j                  |t              }t        j                  ||z
        |z  }||z  }t        j                  |      }t!        d	|cxk  xr d
k  nc        t        j"                  |      }t%        |d       t        j&                  |dk        }|t        |      z  }t!        d|cxk  xr dk  nc        t        j                  |      }t!        d|cxk  xr
 dk         y c        y )Nr/   r   r0   r1   r2   y              ?r   r3   r8   r5   r   r7   gffffff?g?   )r   r;   r<   r=   randr>   r?   r@   r   r%   rB   rC   rD   rE   rF   rG   r   rH   r   rI   )r   rK   rL   rM   rN   rO   rP   rQ   rR   rS   A_invr   rT   rU   rV   rW   rX   rY   rZ   underestimation_ratio_meanmax_nresamplesr[   r\   
mean_nmults                           r   test_onenormest_table_6_t_2z*TestOnenormest.test_onenormest_table_6_t_2   s    			t
x 	.AIINN1a(2		q!0D+DDE  'A,<QQ,N)CAvzOOC OOELL--a34f%!!*-	. 88HE288HE2&&H!45@ !)8 3%'WW-B%C"18D89 /^Q' !!/E"9:!E(O3&,,- WWZ(
J""#"#r   c                 n    t        j                  ||      }t        j                  j	                  |d      S )Nr   )r   r   r>   r?   rC   )r   r   r   Cs       r   _help_product_norm_slowz&TestOnenormest._help_product_norm_slow   s(    FF1aL||  A&&r   c                 b    d}d}t        ||      }t        ||j                  ||      \  }}}}	}
|S )Nr   r1   )r	   r   r%   )r   r   r   rK   rM   DrT   rU   rV   rW   rX   s              r   _help_product_norm_fastz&TestOnenormest._help_product_norm_fast   s<    !!Q'(8ACCE(J%Q6:
r   c                 \   t         j                  j                  d       d}d}t         j                  j                  ||      }t         j                  j                  ||      }| j	                  ||      }| j                  ||      }t        ||cxk  xr d|z  k  nc d|dd|d       y )Nr/   ip     zfast: gz
exact:)r   r;   r<   rA   r   r   r   )r   rL   re   r   r   fast_estimaterq   s          r   test_onenormest_linear_operatorz.TestOnenormest.test_onenormest_linear_operator   s     			tIIOOAq!IIOOAq!44Q:221a8?-?q)+aA	Cr   c                    t         j                  j                  d       t        j                  j                  ddd      }t        j                  j                  |j                         d      }t        j                  j                  j                  |d      \  }}t        j                  j                  j                  |d      \  }}t        j                  j                  j                  |dd      \  }}}	t        ||d	
       t        t         j                  j                  |j                  |      d      |t         j                  j                  |d      z  d	
       t        |j                  |      |d	
       y )Nr/   2   g?r   T)	compute_v)	compute_w)r   r   g&.>rf   )r   r;   r<   r>   rl   rw   r?   rC   toarrayrm   r   r   )
r   r   s0s1rU   s2rV   s3v2w2s
             r   test_returnszTestOnenormest.test_returns   s   
		tLLb"c*\\qyy{A.##..qD.AA##..qD.AA\\((33AQU3V
BBT*		quuQx3R		q!8L5LSWXa!$/r   N)r&   r'   r(   pytestmarkxslowr]   rc   rt   r|   r   r   slowr   r   r+   r   r   r-   r-   )   s    [[%1 %1N [[$1 $1L(* [[+$ +$Z'
 [[C C 0r   r-   c                   D    e Zd Zej                  j
                  d        Zy)TestAlgorithm_2_2c                 D   t         j                  j                  d      }d}d}t        |      D ]o  }|j	                  dd      }|j	                  dd      }t
        j                  j                  |j                  ||            }t        ||j                  |      \  }}q y )Nr/      r0   r   rv   
   )   )r   r;   RandomStater=   rb   r>   r?   r@   rA   r   r%   )	r   rngrL   rN   rS   rK   r   r   inds	            r   test_randn_invz TestAlgorithm_2_2.test_randn_inv   s    ii##D)x 	/A Aq!A B#A   1a1A $AqssA.FAs	/r   N)r&   r'   r(   r   r   thread_unsafer   r+   r   r   r   r      s    [[/ /r   r   )r)   numpyr   numpy.testingr   r   r   r   scipy.linalgr>   scipy.sparse.linalgscipy.sparse.linalg._onenormestr   r   rl   r?   LinearOperatorr	   r-   r   r+   r   r   <module>r      sT     @ @    L9ELL//>> 9:}0 }0@/ /r   