
    zj)                    d   U d dl mZ d dlmZ d dlZd dlm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 erYd d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ eeee         ee         ej        eej        ej         f                  ef         Z!de"d<    G d dej#                  Z$dS )    )annotations)TYPE_CHECKINGN)_C_ops)
check_typeconvert_dtype)Variable)distribution)in_dynamic_mode)random)Sequence)Union)	TypeAlias)Tensor)NestedSequencer   _UniformBoundaryc                  d     e Zd ZU dZded<   ded<   	 dd fdZg dfddZddZddZddZ	 x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
    `Broadcasting` (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])
    r   lowhighNr   name
str | NonereturnNonec                f   t                      st          |dt          t          t          j        t          t          j        j	        t          t          fd           t          |dt          t          t          j        t          t          j        j	        t          t          fd           d| _        d| _        ||nd| _        d| _        t!          |t                    rt          |          }t!          |t                    rt          |          }|                     ||          r)|| _        || _        t)          |j                  | _        n9t!          |t                    rt!          |t                    rd| _        t!          |t          j                  r#t+          |j                  dv r|j        | _        n<t!          |t          j                  r"t+          |j                  dv r|j        | _        |                     ||          \  | _        | _        | j        t)          | j        j                  k    rJt          j        | j        | j                  | _        t          j        | j        | j                  | _        t1                                          | j        j                   d S )	Nr   r   r   Ffloat32T)r   float64dtype)r
   r   intfloatnpndarrayr   paddlepirValuelisttupleall_arg_is_floatbatch_size_unknownr   r   
isinstance_validate_argsr   r   r   str
_to_tensorcastsuper__init__shape)selfr   r   r   	__class__s       k/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/uniform.pyr0   zUniform.__init__   sX       	JJ$    JJ$    !&"' ,DD)	
c3 	**CdC   	;;DsD)) 	EDHDI&sy11DJJ#u%% -*T5*A*A -(,%#rz** 	(s39~~ B 0 0 !Y

D"*-- (#dj// F 3 3 "Z
"&//#t"<"<DHdiz]48>::::!;txtzBBB"K	DDD	(((((    r   r1   Sequence[int]seedr   c                   t                      s5t          |dt          t          fd           t          |dt          d           t          |          }| j        dz   }t          | j        | j        z   j                  }d|v r||z   }t          ||z             }t          j        | j        | j        z             d         
                                |d<   t          j        |d| j                  }t          j        ||j        | j        dd|	          }t          j        ||          }	t          j        ||          }
|
|	| j        z   | j        z
  z  }t          j        || j        |
          }|S ||z   }t          j        || j        dd|	          t          j        || j                  | j        | j        z
  z   z  }t          j        || j        |
          }| j        rt          j        |||
          S |S )aS  Generate samples of the specified shape.

        Args:
            shape (Sequence[int], optional): 1D `int32`. Shape of the generated samples.
                Defaults to [].
            seed (int): Python integer number.

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

        r1   sampler7   _sampler   g        g      ?)r   minmaxr7   r   r   )r
   r   r&   r'   r   r   r   r   r1   r#   itemfullr   r   uniform_random_batch_size_likereshapeadduniformzerosr(   )r2   r1   r7   r   batch_shapeoutput_shape
fill_shapezero_tmpuniform_random_tmpzero_tmp_reshapeuniform_random_tmp_reshapeoutputs               r4   r9   zUniform.sample   s       	6uge}h???tVcH555Uy9$DHty0788 ;.LkE122J"LDI)=>>qAFFHHJqM{:sDJ??H!'!Fj" " "  &~hEE)/"L* *& 0 49,tx7F Zt<<<FM ;.L^DJCSt   \<<<9tx')F Zt<<<F$ ~fe$????r5   valuec                   |                      | j        |          }t                      r| j        |k     }|| j        k     }t	          j        ||j                  }t	          j        ||j                  }t          j        ||z            t          j        | j        | j        z
            z
  S | j	        dz   }| j        |k     }|| j        k     }t          j        ||j                  }t          j        ||j                  }t          j
        t          j        ||z            t          j        | j        | j        z
            |          S )zLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

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

        	_log_probr   r>   )_check_values_dtype_in_probsr   r
   r   r   r.   r   r#   logr   subtractr2   rN   lb_boolub_boollbubr   s          r4   log_probzUniform.log_prob   s$    11$(EBB 	h&Gdi'GWek22BWek22B:b2g&&DI4H)I)III9{*Dh&Gdi'GWEK888BWEK888B?
27##VZ	DH0D%E%ED   r5   c                   |                      | j        |          }t                      r_| j        |k     }|| j        k     }t	          j        ||j                  }t	          j        ||j                  }||z  | j        | j        z
  z  S | j        dz   }| j        |k     }|| j        k     }t          j        ||j                  }t          j        ||j                  }t          j	        ||z  | j        | j        z
  |          S )zProbability density/mass function.

        Args:
            value (Tensor): The input tensor.

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

        _probsr   r>   )
rQ   r   r
   r   r   r.   r   r   r#   dividerT   s          r4   probszUniform.probs  s     11$(EBB 	Oh&Gdi'GWek22BWek22BG	DH 4559x'Dh&Gdi'GWEK888BWEK888B="r'TY-ANNNNr5   c                \    | j         dz   }t          j        | j        | j        z
  |          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.

        _entropyr>   )r   r#   rR   r   r   )r2   r   s     r4   entropyzUniform.entropy1  s.     y:%z$)dh.T::::r5   )N)r   r   r   r   r   r   r   r   )r1   r6   r7   r   r   r   )rN   r   r   r   )r   r   )__name__
__module____qualname____doc____annotations__r0   r9   rY   r]   r`   __classcell__)r3   s   @r4   r   r   /   s         J JX KKKLLL  	D) D) D) D) D) D) D)L -/A 4 4 4 4 4l   :O O O O2; ; ; ; ; ; ; ;r5   r   )%
__future__r   typingr   numpyr!   numpy.typingnptr#   r   paddle.base.data_feederr   r   paddle.base.frameworkr   paddle.distributionr	   paddle.frameworkr
   paddle.tensorr   collections.abcr   r   typing_extensionsr   r   paddle._typingr   r    NDArrayr   r   r   re   Distributionr    r5   r4   <module>rw      s   # " " " " " "                              = = = = = = = = * * * * * * , , , , , , , , , , , ,             ((((((++++++------"'uE"*bj012		#    P; P; P; P; P;l' P; P; P; P; P;r5   