
    ȅiX                        S SK Jr  S SKJr   S SKr/ SQr " S S5      r " S S\5      rS	 r " S
 S\	\5      r
 " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S  S!\5      r " S" S#\5      r " S$ S%\5      r " S& S'\5      r " S( S)\5      r " S* S+\5      r " S, S-\5      r " S. S/\5      r " S0 S1\5      r " S2 S3\5      r " S4 S5\5      r " S6 S7\5      r  " S8 S9\5      r! " S: S;\5      r"\" 5       r#\
r$\r%\" 5       r&\" 5       r'\" S 5      r(\" S<5      r)\r*\" 5       r+\%" \+S<5      r,\" S=5      r-\" S=5      r.\r/\r0\r1\r2\" S=S>5      r3\r4\r5\" 5       r6\" 5       r7\" 5       r8\" 5       r9\" 5       r:\" 5       r;\" 5       r<\ " 5       r=\!r>\"r?g)?    )Callable)AnyN) 
Constraintbooleancatcorr_cholesky	dependentdependent_propertygreater_thangreater_than_eqindependentinteger_intervalintervalhalf_open_intervalis_dependent	less_thanlower_choleskylower_triangularMixtureSameFamilyConstraintmultinomialnonnegativenonnegative_integerone_hotpositivepositive_semidefinitepositive_definitepositive_integerrealreal_vectorsimplexsquarestack	symmetricunit_intervalc                   ,    \ rS rSrSrSrSrS rS rSr	g)	r   P   a  
Abstract base class for constraints.

A constraint object represents a region over which a variable is valid,
e.g. within which a variable can be optimized.

Attributes:
    is_discrete (bool): Whether constrained space is discrete.
        Defaults to False.
    event_dim (int): Number of rightmost dimensions that together define
        an event. The :meth:`check` method will remove this many dimensions
        when computing validity.
Fr   c                     [         e)z{
Returns a byte tensor of ``sample_shape + batch_shape`` indicating
whether each event in value satisfies this constraint.
)NotImplementedErrorselfvalues     Y/home/james-whalen/.local/lib/python3.13/site-packages/torch/distributions/constraints.pycheckConstraint.checkb   s
    
 "!    c                 :    U R                   R                  SS  S-   $ )N   z())	__class____name__r*   s    r,   __repr__Constraint.__repr__i   s    ~~&&qr*T11r/    N)
r3   
__module____qualname____firstlineno____doc__is_discrete	event_dimr-   r5   __static_attributes__r7   r/   r,   r   r   P   s     KI"2r/   r   c                   |   ^  \ rS rSrSr\\S.U 4S jjr\S\4S j5       r	\S\
4S j5       r\\S.S jrS	 rS
rU =r$ )
_Dependentm   a!  
Placeholder for variables whose support depends on other variables.
These variables obey no simple coordinate-wise constraints.

Args:
    is_discrete (bool): Optional value of ``.is_discrete`` in case this
        can be computed statically. If not provided, access to the
        ``.is_discrete`` attribute will raise a NotImplementedError.
    event_dim (int): Optional value of ``.event_dim`` in case this
        can be computed statically. If not provided, access to the
        ``.event_dim`` attribute will raise a NotImplementedError.
r<   r=   c                :   > Xl         X l        [        TU ]  5         g N)_is_discrete
_event_dimsuper__init__)r*   r<   r=   r2   s      r,   rH   _Dependent.__init__{   s    '#r/   returnc                 V    U R                   [        L a  [        S5      eU R                   $ )Nz,.is_discrete cannot be determined statically)rE   NotImplementedr(   r4   s    r,   r<   _Dependent.is_discrete   s(    .%&TUU   r/   c                 V    U R                   [        L a  [        S5      eU R                   $ )Nz*.event_dim cannot be determined statically)rF   rL   r(   r4   s    r,   r=   _Dependent.event_dim   s$    ??n,%&RSSr/   c                h    U[         L a  U R                  nU[         L a  U R                  n[        XS9$ )zo
Support for syntax to customize static attributes::

    constraints.dependent(is_discrete=True, event_dim=1)
rB   )rL   rE   rF   r@   )r*   r<   r=   s      r,   __call___Dependent.__call__   s3     .(++K&IkGGr/   c                     [        S5      e)Nz1Cannot determine validity of dependent constraint)
ValueErrorr*   xs     r,   r-   _Dependent.check   s    LMMr/   rF   rE   )r3   r8   r9   r:   r;   rL   rH   propertyboolr<   intr=   rQ   r-   r>   __classcell__r2   s   @r,   r@   r@   m   sh     '5  
 !T ! !
 3  
 '5 
HN Nr/   r@   c                 "    [        U [        5      $ )a  
Checks if ``constraint`` is a ``_Dependent`` object.

Args:
    constraint : A ``Constraint`` object.

Returns:
    ``bool``: True if ``constraint`` can be refined to the type ``_Dependent``, False otherwise.

Examples:
    >>> import torch
    >>> from torch.distributions import Bernoulli
    >>> from torch.distributions.constraints import is_dependent

    >>> dist = Bernoulli(probs=torch.tensor([0.6], requires_grad=True))
    >>> constraint1 = dist.arg_constraints["probs"]
    >>> constraint2 = dist.arg_constraints["logits"]

    >>> for constraint in [constraint1, constraint2]:
    >>>     if is_dependent(constraint):
    >>>         continue
)
isinstancer@   )
constraints    r,   r   r      s    . j*--r/   c            
          ^  \ rS rSrSr S\\S.S\S\4   S-  S\S-  S\	S-  S	S4U 4S
 jjjjr
S\S\4   S	S 4S jrSrU =r$ )_DependentProperty   a  
Decorator that extends @property to act like a `Dependent` constraint when
called on a class and act like a property when called on an object.

Example::

    class Uniform(Distribution):
        def __init__(self, low, high):
            self.low = low
            self.high = high

        @constraints.dependent_property(is_discrete=False, event_dim=0)
        def support(self):
            return constraints.interval(self.low, self.high)

Args:
    fn (Callable): The function to be decorated.
    is_discrete (bool): Optional value of ``.is_discrete`` in case this
        can be computed statically. If not provided, access to the
        ``.is_discrete`` attribute will raise a NotImplementedError.
    event_dim (int): Optional value of ``.event_dim`` in case this
        can be computed statically. If not provided, access to the
        ``.event_dim`` attribute will raise a NotImplementedError.
NrB   fn.r<   r=   rJ   c                <   > [         TU ]  U5        X l        X0l        g rD   )rG   rH   rE   rF   )r*   rd   r<   r=   r2   s       r,   rH   _DependentProperty.__init__   s     	'#r/   c                 >    [        XR                  U R                  S9$ )z
Support for syntax to customize static attributes::

    @constraints.dependent_property(is_discrete=True, event_dim=1)
    def support(self): ...
rB   )rb   rE   rF   )r*   rd   s     r,   rQ   _DependentProperty.__call__   s      "--
 	
r/   rX   rD   )r3   r8   r9   r:   r;   rL   r   r   rZ   r[   rH   rQ   r>   r\   r]   s   @r,   rb   rb      s    6 )-	$ $2 .	$S#X%	$ D[		$
 :	$ 
	$ 	$	
8CH- 	
2F 	
 	
r/   rb   c                   h   ^  \ rS rSrSrU 4S jr\S\4S j5       r\S\	4S j5       r
S rS rS	rU =r$ )
_IndependentConstraint   z
Wraps a constraint by aggregating over ``reinterpreted_batch_ndims``-many
dims in :meth:`check`, so that an event is valid only if all its
independent entries are valid.
c                 :  > [        U[        5      (       d!  [        S[        U5      R                   35      e[        U[
        5      (       d!  [        S[        U5      R                   35      eUS:  a  [        SU 35      eXl        X l        [        TU ]%  5         g )N*base_constraint must be a Constraint, got z.reinterpreted_batch_ndims must be an int, got r   z,reinterpreted_batch_ndims must be >= 0, got )
r_   r   AssertionErrortyper3   r[   base_constraintreinterpreted_batch_ndimsrG   rH   )r*   rp   rq   r2   s      r,   rH   _IndependentConstraint.__init__   s    /:66 <T/=R=[=[<\]  3S99 @F_A`AiAi@jk  %q( >?X>YZ   /)B&r/   rJ   c                 .    U R                   R                  $ rD   rp   r<   r4   s    r,   r<   "_IndependentConstraint.is_discrete       ##///r/   c                 H    U R                   R                  U R                  -   $ rD   )rp   r=   rq   r4   s    r,   r=    _IndependentConstraint.event_dim  s    ##--0N0NNNr/   c                    U R                   R                  U5      nUR                  5       U R                  :  aB  U R                   R                  U R                  -   n[        SU SUR                  5        35      eUR                  UR                  S UR                  5       U R                  -
   S-   5      nUR                  S5      nU$ )NExpected value.dim() >= 	 but got r}   )	rp   r-   dimrq   r=   rT   reshapeshapeall)r*   r+   resultexpecteds       r,   r-   _IndependentConstraint.check  s    %%++E2::<$888++558V8VVH*8*Ieiik]K  LLH6::<$*H*HHIEQ
 Br/   c                     U R                   R                  SS   S[        U R                  5       SU R                   S3$ )Nr1   (z, ))r2   r3   reprrp   rq   r4   s    r,   r5   _IndependentConstraint.__repr__  sA    ..))!"-.aT5I5I0J/K2dNlNlMmmnoor/   )rp   rq   r3   r8   r9   r:   r;   rH   rY   rZ   r<   r[   r=   r-   r5   r>   r\   r]   s   @r,   rj   rj      sW    " 0T 0 0 O3 O Op pr/   rj   c                   h   ^  \ rS rSrSrU 4S jr\S\4S j5       r\S\	4S j5       r
S rS rS	rU =r$ )
r   i  av  
Constraint for the :class:`~torch.distribution.MixtureSameFamily`
distribution that adds back the rightmost batch dimension before
performing the validity check with the component distribution
constraint.

Args:
    base_constraint: The ``Constraint`` object of
        the component distribution of
        the :class:`~torch.distribution.MixtureSameFamily` distribution.
c                    > [        U[        5      (       d!  [        S[        U5      R                   35      eXl        [        TU ]  5         g )Nrm   )r_   r   rn   ro   r3   rp   rG   rH   )r*   rp   r2   s     r,   rH   $MixtureSameFamilyConstraint.__init__%  sF    /:66 <T/=R=[=[<\]   /r/   rJ   c                 .    U R                   R                  $ rD   rt   r4   s    r,   r<   'MixtureSameFamilyConstraint.is_discrete-  rv   r/   c                 .    U R                   R                  $ rD   )rp   r=   r4   s    r,   r=   %MixtureSameFamilyConstraint.event_dim1  s    ##---r/   c                    UR                  SU R                  -
  5      nU R                  R                  U5      nUR	                  5       U R                  :  a)  [        SU R                   SUR	                  5        35      eUR	                  5       U R                  -
  nUR                  UR                  SU S-   5      nUR                  S5      nU$ )z
Check validity of ``value`` as a possible outcome of sampling
the :class:`~torch.distribution.MixtureSameFamily` distribution.
r}   rz   r{   Nr|   )		unsqueezer=   rp   r-   r~   rT   r   r   r   )r*   r+   unsqueezed_valuer   num_dim_to_keeps        r,   r-   !MixtureSameFamilyConstraint.check5  s    
 !??2+>?%%++,<=99;'*4>>*:)EIIK=Q   ))+6-=o > FGBr/   c                 `    U R                   R                   S[        U R                  5       S3$ )Nr   r   )r2   r3   r   rp   r4   s    r,   r5   $MixtureSameFamilyConstraint.__repr__E  s,    ..))*!D1E1E,F+GqIIr/   )rp   r   r]   s   @r,   r   r     sT    
 0T 0 0 .3 . . J Jr/   r   c                   "    \ rS rSrSrSrS rSrg)_BooleaniI  z'
Constrain to the two values `{0, 1}`.
Tc                     US:H  US:H  -  $ )Nr   r1   r7   r)   s     r,   r-   _Boolean.checkP  s    
uz**r/   r7   N)r3   r8   r9   r:   r;   r<   r-   r>   r7   r/   r,   r   r   I  s     K+r/   r   c                   &    \ rS rSrSrSrSrS rSrg)_OneHotiT  z
Constrain to one-hot vectors.
Tr1   c                     US:H  US:H  -  nUR                  S5      R                  S5      nUR                  S5      U-  $ )Nr   r1   r}   )sumeqr   )r*   r+   
is_booleanis_normalizeds       r,   r-   _OneHot.check\  s@    qjUaZ0
		"((+~~b!M11r/   r7   N)	r3   r8   r9   r:   r;   r<   r=   r-   r>   r7   r/   r,   r   r   T  s     KI2r/   r   c                   <   ^  \ rS rSrSrSrU 4S jrS rS rSr	U =r
$ )_IntegerIntervalib  z@
Constrain to an integer interval `[lower_bound, upper_bound]`.
Tc                 :   > Xl         X l        [        TU ]  5         g rD   lower_boundupper_boundrG   rH   r*   r   r   r2   s      r,   rH   _IntegerInterval.__init__i      &&r/   c                 P    US-  S:H  U R                   U:*  -  XR                  :*  -  $ Nr1   r   r   r   r)   s     r,   r-   _IntegerInterval.checkn  s0    QY!^ 0 0E 9:eGWGW>WX	
r/   c                 x    U R                   R                  SS  nUSU R                   SU R                   S3-  nU$ Nr1   (lower_bound=z, upper_bound=r   r2   r3   r   r   r*   
fmt_strings     r,   r5   _IntegerInterval.__repr__s  J    ^^,,QR0
D,,-^D<L<L;MQO	

 r/   r   r3   r8   r9   r:   r;   r<   rH   r-   r5   r>   r\   r]   s   @r,   r   r   b  s#     K


 r/   r   c                   <   ^  \ rS rSrSrSrU 4S jrS rS rSr	U =r
$ )_IntegerLessThani{  z9
Constrain to an integer interval `(-inf, upper_bound]`.
Tc                 .   > Xl         [        TU ]	  5         g rD   r   rG   rH   r*   r   r2   s     r,   rH   _IntegerLessThan.__init__      &r/   c                 0    US-  S:H  XR                   :*  -  $ r   r   r)   s     r,   r-   _IntegerLessThan.check      	Q5,<,<#<==r/   c                 ^    U R                   R                  SS  nUSU R                   S3-  nU$ Nr1   z(upper_bound=r   r2   r3   r   r   s     r,   r5   _IntegerLessThan.__repr__  8    ^^,,QR0
d&6&6%7q99
r/   r   r   r]   s   @r,   r   r   {  #     K> r/   r   c                   <   ^  \ rS rSrSrSrU 4S jrS rS rSr	U =r
$ )_IntegerGreaterThani  z8
Constrain to an integer interval `[lower_bound, inf)`.
Tc                 .   > Xl         [        TU ]	  5         g rD   r   rG   rH   r*   r   r2   s     r,   rH   _IntegerGreaterThan.__init__  r   r/   c                 0    US-  S:H  XR                   :  -  $ r   r   r)   s     r,   r-   _IntegerGreaterThan.check  r   r/   c                 ^    U R                   R                  SS  nUSU R                   S3-  nU$ Nr1   r   r   r2   r3   r   r   s     r,   r5   _IntegerGreaterThan.__repr__  r   r/   r   r   r]   s   @r,   r   r     r   r/   r   c                       \ rS rSrSrS rSrg)_Reali  z>
Trivially constrain to the extended real line `[-inf, inf]`.
c                 
    X:H  $ rD   r7   r)   s     r,   r-   _Real.check  s
    ~r/   r7   N)r3   r8   r9   r:   r;   r-   r>   r7   r/   r,   r   r     s    r/   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )_GreaterThani  z5
Constrain to a real half line `(lower_bound, inf]`.
c                 .   > Xl         [        TU ]	  5         g rD   r   r   s     r,   rH   _GreaterThan.__init__  r   r/   c                      U R                   U:  $ rD   r   r)   s     r,   r-   _GreaterThan.check  s    %''r/   c                 ^    U R                   R                  SS  nUSU R                   S3-  nU$ r   r   r   s     r,   r5   _GreaterThan.__repr__  r   r/   r   
r3   r8   r9   r:   r;   rH   r-   r5   r>   r\   r]   s   @r,   r   r         ( r/   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )_GreaterThanEqi  z5
Constrain to a real half line `[lower_bound, inf)`.
c                 .   > Xl         [        TU ]	  5         g rD   r   r   s     r,   rH   _GreaterThanEq.__init__  r   r/   c                      U R                   U:*  $ rD   r   r)   s     r,   r-   _GreaterThanEq.check  s    5((r/   c                 ^    U R                   R                  SS  nUSU R                   S3-  nU$ r   r   r   s     r,   r5   _GreaterThanEq.__repr__  r   r/   r   r   r]   s   @r,   r   r     s    ) r/   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )	_LessThani  z6
Constrain to a real half line `[-inf, upper_bound)`.
c                 .   > Xl         [        TU ]	  5         g rD   r   r   s     r,   rH   _LessThan.__init__  r   r/   c                     XR                   :  $ rD   r   r)   s     r,   r-   _LessThan.check  s    ''''r/   c                 ^    U R                   R                  SS  nUSU R                   S3-  nU$ r   r   r   s     r,   r5   _LessThan.__repr__  r   r/   r   r   r]   s   @r,   r   r     r   r/   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )	_Intervali  z<
Constrain to a real interval `[lower_bound, upper_bound]`.
c                 :   > Xl         X l        [        TU ]  5         g rD   r   r   s      r,   rH   _Interval.__init__  r   r/   c                 >    U R                   U:*  XR                  :*  -  $ rD   r   r)   s     r,   r-   _Interval.check  s!      E)e7G7G.GHHr/   c                 x    U R                   R                  SS  nUSU R                   SU R                   S3-  nU$ r   r   r   s     r,   r5   _Interval.__repr__  r   r/   r   r   r]   s   @r,   r   r     s    
I r/   r   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )_HalfOpenIntervali  z<
Constrain to a real interval `[lower_bound, upper_bound)`.
c                 :   > Xl         X l        [        TU ]  5         g rD   r   r   s      r,   rH   _HalfOpenInterval.__init__  r   r/   c                 >    U R                   U:*  XR                  :  -  $ rD   r   r)   s     r,   r-   _HalfOpenInterval.check  s!      E)e6F6F.FGGr/   c                 x    U R                   R                  SS  nUSU R                   SU R                   S3-  nU$ r   r   r   s     r,   r5   _HalfOpenInterval.__repr__  r   r/   r   r   r]   s   @r,   r   r     s    
H r/   r   c                   "    \ rS rSrSrSrS rSrg)_Simplexi  zt
Constrain to the unit simplex in the innermost (rightmost) dimension.
Specifically: `x >= 0` and `x.sum(-1) == 1`.
r1   c                 ~    [         R                  " US:  SS9UR                  S5      S-
  R                  5       S:  -  $ )Nr   r}   r~   r1   ư>)torchr   r   absr)   s     r,   r-   _Simplex.check  s7    yy!,21B0G0G0ID0PQQr/   r7   Nr3   r8   r9   r:   r;   r=   r-   r>   r7   r/   r,   r  r    s    
 IRr/   r  c                   ,    \ rS rSrSrSrSrS rS rSr	g)	_Multinomiali  a  
Constrain to nonnegative integer values summing to at most an upper bound.

Note due to limitations of the Multinomial distribution, this currently
checks the weaker condition ``value.sum(-1) <= upper_bound``. In the future
this may be strengthened to ``value.sum(-1) == upper_bound``.
Tr1   c                     Xl         g rD   r   )r*   r   s     r,   rH   _Multinomial.__init__$  s    &r/   c                 `    US:  R                  SS9UR                  SS9U R                  :*  -  $ )Nr   r}   r
  )r   r   r   rU   s     r,   r-   _Multinomial.check'  s1    Q|||#quuu}8H8H'HIIr/   r   N)
r3   r8   r9   r:   r;   r<   r=   rH   r-   r>   r7   r/   r,   r  r    s     KI'Jr/   r  c                   "    \ rS rSrSrSrS rSrg)_LowerTriangulari+  z0
Constrain to lower-triangular square matrices.
   c                     UR                  5       nX!:H  R                  UR                  S S S-   5      R                  S5      S   $ )Nr|   r}   r   )trilviewr   min)r*   r+   
value_trils      r,   r-   _LowerTriangular.check2  sA    ZZ\
#))%++cr*:U*BCGGKANNr/   r7   Nr  r7   r/   r,   r  r  +  s     IOr/   r  c                   "    \ rS rSrSrSrS rSrg)_LowerCholeskyi7  zH
Constrain to lower-triangular square matrices with positive diagonals.
r  c                     UR                  5       nX!:H  R                  UR                  S S S-   5      R                  S5      S   nUR	                  SSS9S:  R                  S5      S   nX4-  $ )Nr  r|   r}   r   )dim1dim2)r  r  r   r  diagonal)r*   r+   r  r   positive_diagonals        r,   r-   _LowerCholesky.check>  sv    ZZ\
 &&u{{3B'7%'?@DDRHK 	 #^^"^=AFFrJ1M33r/   r7   Nr  r7   r/   r,   r!  r!  7  s     I4r/   r!  c                   "    \ rS rSrSrSrS rSrg)_CorrCholeskyiH  zq
Constrain to lower-triangular square matrices with positive diagonals and each
row vector being of unit length.
r  c                 n   [         R                  " UR                  5      R                  UR	                  S5      -  S-  n[         R
                  R                  UR                  5       SS9nUS-
  R                  5       R                  U5      R                  SS9n[        5       R                  U5      U-  $ )Nr}   
   r
        ?)r  finfodtypeepssizelinalgnormdetachr  ler   r!  r-   )r*   r+   tolrow_normunit_row_norms        r,   r-   _CorrCholesky.checkP  s    KK$((5::b>9B> 	 <<$$U\\^$<!C,,.11#6::r:B%%e,}<<r/   r7   Nr  r7   r/   r,   r)  r)  H  s    
 I=r/   r)  c                   "    \ rS rSrSrSrS rSrg)_SquareiY  z
Constrain to square matrices.
r  c                     [         R                  " UR                  S S UR                  S   UR                  S   :H  [         R                  UR                  S9$ )Nr  r}   )r0  
fill_valuer.  device)r  fullr   rZ   r=  r)   s     r,   r-   _Square.check`  sG    zzSb!B5;;r?:**<<	
 	
r/   r7   Nr  r7   r/   r,   r:  r:  Y  s     I
r/   r:  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )
_Symmetricii  z)
Constrain to Symmetric square matrices.
c                    > [         TU ]  U5      nUR                  5       (       d  U$ [        R                  " XR
                  SS9R                  S5      R                  S5      $ )Nr  )atolr  r}   )rG   r-   r   r  isclosemT)r*   r+   square_checkr2   s      r,   r-   _Symmetric.checkn  sQ    w}U+!!}}UHH48<<R@DDRHHr/   r7   r3   r8   r9   r:   r;   r-   r>   r\   r]   s   @r,   rA  rA  i  s    I Ir/   rA  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )_PositiveSemidefiniteiu  z.
Constrain to positive-semidefinite matrices.
c                    > [         TU ]  U5      nUR                  5       (       d  U$ [        R                  R                  U5      R                  S5      R                  S5      $ )Nr   r}   )rG   r-   r   r  r1  eigvalshger*   r+   	sym_checkr2   s      r,   r-   _PositiveSemidefinite.checkz  sM    GM%(	}}||$$U+..q155b99r/   r7   rH  r]   s   @r,   rJ  rJ  u      : :r/   rJ  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )_PositiveDefinitei  z*
Constrain to positive-definite matrices.
c                    > [         TU ]  U5      nUR                  5       (       d  U$ [        R                  R                  U5      R                  R                  S5      $ )Nr   )rG   r-   r   r  r1  cholesky_exinfor   rN  s      r,   r-   _PositiveDefinite.check  sH    GM%(	}}||''.3366q99r/   r7   rH  r]   s   @r,   rS  rS    rQ  r/   rS  c                   f   ^  \ rS rSrSrS	U 4S jjr\S\4S j5       r\S\	4S j5       r
S rSrU =r$ )
_Cati  z
Constraint functor that applies a sequence of constraints
`cseq` at the submatrices at dimension `dim`,
each of size `lengths[dim]`, in a way compatible with :func:`torch.cat`.
c                   > [        S U 5       5      (       d  [        S5      e[        U5      U l        Uc  S/[	        U R                  5      -  n[        U5      U l        [	        U R
                  5      [	        U R                  5      :w  a8  [        S[	        U R
                  5       S[	        U R                  5       S35      eX l        [        TU ]!  5         g )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7frD   r_   r   .0cs     r,   	<genexpr> _Cat.__init__.<locals>.<genexpr>       ;d:a,,d   1All elements of cseq must be Constraint instancesr1   z	lengths (z) must match cseq (r   )	r   rn   listcseqlenlengthsr~   rG   rH   )r*   rf  r~   rh  r2   s       r,   rH   _Cat.__init__  s    ;d;;; !TUUJ	?cC		N*GG}t||DII. C-..A#dii.AQQRS  r/   rJ   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frD   r<   r]  s     r,   r`  #_Cat.is_discrete.<locals>.<genexpr>       4)Q==)   anyrf  r4   s    r,   r<   _Cat.is_discrete      4$))444r/   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frD   r=   r]  s     r,   r`  !_Cat.event_dim.<locals>.<genexpr>  s     2	1;;	ro  )maxrf  r4   s    r,   r=   _Cat.event_dim  s    2		222r/   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      e/ nSn[        U R                  U R                  5       HE  u  pEUR                  U R                   X55      nUR                  UR                  U5      5        X5-   nMG     [        R                  " X R                   5      $ )Ndim  out of range for value with  dimensionsr   )
r~   rn   ziprf  rh  narrowappendr-   r  r   )r*   r+   checksstartconstrlengthvs          r,   r-   
_Cat.check  s    6599;6 txxj =eiik]+V  !$))T\\:NFTXXu5AMM&,,q/*NE ; yy**r/   )rf  r~   rh  )r   Nr3   r8   r9   r:   r;   rH   rY   rZ   r<   r[   r=   r-   r>   r\   r]   s   @r,   rY  rY    sM     5T 5 5 33 3 3+ +r/   rY  c                   f   ^  \ rS rSrSrS	U 4S jjr\S\4S j5       r\S\	4S j5       r
S rSrU =r$ )
_Stacki  z
Constraint functor that applies a sequence of constraints
`cseq` at the submatrices at dimension `dim`,
in a way compatible with :func:`torch.stack`.
c                    > [        S U 5       5      (       d  [        S5      e[        U5      U l        X l        [
        TU ]  5         g )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7frD   r\  r]  s     r,   r`  "_Stack.__init__.<locals>.<genexpr>  rb  rc  rd  )r   rn   re  rf  r~   rG   rH   )r*   rf  r~   r2   s      r,   rH   _Stack.__init__  s;    ;d;;; !TUUJ	r/   rJ   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frD   rl  r]  s     r,   r`  %_Stack.is_discrete.<locals>.<genexpr>  rn  ro  rp  r4   s    r,   r<   _Stack.is_discrete  rs  r/   c                 n    [        S U R                   5       5      nU R                  U-   S:  a  US-  nU$ )Nc              3   8   #    U  H  oR                   v   M     g 7frD   rv  r]  s     r,   r`  #_Stack.event_dim.<locals>.<genexpr>  s     1y!++yro  r   r1   )rx  rf  r~   )r*   r~   s     r,   r=   _Stack.event_dim  s4    1tyy1188c>A1HC
r/   c           	      "   UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      e[        UR                  U R                   5      5       Vs/ s H  o!R	                  U R                   U5      PM      nn[
        R                  " [        X0R                  5       VVs/ s H  u  pEUR                  U5      PM     snnU R                   5      $ s  snf s  snnf )Nr{  r|  r}  )
r~   rn   ranger0  selectr  r"   r~  rf  r-   )r*   r+   ivsr  r  s         r,   r-   _Stack.check  s    6599;6 txxj =eiik]+V  27uzz$((7K1LM1LAll488Q'1LM{{.1"ii.@A.@V\\!_.@A488
 	
 NAs   %DD
)rf  r~   )r   r  r]   s   @r,   r  r    sM     5T 5 5 3  
 
r/   r  r1   g        r,  )@collections.abcr   typingr   r  __all__r   r@   r   rY   rb   rj   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r!  r)  r:  rA  rJ  rS  rY  r  r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r    r   r   r   r!   r#   r   r   r   r"   r7   r/   r,   <module>r     sV   %  D !H2 2:,N ,N^.4.
: .
b.pZ .pb.J* .Jb+z +2j 2z 2z (* (J : $Z $
 $
 *
 *	Rz 	RJ: J&	Oz 	O4Z 4"=J ="
j 
 	I 	I	:J 	:	:
 	:(+: (+V!
Z !
J L	' $
*
))!, &q) # w$"S! 	#s#& 
*#% !	L	-/ %' 
r/   