Ë
    ˆ\;j&  ã                   óŠ   — 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	e
j                  «      Zy)
é    N)Ú_C_ops)Ú
check_typeÚconvert_dtype)ÚVariable)Údistribution)Úin_dynamic_mode)Úrandomc                   ó>   ‡ — e Zd ZdZdˆ fd„	Zdd„Zd„ Zd„ Z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
    `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])
    c           
      ó
  •— t        «       slt        |dt        t        t        j
                  t        t        t        fd«       t        |dt        t        t        j
                  t        t        t        fd«       d| _	        d| _
        |�|nd| _        d| _        t        |t        «      rt        |«      }t        |t        «      rt        |«      }| j                  ||«      r*|| _        || _        t#        |j                  «      | _        �nWt        |t        «      rt        |t        «      rd| _	        t        |t        j
                  «      r)t%        |j                  «      dv r|j                  | _        nBt        |t        j
                  «      r(t%        |j                  «      dv r|j                  | _        | j'                  ||«      \  | _        | _        | j                  t#        | j                  j                  «      k7  r`t)        j*                  | j                  | j                  ¬«      | _        t)        j*                  | j                   | j                  ¬«      | _        t,        ‰| �]  | j                  j0                  «       y )	NÚlowr   ÚhighFÚfloat32T)r   Úfloat64©Údtype)r   r   ÚintÚfloatÚnpÚndarrayr   ÚlistÚtupleÚall_arg_is_floatÚbatch_size_unknownÚnamer   Ú
isinstanceÚ_validate_argsr   r   r   ÚstrÚ
_to_tensorÚpaddleÚcastÚsuperÚ__init__Úshape)Úselfr   r   r   Ú	__class__s       €údG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/distribution/uniform.pyr#   zUniform.__init__g   sÆ  ø€ ÜÔ ÜØØÜ”eœRŸZ™Z¬´4¼Ð?Øô	ô ØØÜ”eœRŸZ™Z¬´4¼Ð?Øô	ð !&ˆÔØ"'ˆÔØ Ð,‘D°)ˆŒ	ØˆŒ
ä�cœ3ÔÜ˜“*ˆCÜ�dœCÔ Ü˜“;ˆDà×Ñ˜s DÔ)ØˆDŒHØˆDŒIÜ& s§y¡yÓ1ˆDŽJä˜#œuÔ%¬*°T¼5Ô*AØ(,�Ô%Ü˜#œrŸz™zÔ*¬s°3·9±9«~ð Bñ 0ð !ŸY™Y�•
Ü˜D¤"§*¡*Ô-´#°d·j±j³/ð Fñ 3ð "ŸZ™Z�”
Ø"&§/¡/°#°tÓ"<ÑˆDŒH�d”iØ�z‰zœ]¨4¯8©8¯>©>Ó:Ò:Ü!Ÿ;™; t§x¡x°t·z±zÔB�”Ü"ŸK™K¨¯	©	¸¿¹ÔD�”	ä‰Ñ˜Ÿ™Ÿ™Õ(ó    c                 ó\  — t        «       s$t        |dt        d«       t        |dt        d«       | j                  dz   }t        | j
                  | j                  z   j                  «      }d|v �r||z   }t        ||z   «      }t        j                  | j
                  | j                  z   «      d   j                  «       |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 )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Ú_sampleéÿÿÿÿr   g        g      ð?)r   ÚminÚmaxr+   ©r   r   )r   r   r   r   r   r   r   r$   r    ÚitemÚfullr   r	   Úuniform_random_batch_size_likeÚreshapeÚaddÚuniformÚzerosr   )r%   r$   r+   r   Úbatch_shapeÚoutput_shapeÚ
fill_shapeÚzero_tmpÚuniform_random_tmpÚzero_tmp_reshapeÚuniform_random_tmp_reshapeÚoutputs               r'   r*   zUniform.sample˜   sÓ  € ô Ô Ü�u˜g¬¨xÔ8Ü�t˜V¤c¨HÔ5à�y‰y˜9Ñ$ˆÜ˜DŸH™H t§y¡yÑ0×7Ñ7Ó8ˆØ�ÒØ  ;Ñ.ˆLÜ˜k¨EÑ1Ó2ˆJÜ"ŸL™L¨¯©°D·I±IÑ)=Ó>¸qÑA×FÑFÓHˆJ�q‰MÜ—{‘{ :¨s°D·J±JÓ?ˆHÜ!'×!FÑ!FØØ—‘Ø—j‘jØØØô"Ðô  &Ÿ~™~¨h¸ÓEÐÜ)/¯©Ø" Ló*Ð&ð 0Ø  4§9¡9Ñ,¨t¯x©xÑ7ñˆFô —Z‘Z ¨¯©°tÔ<ˆFØˆMà  ;Ñ.ˆLÜ—^‘^Ø D§J¡J°C¸SÀtôô —‘˜\°·±Ô<Ø—9‘9˜tŸx™xÑ'ñ)ñˆFô —Z‘Z ¨¯©°tÔ<ˆFØ×$Ò$Ü—~‘~ f¨e¸$Ô?Ð?à�r(   c                 ó"  — | j                  | 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 )zÅLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

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

        Ú	_log_probr   r0   )Ú_check_values_dtype_in_probsr   r   r   r   r!   r   r    Úlogr   Úsubtract©r%   ÚvalueÚlb_boolÚub_boolÚlbÚubr   s          r'   Úlog_probzUniform.log_probÍ   s  € ð ×1Ñ1°$·(±(¸EÓBˆÜÔà—h‘h Ñ&ˆGØ˜dŸi™iÑ'ˆGä—‘˜W e§k¡kÓ2ˆBÜ—‘˜W e§k¡kÓ2ˆBÜ—:‘:˜b 2™gÓ&¬¯©°D·I±IÀÇÁÑ4HÓ)IÑIÐIà—9‘9˜{Ñ*ˆDØ—h‘h Ñ&ˆGØ˜dŸi™iÑ'ˆGÜ—‘˜W¨E¯K©KÔ8ˆBÜ—‘˜W¨E¯K©KÔ8ˆBÜ—?‘?Ü—
‘
˜2 ™7Ó#¤V§Z¡Z°·	±	¸D¿H¹HÑ0DÓ%EÈDôð r(   c                 óŠ  — | j                  | 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 )z¾Probability density/mass function.

        Args:
            value (Tensor): The input tensor.

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

        Ú_probsr   r0   )
rB   r   r   r   r   r!   r   r   r    ÚdividerE   s          r'   ÚprobszUniform.probsê   sö   € ð ×1Ñ1°$·(±(¸EÓBˆÜÔØ—h‘h Ñ&ˆGØ˜dŸi™iÑ'ˆGÜ—‘˜W e§k¡kÓ2ˆBÜ—‘˜W e§k¡kÓ2ˆBØ˜‘G §	¡	¨D¯H©HÑ 4Ñ5Ð5à—9‘9˜xÑ'ˆDØ—h‘h Ñ&ˆGØ˜dŸi™iÑ'ˆGÜ—‘˜W¨E¯K©KÔ8ˆBÜ—‘˜W¨E¯K©KÔ8ˆBÜ—=‘= " r¡'¨T¯Y©Y¸¿¹Ñ-AÈÔNÐNr(   c                 ó|   — | j                   dz   }t        j                  | j                  | j                  z
  |¬«      S )zêShannon 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.

        Ú_entropyr0   )r   r    rC   r   r   )r%   r   s     r'   ÚentropyzUniform.entropy  s0   € ð �y‰y˜:Ñ%ˆÜ�z‰z˜$Ÿ)™) d§h¡hÑ.°TÔ:Ð:r(   )N)r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r*   rK   rO   rR   Ú__classcell__)r&   s   @r'   r   r      s&   ø„ ñJõX/)ób3òjò:Oö2;r(   r   )Únumpyr   r    r   Úpaddle.base.data_feederr   r   Úpaddle.base.frameworkr   Úpaddle.distributionr   Úpaddle.frameworkr   Úpaddle.tensorr	   ÚDistributionr   © r(   r'   Ú<module>r`      s3   ðó ã Ý ß =Ý *Ý ,Ý ,Ý  ôw;ˆl×'Ñ'õ w;r(   