o
    8ήc                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZ d dlmZ d dlm	Z	 G dd de
ZG d	d
 d
eZG dd deZdddZdd ZdS )    N)	_internal)ParallelUFuncBuilderParallelGUFuncBuilder)DelayedRegistry)dufunc)gufuncc                   @   s0   e Zd Zedd Zedd Zedd ZdS )_BaseVectorizec                 C   s   | dd S )Nidentitypopclskwargs r   @/tmp/pip-target-vg8gfxp4/lib/python/numba/np/ufunc/decorators.pyget_identity      z_BaseVectorize.get_identityc                 C   s   | ddS )NcacheFr
   r   r   r   r   	get_cache   r   z_BaseVectorize.get_cachec                 C   s4   | dd}z| j| W S  ty   td| w )NtargetcpuzUnsupported target: %s)r   target_registryKeyError
ValueError)r   r   r   r   r   r   get_target_implementation   s   z(_BaseVectorize.get_target_implementationN)__name__
__module____qualname__classmethodr   r   r   r   r   r   r   r      s    

r   c                   @   $   e Zd ZeejedZdd ZdS )	Vectorizer   parallelc                 K   s.   |  |}| |}| |}|||||dS )Nr	   r   targetoptions)r   r   r   )r   funckwsr	   r   impr   r   r   __new__"   s   


zVectorize.__new__N)	r   r   r   r   r   DUFuncr   r   r(   r   r   r   r   r       
    r    c                   @   r   )GUVectorizer!   c                 K   sZ   |  |}| |}| |}|tju r$|dd}|||||||dS ||||||dS )N
is_dynamicF)r	   r   r,   r$   r#   )r   r   r   r   GUFuncr   )r   r%   	signaturer&   r	   r   r'   is_dynr   r   r   r(   -   s   





zGUVectorize.__new__N)	r   r   r   r   r   r-   r   r   r(   r   r   r   r   r+   )   r*   r+   r   c                    sL   t | tr	| g nt| rtj| fi S | dur|   fdd}|S )a  vectorize(ftylist_or_function=(), target='cpu', identity=None, **kws)

    A decorator that creates a Numpy ufunc object using Numba compiled
    code.  When no arguments or only keyword arguments are given,
    vectorize will return a Numba dynamic ufunc (DUFunc) object, where
    compilation/specialization may occur at call-time.

    Args
    -----
    ftylist_or_function: function or iterable

        When the first argument is a function, signatures are dealt
        with at call-time.

        When the first argument is an iterable of type signatures,
        which are either function type object or a string describing
        the function type, signatures are finalized at decoration
        time.

    Keyword Args
    ------------

    target: str
            A string for code generation target.  Default to "cpu".

    identity: int, str, or None
        The identity (or unit) value for the element-wise function
        being implemented.  Allowed values are None (the default), 0, 1,
        and "reorderable".

    cache: bool
        Turns on caching.


    Returns
    --------

    A NumPy universal function

    Examples
    -------
        @vectorize(['float32(float32, float32)',
                    'float64(float64, float64)'], identity=1)
        def sum(a, b):
            return a + b

        @vectorize
        def sum(a, b):
            return a + b

        @vectorize(identity=1)
        def mul(a, b):
            return a * b

    Nc                    s@   t | fi } D ]}|| q
t dkr|  | S Nr   )r    addlendisable_compilebuild_ufunc)r%   vecsigftylistr&   r   r   wrapz   s   zvectorize.<locals>.wrap)
isinstancestrinspect
isfunctionr   r)   )ftylist_or_functionr&   r9   r   r7   r   	vectorize:   s   
8
r?   c                     sp   t | dkrg  | d dd nt | dkr"| d  | d ntdt tr. g  fdd}|S )	a  guvectorize(ftylist, signature, target='cpu', identity=None, **kws)

    A decorator to create numpy generialized-ufunc object from Numba compiled
    code.

    Args
    -----
    ftylist: iterable
        An iterable of type signatures, which are either
        function type object or a string describing the
        function type.

    signature: str
        A NumPy generialized-ufunc signature.
        e.g. "(m, n), (n, p)->(m, p)"

    identity: int, str, or None
        The identity (or unit) value for the element-wise function
        being implemented.  Allowed values are None (the default), 0, 1,
        and "reorderable".

    cache: bool
        Turns on caching.

    target: str
            A string for code generation target.  Defaults to "cpu".

    Returns
    --------

    A NumPy generialized universal-function

    Example
    -------
        @guvectorize(['void(int32[:,:], int32[:,:], int32[:,:])',
                      'void(float32[:,:], float32[:,:], float32[:,:])'],
                      '(x, y),(x, y)->(x, y)')
        def add_2d_array(a, b, c):
            for i in range(c.shape[0]):
                for j in range(c.shape[1]):
                    c[i, j] = a[i, j] + b[i, j]

       r   r,   T   z3guvectorize() takes one or two positional argumentsc                    sB   t | fi } D ]}|| qt dkr|  | S r0   )r+   r1   r2   r3   r4   )r%   guvecftyr8   r   r.   r   r   r9      s   zguvectorize.<locals>.wrap)r2   
setdefault	TypeErrorr:   r;   )argsr   r9   r   rD   r   guvectorize   s   ,

rH   )r   )r<   numba.np.ufuncr   numba.np.ufunc.parallelr   r   numba.core.registryr   r   r   objectr   r    r+   r?   rH   r   r   r   r   <module>   s    
K