o
    1&iO                     @   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   FC:\wamp64\www\opt\env\Lib\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   Z	as_series)aresr   r   r   polyasseries1"      
r%   c                 C      t | |}|S r   r"   )r#   Z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   Zpolyvalxcr$   r   r   r   polyval21   r)   r/   c                 C      t | |d}|S )NTr+   r,   r   r   r   	polyval3T6      r1   c                 C   r0   )NFr+   r,   r   r   r   	polyval3F;   r2   r3      c                 C   r'   r   )r   polyint)r.   mr$   r   r   r   r5   @   r)   r5   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   )gcZcollectsuperr7   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.rH   )r<   rC   rD   rE   r   r   r   assert_1d_inputW   s   z"TestPolynomialBase.assert_1d_input)__name__
__module____qualname____doc__r   float64float32
complex128Z	complex64dtypesr:   
ValueErrorrH   rJ   __classcell__r   r   r=   r   r7   E   s    r7   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.rI   )r<   namerC   rD   rE   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)Z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   )ZrtolZatol)	r   dtyperR   r   finfo
resolutiontestingZassert_allcloseZassertNoNRTLeak)r#   kwargsexpectedgotr\   rC   Zdefault_resolutionr<   r   r   checki   s   
"z$TestPoly1D.test_roots.<locals>.checkr4   )r4         )r   r   r   )r4         re   )r   r   r   r4   rc   rd   )r4   r4   r   r   r   )r   r   r4   r   r   r         ?      ?)r4   y      @      ?rd   )r   y                r   )r4   y      @      ?rf   re   )r   r   r   rg   rc   rd   )rg   r4   r   r   r   )r   r   rg   r   r   r   rd   g      @)g      @       @              ?Zeigvals)r   r   r   r[   rO   r\   arrayr   Zint32Zint64listrR   ZastyperJ   arangereshaperW   rQ   )r<   rb   Z	r_vectorsvrZ   r#   Z	c_vectorsr-   r   ra   r   
test_rootsb   s<   






zTestPoly1D.test_rootsN)rK   rL   rM   rW   r   rp   r   r   r   r   rU   \   s    rU   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   r4   r   ranger   rk   )ir   r   r   inputs   s   z1TestPolynomial.test_trimseq_basic.<locals>.inputs)r   r   assertPreciseEqual)r<   pyfuncrC   rv   Z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-likerX   rr   rd   Coefficient array is not 1-dr4   rd   rc   r   z6Unsupported type UniTuple(int64, 4) for argument "seq")r   r   disable_leak_checkr?   r   r@   rA   rB   r   rm   rn   )r<   rC   rG   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   re   )rd   rc   r4   rc   rd   rZ   )r4   rd   rc   r   r   r4   rd   rc   )r   r   MbP?r   gh㈵>r   r   )r4   rd   rc   r   rr   re      )r   )              ?rd                 @y              @rr   y              @r   g?ri   r|   )r|   )r   rm   rn   rP   rk   r   r   r   r   rv      s   z6TestPolynomial.test_pu_as_series_basic.<locals>.inputsFT)r%   r   r(   rw   )r<   pyfunc1cfunc1pyfunc2cfunc2rv   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 )Nrz   z'The argument "alist" must be array-likeTr   z#The argument "trim" must be booleanr      r{      rd   rd   rd   r   zCoefficient array is empty)r4   rd   r4   )r   r%   r(   r}   r?   r   r@   rA   rB   r   rm   rn   rk   rS   rO   )r<   r   r   rG   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 )Nrr   r   r4   r   rc   )rc   r   )rj   rh         @)r   y               @r   )r   r   )r   r   ))r4   r   rc   r   rs   )ru   jp1p2r   r   r   rv     s$   
z5TestPolynomial._test_polyarithm_basic.<locals>.inputsignore_sign_on_zero)r   rw   )r<   rx   r   rC   rv   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 )	Nrz   r   z$The argument "c1" must be array-likez$The argument "c2" must be array-likerX   rr   rd   r{   )r   r}   r?   r   r   rk   r@   rA   rB   rm   rn   )r<   rx   rC   rG   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 )Nrd   rr   r   r4   )r4   r   r   r   r   r   )r4   r   r   r   )rt   r   r   r   rk   )ru   r   cicjZtgtr   r   r   rv   i  s   "z6TestPolynomial.test_poly_polydiv_basic.<locals>.inputs)r*   r   r   rw   )r<   rx   rC   rv   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 )Nr4   r   )r   r*   r   r?   ZeroDivisionError)r<   rC   _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   r4   r   r   rd   rr   r   r   )r   r   r   r4   rc   )r4   r   r   )r   rk   rO   rm   rn   rt   ZlinspaceZzeros)ru   dimsr-   r   r   r   rv     s    

" z6TestPolynomial.test_poly_polyval_basic.<locals>.inputs)r/   r   r1   r3   rw   )
r<   r   r   Zpyfunc3Tcfunc3TZpyfunc3Fcfunc3Frv   r-   r.   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 )	Nrc   rz   #The argument "c" must be array-likez#The argument "x" must be array-likedefc                 S      t | ||}|S r   r+   )r-   r.   Ztensorr$   r   r   r   polyval3  s   z<TestPolynomial.test_poly_polyval_exception.<locals>.polyval3z%The argument "tensor" must be boolean)
r   r/   r1   r3   r}   r?   r   r@   rA   rB   )r<   r   r   r   rG   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   rd   rr   r   )r6   r4   r   r   )r5   r   rw   rt   r   rk   rm   rn   )r<   rx   rC   ru   Z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 )Nrz   r   r   z#The argument "m" must be an integer)r#   br.   r4   zInput dtype must be scalar.)
r   r5   r}   r?   r   r@   rA   rB   r   rk   )r<   rC   rG   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   Z
Polynomialr.   domwinr   r   r   r   pyfunc3  r2   z;TestPolynomial.test_Polynomial_constructor.<locals>.pyfunc3c                 S   r!   r   r   )r.   r   r   r   r   r     r&   z;TestPolynomial.test_Polynomial_constructor.<locals>.pyfunc1r   r4   ri   rj   )r   r   rk   rw   ZcoefdomainZwindow)r<   r   cfunc3r   r   Zlist1Zlist2Zlist3r.   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   )  r2   z8TestPolynomial.test_Polynomial_exeption.<locals>.pyfunc3r4   rd   r   r   )rd   rd   z$Domain has wrong number of elements.z$Window has wrong number of elements.r{   )r   r}   r   rk   rm   rn   r?   rS   r@   rA   rB   r   )r<   r   r   Zinput2Zinput3Zinput2DrG   r   r   r   test_Polynomial_exeption(  s0   z'TestPolynomial.test_Polynomial_exeptionN)F)rK   rL   rM   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rq      s*    
/#.rq   )r4   )#r8   	itertoolsr   numpyr   Znumpy.polynomialr   r   r   r   Znumbar   r   Znumba.tests.supportr   r   r	   r
   Znumba.core.errorsr   r   r   r   r   r   r%   r(   r*   r/   r1   r3   r5   r7   rU   rq   r   r   r   r   <module>   s.    
R