o
    1&i                     @   s  d dl mZ d dlmZ d dlZd dlZd dlmZm	Z	m
Z
mZ d dlmZ d dlmZmZ d dlmZmZ dd	iZd	d	d
Zdd Ze	dd Ze	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"e	d%d& Z#e	d'd( Z$e	d)d* Z%e	d+d, Z&e	d-d. Z'd/d0 Z(d1d2 Z)d3d4 Z*d5d6 Z+d7d8 Z,d9d: Z-d;d< Z.e	dSd=d>Z/d?d@ Z0dAdB Z1dTdDdEZ2dFdG Z3dHdI Z4dJdK Z5dLdM Z6dNdO Z7G dPdQ dQeeZ8e9dRkre:  dS dS )U    )partial)permutationsN)jitnjit
from_dtypetypeof)TypingError)typeserrors)TestCaseMemoryLeakMixinZforceobjT)Z_nrtnopythonc                    s    fdd}|S )aP  Decorator for generic check functions.
        Iterates over 'pyfuncs_to_use', calling 'func' with the iterated
        item as first argument. Example:

        @from_generic(numpy_array_reshape, array_reshape)
        def check_only_shape(pyfunc, arr, shape, expected_shape):
            # Only check Numba result to avoid Numpy bugs
            self.memory_leak_setup()
            got = generic_run(pyfunc, arr, shape)
            self.assertEqual(got.shape, expected_shape)
            self.assertEqual(got.size, arr.size)
            del got
            self.memory_leak_teardown()
    c                    s    fdd}|S )Nc                     s    fddD S )Nc                    s"   g | ]}|g R i qS  r   ).0pyfunc)argsfunckwargsr   NC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_array_manipulation.py
<listcomp>"   s   " zCfrom_generic.<locals>.decorator.<locals>.result.<locals>.<listcomp>r   r   r   )r   pyfuncs_to_user   r   result!      z/from_generic.<locals>.decorator.<locals>.resultr   )r   r   r   )r   r   	decorator    s   zfrom_generic.<locals>.decoratorr   )r   r   r   r   r   from_generic   s   r   c                 C   
   |  |S N)reshapearrZnewshaper   r   r   array_reshape'      
r"   c                 C      t | |S r   )npr   r    r   r   r   numpy_array_reshape+      r&   c                 C   r$   r   r%   broadcast_to)r!   shaper   r   r   numpy_broadcast_to0      r+   c                  G   
   t j|  S r   )r%   Zbroadcast_shapesr   r   r   r   numpy_broadcast_shapes4      
r/   c                  G   r-   r   )r%   Zbroadcast_arraysr.   r   r   r   numpy_broadcast_arrays8   r0   r1   c                 C   s   t | || S r   r(   )r!   r*   idxr   r   r   numpy_broadcast_to_indexing<      r3   c                 C      |   S r   )flattenar   r   r   flatten_array@      r9   c                 C   r5   r   ravelr7   r   r   r   ravel_arrayD   r:   r=   c                 C   s
   |   jS r   )r<   sizer7   r   r   r   ravel_array_sizeH   r0   r?   c                 C   
   t | S r   )r%   r<   r7   r   r   r   numpy_ravel_arrayL   r0   rA   c                 C   r5   r   	transposer7   r   r   r   transpose_arrayP   r:   rD   c                 C   r@   r   r%   rC   r7   r   r   r   numpy_transpose_arrayT   r0   rF   c                 C   s   t j| |dS N)axesrE   r!   rH   r   r   r    numpy_transpose_array_axes_kwargX      rJ   c                 C   s   t j| |d S rG   )r%   rC   copyrI   r   r   r   %numpy_transpose_array_axes_kwarg_copy]   s   rM   c                 C   r   r   rB   rI   r   r   r   array_transpose_axesb   r#   rN   c                 C   s   |  | S r   )rC   rL   rI   r   r   r   array_transpose_axes_copyg   rK   rO   c                 C   s^   t t | | d | d|f}t t |d |df}||j j}t |d}|d }|S )N   )   r      rR   )r%   r   arangeTrC   )mnr1r2Zr_difzr   r   r   transpose_issue_4708l   s    rZ   c                 C   r5   r   )squeezer7   r   r   r   squeeze_arrayv   r:   r\   c                 C   r$   r   )r%   expand_dims)r8   axisr   r   r   r]   z   r,   r]   c                  G   r-   r   )r%   
atleast_1dr.   r   r   r   r_   ~   r0   r_   c                  G   r-   r   )r%   
atleast_2dr.   r   r   r   r`      r0   r`   c                  G   r-   r   )r%   
atleast_3dr.   r   r   r   ra      r0   ra   c                 C   s0   | j d d f| j dd   }tjjj| |dS )Nr   rQ   rR   strides)rc   r%   libstride_tricks
as_strided)r8   rc   r   r   r   as_strided1   s   rg   c                 C   sN   d}| j d d | j d | d |f }| j| jd f }tjjj| ||dS )NrP   rR   )r*   rc   )r*   rc   r%   rd   re   rf   )r8   Zwindowr*   rc   r   r   r   as_strided2   s   $ri   c                 C   s   t jjj| ||dS )N)r^   )r%   rd   re   sliding_window_view)xwindow_shaper^   r   r   r   rj      s   rj   c                 C   s   | j f}| j}d|||f< d S )N      ?)rk   y)r!   Zarr2drk   rn   r   r   r   	bad_index   s   ro   c                 C   s   | d S )N)rR   g       @r   r!   r   r   r   bad_float_index   s   rq   Fc                 C   s   t | ||S r   )r%   Zfill_diagonal)r!   valwrapr   r   r   numpy_fill_diagonal   s   rt   c                 C   r@   r   )r%   r*   rp   r   r   r   numpy_shape   r0   ru   c                 C   r@   r   )r%   r>   rp   r   r   r   
numpy_size   r0   rv   c                 C   r@   r   )r%   Zflatnonzeror7   r   r   r   numpy_flatnonzero   r0   rw   c                 C   r@   r   )r%   Zargwherer7   r   r   r   numpy_argwhere   r0   rx   c                 C   r$   r   )r%   resize)r8   	new_shaper   r   r   numpy_resize   r,   r{   c                   @   s  e Zd Z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ed fd!d"Zd#d$ Zefd%d&Zefd'd(Zd)d* Zd+d, Zefd-d.Zd/d0 Zefd1d2Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1e2d]d^ Z3d_d` Z4dadb Z5dcdd Z6dedf Z7dgdh Z8didj Z9dkdl Z:dmdn Z;dodp Z<dqdr Z=dsdt Z>dudv Z?dwdx Z@dydz ZAd{d| ZBd}S )~TestArrayManipulationz4
    Check shape-changing operations on arrays.
    c                    s4  t tg}dd t|fdd t|fddt|fdd}t|fd	d
t|fdd}td} |d  |d  |d  |d  |d tdd} |d  |d  |d  |d  |d |d |d |d tdd} |d  |d  |d  |d tddj} |d  |d ||d ||d ||d tddd} |d  |d  |d  |d   |d! |d" ||d# ||d$  fd%d&}tg }|| ||d'   d S )(Nc                 S   s
   | ||S r   r   )r   r!   r*   r   r   r   generic_run   r0   z=TestArrayManipulation.test_array_reshape.<locals>.generic_runc                    s:   |  ||}   | ||}|| ~  d S r   )py_funcmemory_leak_setupassertPreciseEqualmemory_leak_teardown)r   r!   r*   expectedgotr}   selfr   r   check   s   z7TestArrayManipulation.test_array_reshape.<locals>.checkc                    s@       | ||}|j| |j|j ~  d S r   )r   assertEqualr*   r>   r   )r   r!   r*   expected_shaper   r   r   r   check_only_shape   s   zBTestArrayManipulation.test_array_reshape.<locals>.check_only_shapec                    L    t} | || W d    n1 sw   Y  t|jd d S )Nzincompatible shape for array)assertRaisesNotImplementedErrorr   str	exceptionr   r!   r*   raisesr   r   r   check_err_shape      zATestArrayManipulation.test_array_reshape.<locals>.check_err_shapec                    r   )Nz)total size of new array must be unchangedr   
ValueErrorr   r   r   r   r   r   r   check_err_size   r   z@TestArrayManipulation.test_array_reshape.<locals>.check_err_sizec                    r   )Nzmultiple negative shape valuesr   r   r   r   r   check_err_multiple_negative   r   zMTestArrayManipulation.test_array_reshape.<locals>.check_err_multiple_negative   )r   )      )   rP   )r   rR   rP   )rR   r   rR   rR   rP   rR   rQ   rP   r   r   )   )r   r   )r   rP   rQ   )rR   r   rR   rP   rR   rQ   rR   r   r   )rQ      r      rh   rh   )rh   r   )r   rh   r   )r   r   rh   )rh   r   )rh   r   r   )r   r   rh   rh   c                    s    | d  | d  | d  | d | dd | dd | dd | d	d
 | dd | d | d | d d S )Nr   r   )rR   r   rQ   )r   7   rR   r   rQ   rh   r   r   rh   r   r   )r   rh   )r   r   )rh   r   r   )r   r   r   r   rR   rR   rQ   r   rp   )r   r   r   r   r   check_empty  s   





z=TestArrayManipulation.test_array_reshape.<locals>.check_empty)rP   rQ   r   )	r"   r&   r   r%   rS   r   rT   arraydisable_leak_check)r   r   r   r   r!   r   r   )r   r   r   r}   r   r   test_array_reshape   sh   	































z(TestArrayManipulation.test_array_reshapec              	      s  t tttg}t|fdd}t|fdd}t|fdd}t|fdd}td	td	d
dtd	ddd
td	dddd
tddd
dd d dd d dd d f g}tt	|D ]4}||| d  t
tt|| jD ]}t	| t fdd|D }	||| | ||| |	 qqkttgdd }
|
dd |
dd |
dd
   ||d d ||d d ||d d ||d d ||d d ||d d ||d d ||d d ||d d ||d d  ||d d! ||d d" ||d d# ||d d$ ||d d% ||d d& t}td'd(ttddgf W d    n	1 sLw   Y  d)t|j d S )*Nc                    sN   |  ||}| ||} ||  |jj|jj  |jj|jj d S r   )r~   r   r   flagsf_contiguousc_contiguous)r   r!   rH   r   r   r   r   r   r   9  s   


z>TestArrayManipulation.test_array_transpose_axes.<locals>.checkc                    J     t}| || W d    n1 sw   Y   t|jd d S )Nzrepeated axis in transposer   r   r!   rH   r   r   r   r   check_err_axis_repeatedC     zPTestArrayManipulation.test_array_transpose_axes.<locals>.check_err_axis_repeatedc                    r   )Nz2axis is out of bounds for array of given dimensionr   r   r   r   r   check_err_axis_oobJ  r   zKTestArrayManipulation.test_array_transpose_axes.<locals>.check_err_axis_oobc                    s>     ttf | || W d    d S 1 sw   Y  d S r   )r   	TypeErrorr   )r   r!   rH   r   r   r   check_err_invalid_argsQ  s   "zOTestArrayManipulation.test_array_transpose_axes.<locals>.check_err_invalid_argsr   r   r   rQ   rP   rR   @   r   c                    s   g | ]}|  qS r   r   r   rk   )ndimr   r   r   b      zCTestArrayManipulation.test_array_transpose_axes.<locals>.<listcomp>c                 S   s(   |  ||}| ||}tj|| d S r   )r~   r%   testingassert_equal)r   rU   rV   r   r   r   r   r   check_issue_4708f  s   
zITestArrayManipulation.test_array_transpose_axes.<locals>.check_issue_4708r   foo)r   g333333@)333333?r   r   )rQ   r   r   )rP   rQ   rR   rR   r   rR   )r   r   rQ   )rQ   r   r   )rP   rR   rQ   )rR   rQ   )rP   rR   rQ   r   )rP   rR   rQ   Tr   z#np.transpose does not accept tuples)rJ   rM   rN   rO   r   r%   rS   r   rangelenr   tupler   rZ   r   r   r   r   rF   r   assertInr   r   )r   r   r   r   r   r   ZarrsirH   Zneg_axesr   er   )r   r   r   test_array_transpose_axes3  sn   	,



 z/TestArrayManipulation.test_array_transpose_axesc                 C   s@   t }t|}dd }| D ]\}}| |||||| qd S )Nc                   s   s6   t ddgddggdfV  t ddgddggdfV  t ddgddggdfV  t ddgddggdfV  t ddgddggd	fV  t ddgddggd
fV  t jdtddfV  t ddgddggdfV  t ddgddggdfV  t jdtddfV  t jg tddfV  g ddfV  dV  d S )NrR   rQ   rP   r   )rQ   r   )r   rQ   )r   rP   r   r   rQ   r   r   dtyperQ   rR   r   rR   rP   )r   rR   rQ   rP   rQ   rP   )r   r   )r%   r   zerosfloatintr   r   r   r   inputs  s   
z=TestArrayManipulation.test_numpy_resize_basic.<locals>.inputs)r{   r   r   )r   r   cfuncr   r8   rz   r   r   r   test_numpy_resize_basic  s   z-TestArrayManipulation.test_numpy_resize_basicc                 C   sb  |    tt}| t}|dd W d    n1 sw   Y  | dt|j | t}|t	ddgddggd W d    n1 sIw   Y  | dt|j | t
}|t	ddgddggd	 W d    n1 suw   Y  | d
t|j | t
}|t	ddgddggd W d    n1 sw   Y  | d
t|j d S )Nabcr   z#The argument "a" must be array-liker   rR   rQ   rP   zBThe argument "new_shape" must be an integer or a tuple of integers)r   rP   z0All elements of `new_shape` must be non-negativer   )r   r   r{   r   r   r   r   r   r%   r   r   )r   r   r   r   r   r   test_numpy_resize_exception  s4   z1TestArrayManipulation.test_numpy_resize_exceptionc                    s   t t  fddfdd}td}|| tdd}|| ||j ||d d d  td	}|| d S )
Nc                    s6   | |}    | |}|| ~  d S r   )r   r   r   )r!   r^   r   r   r   r   r   r   r   r     s   

z5TestArrayManipulation.test_expand_dims.<locals>.checkc                    s,   t | j d | jd D ]} | | qd S )NrR   )r   r   )r!   r^   )r   r   r   check_all_axes  s   z>TestArrayManipulation.test_expand_dims.<locals>.check_all_axesr   r   r   rh   *   )r]   r   r%   rS   r   rT   r   )r   r   r!   r   )r   r   r   r   r   test_expand_dims  s   


z&TestArrayManipulation.test_expand_dimsc                 C   s   t }tdd|}td}|  }|dd W d    n1 s"w   Y  | dt|j |  }||d W d    n1 sDw   Y  | dt|j d S )NTr   r   hellorP   z#First argument "a" must be an arrayz"Argument "axis" must be an integer)r]   r   r%   rS   assertTypingErrorr   r   r   )r   r   r   r!   r   r   r   r   test_expand_dims_exceptions  s   


z1TestArrayManipulation.test_expand_dims_exceptionsc                    s(  fdd fdd} fdd}t d}t d}|| ||| t d	}t d
d }|d d d }	|| ||||	 t d
d}
|
j}|
d d d }||
 ||
|| t dd}|j}|d d d }|| |||| t dd}|| ||||| d S )Nc                    s$     | |  |  |  d S r   )ZassertStridesEqualr   r6   )r   r   r   r   r   check_result  s   z<TestArrayManipulation.check_atleast_nd.<locals>.check_resultc                    s    | |  d S r   r   )arg)r   r   r   r   r   check_single  r   z<TestArrayManipulation.check_atleast_nd.<locals>.check_singlec                     sF   |  } |  } t|t| t||D ]	\}}|| qd S r   )r   r   zip)r   Zexpected_tupleZ	got_tupler   r   r   r   r   r   r   r   check_tuple  s   z;TestArrayManipulation.check_atleast_nd.<locals>.check_tupler   y              @r   r                 ?rh   r   r   r      )rQ   rQ   rQ   rQ   )r%   r   rS   r   rT   )r   r   r   r   r   Za1Za2b1b2Zb3c1c2c3Zd1Zd2Zd3r   r   r   r   check_atleast_nd  s2   



z&TestArrayManipulation.check_atleast_ndc                 C   "   t }tdd|}| || d S NTr   )r_   r   r   r   r   r   r   r   r   test_atleast_1d     z%TestArrayManipulation.test_atleast_1dc                 C   r   r   )r`   r   r   r   r   r   r   test_atleast_2d  r   z%TestArrayManipulation.test_atleast_2dc                 C   r   r   )ra   r   r   r   r   r   r   test_atleast_3d"  r   z%TestArrayManipulation.test_atleast_3dc                    sJ   t   fdd}td}|| ||d ||d d S )Nc                        | } | } || d S r   r   )r!   r   r   r   r   r   r   *     z5TestArrayManipulation.check_as_strided.<locals>.checkr   r   )r   rR   r   )r   r%   rS   r   )r   r   r   r!   r   r   r   check_as_strided'  s   
z&TestArrayManipulation.check_as_stridedc                 C   s   |  t |  t d S r   )r   rg   ri   r   r   r   r   test_as_strided4  s   
z%TestArrayManipulation.test_as_stridedc                 C   sX   t dd }| tj}|  W d    n1 sw   Y  d}| |t|j d S )Nc                  S   s"   t dd} t jjj| d dS )Nr   r   rb   )r%   rS   r   rd   re   rf   rp   r   r   r   r   :  s   z>TestArrayManipulation.test_as_strided_stride_none.<locals>.foozstrides argument cannot be None)r   r   r
   r   r   r   r   )r   r   r   msgr   r   r   test_as_strided_stride_none8  s   
z1TestArrayManipulation.test_as_strided_stride_nonec              	      s   fdd}t d}dD ] } d|  ||d| W d    n1 s(w   Y  qt ddd	}d
D ] } d|  ||d| W d    n1 sTw   Y  q9dD ] } d|  ||d| W d    n1 sww   Y  q\t ddddd}dD ] } d|  ||d| W d    n1 sw   Y  q d ||dd W d    d S 1 sw   Y  d S )Nc                    s2   t jjj| ||dd}t| ||} || d S )NT)	writeable)r%   rd   re   rj   r   )r!   rl   r^   r   r   r   r   r   r   F  s
   z=TestArrayManipulation.test_sliding_window_view.<locals>.checkr   )Nr   rh   r   z1d array, axis=r      
      r   z2d array, axis=)Nr   rR   rR   r   )rR   r   )r   r   r   rQ   )r   )r   r   z4d array, axis=rP   rQ   z2d array, repeated axes)r   rP   rP   )r   rR   r   )r%   rS   subTestr   )r   r   arr1r^   arr2Zarr4r   r   r   test_sliding_window_viewE  s6   
	"z.TestArrayManipulation.test_sliding_window_viewc                    s   fdd} fdd}    td}tddd} d	 |d
|dd  W d    n1 s5w   Y   d |d
|dd  W d    n1 sQw   Y   d |d|dd  W d    n1 smw   Y   d |d|dd  W d    n1 sw   Y   d |d|dd  W d    n1 sw   Y   d |d|dd W d    n1 sw   Y   d |d|dd W d    n1 sw   Y   d |d|dd W d    n1 sw   Y   d |d|dd W d    n	1 sw   Y   d  |d|d!d" W d    n	1 s3w   Y   d# |d|d!d$ W d    n	1 sPw   Y   d% |d&|d  W d    n	1 slw   Y   d' |d&|d( W d    n	1 sw   Y   d) |d&|d* W d    n	1 sw   Y   d+ |d,|dd( W d    n	1 sw   Y   d- |d,|dd* W d    d S 1 sw   Y  d S ).Nc                    sH     t}t|  W d    n1 sw   Y   | t|j d S r   )r   r   rj   r   r   r   r   r   r   r   r   r   _raisesj  s   
zFTestArrayManipulation.test_sliding_window_view_errors.<locals>._raisesc                    sJ     tj}t|  W d    n1 sw   Y   | t|j d S r   )r   r
   r   rj   r   r   r   r
  r   r   r   _typing_erroro  s   
zLTestArrayManipulation.test_sliding_window_view_errors.<locals>._typing_errorr   r   r  r  z1d window shape too largezwindow_shape cannot be largerr   z2d window shape too large)r      z1d window negative sizez&`window_shape` cannot contain negativerh   z2d window with a negative size)r   r   z1d array, 2d window shapez%matching length window_shape and axis)r  rQ   z$2d window shape, only one axis givenrR   z1d window shape, 2 axes givenr   r   z1d array, second axiszArgument axis out of boundsr   z1d array, axis -2r   z2d array, fourth axis)r   r   r   rP   z2d array, axis -3r   zwindow_shape=Nonez3window_shape must be an integer or tuple of integerzwindow_shape=float@zwindow_shape=tuple(float))r  z
axis=floatz1axis must be None, an integer or tuple of integerzaxis=tuple(float))r   r%   rS   r   r  )r   r  r  r  r  r   r   r   test_sliding_window_view_errorsi  s~   
$z5TestArrayManipulation.test_sliding_window_view_errorsCc           	      C   s   t ddd}|dkr|j}t}t|}|dkr |jdd}| |j| t	|ffi ||}||}||}t j
|| d S )N	   rP   FA)layout)r%   rS   r   rT   r9   r   rL   r   r  r   r   r   )	r   r   r  r8   r   
arraytype1r   r   r   r   r   r   test_flatten_array  s   z(TestArrayManipulation.test_flatten_arrayc                 C   s,   | j td | j tdd | j tdd d S )Nr   r  )r   r  r  )r  no_pyobj_flagsr   r   r   r   test_flatten_array_npm  s   z,TestArrayManipulation.test_flatten_array_npmc                    s  fdd}t |tt |t  fdd}|tddddd |tdjddd	d
d	d |tddddd d d d df dd |tdddddd |tdjdddd	d
d	d |tdddddd d d d d d df dd d S )Nc           	         s   t |}|j| t|ffi  | }| |}||}tj|| |jj|jjk}|jj|jjk}||dk || d S )Nr  )	r   r   r  r   r%   r   r   ctypesdata)	r   r8   assume_layoutr  r   r   r   Z	py_copiedZ	nb_copied)r   r   r   r   generic_check  s   z=TestArrayManipulation.test_ravel_array.<locals>.generic_checkc                     s    | i |  | i | d S r   r   r   )check_functioncheck_methodr   r   r     s   z5TestArrayManipulation.test_ravel_array.<locals>.checkr  rP   r  )r  r  order   rQ   r   r  $   )r   r=   rA   r%   rS   r   )r   r   r  r   r   )r  r   r   r   r   test_ravel_array  s   

. :z&TestArrayManipulation.test_ravel_arrayc                 C   sV   t ddd}t}t|}t|ffi ||}||}||}t j|| d S Nr  rP   )r%   rS   r   r?   r   r   r   r   r   r   r8   r   r  r   r   r   r   r   r   test_ravel_array_size  s   z+TestArrayManipulation.test_ravel_array_sizec                 C      | j td d S Nr  )r%  r  r   r   r   r   test_ravel_array_npm  r4   z*TestArrayManipulation.test_ravel_array_npmc                 C   r)  r*  )r(  r  r   r   r   r   test_ravel_array_size_npm  r4   z/TestArrayManipulation.test_ravel_array_size_npmc                    s"   t ttg fdd}|  d S )Nc                    sR   t ddd}t|}t|ffi  | }| |}||}t j|| d S r&  )r%   rS   r   r   r   r   r   )r   r8   r  r   r   r   r  r   r   r     s   z9TestArrayManipulation.test_transpose_array.<locals>.check)r   rD   rF   )r   r   r   r   r  r   test_transpose_array  s   


z*TestArrayManipulation.test_transpose_arrayc                 C   r)  r*  )r-  r  r   r   r   r   test_transpose_array_npm  r4   z.TestArrayManipulation.test_transpose_array_npmc                 C   s\   t dddddd}t}t|}t|ffi ||}||}||}t j|| d S )Nr   rQ   rR   rP   r   )r%   rS   r   r\   r   r   r   r   r'  r   r   r   test_squeeze_array  s   z(TestArrayManipulation.test_squeeze_arrayc                 C   sN   |  tj}| jtd W d    n1 sw   Y  | dt|j d S )Nr  r[   )r   r
   r   r/  r  r   r   r   r   r   r   r   r   test_squeeze_array_npm  s   z,TestArrayManipulation.test_squeeze_array_npmc                 C   sh  t dd }t dd }tdd}td tjftjtd ftdtjdftjdtd ftdttjdfdtjtftjtdtjdfdtd tjftjtdttjdfdtjtjtftjdtjtftd	dtjd ftjdtd fg}|j}|}|j}|}|D ]3}	|||	}
|||	}tj	|
| t
|}||	 }|| |	|}
|| |	|}tj	|
| q~d S )
Nc                 S   s   | | S r   r   )r8   r2   r   r   r   np_new_axis_getitem  s   z@TestArrayManipulation.test_add_axis.<locals>.np_new_axis_getitemc                 S   s   || |< | S r   r   )r8   r2   itemr   r   r   np_new_axis_setitem  s   z@TestArrayManipulation.test_add_axis.<locals>.np_new_axis_setitemiH  )r   r   r      rR   rQ   rP   )r   r%   rS   r   sliceZnewaxisEllipsisr~   r   r   Z
zeros_likerL   )r   r2  r4  r8   Z	idx_casesZpyfunc_getitemZcfunc_getitemZpyfunc_setitemZcfunc_setitemr2   r   r   Za_emptyr3  r   r   r   test_add_axis  sB   





z#TestArrayManipulation.test_add_axisc                 C   s|   |   '}ttdtjfdtjfg}ttjdd}t||ft W d    n1 s.w   Y  | 	dt
|j d S )Nrk   rn   rQ   r  zUnsupported array index type)r   r   r%   r   int32r	   Arrayr   ro   r   r   r   )r   r   r  Z
arraytype2r   r   r   test_bad_index_npmE  s   
z(TestArrayManipulation.test_bad_index_npmc                 C   sX   |   }tttjddft W d    n1 sw   Y  | dt|j d S )NrQ   r  z$Unsupported array index type float64)	r   r   r	   r:  float64rq   r   r   r   r0  r   r   r   test_bad_float_index_npmM  s   
z.TestArrayManipulation.test_bad_float_index_npmc                    s   t tdddd  dd } fdd} fd	d
}fdd}|dD ]}| D ]}||| q.q)|dD ]}| D ]}||| q@q;tdgd dd}||d ||g d ||tg d d S )NTr   c                 s   sZ    | | fV  d|  | fV  | d|  fV  d|  d d|  d fV  | | | | fV  dV  d S )NrQ   rR   rR   rR   rR   r   )rV   r   r   r   _shape_variationsW  s   

zITestArrayManipulation.test_fill_diagonal_basic.<locals>._shape_variationsc                   s   s    dV  dV  t jV  t j V  dV  t dV  dV  ddgV  t ddd	d
dV  t t dd	d	V  t dd	d	d d d V  d S )NrR   gA`"	@Tr   r   r   r  6   rP   rQ   rh   )r%   naninfrS   r   asfortranarrayr   r   r   r   _val_variationse  s   

"zGTestArrayManipulation.test_fill_diagonal_basic.<locals>._val_variationsc                 3   s>     | D ]}t j|t jdV  t t j|t jdV  qd S )Nr   )r%   r   r<  rC  ones)rV   r*   r?  r   r   #_multi_dimensional_array_variationss  s
   z[TestArrayManipulation.test_fill_diagonal_basic.<locals>._multi_dimensional_array_variationsc                 3   sN     | D ]}t jtdd |D t jd}tdd |D }|| V  qd S )Nc                 S   s   g | ]}|d  qS rQ   r   r   r   r   r   r   {  r   zwTestArrayManipulation.test_fill_diagonal_basic.<locals>._multi_dimensional_array_variations_strided.<locals>.<listcomp>r   c                 s   s     | ]}t d |d dV  qdS )r   rQ   N)r6  r   r   r   r   	<genexpr>|  s    zvTestArrayManipulation.test_fill_diagonal_basic.<locals>._multi_dimensional_array_variations_strided.<locals>.<genexpr>)r%   r   r   r<  )rV   r*   tmpZslicerrF  r   r   +_multi_dimensional_array_variations_stridedy  s   zcTestArrayManipulation.test_fill_diagonal_basic.<locals>._multi_dimensional_array_variations_stridedc                    sd   dD ]-}|   }|   }|d u ri }nd|i}||fi |  ||fi | || qd S )N)NTFrs   )rL   r   )r!   rr   rs   r8   bparamsr   r   r   _check_fill_diagonal  s   zLTestArrayManipulation.test_fill_diagonal_basic.<locals>._check_fill_diagonalrP   r  F)FTFTFT)rt   r   r%   r   r   )r   rD  rG  rK  rN  r!   rr   r   )r?  r   r   r   r   test_fill_diagonal_basicS  s&   


z.TestArrayManipulation.test_fill_diagonal_basicc              	      sx  t }tdd| d}  tg tdfD ]%}t} || W d    n1 s0w   Y  dt|j	v s>J qt
}td} || dt|j	 W d    n1 sbw   Y   fdd	}tjd
tjd}tj}||| dtjg}||| tjg dtjd}||| tjd
tjd}g d}||| d}||| tdg}||| d S )NTr   rR   r   z'The first argument must be at least 2-D)rP   rP   r   z/All dimensions of input must be of equal lengthc                    sJ    t} | | W d    n1 sw   Y  dt|j d S )Nz'Unable to safely conform val to a.dtyper   )r!   rr   r   r   r   r   r   _assert_raises  s   zPTestArrayManipulation.test_fill_diagonal_exception_cases.<locals>._assert_raisesrP   rP   r   ffffff
@)rR   rQ   g    _B)gffffff?g@}Ô%Igp=TrU  )rt   r   r   r%   r   rE  r   r   r   r   r   r   r   r9  rA  rB  int64float32)r   r   rr   r8   r   rR  r!   r   rQ  r   "test_fill_diagonal_exception_cases  s:   







z8TestArrayManipulation.test_fill_diagonal_exception_casesc                 C   s  t }tdd|}tddgtddgtddgtddgtddgtdd	gtd
dgtd
dgtd
dgtddgtddgtddgtddgtddgtddgddgddgg}|D ]\}}|||}|||}| || qmd S )NTr   r   r   r   rP   rR   rH  rR   rQ   rP   rP   r   r   rQ   r   r   r   r   rQ   rQ   )r+   r   r%   r   rE  rS   r   )r   r   r   r  input_arrayr*   r   r   r   r   r   test_broadcast_to  s2   

z'TestArrayManipulation.test_broadcast_toc                 C   sd   t }tdd|}tdddg}d}|D ]}|||}|||}| || | |jd  qd S )NTr   {   r   Z	WRITEABLE)r+   r   r%   r   r   ZassertFalser   )r   r   r   r   r*   r!   r   r   r   r   r   test_broadcast_to_0d_array  s   


z0TestArrayManipulation.test_broadcast_to_0d_arrayc           	      C   sr  t }tdd|}tddtdgtddtdgtddtdgddtdgddtdgtddtd	gtdd
td	gtddtd	gtddtd	gtddtdgtddtdgtddtdgtddtdgtddtdgtddtdgddtdgddtdgg}|   |D ])\}}}}| |}||| W d    n1 sw   Y  | |t	|j
 qd S )NTr   r   r   z/Cannot broadcast a non-scalar to a scalar arrayr   rY  )r^  z=operands could not be broadcast together with remapped shapesrH  r   r   r   rR   rR   zDinput operand has more dimensions than allowed by the axis remappingr[  rh   z4all elements of broadcast shape must be non-negativer   )rh   rQ   g?g@z7The second argument "shape" must be a tuple of integersr   z-The first argument "array" must be array-likerP   )rQ   r8   z*object cannot be interpreted as an integer)r+   r   r%   r   r   r   r   r   r   r   r   )	r   r   r   r  r!   Ztarget_shapeerrr   r   r   r   r   test_broadcast_to_raises  sz   $z.TestArrayManipulation.test_broadcast_to_raisesc                 C   s*   t dd }| }| }| || d S )Nc                   S   s   t ddS )Nr8   r   r(   r   r   r   r   _broadcast_to_13  r'   zMTestArrayManipulation.test_broadcast_to_corner_cases.<locals>._broadcast_to_1)r   r~   r   )r   rd  r   r   r   r   r   test_broadcast_to_corner_cases2  s
   
z4TestArrayManipulation.test_broadcast_to_corner_casesc                 C   s\   t }tdd|}tjdtjd}d}|||}d|d< | | d | | d d S )	NTr   rQ   r   r[  r  r   r  )r+   r   r%   r   r9  r   sum)r   r   r   r\  r*   viewr   r   r   test_broadcast_to_change_view;  s   
z3TestArrayManipulation.test_broadcast_to_change_viewc           	      C   s\   t }tdd|}tdddgg}|D ]\}}}||||}||||}| || qd S )NTr   rQ   r[  r   )r3   r   r%   rE  r   )	r   r   r   r  r\  r*   r2   r   r   r   r   r   test_broadcast_to_indexingF  s   z0TestArrayManipulation.test_broadcast_to_indexingc                 C   s8   t dd }td}||}||}| || d S )Nc                 S   s   t | d}||j|j|jfS )Nr   )r%   r)   r>   r*   rc   )r!   retr   r   r   r   T  s   z@TestArrayManipulation.test_broadcast_to_array_attrs.<locals>.foorP   )r   r%   rS   r~   r   )r   r   r!   r   r   r   r   r   test_broadcast_to_array_attrsQ  s   


z3TestArrayManipulation.test_broadcast_to_array_attrsc                 C   s   t }tdd|}dgddgdgdgdgddgg dd	gd
dgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgg}|D ]}|| }|| }| |t | || qYd S )NTr   r   r5  r   r`  )rP   r   ))r   r5  r   r   rR   rl  )r   rR   r5  rm  r   rP   rR   r  r   r  r   r   rY  rQ   r  )r/   r   ZassertIsInstancer   r   )r   r   r   r  input_shaper   r   r   r   r   test_broadcast_shapes_  sH   z+TestArrayManipulation.test_broadcast_shapesc              	   C   s   t }tdd|}|   ddgddgg ddd	gg d
ddgg}|D ]$}| t}||  W d    n1 s9w   Y  | dt|j q#d S )NTr   rY  r   r   rH  rY  rY  r   rR   rP   r   rQ   rP   rP   )r   rn  r  )r  r   rQ   =shape mismatch: objects cannot be broadcast to a single shaper/   r   r   r   r   r   r   r   )r   r   r   r  ro  r   r   r   r   test_broadcast_shapes_raises  s$   
z2TestArrayManipulation.test_broadcast_shapes_raisesc                 C   sf   t }tdd|}|   | t}|ddd W d    n1 s#w   Y  | dt|j d S )NTr   r   rQ   r   z#negative dimensions are not allowedru  )r   r   r   r   r   r   r   (test_broadcast_shapes_negative_dimension  s   z>TestArrayManipulation.test_broadcast_shapes_negative_dimensionc              	   C   st   t }tdd|}|   g d}|D ]$}| t}||  W d    n1 s)w   Y  | dt|j qd S )NTr   ))r   )r   )g333333@)stringr   r8   )rR   ry  z#must be either an int or tuple[int])r/   r   r   r   r   r   r   r   )r   r   r   Zinpsinpr   r   r   r   "test_broadcast_shapes_invalid_type  s   
z8TestArrayManipulation.test_broadcast_shapes_invalid_typec                    s   t tdd  fdd}dD ]}t|}|| qdddgddgd	d
ggddfD ]}|| q.t} d W d    n1 sIw   Y  dt|j d S )NTr   c                    r   r   r   rk   r   r   r   r   r   r     r   z/TestArrayManipulation.test_shape.<locals>.checkr   r   r   )r   r   r   rR   FrQ   rP   r   r   rZ  r8   z+The argument to np.shape must be array-like)	ru   r   r%   emptyr   r   r   r   r   r   r   tr!   r   r   r   r   
test_shape  s   

"

z TestArrayManipulation.test_shapec                    s   t tdd  fdd}dD ]}t|}|| qdddtd	td
fD ]}|| q,t} d W d    n1 sGw   Y  dt	|j
 d S )NTr   c                    r   r   r   r|  r   r   r   r     r   z.TestArrayManipulation.test_size.<locals>.checkr}  rR   FgQ	@r   gX9v@r8   z*The argument to np.size must be array-like)rv   r   r%   r~  Zint8rW  r   r   r   r   r   r  r   r   r   	test_size  s   



zTestArrayManipulation.test_sizec                    J   t }tdd|} fdd}| D ]}||}||} || qd S )NTr   c                  3   s    t ddV  t jdddV  t g V   jd} d| t | dk< | V  | dddV  | jd	d
ddV  | dddd d
 V  | d V  d S )Nr   r   r   Z
fill_valued           皙?r   2   rQ   r  r!  r   rR   r   )r%   rS   fullr   randomrandnabsr   r7   r   r   r   a_variations  s   zBTestArrayManipulation.test_flatnonzero_basic.<locals>.a_variations)rw   r   r   r   r   r   r  r8   r   r   r   r   r   test_flatnonzero_basic  s   
z,TestArrayManipulation.test_flatnonzero_basicc                    r  )NTr   c                  3   s    t dddkV  t jdddV  t jdddV  t g V  t g dV   jd} | d	kV  | ddd
dkV  | jdddddkV  | dd
dd d dkV  | | d kV  | |  kV  d S )Nr   r   rQ   r   r  rR   )g      r  rm   r  r  r   g      ?r  r  r!  r   )r%   rS   r  r   r  r  r   r7   r   r   r   r    s   
z?TestArrayManipulation.test_argwhere_basic.<locals>.a_variations)rx   r   r   r  r   r   r   test_argwhere_basic  s   
z)TestArrayManipulation.test_argwhere_basicc                   c   sh    dV  dV  ddgddgfV  dV  t jV  dV  dV  dV  d	V  d
V  dV  d V  dV  dV  d S )N)ra  )rT  g@)g      @gffffff@)r  rm   r  g      r   rR   rQ   rP   r   FTrO  y       @      ?Za_string )r%   rA  r   r   r   r   array_like_variations  s   
z+TestArrayManipulation.array_like_variationsc                 C   @   t }tdd|}|  D ]}||}||}| || qd S r   )rw   r   r  r   r   r   r   r8   r   r   r   r   r   test_flatnonzero_array_like-  s   z1TestArrayManipulation.test_flatnonzero_array_likec                 C   r  r   )rx   r   r  r   r  r   r   r   test_argwhere_array_like6  s   z.TestArrayManipulation.test_argwhere_array_likec           	      C   sT   t }tdd|}dd |D }|| }|gt| }dd |D }| || d S )NTr   c                 S      g | ]}t |qS r   r%   r   r   sr   r   r   r   D      zOTestArrayManipulation.broadcast_arrays_assert_correct_shape.<locals>.<listcomp>c                 S      g | ]}|j qS r   r*   r   r8   r   r   r   r   G      r1   r   r   r   )	r   input_shapesr   r   r   inarrays	outarraysr   r   r   r   r   %broadcast_arrays_assert_correct_shape>  s   z;TestArrayManipulation.broadcast_arrays_assert_correct_shapec                 C   sd   t }tdd|}g d}|D ] }|g}| || ||g}| || |||g}| || qd S )NTr   )	r   rY  r  r  r  )rP   r   r   rn  rS  )r1   r   r  )r   r   r   r  r*   r  Zinput_shapes2Zinput_shapes3r   r   r   'test_broadcast_arrays_same_input_shapesJ  s   
z=TestArrayManipulation.test_broadcast_arrays_same_input_shapesc                 C   s   ddgdgddgdgddgdgddgdgddgdgddgdgddgdgddgdgd	dgdgdd	gdgddgdgddgdgdd	gd	gg}|D ]\}}|  || |  |d d d
 | qEd S )Nr   rY  r   rS  rn  r`  r  r   r  rh   r  r   r  r  r   r   r   r   9test_broadcast_arrays_two_compatible_by_ones_input_shapesh  s$   












zOTestArrayManipulation.test_broadcast_arrays_two_compatible_by_ones_input_shapesc                 C   s   ddgdgddgdgddgdgddgdgddgdgddgdgddgdgddgdgddgdgddgdgddgdgdd	gd	gdd
gd	gdd	gd	gdd	gd	gddgdgdd
gd
gddgdgddgdgdd
gd
gg}|D ]\}}|  || |  |d d d | qhd S )Nr   rY  rS  rn  r   r`  r   r   r   r  r  rh   r  r  r   r   r   Dtest_broadcast_arrays_two_compatible_by_prepending_ones_input_shapes  s2   



















zZTestArrayManipulation.test_broadcast_arrays_two_compatible_by_prepending_ones_input_shapesc           	      C   sv   t }tdd|}ddgdgddgdgddgdgg}|D ]\}}|| }d	d
 |D }|gt| }| || qd S )NTr   Fr   rR   rQ   r   rH  c                 S   r  r   r  r  r   r   r   r     r  zLTestArrayManipulation.test_broadcast_arrays_scalar_input.<locals>.<listcomp>r  )	r   r   r   r  r  r   r  r   r   r   r   r   "test_broadcast_arrays_scalar_input  s   


z8TestArrayManipulation.test_broadcast_arrays_scalar_inputc                 C   sB   t }tdd|}|dd}ddg}dd |D }| || d S )NTr   )r^  i  )i  rH  c                 S   r  r   r  r  r   r   r   r     r  zKTestArrayManipulation.test_broadcast_arrays_tuple_input.<locals>.<listcomp>)r1   r   r   r   r   r   r  r   r   r   r   r   !test_broadcast_arrays_tuple_input  s   
z7TestArrayManipulation.test_broadcast_arrays_tuple_inputc                 C   sT   t }tdd|}|tdtjdtjd}ddg}dd |D }| || d S )NTr   rQ   r   r   c                 S   r  r   r  r  r   r   r   r     r  zOTestArrayManipulation.test_broadcast_arrays_non_array_input.<locals>.<listcomp>)r1   r   r%   Zintpr   r   r  r   r   r   %test_broadcast_arrays_non_array_input  s   z;TestArrayManipulation.test_broadcast_arrays_non_array_inputc                 C   sx   t }tdd|}|   | t}tdd}d}||| W d    n1 s,w   Y  | dt	|j
 d S )NTr   r   r   zMismatch of argument types)r1   r   r   r   r   r%   rS   r   r   r   r   )r   r   r   r   r!   rL  r   r   r   /test_broadcast_arrays_invalid_mixed_input_types     zETestArrayManipulation.test_broadcast_arrays_invalid_mixed_input_typesc                 C   sx   t }tdd|}|   | t}tjdtjd}d}||| W d    n1 s,w   Y  | dt	|j
 d S )NTr   rP   r   zhello worldzArgument "1" must be array-like)r1   r   r   r   r   r%   r   rV  r   r   r   )r   r   r   r   r!   r  r   r   r   #test_broadcast_arrays_invalid_input  r  z9TestArrayManipulation.test_broadcast_arrays_invalid_inputc              
   C   s   t }tdd|}|   ddgddgg ddd	gg}|D ]7}||d d d
 fD ]+}| t}dd |D }||  W d    n1 sEw   Y  | dt|j q(qd S )NTr   rY  r   r   rH  rq  rr  rs  rh   c                 S   r  r   r  r  r   r   r   r     r  zdTestArrayManipulation.test_broadcast_arrays_incompatible_shapes_raise_valueerror.<locals>.<listcomp>rt  )r1   r   r   r   r   r   r   r   )r   r   r   r  r  r*   r   r  r   r   r   :test_broadcast_arrays_incompatible_shapes_raise_valueerror  s&   
zPTestArrayManipulation.test_broadcast_arrays_incompatible_shapes_raise_valueerrorc                 C   p   dd }|}t dd|}|tg dg dg dgd}|tg dg dg dgd}tj|| d S )	Nc                 S   j   | j \}}t| |||f}t|dd}| }t|d|f}|d d|  ||d d d d f< |S Nr   rR   rh   )r*   r%   r)   swapaxesr6   r   rk   rn   rcr8   rL  ccdr   r   r   unfold_flatten     
$zITestArrayManipulation.test_readonly_after_flatten.<locals>.unfold_flattenTr   r>  rQ   rQ   rQ   rP   rP   rP   rQ   r   r%   r   r   Zassert_array_equal)r   r  r   r   res_nbres_pyr   r   r   test_readonly_after_flatten     	""z1TestArrayManipulation.test_readonly_after_flattenc                 C   r  )	Nc                 S   r  r  )r*   r%   r)   r  r<   r   r  r   r   r   unfold_ravel  r  zETestArrayManipulation.test_readonly_after_ravel.<locals>.unfold_ravelTr   r>  r  r  rQ   r  )r   r  r   r   r  r  r   r   r   test_readonly_after_ravel  r  z/TestArrayManipulation.test_readonly_after_ravelc           	      C   s   |    tdd }|jdd}d|j_d|j_dd }|}tdd	|}||}||}d
}| |jj | d}| |jj| d S )Nr  rS  r  r!  Fc                 S   s   |   }|S r   r;   )r8   r   r   r   r   try_ravel_w_copy!  s   zKTestArrayManipulation.test_mutability_after_ravel.<locals>.try_ravel_w_copyTr   zCNo copy was performed, so the resulting array must not be writeablez?A copy was performed, yet the resulting array is not modifiable)	r   r%   rS   r   rL   r   r   r   
assertTrue)	r   Za_cZa_fr  r   r   Zret_cZret_fr   r   r   r   test_mutability_after_ravel  s   z1TestArrayManipulation.test_mutability_after_ravelN)C__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  enable_pyobj_flagsr  r  r%  r(  r+  r,  r-  r.  r/  r1  r8  r;  r=  rP  rX  r]  r_  rc  re  rh  ri  rk  rp  rv  rw  r{  r  r  r  r  staticmethodr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r|      s    oZ/$D",H.#0	*

	!r|   __main__r   )F);	functoolsr   	itertoolsr   numpyr%   ZunittestZnumbar   r   r   r   Znumba.core.errorsr   Z
numba.corer	   r
   Znumba.tests.supportr   r   r  r  r   r"   r&   r+   r/   r1   r3   r9   r=   r?   rA   rD   rF   rJ   rM   rN   rO   rZ   r\   r]   r_   r`   ra   rg   ri   rj   ro   rq   rt   ru   rv   rw   rx   r{   r|   r  mainr   r   r   r   <module>   s    







	
          }