
    {-j                        d dl mZ d dlZd dlmZ d dlmZ d dlmZ ddlm	Z	m
Z
 ddlmZ g Z ed	gd
gdgd          	 	 	 	 ddd            Z	 dd dZdS )!    )annotationsN)_C_ops)in_dynamic_or_pir_mode)ParamAliasDecorator   )
check_typecheck_variable_and_dtype)LayerHelperx1x2eps)xyepsilon       @ư>Fr   paddle.Tensorr   pfloatr   keepdimboolname
str | Nonereturnc                N   t                      ret          j        | |          }|dk    r1t          j        |g|j                  }t          j        ||          }t          j        ||dd|d          S t          |dt          t          fd           t          |dt          d           t          |dt          d           t          | d	g d
d           t          |dg d
d           t          j        | |          }|dk    rR|j                            |j                  }t          j        dg||j                  }t          j        ||          }t!          d|          }d||dd}	|                    | j                  }
|                    dd|id|
i|	           |
S )a  

    It computes the pairwise distance between two vectors. The
    distance is calculated by p-order norm:

    .. math::

        \Vert x \Vert _p = \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}.

    Parameters:
        x (Tensor): Tensor, shape is :math:`[N, D]` or :math:`[D]`, where :math:`N`
            is batch size, :math:`D` is the dimension of vector. Available dtype is
            float16, float32, float64.
        y (Tensor): Tensor, shape is :math:`[N, D]` or :math:`[D]`, where :math:`N`
            is batch size, :math:`D` is the dimension of vector. Available dtype is
            float16, float32, float64.
        p (float, optional): The order of norm. Default: :math:`2.0`.
        epsilon (float, optional): Add small value to avoid division by zero.
            Default: :math:`1e-6`.
        keepdim (bool, optional): Whether to reserve the reduced dimension
            in the output Tensor. The result tensor is one dimension less than
            the result of ``|x-y|`` unless :attr:`keepdim` is True. Default: False.
        name (str|None, optional): For details, please refer to :ref:`api_guide_Name`.
            Generally, no setting is required. Default: None.

    Returns:
        Tensor, the dtype is same as input tensor.

        - If :attr:`keepdim` is True, the output shape is :math:`[N, 1]` or :math:`[1]`,
          depending on whether the input has data shaped as :math:`[N, D]`.
        - If :attr:`keepdim` is False, the output shape is :math:`[N]` or :math:`[]`,
          depending on whether the input has data shaped as :math:`[N, D]`.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> x = paddle.to_tensor([[1., 3.], [3., 5.]], dtype=paddle.float64)
            >>> y = paddle.to_tensor([[5., 6.], [7., 8.]], dtype=paddle.float64)
            >>> distance = paddle.nn.functional.pairwise_distance(x, y)
            >>> print(distance)
            Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
            [4.99999860, 4.99999860])
    g        dtypeFporderPairwiseDistancer   r   r   )float16float32float64r      )shape
fill_valuer   )r   )axisr   r   r   p_normXOut)typeinputsoutputsattrs)r   r   subtractpaddle	to_tensorr   addr(   r   r   intr   r	   block
create_varfullr
   "create_variable_for_type_inference	append_op)r   r   r   r   r   r   subepsilon_varhelperr.   outs              ]/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/functional/distance.pypairwise_distancer>      s   v  &oa##c>>&y	BBBG*S'**C}S!Rgu=== 	1h.@AAA7I0BCCC7I/ABBB s5557I	
 	
 	
 	!s5557I	
 	
 	
 oa##c>>)..SY.??K +cgSY  K *S+..C/d;;;	
 
 77ag7FF3*ucl% 	 	
 	
 	
 
    c           	     f   t          | j                  }t          |          dk    s
J d            t          j                            | ddddf         | ddddddf         z
  |d          }t          j        t          j        |j        d                     }t          j        ||          S )	ab  
    Computes the p-norm distance between every pair of row vectors in the input.

    Args:
        x (Tensor): The input tensor with shape :math:`N \times M`.
        p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        Tensor with shape :math:`N(N-1)/2` , the dtype is same as input tensor.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(2023)
            >>> a = paddle.randn([4, 5])
            >>> print(a)
            Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
                   [[ 0.06132207,  1.11349595,  0.41906244, -0.24858207, -1.85169315],
                    [-1.50370061,  1.73954511,  0.13331604,  1.66359663, -0.55764782],
                    [-0.59911072, -0.57773495, -1.03176904, -0.33741450, -0.29695082],
                    [-1.50258386,  0.67233968, -1.07747352,  0.80170447, -0.06695852]])
            >>> pdist_out=paddle.pdist(a)
            >>> print(pdist_out)
            Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
                   [2.87295413, 2.79758120, 3.02793980, 3.40844536, 1.89435327, 1.93171620])
       zThe x must be 2-dimensional.Nr   )r   r'   r   r   )	listr%   lenr0   linalgnormtrilonesmasked_select)r   r   r   x_shapedmasks         r=   pdistrL      s    @ 17mmGw<<1;1S$\?QsD!!!QQQ-??12NNAKAG6:::;;;D4(((r?   )r   r   FN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   N)r   r   r   r   r   r   r   r   )
__future__r   r0   r   paddle.frameworkr   paddle.utils.decorator_utilsr   base.data_feederr   r	   base.layer_helperr
   __all__r>   rL    r?   r=   <module>rT      s   # " " " " "        3 3 3 3 3 3 < < < < < < D D D D D D D D , , , , , ,
 VV7   Z Z Z Z Z| :>$) $) $) $) $) $) $)r?   