o
    oi|                     @   s   d dl mZ d dlZd dlmZmZmZ ddedededefd	d
Z	ddedededefddZ
dedefddZdedefddZ			ddedee dee dedef
ddZdS )    )OptionalN)TensoreyezerosFninputshared_memoryreturnc                 C   sx   | dkrt t| | t|jdk rt |jt| |jd|j}|r0|d |jd | | S |d |jd ddS )a5  Return a 2-D tensor with ones on the diagonal and zeros elsewhere with the same batch size as the input.

    Args:
        n: the number of rows :math:`(N)`.
        input: image tensor that will determine the batch size of the output matrix.
          The expected shape is :math:`(B, *)`.
        shared_memory: when set, all samples in the batch will share the same memory.

    Returns:
       The identity matrix with the same batch size as the input :math:`(B, N, N)`.

    Notes:
        When the dimension to expand is of size 1, using torch.expand(...) yields the same tensor as torch.repeat(...)
        without using extra memory. Thus, when the tensor obtained by this method will be later assigned -
        use this method with shared_memory=False, otherwise, prefer using it with shared_memory=True.

    r      )deviceN)	AssertionErrortypelenshaper   r   dtypeexpandrepeat)r   r   r   identity r   E/home/ubuntu/.local/lib/python3.10/site-packages/kornia/utils/misc.pyeye_like   s   
4r   tensorc                 C   sv   | dkrt t| | t|jdk rt |jt| d|j|jd}|r/|d |jd | dS |d |jd ddS )a  Return a 2-D tensor with a vector containing zeros with the same batch size as the input.

    Args:
        n: the number of rows :math:`(N)`.
        tensor: image tensor that will determine the batch size of the output matrix.
          The expected shape is :math:`(B, *)`.
        shared_memory: when set, all samples in the batch will share the same memory.

    Returns:
        The vector with the same batch size as the input :math:`(B, N, 1)`.

    Notes:
        When the dimension to expand is of size 1, using torch.expand(...) yields the same tensor as torch.repeat(...)
        without using extra memory. Thus, when the tensor obtained by this method will be later assigned -
        use this method with shared_memory=False, otherwise, prefer using it with shared_memory=True.

    r   r
   )r   r   N)	r   r   r   r   r   r   r   r   r   )r   r   r   vecr   r   r   vec_like5   s   
4r   c                 C   s   |   }|| | d  }|S )zDifferentiable rounding.

    Args:
        input (Tensor): Input tensor of any shape to be rounded.

    Returns:
        output (Tensor): Pseudo rounded tensor of the same shape as input tensor.

       )round)r   input_roundoutputr   r   r   "differentiable_polynomial_roundingP   s   r   c                 C   s    |   }|| d | d  }|S )zPerform floor via a differentiable operation.

    Args:
        input (Tensor): Input tensor of any shape to be floored.

    Returns:
        output (Tensor): Pseudo rounded tensor of the same shape as input tensor.

    g      ?r   )floor)r   input_floorr   r   r   r   differentiable_polynomial_floor`   s   r!   {Gz?min_valmax_valscalec                 C   st   |   }|dur| t|||k  | d  | |||k< |dur8|t|||k  | d  | |||k < |S )a  Clip via a differentiable and soft approximation of the clipping operation.

    Args:
        input (Tensor): Input tensor of any shape.
        min_val (Optional[float]): Minimum value.
        max_val (Optional[float]): Maximum value.
        scale (float): Scale value. Default 0.02.

    Returns:
        output (Tensor): Clipped output tensor of the same shape as the input tensor.

    Ng      ?)clonetorchexp)r   r#   r$   r%   r   r   r   r   differentiable_clippingp   s   .*r)   )F)NNr"   )typingr   r'   kornia.corer   r   r   intboolr   r   r   r!   floatr)   r   r   r   r   <module>   s*   