U
    ÞYf
  ã                   @   s$   d Z ddlZd	dd„Zd
dd„ZdS )z	
Medoid.
é    Né   Fc                 C   sÚ   |dkrf| j dd…ddd…f | j  }t d||¡}t tjt |¡dd¡}|rV|S | dd…|f S |dkrÈ| dd…ddd…f |  }t d||¡}t tjt |¡dd¡}|r¸|S | |dd…f S td |¡ƒ‚dS )a  
    Compute the medoid along the specified axis.

    Returns the medoid of the array elements.

    Parameters
    ----------
    a : array_like
        Input array or object that can be converted to an array.
    axis : int
        Axis along which the medoid is computed. The default
        is to compute the median along the last axis of the array.
    indexonly : bool, optional
        If this is set to True, only the index of the medoid is returned.
    Returns
    -------
    medoid : ndarray or int
    r   Núijk,ijk->ij©Úaxisr   úaxis {} out of bounds)ÚTÚnpÚeinsumZargminÚsumÚsqrtÚ
IndexErrorÚformat)Úar   Ú	indexonlyÚdiffÚssumÚidx© r   ú/lib/python3.8/site-packages/hdmedians/medoid.pyÚmedoid	   s    r   c                 C   s  |dkr†| j dd…ddd…f | j  }t d||¡}tjt |¡dd}t | ¡jdd}tj||< t |¡}|rv|S | dd…|f S |dkr
| dd…ddd…f |  }t d||¡}tjt |¡dd}t | ¡jdd}tj||< t |¡}|rú|S | |dd…f S t	d 
|¡ƒ‚dS )a/  
    Compute the medoid along the specified axis, omitting
    observations containing NaNs.

    Returns the medoid of the array elements.

    Parameters
    ----------
    a : array_like
        Input array or object that can be converted to an array.
    axis : int
        Axis along which the medoid is computed. The default
        is to compute the median along the last axis of the array.
    indexonly : bool, optional
        If this is set to True, only the index of the medoid is returned.
    Returns
    -------
    medoid : ndarray or int
    r   Nr   r   r   r   )r   r   r	   Znansumr   ZisnanÚanyÚnanZ	nanargminr   r   )r   r   r   r   r   ZdistÚmaskr   r   r   r   Ú	nanmedoid1   s*    




r   )r   F)r   F)Ú__doc__Znumpyr   r   r   r   r   r   r   Ú<module>   s   
(