o
    Z۷i@M                     @   s  d dl Z d dlmZ d dlZd dlmZ d dlmZ	 d dl
mZmZ d dlmZmZmZmZ d dlmZ dd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd'dd ZG d!d" d"eeZ G d#d$ d$e Z!G d%d& d&eeZ"dS )(    N)product)
polynomial)	polyutils)jitnjit)TestCaseneeds_lapackEnableNRTStatsMixinMemoryLeakMixin)TypingErrorc                 C   
   t | S N)nproots)p r   Q/home/ubuntu/vllm_env/lib/python3.10/site-packages/numba/tests/test_polynomial.pyroots_fn      
r   c                 C      t | |S r   )polypolyaddc1c2r   r   r   r         r   c                 C   r   r   )r   polysubr   r   r   r   r      r   r   c                 C   r   r   )r   polymulr   r   r   r   r      r   r   c                 C   r   r   )putrimseq)seqr   r   r   r      r   r   c                 C      t | }|S r   r   	as_series)aresr   r   r   polyasseries1"      
r&   c                 C      t | |}|S r   r"   )r$   trimr%   r   r   r   polyasseries2'      r*   c                 C   r(   r   )r   polydiv)r   r   r%   r   r   r   r,   ,   r+   r,   c                 C   r(   r   r   polyvalxcr%   r   r   r   polyval21   r+   r2   c                 C      t | |d}|S )NTr-   r/   r   r   r   	polyval3T6      r4   c                 C   r3   )NFr-   r/   r   r   r   	polyval3F;   r5   r6      c                 C   r(   r   )r   polyint)r1   mr%   r   r   r   r8   @   r+   r8   c                       sH   e Zd ZdZejejejejfZ	 fddZ
efddZdd Z  ZS )TestPolynomialBasezV
    Provides setUp and common data/error modes for testing polynomial functions.
    c                    s   t   tt|   d S r   )gccollectsuperr:   setUpself	__class__r   r   r>   M   s   zTestPolynomialBase.setUpc                 C   sH   |  |}||  W d    n1 sw   Y  | |t|j d S r   )assertRaisesassertInstr	exception)r@   cfuncargsmsgerrraisesr   r   r   assert_errorR   s   
zTestPolynomialBase.assert_errorc                 C   s   d}|  ||| d S )NzInput must be a 1d array.rL   )r@   rG   rH   rI   r   r   r   assert_1d_inputW   s   z"TestPolynomialBase.assert_1d_input)__name__
__module____qualname____doc__r   float64float32
complex128	complex64dtypesr>   
ValueErrorrL   rN   __classcell__r   r   rA   r   r:   E   s    r:   c                   @   s    e Zd Zdd Zedd ZdS )
TestPoly1Dc                 C   s   |d }|  ||| d S )Nz+() argument must not cause a domain change.rM   )r@   namerG   rH   rI   r   r   r   assert_no_domain_change^   s   z"TestPoly1D.assert_no_domain_changec              
      s  t ddt ttjj fdd}tdgtg dtg dtg dtg d	tg d
tg df}t|tjtj	gt
j D ]\}}||}|| qOtdgtg dtg dtg dtg dtg dtg df}t|jdd  D ]\}}||}|| q tdddf tg d}d |f  |tj d S )NT)nopythonc                    s   t | fi |} | fi |}| jjv rt| jj}n}tjj||d| d| d    | fi | W d    d S 1 sFw   Y  d S )N
   d   )rtolatol)	r   dtyperW   r   finfo
resolutiontestingassert_allcloseassertNoNRTLeak)r$   kwargsexpectedgotrd   rG   default_resolutionr@   r   r   checki   s   
"z$TestPoly1D.test_roots.<locals>.checkr7   )r7         )r   r   r   )r7         rp   )r   r   r   r7   rn   ro   )r7   r7   r   r   r   )r   r   r7   r   r   r         ?      ?)r7   y      @      ?ro   )r   y                r   )r7   y      @      ?rq   rp   )r   r   r   rr   rn   ro   )rr   r7   r   r   r   )r   r   rr   r   r   r   ro   g      @)g      @       @              ?eigvals)r   r   r   rc   rS   rd   arrayr   int32int64listrW   astyperN   arangereshaper\   rU   )r@   rm   	r_vectorsvrb   r$   	c_vectorsr0   r   rk   r   
test_rootsb   s<   






zTestPoly1D.test_rootsN)rO   rP   rQ   r\   r   r   r   r   r   r   rZ   \   s    rZ   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd+d
dZdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*S ),TestPolynomialc                 C   s8   t }tt }dd }| D ]}| |||| qd S )Nc                  s   s,    t dD ]} tdgdg|   V  qd S )N   r7   r   ranger   rw   )ir   r   r   inputs   s   z1TestPolynomial.test_trimseq_basic.<locals>.inputs)r   r   assertPreciseEqual)r@   pyfuncrG   r   coefsr   r   r   test_trimseq_basic   s   
z!TestPolynomial.test_trimseq_basicc                 C   s   t t}|   | t}|d W d    n1 sw   Y  | dt|j | t}|t	d
dd W d    n1 sEw   Y  | dt|j | t}|d W d    n1 sgw   Y  | dt|j d S )	Nabcz%The argument "seq" must be array-liker^   r   ro   Coefficient array is not 1-dr7   ro   rn   r   z6Unsupported type UniTuple(int64, 4) for argument "seq")r   r   disable_leak_checkrC   r   rD   rE   rF   r   r|   r}   )r@   rG   rK   er   r   r   test_trimseq_exception   s(   

z%TestPolynomial.test_trimseq_exceptionc                 C   st   t }tt }t}tt}dd }| D ]$}| |||| | ||d||d | ||d||d qd S )Nc                   s   s    t dV  t ddV  dt dt jdt jdfV  g dg d	fV  d
V  dV  dddgfV  g dfV  dV  t g dfV  t g dt g dgV  t g d	gV  d S )N   rp   )ro   rn   r7   rn   ro   rb   )r7   ro   rn   r   r   r7   ro   rn   )r   r   MbP?r   gh㈵>r   r   )r7   ro   rn   r   r   rp      )r   )              ?ro                 @y              @r   y              @r   g?rt   r   )r   )r   r|   r}   rT   rw   r   r   r   r   r      s   z6TestPolynomial.test_pu_as_series_basic.<locals>.inputsFT)r&   r   r*   r   )r@   pyfunc1cfunc1pyfunc2cfunc2r   inputr   r   r   test_pu_as_series_basic   s   
z&TestPolynomial.test_pu_as_series_basicc                 C   s  t t}t t}|   | t}|d W d    n1 s w   Y  | dt|j | t}|dd W d    n1 sCw   Y  | dt|j | t}|t	
dd W d    n1 siw   Y  | dt|j | t}|g dt	
dddf W d    n1 sw   Y  | dt|j | t}|t	
d	d
 W d    n1 sw   Y  | dt|j | t}|t	g dg dgg W d    n1 sw   Y  | dt|j | t}|t	jg gt	jd W d    n	1 sw   Y  | dt|j | t}|g dt	jg t	jdt	g df W d    n	1 sGw   Y  | dt|j d S )Nr   z'The argument "alist" must be array-likeTr   z#The argument "trim" must be booleanr      r      ro   ro   ro   r   zCoefficient array is empty)r7   ro   r7   )r   r&   r*   r   rC   r   rD   rE   rF   r   r|   r}   rw   rX   rS   )r@   r   r   rK   r   r   r   test_pu_as_series_exception   sj   
 z*TestPolynomial.test_pu_as_series_exceptionFc                 C   s@   t |}dd }| D ]\}}| j|||||||d qd S )Nc                  s   s   t dD ]$} t dD ]}tdg|  dg }tdg| dg }||fV  qqg dg dfV  g ddfV  dg dfV  g ddfV  dV  tg dtg dfV  tg dtg dfV  tg dtg dfV  d	V  d
V  dV  d S )Nr   r   r7   r   rn   )rn   r   )ru   rs         @)r   y               @r   )r   r   )r   r   ))r7   r   rn   r   r   )r   jp1p2r   r   r   r     s$   
z5TestPolynomial._test_polyarithm_basic.<locals>.inputsignore_sign_on_zero)r   r   )r@   r   r   rG   r   r   r   r   r   r   _test_polyarithm_basic  s   z%TestPolynomial._test_polyarithm_basicc                 C   sp  t |}|   | t}|dtg d W d    n1 s"w   Y  | dt|j | t}|tg dd W d    n1 sJw   Y  | dt|j | t}|t	d
ddtg d W d    n1 syw   Y  | dt|j | t}|tg dt	d
dd W d    n1 sw   Y  | dt|j d S )	Nr   r   z$The argument "c1" must be array-likez$The argument "c2" must be array-liker^   r   ro   r   )r   r   rC   r   r   rw   rD   rE   rF   r|   r}   )r@   r   rG   rK   r   r   r   r   _test_polyarithm_exception8  s4   $$z)TestPolynomial._test_polyarithm_exceptionc                 C      |  t d S r   )r   r   r?   r   r   r   test_polyadd_basicR     z!TestPolynomial.test_polyadd_basicc                 C   r   r   )r   r   r?   r   r   r   test_polyadd_exceptionU  r   z%TestPolynomial.test_polyadd_exceptionc                 C   s   | j tdd d S )NTr   )r   r   r?   r   r   r   test_polysub_basicX  s   z!TestPolynomial.test_polysub_basicc                 C   r   r   )r   r   r?   r   r   r   test_polysub_exception[  r   z%TestPolynomial.test_polysub_exceptionc                 C   r   r   )r   r   r?   r   r   r   test_polymul_basic^  r   z!TestPolynomial.test_polymul_basicc                 C   r   r   )r   r   r?   r   r   r   test_polymul_exceptiona  r   z%TestPolynomial.test_polymul_exceptionc                 C   sJ   t }tt }| t  dd }| D ]\}}| |||||| qd S )Nc                  s   s    dgdgfV  ddgdgfV  t dD ]&} t dD ]}dg|  ddg }dg| ddg }t||}||fV  qqtg dtg dfV  d S )Nro   r   r   r7   )r7   r   r   r   r   r   )r7   r   r   r   )r   r   r   r   rw   )r   r   cicjtgtr   r   r   r   i  s   "z6TestPolynomial.test_poly_polydiv_basic.<locals>.inputs)r,   r   r   r   )r@   r   rG   r   r   r   r   r   r   test_poly_polydiv_basicd  s   
z&TestPolynomial.test_poly_polydiv_basicc                 C   sP   |  t tt}| t}|dgdg W d    d S 1 s!w   Y  d S )Nr7   r   )r   r,   r   rC   ZeroDivisionError)r@   rG   _r   r   r   test_poly_polydiv_exceptionz  s
   
"z*TestPolynomial.test_poly_polydiv_exceptionc           
      C   s   t }tt }t}tt}t}tt}dd }| D ](\}}	| |||	|||	 | |||	|||	 | |||	|||	 qd S )Nc                  s   s   t jg t jddgfV  dg dfV  t dddg dfV  tdD ]} t dddg|  dg fV  q&t ddg d	fV  td
D ] } dg|  }t |}|dgfV  |ddgfV  |g dfV  qGt ddgt dddfV  ddgt dddfV  d S )Nr   r7   r   r   ro   r   r   r   )r   r   r   r7   rn   )r7   r   r   )r   rw   rS   r|   r}   r   linspacezeros)r   dimsr0   r   r   r   r     s    

" z6TestPolynomial.test_poly_polyval_basic.<locals>.inputs)r2   r   r4   r6   r   )
r@   r   r   pyfunc3Tcfunc3Tpyfunc3Fcfunc3Fr   r0   r1   r   r   r   test_poly_polyval_basic  s   z&TestPolynomial.test_poly_polyval_basicc                 C   s  t t}t t}t t}|   | t}|dd W d    n1 s%w   Y  | dt|j	 | t}|dd W d    n1 sHw   Y  | dt|j	 | t}|dd W d    n1 skw   Y  | dt|j	 | t}|dd W d    n1 sw   Y  | dt|j	 | t}|dd W d    n1 sw   Y  | dt|j	 t dd }| t}|ddd W d    n1 sw   Y  | dt|j	 | t}|dd W d    n1 sw   Y  | dt|j	 d S )	Nrn   r   #The argument "c" must be array-likez#The argument "x" must be array-likedefc                 S      t | ||}|S r   r-   )r0   r1   tensorr%   r   r   r   polyval3  s   z<TestPolynomial.test_poly_polyval_exception.<locals>.polyval3z%The argument "tensor" must be boolean)
r   r2   r4   r6   r   rC   r   rD   rE   rF   )r@   r   r   r   rK   r   r   r   r   test_poly_polyval_exception  s`   
z*TestPolynomial.test_poly_polyval_exceptionc              	   C   s.  t }tt }| |g d|g d tddD ]}| |dg|d|dg|d qtdD ]}dg| dg }| ||dd||dd q0tdD ] }tddD ]}dg| dg }| |||d|||d qTqMtddgddgg}| |||| tdd}| |||| d S )	Nr   ro   r   r   )r9   r7   r   r   )r8   r   r   r   r   rw   r|   r}   )r@   r   rG   r   polr   r   c3r   r   r   test_poly_polyint_basic  s"   "z&TestPolynomial.test_poly_polyint_basicc                 C   s8  t t}|   | t}|d W d    n1 sw   Y  | dt|j | t}|t	g dd W d    n1 sDw   Y  | dt|j | t}|g dd W d    n1 siw   Y  | dt|j | t}|dd W d    n1 sw   Y  | dt|j d S )Nr   r   r   z#The argument "m" must be an integer)r$   br1   r7   zInput dtype must be scalar.)
r   r8   r   rC   r   rD   rE   rF   r   rw   )r@   rG   rK   r   r   r   test_poly_polyint_exception  s4   
z*TestPolynomial.test_poly_polyint_exceptionc                 C   s*  dd }t |}dd }t |}tddgtddgf}tddgtddgf}tddgtddgf}|D ]V}|D ]Q}	|D ]L}
|||	|
}|||	|
}||}||}| || | |j|j | |j|j | |j|j | |j|j | |j|j | |j|j qDq@q<d S )	Nc                 S   r   r   r   
Polynomialr1   domwinr   r   r   r   pyfunc3  r5   z;TestPolynomial.test_Polynomial_constructor.<locals>.pyfunc3c                 S   r!   r   r   )r1   r   r   r   r   r     r'   z;TestPolynomial.test_Polynomial_constructor.<locals>.pyfunc1r   r7   rt   ru   )r   r   rw   r   coefdomainwindow)r@   r   cfunc3r   r   list1list2list3r1   r   r   r   r   q1q2r   r   r   test_Polynomial_constructor  s0   z*TestPolynomial.test_Polynomial_constructorc                 C   s   dd }t |}|   tddg}tg d}tdd}| t}|||| W d    n1 s8w   Y  | dt	|j
 | t}|||| W d    n1 s\w   Y  | d	t	|j
 | t}|||| W d    n1 sw   Y  | d
t	|j
 d S )Nc                 S   r   r   r   r   r   r   r   r   )  r5   z8TestPolynomial.test_Polynomial_exeption.<locals>.pyfunc3r7   ro   r   r   )ro   ro   z$Domain has wrong number of elements.z$Window has wrong number of elements.r   )r   r   r   rw   r|   r}   rC   rX   rD   rE   rF   r   )r@   r   r   input2input3input2DrK   r   r   r   test_Polynomial_exeption(  s0   z'TestPolynomial.test_Polynomial_exeptionN)F)rO   rP   rQ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s*    
/#.r   )r7   )#r;   	itertoolsr   numpyr   numpy.polynomialr   r   r   r   numbar   r   numba.tests.supportr   r   r	   r
   numba.core.errorsr   r   r   r   r   r   r&   r*   r,   r2   r4   r6   r8   r:   rZ   r   r   r   r   r   <module>   s.    
R