
    Αi{E                    <   % S SK Jr  S SKrS SKJrJr  S SKJr  S SKr	S SK
Jr  S SKrS SKJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  \(       a  S S
KJr  S SKJr  S SKJrJr  S SKJr  \\\ 4   r!S\"S'   \\	RF                  \	RH                  \	RJ                  \	RL                  4   r'S\"S'   \\!\\!   \\!   \RP                  \'   \4   r)S\"S'   \\\\   \\   \RP                  \\	RF                  \	RH                  4      \4   r*S\"S'    " S S\RV                  5      r,g)    )annotationsN)IterableSequence)TYPE_CHECKING)
check_typeconvert_dtype)Variable)distribution)in_dynamic_mode)random)Union)	TypeAlias)Tensordtype)NestedSequencer   _NormalLocBase_NormalLocNDArray
_NormalLoc_NormalScalec                     ^  \ rS rSr% SrS\S'   S\S'   S\S'   S\S'    S       SU 4S	 jjjr\SS
 j5       r\SS j5       r	/ S4SS jjr
/ 4SS jjrSS jrSS jrSS jrSS jrSrU =r$ )Normal:   a
  The Normal distribution with location `loc` and `scale` parameters.

Mathematical details

If 'loc' is real number, the probability density function (pdf) is

.. math::

    pdf(x; \mu, \sigma) = \frac{1}{Z}e^{\frac {-0.5 (x - \mu)^2}  {\sigma^2} }

.. math::

    Z = (2 \pi \sigma^2)^{0.5}

If 'loc' is complex number, the probability density function (pdf) is

.. math::

    pdf(x; \mu, \sigma) = \frac{1}{Z}e^{\frac {-(x - \mu)^2}  {\sigma^2} }

.. math::

    Z = \pi \sigma^2

In the above equations:

* :math:`loc = \mu`: is the mean.
* :math:`scale = \sigma`: is the std.
* :math:`Z`: is the normalization constant.

Args:
    loc(int|float|complex|list|tuple|numpy.ndarray|Tensor): The mean of normal distribution.The data type is float32, float64, complex64 and complex128.
    scale(int|float|list|tuple|numpy.ndarray|Tensor): The std of normal distribution.The data type is float32 and float64.
    name(str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Normal

        >>> # Define a single scalar Normal distribution.
        >>> dist = Normal(loc=0., scale=3.)
        >>> # Define a batch of two scalar valued Normals.
        >>> # The first has mean 1 and standard deviation 11, the second 2 and 22.
        >>> dist = Normal(loc=[1., 2.], scale=[11., 22.])
        >>> # Get 3 samples, returning a 3 x 2 tensor.
        >>> dist.sample([3])

        >>> # Define a batch of two scalar valued Normals.
        >>> # Both have mean 1, but different standard deviations.
        >>> dist = Normal(loc=1., scale=[11., 22.])

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

        >>> normal_a = Normal([0.], [1.])
        >>> normal_b = Normal([0.5], [2.])
        >>> sample = normal_a.sample([2])
        >>> # a random tensor created by normal distribution with shape: [2, 1]
        >>> entropy = normal_a.entropy()
        >>> print(entropy)
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [1.41893852])
        >>> lp = normal_a.log_prob(value_tensor)
        >>> print(lp)
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [-1.23893857])
        >>> p = normal_a.probs(value_tensor)
        >>> print(p)
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.28969154])
        >>> kl = normal_a.kl_divergence(normal_b)
        >>> print(kl)
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.34939718])
r   locscalestrnamer   c                  > [        5       (       d  [        US[        [        [        [
        R                  [        [        R                  R                  [        [        4S5        [        US[        [        [
        R                  [        [        R                  R                  [        [        4S5        SU l        Ub  UOSU l        SU l        SU l        [#        U[        5      (       a  [        U5      n[#        U[        5      (       a  [        U5      n[#        U[        [        45      (       at  [
        R$                  " U5      nUR                  [
        R&                  :X  a  UR)                  S5      nUR                  [
        R*                  :X  a  UR)                  S5      n[#        U[        [        45      (       a#  [
        R$                  " U[
        R,                  S9n[#        U[        5      (       dz  [#        U[
        R                  5      (       a.  UR                  [
        R.                  [
        R*                  4;   d-  U R1                  U5      (       Ga#  UR3                  5       (       Ga  SU l        [#        U[        5      (       a  [#        U[        5      (       a  SU l        [#        U[
        R                  5      (       a  UR                  [
        R.                  :X  a  SOS	nUR                  [
        R.                  :X  a  SOS	n[        R4                  " UR6                  U5      n[        R4                  " UR8                  U5      n[        R                  " Xg5      U l        Ou[#        U[        5      (       aZ  [        R4                  " UR6                  SS9n[        R4                  " UR8                  SS9n[        R                  " Xg5      U l        OXl        [#        U[
        R                  5      (       a$  [        R4                  " X"R                  S9U l        O6[#        U[        5      (       a  [        R4                  " USS9U l        OX l        [?        U R:                  R                  5      U l        GOU R1                  X5      (       a(  Xl        X l        [?        UR                  5      U l        GOk[#        U[        5      (       a  [#        U[        5      (       a  SU l        [#        U[
        R                  5      (       a+  [A        UR                  5      S
;   a  UR                  U l        OI[#        U[
        R                  5      (       a*  [A        UR                  5      S
;   a  UR                  U l        U RC                  X5      u  U l        U l        U R                  [?        U R:                  R                  5      :w  a\  [        RD                  " U R:                  U R                  S9U l        [        RD                  " U R<                  U R                  S9U l        [F        TU ]  U R:                  RJ                  5        g )Nr   r   r   Ffloat32	complex64r   Tfloat64)r   r!   )&r   r   intfloatcomplexnpndarrayr	   paddlepirValuelisttupleall_arg_is_floatr   r   _complex_gaussian
isinstancearrayr!   astype
complex128r   r   _validate_args
is_complex	to_tensorrealimagr   r   r   r   
_to_tensorcastsuper__init__shape)	selfr   r   r   
real_dtype
imag_dtyper5   r6   	__class__s	           Z/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/normal.pyr:   Normal.__init__   s8      JJJJ$$	  JJJJ$$  !& ,D(	
!&c3*CeS!!%LEcE4=))((3-CyyBJJ&jj+yyBMM)jj-eeT]++HHU"**5E sG$$3

++II",,!>>##C((S^^-=-=%)D"#w''Jue,D,D(,%#rzz**!$bll!:I	  "%bll!:I	  ''*=''*=!>>$5C))''	B''	B!>>$5%,,#--e;;G
E5))#--e9E
"
&txx~~6DJ""3.."
*3995
c5))j.F.F,0D)c2::..3syy> F 4 "%DJrzz22s5;;7G L 8 "'DJ'+s'B$$*::txx~~!>>%{{4884::FDH!'TZZtzz!JDJ(    c                    U R                   $ )z?Mean of normal distribution.

Returns:
    Tensor: mean value.
)r   r<   s    r@   meanNormal.mean  s     xxrB   c                8    U R                   R                  S5      $ )zGVariance of normal distribution.

Returns:
    Tensor: variance value.
   )r   powrD   s    r@   varianceNormal.variance
  s     zz~~a  rB   r   c                   [        U[        5      (       d  [        S5      e[        5       (       d  [	        US[
        S5        [        U5      n[        U R                  U R                  -   R                  5      nU R                  S-   nSU;   Ga  X-   n[        X1-   5      n[        R                  " U R                  U R                  -   5      S   R                  5       US'   [        R                  " USU R                  5      n[        R                  " Xu5      n[        R                  " U5      n	[         R"                  " U	U R$                  (       a  SOSS	UU R                  S
9n
XU R                  -   -  n[        R&                  " XR                  US9nU$ X-   n[         R"                  " UU R$                  (       a  SOSS	UU R                  S
9[        R(                  " XPR                  S9U R                  -   -  n[        R&                  " XR                  US9nU R*                  (       a  [        R                  " XUS9$ U$ )zGenerate samples of the specified shape.

Args:
    shape (Sequence[int], optional): Shape of the generated samples.
    seed (int): Python integer number.

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

%sample shape must be Iterable object.seedsample_sampler                                 ?)rE   stdrN   r   r   r    )r.   r   	TypeErrorr   r   r"   r*   r   r   r;   r   r'   itemfullr   reshaper   gaussianr-   addzerosr,   )r<   r;   rN   batch_shaper   output_shape
fill_shapezero_tmpzero_tmp_reshapezero_tmp_shapenormal_random_tmpoutputs               r@   rO   Normal.sample  s    %**CDD  tVcH5UDHHtzz1889yy9$ .Lk12J"LLDJJ)>?BGGIJqM{{:sDJJ?H%~~hE#\\*:;N &%)%;%;jjj! 'TZZ*GHFZZt<FM .L__%)%;%;jjj l**=

JLF ZZt<F$$~~f$??rB   c                    [        U[        5      (       d  [        S5      eU R                  [	        U5      5      n[
        R                  " U R                  (       a  SOSUS9nU R                  X R                  -  -   $ )zGenerate reparameterized samples of the specified shape.

Args:
  shape (Sequence[int], optional): Shape of the generated samples.

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

rM   rS   rR   )rE   r;   )
r.   r   rW   _extend_shaper+   r'   normalr-   r   r   )r<   r;   epss      r@   rsampleNormal.rsampleH  sf     %**CDD""5<0mm!%!7!7*S
 xx#

***rB   c           	        U R                   S-   n[        U R                  U R                  -   R                  5      nU R
                  (       Ga  SU;   a{  [        U5      n[        R                  " U R                  U R                  -   5      S   R                  5       US'   U R                  R                  n[        R                  " USU5      nO,[        R                  " USU R                  R                  5      n[        R                  " SU-   [        R                  " [        R                  5      S[        R                  " U R                  U-   5      -  -   US9$ SU;   a  [        U5      n[        R                  " U R                  U R                  -   5      S   R                  5       US'   U R                  U R                  -   R                  n[        R                  " USU5      nO"[        R                  " USU R                  5      n[        R                  " SU-   S[        R                  " S	[        R                  -  5      -  [        R                  " U R                  U-   5      -   US9$ )
ao  Shannon entropy in nats.

If non-complex, the entropy is

.. math::

    entropy(\sigma) = 0.5 \log (2 \pi e \sigma^2)

If complex gaussian, the entropy is

.. math::

    entropy(\sigma) = \log (\pi e \sigma^2) + 1

In the above equation:

* :math:`scale = \sigma`: is the std.

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

_entropyrQ   r   rR   rT          @rV         ?rH   )r   r*   r   r   r;   r-   r'   rX   r   rY   r\   mathlogpi)r<   r   r^   r`   
fill_dtypera   s         r@   entropyNormal.entropy[  s   . yy:%DHHtzz1889!!![ !+.
 &TXX

-B CA F K K M
1!ZZ--
!;;z3
C!;;{C9I9IJ::h!C&**TZZ(5J*K$KK  [ !+.
 &TXX

-B CA F K K M
1"hh3::
!;;z3
C!;;{CD::hdhhq477{++fjjh9N.OO rB   c           
        U R                   S-   nU R                  U R                  U5      nU R                  U R                  -  n[        R
                  " U R                  5      nU R                  (       am  [        R                  " SXR                  -
  R                  5       XR                  -
  -  -  U-  SU-  [        R
                  " [        R                  5      -   US9$ [        R                  " SXR                  -
  XR                  -
  -  -  SU-  -  U[        R
                  " [        R                  " S[        R                  -  5      5      -   US9$ )zLog probability density/mass function.

Args:
  value (Tensor): The input tensor.

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

	_log_prob      ro   rV   )r   _check_values_dtype_in_probsr   r   r'   rr   r-   subtractconjrq   rs   sqrt)r<   valuer   var	log_scales        r@   log_probNormal.log_prob  s    yy;&11$((EBjj4::%JJtzz*	!!??)//1UXX5EFG3Oi$((477"33  ??)ehh.>?@C#INDHHTYYsTWW}%=>> rB   c                   U R                   S-   nU R                  U R                  U5      nU R                  U R                  -  nU R                  (       aj  [
        R                  " [
        R                  " SXR                  -
  R                  5       XR                  -
  -  -  U-  5      [        R                  U-  US9$ [
        R                  " [
        R                  " SXR                  -
  XR                  -
  -  -  SU-  -  5      [        R                  " S[        R                  -  5      U R                  -  US9$ )zProbability density/mass function.

Args:
    value (Tensor): The input tensor.

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

_probsry   rV   ro   rH   )r   rz   r   r   r-   r'   divideexpr|   rq   rs   r}   )r<   r~   r   r   s       r@   probsNormal.probs  s    yy8#11$((EBjj4::%!!==

(..0EHH4DEG
 3  ==

(UXX-=>@Sy"
 1tww;'$**4 rB   c                *   [        5       (       d  [        US[        S5        U R                  UR                  :w  a  [	        S5      eU R
                  S-   nU R                  UR                  -  nX3-  nU R                  UR                  -
  UR                  -  nU R                  (       a1  UR                  5       U-  nX4-   S-
  [        R                  " U5      -
  $ XD-  n[        R                  " SU-  SUS-
  [        R                  " U5      -
  -  US9$ )a  The KL-divergence between two normal distributions.

If non-complex, the KL-divergence is

.. math::

    KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = 0.5 (ratio^2 + (\frac{diff}{\sigma_1})^2 - 1 - 2 \ln {ratio})

If complex gaussian:

.. math::

    KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = ratio^2 + (\frac{diff}{\sigma_1})^2 - 1 - 2 \ln {ratio}

.. math::

    ratio = \frac{\sigma_0}{\sigma_1}

.. math::

    diff = \mu_1 - \mu_0

In the above equation:

* :math:`loc = \mu_0`: is the mean of current Normal distribution.
* :math:`scale = \sigma_0`: is the std of current Normal distribution.
* :math:`loc = \mu_1`: is the mean of other Normal distribution.
* :math:`scale = \sigma_1`: is the std of other Normal distribution.
* :math:`ratio`: is the ratio of scales.
* :math:`diff`: is the difference between means.

Args:
    other (Normal): instance of Normal.

Returns:
    Tensor, kl-divergence between two normal distributions.The data type is float32.

otherkl_divergencezVThe kl divergence must be computed between two distributions in the same number field._kl_divergencerT   rp   rV   )r   r   r   r-   
ValueErrorr   r   r   r|   r'   rr   r\   )r<   r   r   	var_ratiot1s        r@   r   Normal.kl_divergence  s    N   ugv?!!U%<%<<h  yy++JJ,	)	hh"ekk1!!RB>C'&**Y*???B::irCx&**Y"778 rB   )r-   r,   r   r   r   r   )N)r   r   r   r   r   z
str | NonereturnNone)r   r   )r;   Sequence[int]rN   r"   r   r   )r;   r   r   r   )r~   r   r   r   )r   r   r   r   )__name__
__module____qualname____firstlineno____doc____annotations__r:   propertyrE   rJ   rO   rk   ru   r   r   r   __static_attributes____classcell__)r?   s   @r@   r   r   :   s    L\ 
KM
IL HLq)q)&2q):Dq)	q) q)f   ! ! -/A 3j .0 +&3j8!F; ;rB   r   )-
__future__r   rq   collections.abcr   r   typingr   numpyr%   numpy.typingnptr'   paddle.base.data_feederr   r   paddle.base.frameworkr	   paddle.distributionr
   paddle.frameworkr   paddle.tensorr   r   typing_extensionsr   r   r   paddle._typingr   r#   r$   r   r   r   r!   r   r1   r   NDArrayr   r   Distributionr    rB   r@   <module>r      s   #  .      = * , ,  +$- %eWn 5NI5#(


BJJbmm;$y  " ~&%&		J	  $uE"**bjj012		L) P\&& PrB   