
    Ui$$                         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dS )z8Test functions for the sparse.linalg._onenormest module
    N)assert_allcloseassert_equalassert_)_onenormest_core_algorithm_2_2c                   @    e Zd ZdZd Zd Zd Zd Zed             Z	dS )MatrixProductOperatorz0
    This is purely for onenormest testing.
    c                    |j         dk    s|j         dk    rt          d          |j        d         |j        d         k    rt          d          || _        || _        d| _         |j        d         |j        d         f| _        d S )N   z'expected ndarrays representing matrices   r   zincompatible shapes)ndim
ValueErrorshapeAB)selfr   r   s      /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/scipy/sparse/linalg/tests/test_onenormest.py__init__zMatrixProductOperator.__init__   s}    6Q;;!&A++FGGG71:##2333	gaj!'!*-


    c                 f    t          j        | j        t          j        | j        |                    S Nnpdotr   r   r   xs     r   _matveczMatrixProductOperator._matvec   $    vdfbfTVQ//000r   c                 f    t          j        t          j        || j                  | j                  S r   r   r   s     r   _rmatveczMatrixProductOperator._rmatvec   s$    vbfQ''000r   c                 f    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    $TVXtvx888r   N)
__name__
__module____qualname____doc__r   r   r    r#   propertyr%    r   r   r	   r	      su         . . .1 1 11 1 11 1 1 9 9 X9 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d	S )
TestOnenormestc                    t           j                            d           d}d}d}d}g }g }g }g }t          |          D ]}	t          j                            t           j                            ||                    }
t          |
|
j	        ||          \  }}}}}|
                    |           |
                    t          j                            |
d                     |
                    |           |
                    |           t          j        |t                    }t          j        |t                    }t          j        ||z
            |z  }||z  }t          dt          j        |          cxk     od	k     nc            t#          t          j        |          d           t          d
t          j        |          cxk     odk     nc            t          j        |dk               }|t          |          z  }t          d|cxk     odk     nc            t          dt          j        |          cxk     odk     nc            d S )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!677A,<QQ,N,N)CAvzOOC   OOEL--a33444f%%%!!*----8HE2228HE222&H!455@ !)8 3rw455;;;;;;;;<<< 	RVN++Q///rw~..44444444555 !/E"9::!E(OO3&--------... 	bgj))////C////00000r   c                 &   t           j                            d           d}d}d}d}g }g }g }g }t          |          D ]}	t           j                            dd||f          }
t          |
|
j        ||          \  }}}}}|                    |           |                    t          j	        
                    |
d	                     |                    |           |                    |           t          j        |t          
          }t          j        |t          
          }t          j        ||z
            |z  }||z  }t          dt          j        |          cxk     odk     nc            t!          t          j        |          d           t          j        |dk               }|t          |          z  }t          d|cxk     odk     nc            t          dt          j        |          cxk     odk     nc            d S )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!55A,<QQ,N,N)CAvzOOC   OOEL--a33444f%%%!!*----8HE2228HE222&H!455@ !)8 3rw455<<<<<<<<=== 	RVN++Q/// !/E"9::!E(OO3'....$..../// 	bgj))////C////00000r   c                     d}d}d}dt           j                            t          j        |          t          j        |d          z  z              }t          j        dgdg|dz
  z  z             }t          j        fdt          |          D                       }t           j                            ||           }t          ||           t          ||j        ||          \  }}	}
}}t           j                            |d          }||z  }t          |dd	
           t          |d           t          |d           t           j        j                            |||          }t          ||           d S )Nr   r0   r1   g!?)kr   c                     g | ]} |z  	S r+   r+   ).0rS   alphas     r   
<listcomp>z>TestOnenormest.test_onenormest_table_5_t_1.<locals>.<listcomp>   s    <<<avk<<<r   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   r   	first_col	first_rowr   rT   rU   rV   rW   rX   exact_valueunderest_ratio	est_plainrh   s                   @r   test_onenormest_table_5_t_1z*TestOnenormest.test_onenormest_table_5_t_1z   sk   \bk!nnuRVA^^^/CCDDDHaSA3!9_--	H<<<<588<<<==	\""9i8881(8ACE(J(J%Q6:l''1--{*48888VR   Z###L'2212GG	Y'''''r   c                    t           j                            d           d}d}d}d}g }g }g }g }t          |          D ]}	t           j                            ||          dt           j                            ||          z  z   }
t
          j                            |
          }t          ||j	        ||          \  }}}}}|
                    |           |
                    t
          j                            |d                     |
                    |           |
                    |           t          j        |t                    }t          j        |t                    }t          j        ||z
            |z  }||z  }t          j        |          }t!          d	|cxk     od
k     nc            t          j        |          }t%          |d           t          j        |dk               }|t          |          z  }t!          d|cxk     odk     nc            t          j        |          }t!          d|cxk     odk     nc            d S )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   sY    		t
x 	. 	.AINN1a((2	q!0D0D+DDE  ''A,<QQ,N,N)CAvzOOC   OOEL--a33444f%%%!!*----8HE2228HE222&H!455@ !)8 3%'W-B%C%C"18888D8888999 //^Q''' !/E"9::!E(OO3&,,,,,,,,--- WZ((
J""""""""#####r   c                 l    t          j        ||          }t          j                            |d          S )Nr   )r   r   r>   r?   rC   )r   r   r   Cs       r   _help_product_norm_slowz&TestOnenormest._help_product_norm_slow   s)    F1aLL|  A&&&r   c                 h    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ACE(J(J%Q6:
r   c                 v   t           j                            d           d}d}t           j                            ||          }t           j                            ||          }|                     ||          }|                     ||          }t          ||cxk    od|z  k    nc d|dd|d           d S )Nr/   ip     zfast: gz
exact:)r   r;   r<   rA   r   r   r   )r   rL   re   r   r   fast_estimatert   s          r   test_onenormest_linear_operatorz.TestOnenormest.test_onenormest_linear_operator   s     		tIOOAq!!IOOAq!!44Q::221a88????-????AAAA+AAA	C 	C 	C 	C 	Cr   c                    t           j                            d           t          j                            ddd          }t          j                            |                                d          }t          j        j        	                    |d          \  }}t          j        j        	                    |d          \  }}t          j        j        	                    |dd          \  }}}	t          ||d	
           t          t           j                            |                    |          d          |t           j                            |d          z  d	
           t          |                    |          |d	
           d S )Nr/   2   g?r   T)	compute_v)	compute_w)r   r   g&.>rj   )r   r;   r<   r>   rp   rz   r?   rC   toarrayrq   r   r   )
r   r   s0s1rU   s2rV   s3v2w2s
             r   test_returnszTestOnenormest.test_returns   s5   
	tLb"c**\qyy{{A..#..qD.AAA#..qD.AAA\(33AQU3VV
BBT****	quuQxx33R	q!8L8L5LSWXXXXa!$//////r   N)r&   r'   r(   pytestmarkxslowr]   rc   rw   r   r   r   slowr   r   r+   r   r   r-   r-   )   s        [%1 %1 %1N [$1 $1 $1L( ( (* [+$ +$ +$Z' ' '
   [C C C 0 0 0 0 0r   r-   c                   8    e Zd Zej        j        d             ZdS )TestAlgorithm_2_2c                 `   t           j                            d          }d}d}t          |          D ]z}|                    dd          }|                    dd          }t
          j                            |                    ||                    }t          ||j
        |          \  }}{d S )Nr/      r0   r   ry   
   )   )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    i##D))x 	/ 	/A Aq!!A B##A   1a11A $AqsA..FAss	/ 	/r   N)r&   r'   r(   r   r   thread_unsafer   r+   r   r   r   r      s3        [/ / / / /r   r   )r)   numpyr   numpy.testingr   r   r   r   scipy.linalgr>   scipy.sparse.linalgscipy.sparse.linalg._onenormestr   r   rp   r?   LinearOperatorr	   r-   r   r+   r   r   <module>r      s	        @ @ @ @ @ @ @ @ @ @          L L L L L L L L9 9 9 9 9EL/> 9 9 9:}0 }0 }0 }0 }0 }0 }0 }0@/ / / / / / / / / /r   