U
    ©Ê¹jˆ  ã                   @   s¼   d Z ddlZddlZddlmZ dd„ ZG dd„ deeƒZG d	d
„ d
e	e
ƒZdd„ Zdd„ Zd%dd„Zd&dd„Zdd„ Zdd„ Zdd„ Zddœdd„Zdd„ Zdd „ Zd'd!d"„Zd#d$„ ZdS )(zLAssorted utilities, which do not need anything other then torch and stdlib.
é    Né   )Ú_dtypes_implc                 C   s6   t | tƒrdS zt| ƒ W n tk
r0   Y dS X dS )NFT)Ú
isinstanceÚstrÚlenÚ	Exception)Úseq© r	   úQ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/_numpy/_util.pyÚis_sequence   s    
r   c                   @   s   e Zd ZdS )Ú	AxisErrorN©Ú__name__Ú
__module__Ú__qualname__r	   r	   r	   r
   r      s   r   c                   @   s   e Zd ZdS )ÚUFuncTypeErrorNr   r	   r	   r	   r
   r      s   r   c                 C   s    |d k	r| j |kr|  |¡} | S ©N)ÚdtypeÚto)Útensorr   r	   r	   r
   Úcast_if_needed   s    
r   c                 C   s$   t  | j¡dk r |  t  ¡ j¡} | S )Né   )r   Z	_categoryr   r   Zdefault_dtypesZfloat_dtype)Úxr	   r	   r
   Úcast_int_to_float%   s    r   c                 C   s@   | |   kr|k s,n t d| › d|› �ƒ‚| dk r<| |7 } | S )Nzaxis z) is out of bounds for array of dimension r   )r   )ÚaxÚndimÚargnamer	   r	   r
   Únormalize_axis_index-   s
    r   Fc                    sˆ   t | ƒttfkr6zt | ¡g} W n tk
r4   Y nX t‡ ‡fdd„| D ƒƒ} |s„tt| ƒƒt| ƒkr„ˆ r|tdˆ › d�ƒ‚ntdƒ‚| S )aÎ  
    Normalizes an axis argument into a tuple of non-negative integer axes.

    This handles shorthands such as ``1`` and converts them to ``(1,)``,
    as well as performing the handling of negative indices covered by
    `normalize_axis_index`.

    By default, this forbids axes from being specified multiple times.
    Used internally by multi-axis-checking logic.

    Parameters
    ----------
    axis : int, iterable of int
        The un-normalized index or indices of the axis.
    ndim : int
        The number of dimensions of the array that `axis` should be normalized
        against.
    argname : str, optional
        A prefix to put before the error message, typically the name of the
        argument.
    allow_duplicate : bool, optional
        If False, the default, disallow an axis from being specified twice.

    Returns
    -------
    normalized_axes : tuple of int
        The normalized axis index, such that `0 <= normalized_axis < ndim`
    c                    s   g | ]}t |ˆˆ ƒ‘qS r	   )r   ©Ú.0r   ©r   r   r	   r
   Ú
<listcomp>Z   s     z(normalize_axis_tuple.<locals>.<listcomp>zrepeated axis in `z
` argumentzrepeated axis)	ÚtypeÚtupleÚlistÚoperatorÚindexÚ	TypeErrorr   ÚsetÚ
ValueError)Úaxisr   r   Zallow_duplicater	   r    r
   Únormalize_axis_tuple6   s    r+   c                 C   s(   | d kr| S t | ƒdkr tdƒ‚| d S )Nr   zdoes not handle tuple axisr   )r   ÚNotImplementedError©r*   r	   r	   r
   Úallow_only_single_axisc   s
    r.   c                    sT   t ˆ ƒttfkrˆ f‰ tˆ ƒt| ƒ }tˆ |ƒ‰ t| ƒ‰‡ ‡fdd„t|ƒD ƒ}|S )Nc                    s    g | ]}|ˆ krd nt ˆƒ‘qS ©r   )Únextr   ©r*   Zshape_itr	   r
   r!   r   s     z expand_shape.<locals>.<listcomp>)r"   r$   r#   r   r+   ÚiterÚrange)Z	arr_shaper*   Zout_ndimÚshaper	   r1   r
   Úexpand_shapek   s    
r5   c                 C   s:   |d kr d| }|   |¡ ¡ } nt| j|ƒ}|  |¡} | S )Nr/   )ÚexpandÚ
contiguousr5   r4   Zreshape)r   r*   r   r4   r	   r	   r
   Úapply_keepdimsv   s    
r8   r-   c                 G   s.   | dkr"t dd„ |D ƒƒ}|dfS || fS dS )z#Flatten the arrays if axis is None.Nc                 s   s   | ]}|  ¡ V  qd S r   )Úflatten)r   Úarr	   r	   r
   Ú	<genexpr>„   s     z$axis_none_flatten.<locals>.<genexpr>r   ©r#   )r*   Útensorsr	   r	   r
   Úaxis_none_flatten�   s    r>   c                 C   s>   t j}|| j||d�s4td| j› d|› d|› d�ƒ‚t| |ƒS )aÄ  Dtype-cast tensor to target_dtype.

    Parameters
    ----------
    t : torch.Tensor
        The tensor to cast
    target_dtype : torch dtype object
        The array dtype to cast all tensors to
    casting : str
        The casting mode, see `np.can_cast`

     Returns
     -------
    `torch.Tensor` of the `target_dtype` dtype

     Raises
     ------
     ValueError
        if the argument cannot be cast according to the `casting` rule

    )ÚcastingzCannot cast array data from z to z according to the rule 'ú')r   Zcan_cast_implr   r'   r   )ÚtÚtarget_dtyper?   Zcan_castr	   r	   r
   Útypecast_tensorŠ   s    ÿrC   c                    s   t ‡ ‡fdd„| D ƒƒS )Nc                 3   s   | ]}t |ˆˆ ƒV  qd S r   )rC   )r   rA   ©r?   rB   r	   r
   r;   «   s     z#typecast_tensors.<locals>.<genexpr>r<   )r=   rB   r?   r	   rD   r
   Útypecast_tensorsª   s    rE   c                 C   sv   t | tjƒr| }n$t | ¡}t |j¡}tj| |d�}t||ƒ}||j }|dkrf| 	d| |j
 ¡}|rr| ¡ }|S )a¼  The core logic of the array(...) function.

    Parameters
    ----------
    obj : tensor_like
        The thing to coerce
    dtype : torch.dtype object or None
        Coerce to this torch dtype
    copy : bool
        Copy or not
    ndmin : int
        The results as least this many dimensions
    is_weak : bool
        Whether obj is a weakly typed python scalar.

    Returns
    -------
    tensor : torch.Tensor
        a tensor object with requested dtype, ndim and copy semantics.

    Notes
    -----
    This is almost a "tensor_like" coersion function. Does not handle wrapper
    ndarrays (those should be handled in the ndarray-aware layer prior to
    invoking this function).
    )r   r   r/   )r   ÚtorchZTensorZ	as_tensorr   Zget_default_dtype_forr   r   r   Úviewr4   Úclone)Úobjr   ÚcopyZndminr   Ztorch_dtypeZ
ndim_extrar	   r	   r
   Ú_coerce_to_tensor®   s    
	

rK   c                  G   s”   ddl m} t| ƒdkrtƒ S t| ƒdkrz| d }t||ƒrB|jS t|tƒrtg }|D ]}t|ƒ}| |¡ qTt|ƒS |S nt| tƒsˆt	‚t| ƒS dS )zHConvert all ndarrays from `inputs` to tensors. (other things are intact)r   )Úndarrayr   N)
Z_ndarrayrL   r   r)   r   r   r#   Úndarrays_to_tensorsÚappendÚAssertionError)ÚinputsrL   Zinput_ÚresultZ	sub_inputZ
sub_resultr	   r	   r
   rM   ç   s     

rM   )N)NF)NFr   )Ú__doc__r%   rF   Ú r   r   r)   Ú
IndexErrorr   r'   ÚRuntimeErrorr   r   r   r   r+   r.   r5   r8   r>   rC   rE   rK   rM   r	   r	   r	   r
   Ú<module>   s$   

	
-	 
9