o
    "jn%                     @   sr   d dl Z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 d dlmZ G dd	 d	e
jZdS )
    N)_C_ops)
check_typeconvert_dtype)Variable)distribution)in_dynamic_mode)randomc                       sD   e Zd ZdZd fdd	ZdddZdd	 Zd
d Zdd Z  Z	S )Uniformaj
  Uniform distribution with `low` and `high` parameters.

    Mathematical Details

    The probability density function (pdf) is

    .. math::

        pdf(x; a, b) = \frac{1}{Z}, \ a <=x <b

    .. math::

        Z = b - a

    In the above equation:

    * :math:`low = a`,
    * :math:`high = b`,
    * :math:`Z`: is the normalizing constant.

    The parameters `low` and `high` must be shaped in a way that supports
    `Boardcasting` (e.g., `high - low` is a valid operation).

    Note:
        If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .

        .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor

    Args:
        low(int|float|list|tuple|numpy.ndarray|Tensor): The lower boundary of
            uniform distribution.The data type is float32 and float64.
        high(int|float|list|tuple|numpy.ndarray|Tensor): The higher boundary
            of uniform distribution.The data type is float32 and float64.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution import Uniform
            >>> paddle.seed(2023)

            >>> # Without broadcasting, a single uniform distribution [3, 4]:
            >>> u1 = Uniform(low=3.0, high=4.0)
            >>> # 2 distributions [1, 3], [2, 4]
            >>> u2 = Uniform(low=[1.0, 2.0], high=[3.0, 4.0])
            >>> # 4 distributions
            >>> u3 = Uniform(low=[[1.0, 2.0], [3.0, 4.0]],
            ...             high=[[1.5, 2.5], [3.5, 4.5]])
            ...
            >>> # With broadcasting:
            >>> u4 = Uniform(low=3.0, high=[5.0, 6.0, 7.0])

            >>> # Complete example
            >>> value_tensor = paddle.to_tensor([0.8], dtype="float32")

            >>> uniform = Uniform([0.], [2.])

            >>> sample = uniform.sample([2])
            >>> # a random tensor created by uniform distribution with shape: [2, 1]
            >>> entropy = uniform.entropy()
            >>> print(entropy)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.69314718])

            >>> lp = uniform.log_prob(value_tensor)
            >>> print(lp)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [-0.69314718])

            >>> p = uniform.probs(value_tensor)
            >>> print(p)
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [0.50000000])
    Nc              	      sn  t  st|dtttjtttfd t|dtttjtttfd d| _	d| _
|d ur+|nd| _d| _t|tr:t|}t|trCt|}| ||rV|| _|| _t|j| _nWt|trct|trcd| _	t|tjrut|jdv ru|j| _nt|tjrt|jdv r|j| _| ||\| _| _| jt| jjkrtj| j| jd| _tj| j| jd| _t | jj d S )	Nlowr	   highFfloat32T)r   Zfloat64dtype)r   r   intfloatnpZndarrayr   listtupleall_arg_is_floatZbatch_size_unknownnamer   
isinstanceZ_validate_argsr
   r   r   strZ
_to_tensorpaddlecastsuper__init__shape)selfr
   r   r   	__class__ \/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distribution/uniform.pyr   g   sH   


zUniform.__init__r   c                 C   sJ  t  st|dtd t|dtd | jd }t| j| j j}d|v rr|| }t|| }t| j| j d 	 |d< t
|d| j}tj||j| jdd|d	}t||}	t||}
|
|	| j | j  }tj|| j|d
}|S || }tj|| jdd|d	tj|| jd| j| j   }tj|| j|d
}| jrtj|||d
S |S )a   Generate samples of the specified shape.

        Args:
            shape (list): 1D `int32`. Shape of the generated samples.
            seed (int): Python integer number.

        Returns:
            Tensor, A tensor with prepended dimensions shape. The data type is float32.

        r   sampleseed_sampler   g        g      ?)r   minmaxr#   r   r   )r   r   r   r   r   r
   r   r   r   itemfullr   r   Zuniform_random_batch_size_likeZreshapeadduniformZzerosr   )r   r   r#   r   Zbatch_shapeZoutput_shapeZ
fill_shapeZzero_tmpZuniform_random_tmpZzero_tmp_reshapeZuniform_random_tmp_reshapeoutputr    r    r!   r"      sN   

zUniform.samplec                 C   s   |  | j|}t r2| j|k }|| jk }t||j}t||j}t|| t| j| j  S | j	d }| j|k }|| jk }tj||jd}tj||jd}tj
t|| t| j| j |dS )zLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor, log probability.The data type is same with value.

        Z	_log_probr   r(   )_check_values_dtype_in_probsr
   r   r   r   r   r   r   logr   subtractr   valueZlb_boolZub_boolZlbZubr   r    r    r!   log_prob   s   


 


zUniform.log_probc                 C   s   |  | j|}t r,| j|k }|| jk }t||j}t||j}|| | j| j  S | jd }| j|k }|| jk }tj||jd}tj||jd}tj	|| | j| j |dS )zProbability density/mass function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor, probability. The data type is same with value.

        Z_probsr   r(   )
r.   r
   r   r   r   r   r   r   r   divider1   r    r    r!   probs   s   





zUniform.probsc                 C   s    | j d }tj| j| j |dS )zShannon entropy in nats.

        The entropy is

        .. math::

            entropy(low, high) = \\log (high - low)

        Returns:
            Tensor, Shannon entropy of uniform distribution.The data type is float32.

        Z_entropyr(   )r   r   r/   r   r
   )r   r   r    r    r!   entropy  s   
zUniform.entropy)N)r   )
__name__
__module____qualname____doc__r   r"   r3   r5   r6   __classcell__r    r    r   r!   r	      s    L
15r	   )numpyr   r   r   Zpaddle.base.data_feederr   r   Zpaddle.base.frameworkr   Zpaddle.distributionr   Zpaddle.frameworkr   Zpaddle.tensorr   Distributionr	   r    r    r    r!   <module>   s   