
    rhbT                        d dl mZmZmZ 	 d dlZg dZ G d d      Z G d de      Zd Z G d	 d
e	e      Z
 G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z G d! d"e      Z G d# d$e      Z G d% d&e      Z G d' d(e      Z G d) d*e      Z G d+ d,e      Z G d- d.e      Z G d/ d0e      Z G d1 d2e      Z G d3 d4e      Z G d5 d6e      Z  G d7 d8e      Z! G d9 d:e      Z" e       Z#e
Z$eZ% e       Z& e       Z' ed       Z( ed;      Z)eZ* e       Z+ e%e+d;      Z, ed<      Z- ed<      Z.eZ/eZ0eZ1eZ2 ed<d=      Z3eZ4eZ5 e       Z6 e       Z7 e       Z8 e       Z9 e       Z: e       Z; e       Z< e        Z=e!Z>e"Z?y)>    )AnyCallableOptionalN) 
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                   $    e Zd ZdZdZdZd Zd Zy)r   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                     t         )z
        Returns a byte tensor of ``sample_shape + batch_shape`` indicating
        whether each event in value satisfies this constraint.
        )NotImplementedErrorselfvalues     r/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/distributions/constraints.pycheckzConstraint.checka   s
    
 "!    c                 :    | j                   j                  dd  dz   S )N   z())	__class____name__r*   s    r,   __repr__zConstraint.__repr__h   s    ~~&&qr*T11r.   N)r2   
__module____qualname____doc__is_discrete	event_dimr-   r4    r.   r,   r   r   O   s     KI"2r.   r   c                   j     e Zd ZdZeed fd
Zedefd       Zede	fd       Z
eeddZd Z xZS )	
_DependentaI  
    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.
    r8   r9   c                >    || _         || _        t        |           y N)_is_discrete
_event_dimsuper__init__)r*   r8   r9   r1   s      r,   rC   z_Dependent.__init__z   s    '#r.   returnc                 T    | j                   t        u rt        d      | j                   S )Nz,.is_discrete cannot be determined statically)r@   NotImplementedr(   r3   s    r,   r8   z_Dependent.is_discrete   s(    .%&TUU   r.   c                 T    | j                   t        u rt        d      | j                   S )Nz*.event_dim cannot be determined statically)rA   rF   r(   r3   s    r,   r9   z_Dependent.event_dim   s$    ??n,%&RSSr.   c                l    |t         u r| j                  }|t         u r| j                  }t        ||      S )z
        Support for syntax to customize static attributes::

            constraints.dependent(is_discrete=True, event_dim=1)
        r=   )rF   r@   rA   r<   )r*   r8   r9   s      r,   __call__z_Dependent.__call__   s5     .(++K&IkYGGr.   c                     t        d      )Nz1Cannot determine validity of dependent constraint)
ValueErrorr*   xs     r,   r-   z_Dependent.check   s    LMMr.   )r2   r5   r6   r7   rF   rC   propertyboolr8   intr9   rI   r-   __classcell__r1   s   @r,   r<   r<   l   s]     '5 
 !T ! !
 3  
 '5 
HNr.   r<   c                 "    t        | t              S )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            
       v     e Zd ZdZ	 deeddeedef      dee   dee	   ddf fd	Z
dedef   dd fd
Z xZS )_DependentPropertya  
    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.
    Nr=   fn.r8   r9   rD   c                @    t         |   |       || _        || _        y r?   )rB   rC   r@   rA   )r*   rX   r8   r9   r1   s       r,   rC   z_DependentProperty.__init__   s!     	'#r.   c                 F    t        || j                  | j                        S )z
        Support for syntax to customize static attributes::

            @constraints.dependent_property(is_discrete=True, event_dim=1)
            def support(self): ...
        r=   )rW   r@   rA   )r*   rX   s     r,   rI   z_DependentProperty.__call__   s"     "D--
 	
r.   r?   )r2   r5   r6   r7   rF   r   r   r   rO   rP   rC   rI   rQ   rR   s   @r,   rW   rW      sx    6 ,0	$ '5#1	$Xc3h'(	$ d^		$
 C=	$ 
	$	
8CH- 	
2F 	
r.   rW   c                   Z     e Zd ZdZ fdZedefd       Zedefd       Z	d Z
d Z xZS )_IndependentConstraintz
    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                     t        |t              sJ t        |t              sJ |dk\  sJ || _        || _        t
        |           y Nr   )rT   r   rP   base_constraintreinterpreted_batch_ndimsrB   rC   )r*   r_   r`   r1   s      r,   rC   z_IndependentConstraint.__init__   sM    /:6663S999(A---.)B&r.   rD   c                 .    | j                   j                  S r?   r_   r8   r3   s    r,   r8   z"_IndependentConstraint.is_discrete       ##///r.   c                 H    | j                   j                  | j                  z   S r?   )r_   r9   r`   r3   s    r,   r9   z _IndependentConstraint.event_dim   s    ##--0N0NNNr.   c                    | j                   j                  |      }|j                         | j                  k  rB| j                   j                  | j                  z   }t        d| d|j                                |j                  |j                  d |j                         | j                  z
   dz         }|j                  d      }|S )NExpected value.dim() >= 	 but got ri   )	r_   r-   dimr`   r9   rK   reshapeshapeall)r*   r+   resultexpecteds       r,   r-   z_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                     | j                   j                  dd   dt        | j                         d| j                   dS )Nr0   (z, ))r1   r2   reprr_   r`   r3   s    r,   r4   z_IndependentConstraint.__repr__
  sA    ..))!"-.aT5I5I0J/K2dNlNlMmmnoor.   r2   r5   r6   r7   rC   rN   rO   r8   rP   r9   r-   r4   rQ   rR   s   @r,   r\   r\      sQ     0T 0 0 O3 O Opr.   r\   c                   Z     e Zd ZdZ fdZedefd       Zedefd       Z	d Z
d Z xZS )r   a  
    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                 T    t        |t              sJ || _        t        |           y r?   )rT   r   r_   rB   rC   )r*   r_   r1   s     r,   rC   z$MixtureSameFamilyConstraint.__init__  s&    /:666.r.   rD   c                 .    | j                   j                  S r?   rb   r3   s    r,   r8   z'MixtureSameFamilyConstraint.is_discrete   rc   r.   c                 .    | j                   j                  S r?   )r_   r9   r3   s    r,   r9   z%MixtureSameFamilyConstraint.event_dim$  s    ##---r.   c                    |j                  d| j                  z
        }| j                  j                  |      }|j	                         | j                  k  r)t        d| j                   d|j	                                |j	                         | j                  z
  }|j                  |j                  d| dz         }|j                  d      }|S )z
        Check validity of ``value`` as a possible outcome of sampling
        the :class:`~torch.distribution.MixtureSameFamily` distribution.
        ri   rf   rg   Nrh   )		unsqueezer9   r_   r-   rj   rK   rk   rl   rm   )r*   r+   unsqueezed_valuern   num_dim_to_keeps        r,   r-   z!MixtureSameFamilyConstraint.check(  s    
 !??2+>?%%++,<=99;'*4>>*:)EIIK=Q   ))+6-=o > FGBr.   c                 `    | j                   j                   dt        | j                         dS )Nrq   rr   )r1   r2   rs   r_   r3   s    r,   r4   z$MixtureSameFamilyConstraint.__repr__8  s,    ..))*!D1E1E,F+GqIIr.   rt   rR   s   @r,   r   r     sN    

 0T 0 0 .3 . . Jr.   r   c                       e Zd ZdZdZd Zy)_Booleanz/
    Constrain to the two values `{0, 1}`.
    Tc                     |dk(  |dk(  z  S )Nr   r0   r:   r)   s     r,   r-   z_Boolean.checkC  s    
uz**r.   N)r2   r5   r6   r7   r8   r-   r:   r.   r,   r   r   <  s     K+r.   r   c                       e Zd ZdZdZdZd Zy)_OneHotz'
    Constrain to one-hot vectors.
    Tr0   c                     |dk(  |dk(  z  }|j                  d      j                  d      }|j                  d      |z  S )Nr   r0   ri   )sumeqrm   )r*   r+   
is_booleanis_normalizeds       r,   r-   z_OneHot.checkO  s@    qjUaZ0
		"((+~~b!M11r.   N)r2   r5   r6   r7   r8   r9   r-   r:   r.   r,   r   r   G  s     KI2r.   r   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )_IntegerIntervalzH
    Constrain to an integer interval `[lower_bound, upper_bound]`.
    Tc                 >    || _         || _        t        |           y r?   lower_boundupper_boundrB   rC   r*   r   r   r1   s      r,   rC   z_IntegerInterval.__init__\      &&r.   c                 R    |dz  dk(  | j                   |k  z  || j                  k  z  S Nr0   r   r   r   r)   s     r,   r-   z_IntegerInterval.checka  s2    QY!^ 0 0E 9:etGWGW>WX	
r.   c                 x    | j                   j                  dd  }|d| j                   d| j                   dz  }|S Nr0   (lower_bound=z, upper_bound=rr   r1   r2   r   r   r*   
fmt_strings     r,   r4   z_IntegerInterval.__repr__f  J    ^^,,QR0
D,,-^D<L<L;MQO	

 r.   	r2   r5   r6   r7   r8   rC   r-   r4   rQ   rR   s   @r,   r   r   U  s     K


r.   r   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )_IntegerLessThanzA
    Constrain to an integer interval `(-inf, upper_bound]`.
    Tc                 0    || _         t        | 	          y r?   r   rB   rC   r*   r   r1   s     r,   rC   z_IntegerLessThan.__init__u      &r.   c                 2    |dz  dk(  || j                   k  z  S r   r   r)   s     r,   r-   z_IntegerLessThan.checky       	Q5D,<,<#<==r.   c                 ^    | j                   j                  dd  }|d| j                   dz  }|S Nr0   z(upper_bound=rr   r1   r2   r   r   s     r,   r4   z_IntegerLessThan.__repr__|  8    ^^,,QR0
d&6&6%7q99
r.   r   rR   s   @r,   r   r   n       K>r.   r   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )_IntegerGreaterThanz@
    Constrain to an integer interval `[lower_bound, inf)`.
    Tc                 0    || _         t        | 	          y r?   r   rB   rC   r*   r   r1   s     r,   rC   z_IntegerGreaterThan.__init__  r   r.   c                 2    |dz  dk(  || j                   k\  z  S r   r   r)   s     r,   r-   z_IntegerGreaterThan.check  r   r.   c                 ^    | j                   j                  dd  }|d| j                   dz  }|S Nr0   r   rr   r1   r2   r   r   s     r,   r4   z_IntegerGreaterThan.__repr__  r   r.   r   rR   s   @r,   r   r     r   r.   r   c                       e Zd ZdZd Zy)_RealzF
    Trivially constrain to the extended real line `[-inf, inf]`.
    c                     ||k(  S r?   r:   r)   s     r,   r-   z_Real.check  s    ~r.   N)r2   r5   r6   r7   r-   r:   r.   r,   r   r     s    r.   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )_GreaterThanz=
    Constrain to a real half line `(lower_bound, inf]`.
    c                 0    || _         t        | 	          y r?   r   r   s     r,   rC   z_GreaterThan.__init__  r   r.   c                      | j                   |k  S r?   r   r)   s     r,   r-   z_GreaterThan.check  s    %''r.   c                 ^    | j                   j                  dd  }|d| j                   dz  }|S r   r   r   s     r,   r4   z_GreaterThan.__repr__  r   r.   r2   r5   r6   r7   rC   r-   r4   rQ   rR   s   @r,   r   r         (r.   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )_GreaterThanEqz=
    Constrain to a real half line `[lower_bound, inf)`.
    c                 0    || _         t        | 	          y r?   r   r   s     r,   rC   z_GreaterThanEq.__init__  r   r.   c                      | j                   |k  S r?   r   r)   s     r,   r-   z_GreaterThanEq.check  s    5((r.   c                 ^    | j                   j                  dd  }|d| j                   dz  }|S r   r   r   s     r,   r4   z_GreaterThanEq.__repr__  r   r.   r   rR   s   @r,   r   r     s    )r.   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )	_LessThanz>
    Constrain to a real half line `[-inf, upper_bound)`.
    c                 0    || _         t        | 	          y r?   r   r   s     r,   rC   z_LessThan.__init__  r   r.   c                      || j                   k  S r?   r   r)   s     r,   r-   z_LessThan.check  s    t''''r.   c                 ^    | j                   j                  dd  }|d| j                   dz  }|S r   r   r   s     r,   r4   z_LessThan.__repr__  r   r.   r   rR   s   @r,   r   r     r   r.   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )	_IntervalzD
    Constrain to a real interval `[lower_bound, upper_bound]`.
    c                 >    || _         || _        t        |           y r?   r   r   s      r,   rC   z_Interval.__init__  r   r.   c                 @    | j                   |k  || j                  k  z  S r?   r   r)   s     r,   r-   z_Interval.check  s#      E)et7G7G.GHHr.   c                 x    | j                   j                  dd  }|d| j                   d| j                   dz  }|S r   r   r   s     r,   r4   z_Interval.__repr__  r   r.   r   rR   s   @r,   r   r     s    
Ir.   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )_HalfOpenIntervalzD
    Constrain to a real interval `[lower_bound, upper_bound)`.
    c                 >    || _         || _        t        |           y r?   r   r   s      r,   rC   z_HalfOpenInterval.__init__  r   r.   c                 @    | j                   |k  || j                  k  z  S r?   r   r)   s     r,   r-   z_HalfOpenInterval.check  s#      E)ed6F6F.FGGr.   c                 x    | j                   j                  dd  }|d| j                   d| j                   dz  }|S r   r   r   s     r,   r4   z_HalfOpenInterval.__repr__  r   r.   r   rR   s   @r,   r   r     s    
Hr.   r   c                       e Zd ZdZdZd Zy)_Simplexz
    Constrain to the unit simplex in the innermost (rightmost) dimension.
    Specifically: `x >= 0` and `x.sum(-1) == 1`.
    r0   c                     t        j                  |dk\  d      |j                  d      dz
  j                         dk  z  S )Nr   ri   rj   r0   ư>)torchrm   r   absr)   s     r,   r-   z_Simplex.check  s7    yy!,21B0G0G0ID0PQQr.   Nr2   r5   r6   r7   r9   r-   r:   r.   r,   r   r     s    
 IRr.   r   c                   $    e Zd ZdZdZdZd Zd Zy)_Multinomiala3  
    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``.
    Tr0   c                     || _         y r?   r   )r*   r   s     r,   rC   z_Multinomial.__init__  s
    &r.   c                 l    |dk\  j                  d      |j                  d      | j                  k  z  S )Nr   ri   r   )rm   r   r   rL   s     r,   r-   z_Multinomial.check  s1    Q|||#quuu}8H8H'HIIr.   N)r2   r5   r6   r7   r8   r9   rC   r-   r:   r.   r,   r   r     s     KI'Jr.   r   c                       e Zd ZdZdZd Zy)_LowerTriangularz8
    Constrain to lower-triangular square matrices.
       c                     |j                         }||k(  j                  |j                  d d dz         j                  d      d   S )Nrh   ri   r   )trilviewrl   min)r*   r+   
value_trils      r,   r-   z_LowerTriangular.check%  sC    ZZ\
e#))%++cr*:U*BCGGKANNr.   Nr   r:   r.   r,   r   r     s     IOr.   r   c                       e Zd ZdZdZd Zy)_LowerCholeskyzP
    Constrain to lower-triangular square matrices with positive diagonals.
    r   c                     |j                         }||k(  j                  |j                  d d dz         j                  d      d   }|j	                  dd      dkD  j                  d      d   }||z  S )Nr   rh   ri   r   )dim1dim2)r   r   rl   r   diagonal)r*   r+   r   r   positive_diagonals        r,   r-   z_LowerCholesky.check1  s{    ZZ\
5 &&u{{3B'7%'?@DDRHK 	 #^^"^=AFFrJ1M"333r.   Nr   r:   r.   r,   r   r   *  s     I4r.   r   c                       e Zd ZdZdZd Zy)_CorrCholeskyz}
    Constrain to lower-triangular square matrices with positive diagonals and each
    row vector being of unit length.
    r   c                 x   t        j                  |j                        j                  |j	                  d      z  dz  }t         j
                  j                  |j                         d      }|dz
  j                         j                  |      j                  d      }t               j                  |      |z  S )Nri   
   r         ?)r   finfodtypeepssizelinalgnormdetachr   lerm   r   r-   )r*   r+   tolrow_normunit_row_norms        r,   r-   z_CorrCholesky.checkC  s    KK$((5::b>9B> 	 <<$$U\\^$<!C,,.11#6::r:B%%e,}<<r.   Nr   r:   r.   r,   r   r   ;  s    
 I=r.   r   c                       e Zd ZdZdZd Zy)_Squarez'
    Constrain to square matrices.
    r   c                     t        j                  |j                  d d |j                  d   |j                  d   k(  t         j                  |j                        S )Nr   ri   )r   
fill_valuer   device)r   fullrl   rO   r  r)   s     r,   r-   z_Square.checkS  sG    zzSb!B5;;r?:**<<	
 	
r.   Nr   r:   r.   r,   r   r   L  s     I
r.   r   c                   "     e Zd ZdZ fdZ xZS )
_Symmetricz1
    Constrain to Symmetric square matrices.
    c                     t         |   |      }|j                         s|S t        j                  ||j
                  d      j                  d      j                  d      S )Nr   )atolr   ri   )rB   r-   rm   r   isclosemT)r*   r+   square_checkr1   s      r,   r-   z_Symmetric.checka  sP    w}U+!}}UEHH48<<R@DDRHHr.   r2   r5   r6   r7   r-   rQ   rR   s   @r,   r  r  \  s    I Ir.   r  c                   "     e Zd ZdZ fdZ xZS )_PositiveSemidefinitez6
    Constrain to positive-semidefinite matrices.
    c                     t         |   |      }|j                         s|S t        j                  j                  |      j                  d      j                  d      S )Nr   ri   )rB   r-   rm   r   r   eigvalshger*   r+   	sym_checkr1   s      r,   r-   z_PositiveSemidefinite.checkm  sK    GM%(	}}||$$U+..q155b99r.   r
  rR   s   @r,   r  r  h      : :r.   r  c                   "     e Zd ZdZ fdZ xZS )_PositiveDefinitez2
    Constrain to positive-definite matrices.
    c                     t         |   |      }|j                         s|S t        j                  j                  |      j                  j                  d      S r^   )rB   r-   rm   r   r   cholesky_exinfor   r  s      r,   r-   z_PositiveDefinite.checky  sF    GM%(	}}||''.3366q99r.   r
  rR   s   @r,   r  r  t  r  r.   r  c                   V     e Zd ZdZd fd	Zedefd       Zedefd       Z	d Z
 xZS )_Catz
    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                 (   t        d |D              sJ t        |      | _        |dgt        | j                        z  }t        |      | _        t        | j                        t        | j                        k(  sJ || _        t        |           y )Nc              3   <   K   | ]  }t        |t                y wr?   rT   r   .0cs     r,   	<genexpr>z _Cat.__init__.<locals>.<genexpr>       ;:a,;   r0   )rm   listcseqlenlengthsrj   rB   rC   )r*   r$  rj   r&  r1   s       r,   rC   z_Cat.__init__  sw    ;d;;;;J	?cC		N*GG}4<< C		N222r.   rD   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   r8   r  s     r,   r   z#_Cat.is_discrete.<locals>.<genexpr>       4Q1==4   anyr$  r3   s    r,   r8   z_Cat.is_discrete      4$))444r.   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   r9   r  s     r,   r   z!_Cat.event_dim.<locals>.<genexpr>  s     211;;2r+  )maxr$  r3   s    r,   r9   z_Cat.event_dim  s    2		222r.   c                    |j                          | j                   cxk  r|j                         k  sJ  J g }d}t        | j                  | j                        D ]G  \  }}|j	                  | j                   ||      }|j                  |j                  |             ||z   }I t        j                  || j                         S r^   )	rj   zipr$  r&  narrowappendr-   r   r   )r*   r+   checksstartconstrlengthvs          r,   r-   z
_Cat.check  s    		|txx5%))+55555!$))T\\: 	#NFFTXXuf5AMM&,,q/*FNE	# yy**r.   )r   Nr2   r5   r6   r7   rC   rN   rO   r8   rP   r9   r-   rQ   rR   s   @r,   r  r    sH     5T 5 5 33 3 3+r.   r  c                   V     e Zd ZdZd fd	Zedefd       Zedefd       Z	d Z
 xZS )_Stackz
    Constraint functor that applies a sequence of constraints
    `cseq` at the submatrices at dimension `dim`,
    in a way compatible with :func:`torch.stack`.
    c                 x    t        d |D              sJ t        |      | _        || _        t        |           y )Nc              3   <   K   | ]  }t        |t                y wr?   r  r  s     r,   r   z"_Stack.__init__.<locals>.<genexpr>  r!  r"  )rm   r#  r$  rj   rB   rC   )r*   r$  rj   r1   s      r,   rC   z_Stack.__init__  s4    ;d;;;;J	r.   rD   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr?   r)  r  s     r,   r   z%_Stack.is_discrete.<locals>.<genexpr>  r*  r+  r,  r3   s    r,   r8   z_Stack.is_discrete  r.  r.   c                 l    t        d | j                  D              }| j                  |z   dk  r|dz  }|S )Nc              3   4   K   | ]  }|j                     y wr?   r1  r  s     r,   r   z#_Stack.event_dim.<locals>.<genexpr>  s     1!!++1r+  r   r0   )r2  r$  rj   )r*   rj   s     r,   r9   z_Stack.event_dim  s4    1tyy1188c>A1HC
r.   c           	         |j                          | j                   cxk  r|j                         k  sJ  J t        |j                  | j                               D cg c]  }|j                  | j                   |        }}t	        j
                  t        || j                        D cg c]  \  }}|j                  |       c}}| j                         S c c}w c c}}w r?   )	rj   ranger   selectr   r#   r4  r$  r-   )r*   r+   ivsr;  r9  s         r,   r-   z_Stack.check  s    		|txx5%))+5555516uzz$((7K1LMAell488Q'MM{{.1"dii.@AFV\\!_A488
 	
 NAs   #C+C
)r   r<  rR   s   @r,   r>  r>    sH     5T 5 5 3  
r.   r>  r0   g        r   )@typingr   r   r   r   __all__r   r<   r   rN   rW   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   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,   <module>rL     sV   + * D !H2 2:,N ,N^.4.
: .
b%pZ %pP+J* +J\+z +2j 2z 2z (* (J : $Z $
 $
 *
 *	Rz 	RJ: J&	Oz 	O4Z 4"=J ="
j 
 	I 	I	:J 	:	:
 	:!+: !+H
Z 
B L	' $
*
))!, &q) # w$"S! 	#s#& 
*#% !	L	-/ %' 
r.   