
    9j                         d dl mZmZ d dl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 dgZ ed	e	
      Z G d de	ee         Zy)    )GenericTypeVarN)SizeTensor)constraints)Distribution)_sum_rightmost)_sizeIndependentD)boundc            	           e Zd ZU dZi Zeeej                  f   e	d<   e
e	d<   	 dde
dededz  ddf fd	Zd fd
	Zedefd       Zedefd       Zej$                  d        Zedefd       Zedefd       Zedefd       Z ej2                         fdefdZ ej2                         fdedefdZd Zd ZddZd Z  xZ!S )r   a  
    Reinterprets some of the batch dims of a distribution as event dims.

    This is mainly useful for changing the shape of the result of
    :meth:`log_prob`. For example to create a diagonal Normal distribution with
    the same shape as a Multivariate Normal distribution (so they are
    interchangeable), you can::

        >>> from torch.distributions.multivariate_normal import MultivariateNormal
        >>> from torch.distributions.normal import Normal
        >>> loc = torch.zeros(3)
        >>> scale = torch.ones(3)
        >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale))
        >>> [mvn.batch_shape, mvn.event_shape]
        [torch.Size([]), torch.Size([3])]
        >>> normal = Normal(loc, scale)
        >>> [normal.batch_shape, normal.event_shape]
        [torch.Size([3]), torch.Size([])]
        >>> diagn = Independent(normal, 1)
        >>> [diagn.batch_shape, diagn.event_shape]
        [torch.Size([]), torch.Size([3])]

    Args:
        base_distribution (torch.distributions.distribution.Distribution): a
            base distribution
        reinterpreted_batch_ndims (int): the number of batch dims to
            reinterpret as event dims
    arg_constraints	base_distNbase_distributionreinterpreted_batch_ndimsvalidate_argsreturnc                 d   |t        |j                        kD  r$t        d| dt        |j                               |j                  |j                  z   }|t        |j                        z   }|d t        |      |z
   }|t        |      |z
  d  }|| _        || _        t        |   |||       y )NzQExpected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), actual z vs r   )lenbatch_shape
ValueErrorevent_shaper   r   super__init__)	selfr   r   r   shape	event_dimr   r   	__class__s	           _/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/distributions/independent.pyr   zIndependent.__init__3   s     %s+<+H+H'II34D=N=Z=Z9[8\^  (336G6S6SS2S9J9V9V5WW	4c%j945CJ245*)B&kO    c                 V   | j                  t        |      }t        j                  |      }| j                  j                  || j                  d | j                   z         |_        | j                  |_        t        t        |'  || j                  d       | j                  |_
        |S )NFr   )_get_checked_instancer   torchr   r   expandr   r   r   r   _validate_args)r   r   	_instancenewr    s       r!   r&   zIndependent.expandG   s    ((i@jj---$**+KT-K-KLL
 )-(F(F%k3()) 	) 	
 "00
r"   c                 .    | j                   j                  S N)r   has_rsampler   s    r!   r,   zIndependent.has_rsampleT   s    ~~)))r"   c                 N    | j                   dkD  ry| j                  j                  S )Nr   F)r   r   has_enumerate_supportr-   s    r!   r/   z!Independent.has_enumerate_supportX   s#    ))A-~~333r"   c                     | j                   j                  }| j                  r t        j                  || j                        }|S r+   )r   supportr   r   independent)r   results     r!   r1   zIndependent.support^   s9     '')) ,,VT5S5STFr"   c                 .    | j                   j                  S r+   )r   meanr-   s    r!   r5   zIndependent.meanf       ~~"""r"   c                 .    | j                   j                  S r+   )r   moder-   s    r!   r8   zIndependent.modej   r6   r"   c                 .    | j                   j                  S r+   )r   variancer-   s    r!   r:   zIndependent.variancen   s    ~~&&&r"   c                 8    | j                   j                  |      S r+   )r   sampler   sample_shapes     r!   r<   zIndependent.sampler   s    ~~$$\22r"   r>   c                 8    | j                   j                  |      S r+   )r   rsampler=   s     r!   r@   zIndependent.rsampleu   s    ~~%%l33r"   c                 d    | j                   j                  |      }t        || j                        S r+   )r   log_probr	   r   )r   valuerB   s      r!   rB   zIndependent.log_probx   s)    >>**51h(F(FGGr"   c                 b    | j                   j                         }t        || j                        S r+   )r   entropyr	   r   )r   rE   s     r!   rE   zIndependent.entropy|   s'    ..((*gt'E'EFFr"   c                 n    | j                   dkD  rt        d      | j                  j                  |      S )Nr   z5Enumeration over cartesian product is not implemented)r&   )r   NotImplementedErrorr   enumerate_support)r   r&   s     r!   rH   zIndependent.enumerate_support   s:    ))A-%G  ~~//v/>>r"   c                 j    | j                   j                  d| j                   d| j                   dz   S )N(z, ))r    __name__r   r   r-   s    r!   __repr__zIndependent.__repr__   s8    NN##$..!D$B$B#C1EF	
r"   r+   )T)"rL   
__module____qualname____doc__r   dictstrr   
Constraint__annotations__r   intboolr   r&   propertyr,   r/   dependent_propertyr1   r   r5   r8   r:   r%   r   r<   r
   r@   rB   rE   rH   rM   __classcell__)r    s   @r!   r   r      sW   : :<OT#{5556;L &*	PP $'P d{	P
 
P( *T * * 4t 4 4
 ## $ #f # # #f # # '& ' ' #-%**, 36 3 -7EJJL 4E 4V 4HG?
r"   )typingr   r   r%   r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr	   torch.typesr
   __all__r   r    r"   r!   <module>ra      sE    #   + 9 4  / C|$y
,
 y
r"   