
    [Th*                         S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJrJ	r	  S SK
Jr  S/rS rS	 rS
 r " S S\5      rg)    N)Tensor)constraints)Distribution)_standard_normallazy_property)_sizeMultivariateNormalc                 j    [         R                  " XR                  S5      5      R                  S5      $ )a  
Performs a batched matrix-vector product, with compatible but different batch shapes.

This function takes as input `bmat`, containing :math:`n \times n` matrices, and
`bvec`, containing length :math:`n` vectors.

Both `bmat` and `bvec` may have any number of leading dimensions, which correspond
to a batch shape. They are not necessarily assumed to have the same batch shape,
just ones which can be broadcasted.
)torchmatmul	unsqueezesqueeze)bmatbvecs     _/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/multivariate_normal.py	_batch_mvr      s'     <<nnR0199"==    c                    UR                  S5      nUR                  SS n[        U5      nU R                  5       S-
  nXE-
  nXe-   nUSU-  -   nUR                  SU n	[	        U R                  SS UR                  US 5       H  u  pXU
-  U
4-  n	M     X4-  n	UR                  U	5      n[        [        U5      5      [        [        XhS5      5      -   [        [        US-   US5      5      -   U/-   nUR                  U5      nU R                  SX"5      nUR                  SUR                  S5      U5      nUR                  SSS5      n[        R                  R                  XSS9R                  S5      R                  S5      nUR                  5       nUR                  UR                  SS 5      n[        [        U5      5      n[        U5       H  nUUU-   UU-   /-  nM     UR                  U5      nUR                  U5      $ )	a7  
Computes the squared Mahalanobis distance :math:`\mathbf{x}^\top\mathbf{M}^{-1}\mathbf{x}`
for a factored :math:`\mathbf{M} = \mathbf{L}\mathbf{L}^\top`.

Accepts batches for both bL and bx. They are not necessarily assumed to have the same batch
shape, but `bL` one should be able to broadcasted to `bx` one.
r   N      r   Fupper)sizeshapelendimzipreshapelistrangepermuter   linalgsolve_triangularpowsumt)bLbxnbx_batch_shapebx_batch_dimsbL_batch_dimsouter_batch_dimsold_batch_dimsnew_batch_dimsbx_new_shapesLsxpermute_dimsflat_Lflat_xflat_x_swapM_swapM
permuted_Mpermute_inv_dimsi
reshaped_Ms                         r   _batch_mahalanobisr?      s    	AXXcr]N 'MFFHqLM$4%5N%M(99N88--.LbhhsmRXX.>r%BCr2& DDL	L	!B 	U#$%
u%q9
:	;
u%)>1=
>	? 
	  
L	!BZZA!FZZFKKNA.F..Aq)K%%f%GKKANRRSUV  	
A 288CR=)JE"234=!-1>A3EFF "##$45Jn--r   c                 r   [         R                  R                  [         R                  " U S5      5      n[         R                  " [         R                  " US5      SS5      n[         R
                  " U R                  S   U R                  U R                  S9n[         R                  R                  X#SS9nU$ )N)r   r   r   r   dtypedeviceFr   )
r   r$   choleskyflip	transposeeyer   rB   rC   r%   )PLfL_invIdLs        r   _precision_to_scale_trilrM   O   s~    			uzz!X6	7BOOEJJr84b"=E	1772;aggahh	?B%%eu%=AHr   c                     ^  \ rS rSrSr\R                  \R                  \R                  \R                  S.r	\R                  r
Sr    SU 4S jjrSU 4S jjr\S\4S j5       r\S\4S	 j5       r\S\4S
 j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       r\R0                  " 5       4S\S\4S jjrS rS rSrU =r$ )r	   X   a)  
Creates a multivariate normal (also called Gaussian) distribution
parameterized by a mean vector and a covariance matrix.

The multivariate normal distribution can be parameterized either
in terms of a positive definite covariance matrix :math:`\mathbf{\Sigma}`
or a positive definite precision matrix :math:`\mathbf{\Sigma}^{-1}`
or a lower-triangular matrix :math:`\mathbf{L}` with positive-valued
diagonal entries, such that
:math:`\mathbf{\Sigma} = \mathbf{L}\mathbf{L}^\top`. This triangular matrix
can be obtained via e.g. Cholesky decomposition of the covariance.

Example:

    >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_LAPACK)
    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> m = MultivariateNormal(torch.zeros(2), torch.eye(2))
    >>> m.sample()  # normally distributed with mean=`[0,0]` and covariance_matrix=`I`
    tensor([-0.2102, -0.5429])

Args:
    loc (Tensor): mean of the distribution
    covariance_matrix (Tensor): positive-definite covariance matrix
    precision_matrix (Tensor): positive-definite precision matrix
    scale_tril (Tensor): lower-triangular factor of covariance, with positive-valued diagonal

Note:
    Only one of :attr:`covariance_matrix` or :attr:`precision_matrix` or
    :attr:`scale_tril` can be specified.

    Using :attr:`scale_tril` will be more efficient: all computations internally
    are based on :attr:`scale_tril`. If :attr:`covariance_matrix` or
    :attr:`precision_matrix` is passed instead, it is only used to compute
    the corresponding lower triangular matrices using a Cholesky decomposition.
)loccovariance_matrixprecision_matrix
scale_trilTc                   > UR                  5       S:  a  [        S5      eUS LUS L-   US L-   S:w  a  [        S5      eUbj  UR                  5       S:  a  [        S5      e[        R                  " UR                  S S UR                  S S 5      nUR                  US-   5      U l        OUbj  UR                  5       S:  a  [        S	5      e[        R                  " UR                  S S UR                  S S 5      nUR                  US-   5      U l        OiUR                  5       S:  a  [        S
5      e[        R                  " UR                  S S UR                  S S 5      nUR                  US-   5      U l        UR                  US-   5      U l	        U R                  R                  SS  n[        TU ]-  XgUS9  Ub  X@l        g Ub%  [        R                  R                  U5      U l        g [        U5      U l        g )Nr   z%loc must be at least one-dimensional.zTExactly one of covariance_matrix or precision_matrix or scale_tril may be specified.r   zZscale_tril matrix must be at least two-dimensional, with optional leading batch dimensionsr   r   )r   r   zZcovariance_matrix must be at least two-dimensional, with optional leading batch dimensionszYprecision_matrix must be at least two-dimensional, with optional leading batch dimensions)r   validate_args)r   
ValueErrorr   broadcast_shapesr   expandrS   rQ   rR   rP   super__init___unbroadcasted_scale_trilr$   rD   rM   )	selfrP   rQ   rR   rS   rV   batch_shapeevent_shape	__class__s	           r   r[   MultivariateNormal.__init__   s    779q=DEET)j.DED(
 f  !~~!# =   001A1A#21F		RUSUWK(//h0FGDO* $$&* =   00!'',ciinK &7%=%=kH>T%UD"##%) =   00 &&s+SYYs^K %5$;$;K(<R$SD!::kE12hhnnRS)O!-7**-2\\-B-BCT-UD*-EFV-WD*r   c                   > U R                  [        U5      n[        R                  " U5      nXR                  -   nXR                  -   U R                  -   nU R
                  R                  U5      Ul        U R                  Ul        SU R                  ;   a   U R                  R                  U5      Ul	        SU R                  ;   a   U R                  R                  U5      Ul
        SU R                  ;   a   U R                  R                  U5      Ul        [        [        U]7  XR                  SS9  U R                  Ul        U$ )NrQ   rS   rR   FrU   )_get_checked_instancer	   r   Sizer_   rP   rY   r\   __dict__rQ   rS   rR   rZ   r[   _validate_args)r]   r^   	_instancenew	loc_shape	cov_shaper`   s         r   rY   MultivariateNormal.expand   s   (();YGjj-"2"22	"2"22T5E5EE	((//),(,(F(F%$--/$($:$:$A$A)$LC!4==(!__33I>CN.#'#8#8#?#?	#JC  #/)) 	0 	
 "00
r   returnc                     U R                   R                  U R                  U R                  -   U R                  -   5      $ N)r\   rY   _batch_shape_event_shaper]   s    r   rS   MultivariateNormal.scale_tril   s:    --44 1 11D4E4EE
 	
r   c                     [         R                  " U R                  U R                  R                  5      R	                  U R
                  U R                  -   U R                  -   5      $ rn   )r   r   r\   mTrY   ro   rp   rq   s    r   rQ   $MultivariateNormal.covariance_matrix   sQ    ||**D,J,J,M,M

&""T%6%669J9JJ
K	Lr   c                     [         R                  " U R                  5      R                  U R                  U R
                  -   U R
                  -   5      $ rn   )r   cholesky_inverser\   rY   ro   rp   rq   s    r   rR   #MultivariateNormal.precision_matrix   sE    %%d&D&DELL 1 11D4E4EE
 	
r   c                     U R                   $ rn   rP   rq   s    r   meanMultivariateNormal.mean       xxr   c                     U R                   $ rn   rz   rq   s    r   modeMultivariateNormal.mode   r}   r   c                     U R                   R                  S5      R                  S5      R                  U R                  U R
                  -   5      $ )Nr   r   )r\   r&   r'   rY   ro   rp   rq   s    r   varianceMultivariateNormal.variance   sA     **..q1SWVD%%(9(99:	
r   sample_shapec                     U R                  U5      n[        X R                  R                  U R                  R                  S9nU R                  [        U R                  U5      -   $ )NrA   )_extended_shaper   rP   rB   rC   r   r\   )r]   r   r   epss       r   rsampleMultivariateNormal.rsample   sJ    $$\2uHHNN488??Sxx)D$B$BCHHHr   c                 |   U R                   (       a  U R                  U5        XR                  -
  n[        U R                  U5      nU R                  R                  SSS9R                  5       R                  S5      nSU R                  S   [        R                  " S[        R                  -  5      -  U-   -  U-
  $ )Nr   r   dim1dim2g      r   r   )rf   _validate_samplerP   r?   r\   diagonallogr'   rp   mathpi)r]   valuediffr:   half_log_dets        r   log_probMultivariateNormal.log_prob   s    !!%(xxt==tD**33"3EIIKOOPRS 	 t((+dhhq477{.CCaGH<WWr   c                 \   U R                   R                  SSS9R                  5       R                  S5      nSU R                  S   -  S[
        R                  " S[
        R                  -  5      -   -  U-   n[        U R                  5      S:X  a  U$ UR                  U R                  5      $ )Nr   r   r   g      ?r   g      ?r   )
r\   r   r   r'   rp   r   r   r   ro   rY   )r]   r   Hs      r   entropyMultivariateNormal.entropy  s    **33"3EIIKOOPRS 	 $##A&&#TWW0E*EFUt  !Q&H88D--..r   )r\   rQ   rP   rR   rS   )NNNNrn   )__name__
__module____qualname____firstlineno____doc__r   real_vectorpositive_definitelower_choleskyarg_constraintssupporthas_rsampler[   rY   r   r   rS   rQ   rR   propertyr{   r   r   r   rd   r   r   r   r   __static_attributes____classcell__)r`   s   @r   r	   r	   X   s8   "J &&(::'99!00	O %%GK
 7Xr& 
F 
 

 L6 L L
 
& 
 

 f   f   
& 
 
 -2JJL IE IV I
X/ /r   )r   r   r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   torch.typesr   __all__r   r?   rM   r	    r   r   <module>r      sB       + 9 E   
 >/.ds/ s/r   